(request: NextRequest)
| 20 | }); |
| 21 | |
| 22 | export async function POST(request: NextRequest) { |
| 23 | // Logging the start of the image processing API call |
| 24 | console.log('Starting the image processing API call'); |
| 25 | |
| 26 | // Extracting the file (in base64 format) and an optional custom prompt |
| 27 | // from the request body. This is essential for processing the image using OpenAI's API. |
| 28 | const { file: base64Image, prompt: customPrompt } = await request.json(); |
| 29 | |
| 30 | // Check if the image file is included in the request. If not, return an error response. |
| 31 | if (!base64Image) { |
| 32 | console.error('No file found in the request'); |
| 33 | return NextResponse.json({ success: false, message: 'No file found' }); |
| 34 | } |
| 35 | |
| 36 | // Log the receipt of the image in base64 format |
| 37 | console.log('Received image in base64 format'); |
| 38 | |
| 39 | // Utilize the provided custom prompt or a default prompt if it's not provided. |
| 40 | // This prompt guides the analysis of the image by OpenAI's model. |
| 41 | const promptText = customPrompt || "Analyze and describe the image in detail. Focus on visual elements like colors, object details, people's positions and expressions, and the environment. Transcribe any text as 'Content: “[Text]”', noting font attributes. Aim for a clear, thorough representation of all visual and textual aspects."; |
| 42 | |
| 43 | // Log the chosen prompt |
| 44 | console.log(`Using prompt: ${promptText}`); |
| 45 | |
| 46 | // Sending the image and prompt to OpenAI for processing. This step is crucial for the image analysis. |
| 47 | console.log('Sending request to OpenAI'); |
| 48 | try { |
| 49 | const response = await openai.chat.completions.create({ |
| 50 | model: "gpt-4-vision-preview", |
| 51 | messages: [ |
| 52 | { |
| 53 | role: "user", |
| 54 | content: [ |
| 55 | { type: "text", text: promptText }, |
| 56 | { |
| 57 | type: "image_url", |
| 58 | image_url: { |
| 59 | url: base64Image |
| 60 | } |
| 61 | } |
| 62 | ] |
| 63 | } |
| 64 | ], |
| 65 | max_tokens: 200 |
| 66 | }); |
| 67 | |
| 68 | // Log the response received from OpenAI, which includes the analysis of the image. |
| 69 | console.log('Received response from OpenAI'); |
| 70 | console.log('Response:', JSON.stringify(response, null, 2)); // Log the response for debugging |
| 71 | |
| 72 | // Extract and log the analysis from the response |
| 73 | const analysis = response?.choices[0]?.message?.content; |
| 74 | console.log('Analysis:', analysis); |
| 75 | |
| 76 | // Return the analysis in the response |
| 77 | return NextResponse.json({ success: true, analysis: analysis }); |
| 78 | } catch (error) { |
| 79 | // Log and handle any errors encountered during the request to OpenAI |
nothing calls this directly
no outgoing calls
no test coverage detected