-
Notifications
You must be signed in to change notification settings - Fork 2
DGAI: Create embedding api route - ENG-291 #146
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
Merged
mdroidian
merged 6 commits into
eng-233-suggestive-mode-internal
from
add-embedding-endpoint
May 8, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 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 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,86 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import OpenAI from "openai"; | ||
| import cors from "~/utils/llm/cors"; | ||
|
|
||
| const apiKey = process.env.OPENAI_API_KEY; | ||
|
|
||
| if (!apiKey) { | ||
| console.error( | ||
| "Missing OPENAI_API_KEY environment variable. The embeddings API will not function.", | ||
| ); | ||
| } | ||
|
|
||
| const openai = apiKey ? new OpenAI({ apiKey }) : null; | ||
|
|
||
| type RequestBody = { | ||
| input: string | string[]; | ||
| }; | ||
|
|
||
| const OPENAI_REQUEST_TIMEOUT_MS = 30000; | ||
|
|
||
| export async function POST(req: NextRequest): Promise<NextResponse> { | ||
| let response: NextResponse; | ||
|
|
||
| if (!apiKey) { | ||
| response = NextResponse.json( | ||
| { | ||
| error: "Server configuration error.", | ||
| details: "Embeddings service is not configured.", | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| return cors(req, response) as NextResponse; | ||
| } | ||
|
|
||
| try { | ||
| const body: RequestBody = await req.json(); | ||
| const { input } = 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 options: OpenAI.EmbeddingCreateParams = { | ||
| model: "text-embedding-3-small", | ||
| input: input, | ||
| encoding_format: "float", | ||
| dimensions: 1536, | ||
| }; | ||
|
|
||
| const embeddingsPromise = openai!.embeddings.create(options); | ||
| const timeoutPromise = new Promise((_, reject) => | ||
| setTimeout( | ||
| () => reject(new Error("OpenAI API request timeout")), | ||
| OPENAI_REQUEST_TIMEOUT_MS, | ||
| ), | ||
| ); | ||
|
|
||
| const openAIResponse = await Promise.race([ | ||
| embeddingsPromise, | ||
| timeoutPromise, | ||
| ]); | ||
|
|
||
| response = NextResponse.json(openAIResponse, { status: 200 }); | ||
| } catch (error: unknown) { | ||
| console.error("Error calling OpenAI Embeddings API:", error); | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : "Unknown error"; | ||
| response = NextResponse.json( | ||
| { | ||
| error: "Failed to generate embeddings.", | ||
| details: errorMessage, | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| return cors(req, response) as NextResponse; | ||
| } | ||
|
|
||
| export async function OPTIONS(req: NextRequest): Promise<NextResponse> { | ||
| return cors(req, new NextResponse(null, { status: 204 })) 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
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.