( projectId: string, input: ChatCompletionCreateParams, )
| 45 | input: ChatCompletionCreateParams, |
| 46 | ): Promise<ChatCompletion | Stream<ChatCompletionChunk>>; |
| 47 | export async function getOpenaiCompletion( |
| 48 | projectId: string, |
| 49 | input: ChatCompletionCreateParams, |
| 50 | ): Promise<ChatCompletion | Stream<ChatCompletionChunk>> { |
| 51 | const apiKeys = await prisma.apiKey.findMany({ |
| 52 | where: { projectId: projectId }, |
| 53 | }); |
| 54 | |
| 55 | const openaiApiKey = apiKeys.find((key) => key.provider === "OPENAI")?.apiKey; |
| 56 | |
| 57 | if (!openaiApiKey) { |
| 58 | throw new Error("No OpenAI API key found"); |
| 59 | } |
| 60 | |
| 61 | const openai = new OriginalOpenAI({ apiKey: openaiApiKey }); |
| 62 | |
| 63 | return await openai.chat.completions.create({ |
| 64 | ...input, |
| 65 | tools: input.tools?.length ? input.tools : undefined, |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | // Fall back to our own Azure instance for long-running requests |
| 70 | export async function getAzureGpt4Completion(input: ChatCompletionCreateParams) { |
no test coverage detected