Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Dec 27, 2023
1 parent 250dda0 commit b2963db
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions provider/docs/src/corpus/search/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ export async function embeddingsSearch(
): Promise<CorpusSearchResult[]> {
const queryVec = await embedText(query)

const results: CorpusSearchResult[] = []
for (const { doc, chunks } of index.docs) {
for (const [i, chunk] of chunks.entries()) {
const chunkVec = await cachedEmbedText(chunk.text, cache)
const score = cos_sim(queryVec, chunkVec)
results.push({ doc: doc.id, chunk: i, score, excerpt: chunk.text })
}
}
const results: CorpusSearchResult[] = await Promise.all(
index.docs.flatMap(({ doc, chunks }) =>
chunks.map(async (chunk, i) => {
const chunkVec = await cachedEmbedText(chunk.text, cache)
const score = cos_sim(queryVec, chunkVec)
return { doc: doc.id, chunk: i, score, excerpt: chunk.text } satisfies CorpusSearchResult
})
)
)

results.sort((a, b) => b.score - a.score)

Expand Down

0 comments on commit b2963db

Please sign in to comment.