( request: FastifyRequestTypebox<typeof GenerateImageResponseSchema>, reply: FastifyReplyTypebox<typeof GenerateImageResponseSchema>, )
| 371 | }; |
| 372 | |
| 373 | const generateImagesWithDALL_E = async ( |
| 374 | request: FastifyRequestTypebox<typeof GenerateImageResponseSchema>, |
| 375 | reply: FastifyReplyTypebox<typeof GenerateImageResponseSchema>, |
| 376 | ) => { |
| 377 | const { numImages, prompt, size, model } = request.body; |
| 378 | |
| 379 | const openai = new OpenAI({ |
| 380 | apiKey: process.env.OPENAI_API_KEY, |
| 381 | organization: process.env.OPENAI_ORG, |
| 382 | }); |
| 383 | |
| 384 | const promises = Array(numImages) |
| 385 | .fill(null) |
| 386 | .map(() => { |
| 387 | return openai.images |
| 388 | .generate({ |
| 389 | model: model ?? "dall-e-3", |
| 390 | prompt: prompt, |
| 391 | n: 1, |
| 392 | size: size, |
| 393 | }) |
| 394 | .catch(() => { |
| 395 | throw new ApiError(500, "Something went wrong with the AI please try again later"); |
| 396 | }); |
| 397 | }); |
| 398 | |
| 399 | try { |
| 400 | const responses = await Promise.all(promises); |
| 401 | const imageData = responses.flatMap(response => response["data"]); |
| 402 | |
| 403 | if (!imageData.length) { |
| 404 | throw new ApiError(406, "Sorry our AI is currently overloaded please try again"); |
| 405 | } |
| 406 | reply.code(200).send({ response: imageData }); |
| 407 | } catch (e) { |
| 408 | Sentry.captureException(e); |
| 409 | throw new ApiError(500, "Something went wrong with the AI please try again later"); |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | const generateImagesWithStableDiffusion = async ( |
| 414 | request: FastifyRequestTypebox<typeof GenerateImageResponseSchema>, |
no outgoing calls
no test coverage detected