-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,82 @@ | ||
import { NextResponse } from "next/server"; | ||
import OpenAI from "openai"; | ||
import { GameState } from "@/types/game"; | ||
import { generatePrompt, getSystemPromptO1 } from "@/utils/prompts"; | ||
import { delay } from "@/utils/gameUtils"; | ||
import { observeOpenAI } from "langfuse"; | ||
|
||
const MAX_RETRIES = 2; | ||
|
||
const createOpenAIClient = async ( | ||
role: "CLUE_GIVER" | "GUESSER", | ||
sessionId: string, | ||
gameState: GameState, | ||
) => { | ||
return observeOpenAI( | ||
new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}), | ||
{ | ||
generationName: role === "CLUE_GIVER" ? "clue giver" : "guesser", | ||
metadata: { | ||
currentTeam: gameState.currentTeam, | ||
}, | ||
sessionId: sessionId, | ||
}, | ||
); | ||
}; | ||
|
||
export async function POST(request: Request) { | ||
try { | ||
const body = await request.json(); | ||
const { | ||
role, | ||
sessionId, | ||
gameState, | ||
}: { | ||
role: "CLUE_GIVER" | "GUESSER"; | ||
sessionId: string; | ||
gameState: GameState; | ||
} = body; | ||
|
||
const openai = await createOpenAIClient(role, sessionId, gameState); | ||
|
||
const prompt = getSystemPromptO1(role) + generatePrompt(role, gameState); | ||
|
||
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) { | ||
try { | ||
const completion = await openai.chat.completions.create({ | ||
model: "o1-mini-2024-09-12", | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: prompt, | ||
}, | ||
], | ||
}); | ||
|
||
return NextResponse.json({ | ||
response: completion.choices[0].message.content, | ||
}); | ||
} catch (error) { | ||
console.error( | ||
`GPT API Error (attempt ${attempt + 1}/${MAX_RETRIES + 1}):`, | ||
error, | ||
); | ||
await delay(1000); | ||
if (attempt === MAX_RETRIES) break; | ||
} | ||
} | ||
|
||
return NextResponse.json( | ||
{ error: "Failed to process request after multiple attempts" }, | ||
{ status: 500 }, | ||
); | ||
} catch (error) { | ||
console.error("Request processing error:", error); | ||
return NextResponse.json( | ||
{ error: "Failed to process request" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
This file contains 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 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 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