( userQuery: string, org_id: number, nChunksInclude: number, supabase: SupabaseClient<Database>, )
| 5 | import { combineChunks, deduplicateChunks } from "../edge-runtime/utils"; |
| 6 | |
| 7 | export async function getRelevantDocChunks( |
| 8 | userQuery: string, |
| 9 | org_id: number, |
| 10 | nChunksInclude: number, |
| 11 | supabase: SupabaseClient<Database>, |
| 12 | ): Promise<{ text: string; urls: { name: string; url: string }[] }> { |
| 13 | const embedding = await exponentialRetryWrapper( |
| 14 | queryEmbedding, |
| 15 | [userQuery], |
| 16 | 3, |
| 17 | ); |
| 18 | |
| 19 | const { data, error } = await supabase.rpc("match_embeddings", { |
| 20 | query_embedding: embedding[0], |
| 21 | similarity_threshold: 0.8, |
| 22 | match_count: 10, |
| 23 | _org_id: org_id, |
| 24 | }); |
| 25 | if (error) throw new Error(error.message); |
| 26 | |
| 27 | if (!data || data.length === 0) { |
| 28 | console.warn("Found no relevant documentation for query: ", userQuery); |
| 29 | return { text: "No relevant documentation found", urls: [] }; |
| 30 | } |
| 31 | |
| 32 | console.log( |
| 33 | "All relevant doc chunks:", |
| 34 | data.map( |
| 35 | (d) => |
| 36 | `Page: ${d.page_title}\nSection: ${d.section_title}\nSimilarity: ${ |
| 37 | Math.round(d.similarity * 1000) / 1000 |
| 38 | }\n`, |
| 39 | ), |
| 40 | ); |
| 41 | |
| 42 | return combineChunks( |
| 43 | deduplicateChunks(data, nChunksInclude * data[0].text_chunks.length), |
| 44 | ); |
| 45 | } |
no test coverage detected