import { CHAT_SETTING_LIMITS } from "@/lib/chat-setting-limits" import { checkApiKey, getServerProfile } from "@/lib/server/server-chat-helpers" import { ChatSettings } from "@/types" import { OpenAIStream, StreamingTextResponse } from "ai" import OpenAI from "openai" export const runtime = "edge" export async function POST(request: Request) { const json = await request.json() const { chatSettings, messages } = json as { chatSettings: ChatSettings messages: any[] } try { const profile = await getServerProfile() checkApiKey(profile.groq_api_key, "G") // Groq is compatible with the OpenAI SDK const groq = new OpenAI({ apiKey: profile.groq_api_key || "", baseURL: "https://api.groq.com/openai/v1" }) const response = await groq.chat.completions.create({ model: chatSettings.model, messages, max_tokens: CHAT_SETTING_LIMITS[chatSettings.model].MAX_TOKEN_OUTPUT_LENGTH, stream: true }) // Convert the response into a friendly text-stream. const stream = OpenAIStream(response) // Respond with the stream return new StreamingTextResponse(stream) } catch (error: any) { let errorMessage = error.message || "An unexpected error occurred" const errorCode = error.status || 500 if (errorMessage.toLowerCase().includes("api key not found")) { errorMessage = "Groq API Key not found. Please set it in your profile settings." } else if (errorCode === 401) { errorMessage = "Groq API Key is incorrect. Please fix it in your profile settings." } return new Response(JSON.stringify({ message: errorMessage }), { status: errorCode }) } }