Created
June 16, 2024 01:52
-
-
Save calebdre/6d1af47b256d65715ba87d41b99758ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type QueryResult = { | |
score: number, | |
func: FunctionInfo | |
} | |
export async function runQuery( | |
query: string, | |
files: FileInfo[] | |
): Promise<QueryResult[]> { | |
// convert input query to embedding | |
const queryEmbedding = await createEmbedding(query); | |
const similarityScores: QueryResult[] = []; | |
for (const file of files) { | |
for (const func of file.functions) { | |
// calclulate similarity score | |
const similarity = cosineSimilarity(queryEmbedding, func.embedding); | |
similarityScores.push({ | |
score: similarity, | |
func | |
}) | |
} | |
} | |
// sort results by score | |
const sorted = similarityScores.sort((a, b) => b.score - a.score); | |
// return the top 10 | |
return similarityScores.slice(0, 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment