Skip to content

Instantly share code, notes, and snippets.

@calebdre
Created June 16, 2024 01:52
Show Gist options
  • Save calebdre/6d1af47b256d65715ba87d41b99758ab to your computer and use it in GitHub Desktop.
Save calebdre/6d1af47b256d65715ba87d41b99758ab to your computer and use it in GitHub Desktop.
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