-
Notifications
You must be signed in to change notification settings - Fork 4
add search route, truncated. #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import cors from "~/utils/llm/cors"; | ||
| import { genericEmbedding } from "~/utils/llm/embeddings"; | ||
| import { EmbeddingSettings, Provider } from "~/types/llm"; | ||
|
|
||
| type RequestBody = { | ||
| input: string | string[]; | ||
| settings: EmbeddingSettings; | ||
| provider?: Provider; | ||
| }; | ||
|
|
||
| export const POST = async (req: NextRequest): Promise<NextResponse> => { | ||
| let response: NextResponse; | ||
|
|
||
| try { | ||
| const body: RequestBody = await req.json(); | ||
| const { input, settings, provider = "openai" } = body; | ||
|
|
||
| if (!input || (Array.isArray(input) && input.length === 0)) { | ||
| response = NextResponse.json( | ||
| { error: "Input text cannot be empty." }, | ||
| { status: 400 }, | ||
| ); | ||
| return cors(req, response) as NextResponse; | ||
| } | ||
|
|
||
| const embeddings = await genericEmbedding(input, settings, provider); | ||
| if (embeddings === undefined) | ||
| response = NextResponse.json( | ||
| { | ||
| error: "Failed to generate embeddings.", | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| else response = NextResponse.json(embeddings, { status: 200 }); | ||
| } catch (error: unknown) { | ||
| console.error("Error calling OpenAI Embeddings API:", error); | ||
| const errorMessage = | ||
| process.env.NODE_ENV === "development" | ||
| ? error instanceof Error | ||
| ? error.message | ||
| : "Unknown error" | ||
| : "Internal server error"; | ||
| response = NextResponse.json( | ||
| { | ||
| error: "Failed to generate embeddings.", | ||
| details: errorMessage, | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| return cors(req, response) as NextResponse; | ||
| }; | ||
|
|
||
| export const OPTIONS = async (req: NextRequest): Promise<NextResponse> => { | ||
| return cors(req, new NextResponse(null, { status: 204 })) as NextResponse; | ||
| }; |
135 changes: 135 additions & 0 deletions
135
apps/website/app/api/supabase/rpc/search-content/route.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { createClient } from "~/utils/supabase/server"; | ||
| import { NextResponse, NextRequest } from "next/server"; | ||
| import type { SupabaseClient } from "@supabase/supabase-js"; | ||
| import cors from "~/utils/llm/cors"; | ||
| import type { Database } from "@repo/database/types.gen.ts"; | ||
| import { get_known_embedding } from "~/utils/supabase/dbUtils"; | ||
| import { genericEmbedding } from "~/utils/llm/embeddings"; | ||
| import type { Provider, EmbeddingSettings } from "~/types/llm"; | ||
|
|
||
| type RequestBody = { | ||
| queryText: string; // The text that the content embeddings will be compared to. | ||
| subsetPlatformIds: string[]; // Restrict results to these contents. Uses Platform (eg Roam) identifiers. | ||
| }; | ||
|
|
||
| type RpcResponseItem = | ||
| Database["public"]["Functions"]["match_embeddings_for_subset_nodes"]["Returns"]; | ||
|
|
||
| async function callMatchEmbeddingsRpc( | ||
| supabase: SupabaseClient<Database, "public", Database["public"]>, | ||
| query: RequestBody, | ||
| ): Promise<{ data?: RpcResponseItem; error?: string }> { | ||
| const { queryText, subsetPlatformIds } = query; | ||
| const provider: Provider = "openai"; | ||
| const settings: EmbeddingSettings = { model: "text-embedding-3-small" }; | ||
|
|
||
| const table_data = get_known_embedding( | ||
| settings.model, | ||
| settings.dimensions, | ||
| provider, | ||
| ); | ||
| if (table_data === undefined) { | ||
| return { | ||
| error: "Invalid model information", | ||
| }; | ||
| } | ||
|
|
||
| let newEmbedding; | ||
| try { | ||
| newEmbedding = await genericEmbedding(queryText, settings, provider); | ||
| } catch (error) { | ||
| if (error instanceof Error) | ||
| return { | ||
| error: error.message, | ||
| }; | ||
| return { | ||
| error: `Unknown error generating embeddings: ${error}`, | ||
| }; | ||
| } | ||
| if (!Array.isArray(subsetPlatformIds)) { | ||
| console.log( | ||
| "[API Route] callMatchEmbeddingsRpc: Invalid subsetPlatformIds.", | ||
| ); | ||
| return { error: "Invalid subsetPlatformIds" }; | ||
| } | ||
|
|
||
| // If subsetPlatformIds is empty, the RPC might not find anything or error, | ||
| // depending on its implementation. It might be more efficient to return early. | ||
| if (subsetPlatformIds.length === 0) { | ||
| console.log( | ||
| "[API Route] callMatchEmbeddingsRpc: subsetPlatformIds is empty, returning empty array without calling RPC.", | ||
| ); | ||
| return { data: [] }; // Return empty array, no need to call RPC | ||
| } | ||
|
|
||
| const response = await supabase.rpc("match_embeddings_for_subset_nodes", { | ||
| p_query_embedding: JSON.stringify(newEmbedding), | ||
| p_subset_roam_uids: subsetPlatformIds, | ||
| }); | ||
maparent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { data: response.data || undefined, error: response.error?.message }; | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| console.log("[API Route] POST /api/supabase/rpc/search: Request received"); | ||
| const supabase = await createClient(); | ||
| let response: NextResponse; | ||
|
|
||
| try { | ||
| const body: RequestBody = await request.json(); | ||
| console.log("[API Route] POST: Parsed request body:", body); | ||
|
|
||
| console.log("[API Route] POST: Calling callMatchEmbeddingsRpc."); | ||
| const { data, error } = await callMatchEmbeddingsRpc(supabase, body); | ||
| console.log("[API Route] POST: Received from callMatchEmbeddingsRpc:", { | ||
| dataLength: data?.length, | ||
| error, | ||
| }); | ||
|
|
||
| if (error) { | ||
| console.error( | ||
| "[API Route] POST: Error after callMatchEmbeddingsRpc:", | ||
| error, | ||
| ); | ||
| const statusCode = error?.includes("Invalid") ? 400 : 500; | ||
| response = NextResponse.json( | ||
| { | ||
| error: error || "Failed to match embeddings via RPC.", | ||
| }, | ||
| { status: statusCode }, | ||
| ); | ||
| } else { | ||
| console.log( | ||
| "[API Route] POST: Successfully processed request. Sending data back. Data length:", | ||
| data?.length, | ||
| ); | ||
| response = NextResponse.json(data, { status: 200 }); | ||
| } | ||
| } catch (e: any) { | ||
| console.error( | ||
| "[API Route] POST: Exception in POST handler:", | ||
| e.message, | ||
| e.stack, | ||
| ); | ||
| if (e instanceof SyntaxError && e.message.toLowerCase().includes("json")) { | ||
| response = NextResponse.json( | ||
| { error: "Invalid JSON in request body" }, | ||
| { status: 400 }, | ||
| ); | ||
| } else { | ||
| response = NextResponse.json( | ||
| { error: "An unexpected error occurred processing your request." }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
| console.log( | ||
| "[API Route] POST: Sending final response with status:", | ||
| response.status, | ||
| ); | ||
| return cors(request, response) as NextResponse; | ||
| } | ||
|
|
||
| export async function OPTIONS(request: NextRequest) { | ||
| const response = new NextResponse(null, { status: 204 }); | ||
| return cors(request, response) as NextResponse; | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import OpenAI from "openai"; | ||
| import { EmbeddingSettings, Provider } from "~/types/llm"; | ||
| import { openaiConfig } from "./providers"; | ||
|
|
||
| const OPENAI_REQUEST_TIMEOUT_MS = 30000; | ||
|
|
||
| const openaiEmbedding = async ( | ||
| input: string | string[], | ||
| settings: EmbeddingSettings, | ||
| ): Promise<number[] | number[][] | undefined> => { | ||
| const config = openaiConfig; | ||
| const apiKey = process.env[config.apiKeyEnvVar]; | ||
| if (!apiKey) | ||
| throw new Error( | ||
| `API key not configured. Please set the ${config.apiKeyEnvVar} environment variable in your Vercel project settings.`, | ||
| ); | ||
| const openai = new OpenAI({ apiKey: apiKey }); | ||
|
|
||
| let options: OpenAI.EmbeddingCreateParams = { | ||
| model: settings.model, | ||
| input, | ||
| }; | ||
| if (settings.dimensions) { | ||
| options = { ...options, ...{ dimensions: settings.dimensions } }; | ||
| } | ||
|
|
||
| const embeddingsPromise = openai!.embeddings.create(options); | ||
| const timeoutPromise = new Promise<never>((_, reject) => | ||
| setTimeout( | ||
| () => reject(new Error("OpenAI API request timeout")), | ||
| OPENAI_REQUEST_TIMEOUT_MS, | ||
| ), | ||
| ); | ||
|
|
||
| const response = await Promise.race([embeddingsPromise, timeoutPromise]); | ||
| const embeddings = response.data.map((d) => d.embedding); | ||
| if (Array.isArray(input)) return embeddings; | ||
| else return embeddings[0]; | ||
| }; | ||
|
|
||
| export const genericEmbedding = async ( | ||
| input: string | string[], | ||
| settings: EmbeddingSettings, | ||
| provider: Provider = "openai", | ||
| ): Promise<number[] | number[][] | undefined> => { | ||
| if (provider == "openai") { | ||
| return await openaiEmbedding(input, settings); | ||
| } else { | ||
| // Note: There are two paths here. | ||
| // Earlier code choose to add openai to dependencies and use the library. It's what I had built on. | ||
| // We could follow that pattern, add anthropic/gemini, and use those in the handlers as well. | ||
| // The new code pattern uses direct api calls and structures. | ||
| // It should not be too considerable an effort to extend the LLMProviderConfig for embeddings. | ||
| // Either way is minimal work, but I think neither should be pursued without discussing | ||
| // the implicit tradeoff: More dependencies vs more resilience to API changes. | ||
| // right now I choose to minimize changes to my work to reduce scope. | ||
| throw Error("Not implemented"); | ||
| } | ||
| }; | ||
maparent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.