Request JSON analysis from OpenRouter
Some checks failed
Build and deploy Worker / build (push) Has been cancelled
Some checks failed
Build and deploy Worker / build (push) Has been cancelled
This commit is contained in:
@@ -4,10 +4,14 @@ import { prisma } from '../prisma.js';
|
|||||||
|
|
||||||
type OpenRouterResponse = {
|
type OpenRouterResponse = {
|
||||||
choices?: Array<{
|
choices?: Array<{
|
||||||
|
finish_reason?: string;
|
||||||
message?: {
|
message?: {
|
||||||
content?: string;
|
content?: string;
|
||||||
};
|
};
|
||||||
}>;
|
}>;
|
||||||
|
error?: {
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type PlaceAnalysis = {
|
type PlaceAnalysis = {
|
||||||
@@ -84,11 +88,12 @@ async function buildPlaceAnalysis(input: {
|
|||||||
model: config.openRouterModel,
|
model: config.openRouterModel,
|
||||||
temperature: 0.1,
|
temperature: 0.1,
|
||||||
max_tokens: 900,
|
max_tokens: 900,
|
||||||
|
response_format: { type: 'json_object' },
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content:
|
content:
|
||||||
'You classify voice reviews about places. Return only valid JSON. Use only allowed ontology tags.',
|
'You classify voice reviews about places. Return one valid JSON object in message.content. Do not return markdown. Use only allowed ontology tags.',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -118,13 +123,19 @@ async function buildPlaceAnalysis(input: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`OpenRouter analysis failed with ${response.status}.`);
|
const errorBody = await response.text();
|
||||||
|
throw new Error(
|
||||||
|
`OpenRouter analysis failed with ${response.status}: ${errorBody.slice(0, 500)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = (await response.json()) as OpenRouterResponse;
|
const payload = (await response.json()) as OpenRouterResponse;
|
||||||
const content = payload.choices?.[0]?.message?.content;
|
const choice = payload.choices?.[0];
|
||||||
|
const content = choice?.message?.content?.trim();
|
||||||
if (!content) {
|
if (!content) {
|
||||||
throw new Error('OpenRouter returned an empty analysis.');
|
throw new Error(
|
||||||
|
`OpenRouter returned an empty analysis for ${config.openRouterModel}; finish_reason=${choice?.finish_reason ?? 'missing'}.`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const analysis = JSON.parse(content) as PlaceAnalysis;
|
const analysis = JSON.parse(content) as PlaceAnalysis;
|
||||||
|
|||||||
Reference in New Issue
Block a user