fixing api calls
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { openai, defaultModel, serviceOptions, supportsWebSearch } from '../agent.js';
|
||||
import { openai, defaultModel, serviceOptions, supportsWebSearch, parseWithRetry } from '../agent.js';
|
||||
import type { InterpreterOutput, RankingOutput, CuratorOutput, MediaType } from '../types/agents.js';
|
||||
import { z } from 'zod';
|
||||
import { zodTextFormat } from 'openai/helpers/zod';
|
||||
@@ -14,6 +14,8 @@ const CuratorSchema = z.object({
|
||||
}))
|
||||
});
|
||||
|
||||
const CHUNK_SIZE = 20;
|
||||
|
||||
export async function runCurator(
|
||||
ranking: RankingOutput,
|
||||
interpreter: InterpreterOutput,
|
||||
@@ -32,19 +34,16 @@ export async function runCurator(
|
||||
|
||||
if (allShows.length === 0) return [];
|
||||
|
||||
const showList = allShows
|
||||
.map((s) => `- "${s.title}" (${s.category})`)
|
||||
.join('\n');
|
||||
|
||||
const canSearch = useWebSearch && supportsWebSearch;
|
||||
const response = await openai.responses.parse({
|
||||
model: defaultModel,
|
||||
temperature: 0.5,
|
||||
max_completion_tokens: 16384,
|
||||
...serviceOptions,
|
||||
...(canSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
|
||||
text: { format: zodTextFormat(CuratorSchema, "shows") },
|
||||
instructions: `You are a ${mediaLabel} recommendation curator. For each ${mediaLabel}, write a concise explanation and surface the most useful details for the user.${useWebSearch ? '\n\nUse web search to verify details and enrich explanations with accurate information.' : ''}
|
||||
const preferenceSummary = `User preferences summary:
|
||||
Liked: ${interpreter.liked.join(', ') || '(none)'}
|
||||
Themes: ${interpreter.themes.join(', ') || '(none)'}
|
||||
Tone: ${interpreter.tone.join(', ') || '(none)'}
|
||||
Character preferences: ${interpreter.character_preferences.join(', ') || '(none)'}
|
||||
Avoid: ${interpreter.avoid.join(', ') || '(none)'}
|
||||
Requirements: ${interpreter.requirements.join(', ') || '(none)'}`;
|
||||
|
||||
const instructions = `You are a ${mediaLabel} recommendation curator. For each ${mediaLabel}, write a concise explanation and surface the most useful details for the user.${useWebSearch ? '\n\nUse web search to verify details and enrich explanations with accurate information.' : ''}
|
||||
|
||||
Rules:
|
||||
- Preserve the exact title and category as given
|
||||
@@ -52,18 +51,31 @@ Rules:
|
||||
- genre: 1-3 words capturing the most prominent genre of the title (e.g. "Crime Drama", "Sci-Fi Thriller", "Romantic Comedy")
|
||||
- pros: up to 3 short bullet points about what this title does well relative to the user's taste
|
||||
- cons: up to 3 short bullet points about what the user might not like based on their preferences
|
||||
- Be honest — explain why "Questionable" or "Will Not Like" ${mediaLabel}s got that rating`,
|
||||
input: `User preferences summary:
|
||||
Liked: ${interpreter.liked.join(', ') || '(none)'}
|
||||
Themes: ${interpreter.themes.join(', ') || '(none)'}
|
||||
Tone: ${interpreter.tone.join(', ') || '(none)'}
|
||||
Character preferences: ${interpreter.character_preferences.join(', ') || '(none)'}
|
||||
Avoid: ${interpreter.avoid.join(', ') || '(none)'}
|
||||
Requirements: ${interpreter.requirements.join(', ') || '(none)'}
|
||||
- Be honest — explain why "Questionable" or "Will Not Like" ${mediaLabel}s got that rating`;
|
||||
|
||||
const chunks: typeof allShows[] = [];
|
||||
for (let i = 0; i < allShows.length; i += CHUNK_SIZE) {
|
||||
chunks.push(allShows.slice(i, i + CHUNK_SIZE));
|
||||
}
|
||||
|
||||
const results: CuratorOutput[] = [];
|
||||
for (const chunk of chunks) {
|
||||
const showList = chunk.map((s) => `- "${s.title}" (${s.category})`).join('\n');
|
||||
const response = await parseWithRetry(() => openai.responses.parse({
|
||||
model: defaultModel,
|
||||
temperature: 0.5,
|
||||
max_completion_tokens: 16384,
|
||||
...serviceOptions,
|
||||
...(canSearch ? { tools: [{ type: 'web_search' as const }] } : {}),
|
||||
text: { format: zodTextFormat(CuratorSchema, "shows") },
|
||||
instructions,
|
||||
input: `${preferenceSummary}
|
||||
|
||||
${mediaLabel}s to describe:
|
||||
${showList}`,
|
||||
});
|
||||
}));
|
||||
results.push(...(response.output_parsed?.shows ?? []));
|
||||
}
|
||||
|
||||
return response.output_parsed?.shows ?? [];
|
||||
return results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user