Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kristenbrann committed Mar 19, 2023
1 parent 9204ef5 commit 52d192b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/VectorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class VectorStore {
return new Map(dbFile.embeddings)
}

private similarityBatch(searchVectors: Vector[], possibleMatch: Vector): number {
private computeSimilarity(searchVectors: Vector[], possibleMatch: Vector): number {
let highestSimilarity = similarity(searchVectors.first(), possibleMatch)
for (const searchVector of searchVectors) {
const currentSimilarity = similarity(searchVector, possibleMatch)
Expand All @@ -314,14 +314,14 @@ export class VectorStore {
return highestSimilarity
}

getNearestVectorsBatch(searchVectors: Vector[], resultNumber: number, relevanceThreshold: number): NearestVectorResult[] {
getNearestVectors(searchVectors: Vector[], resultNumber: number, relevanceThreshold: number): NearestVectorResult[] {
const nearestVectors: NearestVectorResult[] = []

for (const entry of this.embeddings.entries()) {
const filePath = entry[0]
const fileEntry = entry[1]
if (fileEntry.embedding && fileEntry.embedding.length) {
const fileSimilarity = this.similarityBatch(searchVectors, fileEntry.embedding)
const fileSimilarity = this.computeSimilarity(searchVectors, fileEntry.embedding)
nearestVectors.push({
path: filePath,
chunk: undefined,
Expand All @@ -330,7 +330,7 @@ export class VectorStore {
}
fileEntry.chunks.forEach(chunk => {
if (chunk.embedding && chunk.embedding.length) {
const chunkSimilarity = this.similarityBatch(searchVectors, chunk.embedding)
const chunkSimilarity = this.computeSimilarity(searchVectors, chunk.embedding)
nearestVectors.push({
path: filePath,
chunk: chunk.contents,
Expand Down
12 changes: 6 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class VaultChat extends Plugin {
}

async askChatGpt(question: string) {
// HyDE: request note that answers the question
// HyDE: request note that answers the question https://github.com/texttron/hyde
const conversation: Array<ChatCompletionRequestMessage> = []
const hydeMessage: ChatCompletionRequestMessage = {
role: ChatCompletionResponseMessageRoleEnum.User,
Expand All @@ -154,18 +154,18 @@ export default class VaultChat extends Plugin {
// create embeddings for that note and all of its blocks
const hydeNote = hydeResponse.choices[0].message.content
const hydeNoteBlocks = parseMarkdown(hydeNote, '')
const hydeBlocksStrings = hydeNoteBlocks.map(t => `${t.path} ${t.localHeading} ${t.content}`)
hydeBlocksStrings.push(hydeNote)
hydeBlocksStrings.push(question) // include the users raw question
const embeddingsResponse = await this.openAIHandler.createEmbeddingBatch(hydeBlocksStrings)
const queryBlockStrings = hydeNoteBlocks.map(t => `${t.path} ${t.localHeading} ${t.content}`)
queryBlockStrings.push(hydeNote)
queryBlockStrings.push(question) // include the users raw question
const embeddingsResponse = await this.openAIHandler.createEmbeddingBatch(queryBlockStrings)
const embeddings = embeddingsResponse?.data
if (!embeddings) {
console.error(`Failed to get embeddings for hyde response`)
return
}
const searchVectors = embeddings.map(e => e.embedding)
// search for matches
const nearestVectors = this.vectorStore.getNearestVectorsBatch(searchVectors, 8, this.settings.relevanceThreshold)
const nearestVectors = this.vectorStore.getNearestVectors(searchVectors, 8, this.settings.relevanceThreshold)
const results = await Promise.all(nearestVectors.map(async (nearest, i) => {
let name = nearest.path.split('/').last() || ''
let contents = nearest.chunk
Expand Down

0 comments on commit 52d192b

Please sign in to comment.