fix: tweak llama usage
All checks were successful
Deploy / lint-build-deploy (push) Successful in 2m28s
All checks were successful
Deploy / lint-build-deploy (push) Successful in 2m28s
This commit is contained in:
@@ -30,66 +30,91 @@ export interface ToolUseBlock {
|
|||||||
input: Record<string, unknown>;
|
input: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_RETRIES = 3;
|
||||||
|
|
||||||
export async function makeRequest<T extends BaseTool>(
|
export async function makeRequest<T extends BaseTool>(
|
||||||
prompt: string,
|
prompt: string,
|
||||||
toolDef: T
|
toolDef: T
|
||||||
): Promise<Record<string, unknown>> {
|
): Promise<Record<string, unknown>> {
|
||||||
try {
|
const requiredFields = toolDef.input_schema.required
|
||||||
const completion = await getClient().chat.completions.create({
|
? [...toolDef.input_schema.required]
|
||||||
model: 'Meta-Llama-3_3-70B-Instruct',
|
: Object.keys(toolDef.input_schema.properties);
|
||||||
temperature: 1,
|
|
||||||
max_tokens: 16000,
|
let lastError: Error | null = null;
|
||||||
tools: [
|
|
||||||
{
|
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
||||||
type: 'function',
|
try {
|
||||||
function: {
|
console.log(`AI request attempt ${attempt}/${MAX_RETRIES}`);
|
||||||
name: toolDef.name,
|
|
||||||
description: toolDef.input_schema.description || '',
|
const completion = await getClient().chat.completions.create({
|
||||||
parameters: {
|
model: 'Meta-Llama-3_3-70B-Instruct',
|
||||||
type: 'object',
|
temperature: 0.7,
|
||||||
properties: toolDef.input_schema.properties,
|
max_tokens: 16000,
|
||||||
required: toolDef.input_schema.required
|
tools: [
|
||||||
? [...toolDef.input_schema.required]
|
{
|
||||||
: undefined
|
type: 'function',
|
||||||
|
function: {
|
||||||
|
name: toolDef.name,
|
||||||
|
description: toolDef.input_schema.description || '',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: toolDef.input_schema.properties,
|
||||||
|
required: requiredFields
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
],
|
||||||
],
|
tool_choice: {
|
||||||
tool_choice: {
|
type: 'function',
|
||||||
type: 'function',
|
function: { name: toolDef.name }
|
||||||
function: { name: toolDef.name }
|
|
||||||
},
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: 'system',
|
|
||||||
content:
|
|
||||||
'You are a data generation assistant. Generate realistic, diverse synthetic data. You must respond ONLY with the function call. Do not include any text outside the function call.'
|
|
||||||
},
|
},
|
||||||
{
|
messages: [
|
||||||
role: 'user',
|
{
|
||||||
content: prompt
|
role: 'system',
|
||||||
}
|
content: `You are a data generation assistant that creates realistic synthetic data.
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = completion.choices[0]?.message;
|
CRITICAL REQUIREMENTS:
|
||||||
|
1. You MUST call the function with ALL required fields populated
|
||||||
|
2. Required top-level fields: ${requiredFields.join(', ')}
|
||||||
|
3. Every nested object and array must be fully populated with realistic data
|
||||||
|
4. Do NOT leave any field as null, undefined, or empty
|
||||||
|
5. Generate diverse, realistic Italian consumer data`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: prompt
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
if (!message?.tool_calls || message.tool_calls.length === 0) {
|
const message = completion.choices[0]?.message;
|
||||||
throw new Error('No function call found in response');
|
|
||||||
|
if (!message?.tool_calls || message.tool_calls.length === 0) {
|
||||||
|
throw new Error('No function call found in response');
|
||||||
|
}
|
||||||
|
|
||||||
|
const toolCall = message.tool_calls[0];
|
||||||
|
|
||||||
|
if (toolCall.function.name !== toolDef.name) {
|
||||||
|
throw new Error(
|
||||||
|
`Expected tool ${toolDef.name} but got ${toolCall.function.name}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = JSON.parse(toolCall.function.arguments);
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Attempt ${attempt} failed:`, error);
|
||||||
|
lastError = error as Error;
|
||||||
|
|
||||||
|
if (attempt < MAX_RETRIES) {
|
||||||
|
const delay = 1000 * attempt;
|
||||||
|
console.log(`Retrying in ${delay}ms...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, delay));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const toolCall = message.tool_calls[0];
|
|
||||||
|
|
||||||
if (toolCall.function.name !== toolDef.name) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected tool ${toolDef.name} but got ${toolCall.function.name}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = JSON.parse(toolCall.function.arguments);
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error making request:', error);
|
|
||||||
throw Error('OVH AI Endpoints client error.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.error('All retry attempts failed');
|
||||||
|
throw lastError || new Error('OVH AI Endpoints client error.');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user