This repository has been archived on 2026-02-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
synthetic-consumer-data/utils/aiGatewayClient.ts
Riccardo Senica da98e0345d
All checks were successful
Deploy / lint-build-deploy (push) Successful in 2m28s
fix: tweak llama usage
2026-01-19 20:36:13 +01:00

121 lines
3.3 KiB
TypeScript

import 'dotenv/config';
import OpenAI from 'openai';
let ovhAI: OpenAI | null = null;
function getClient(): OpenAI {
if (!ovhAI) {
ovhAI = new OpenAI({
apiKey: process.env.OVHCLOUD_API_KEY,
baseURL: 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1'
});
}
return ovhAI;
}
export interface BaseTool {
readonly name: string;
readonly input_schema: {
readonly type: 'object';
readonly properties: Record<string, unknown>;
readonly required?: readonly string[];
readonly description?: string;
};
}
export interface ToolUseBlock {
type: 'tool_use';
id: string;
name: string;
input: Record<string, unknown>;
}
const MAX_RETRIES = 3;
export async function makeRequest<T extends BaseTool>(
prompt: string,
toolDef: T
): Promise<Record<string, unknown>> {
const requiredFields = toolDef.input_schema.required
? [...toolDef.input_schema.required]
: Object.keys(toolDef.input_schema.properties);
let lastError: Error | null = null;
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
console.log(`AI request attempt ${attempt}/${MAX_RETRIES}`);
const completion = await getClient().chat.completions.create({
model: 'Meta-Llama-3_3-70B-Instruct',
temperature: 0.7,
max_tokens: 16000,
tools: [
{
type: 'function',
function: {
name: toolDef.name,
description: toolDef.input_schema.description || '',
parameters: {
type: 'object',
properties: toolDef.input_schema.properties,
required: requiredFields
}
}
}
],
tool_choice: {
type: 'function',
function: { name: toolDef.name }
},
messages: [
{
role: 'system',
content: `You are a data generation assistant that creates realistic synthetic data.
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
}
]
});
const message = completion.choices[0]?.message;
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));
}
}
}
console.error('All retry attempts failed');
throw lastError || new Error('OVH AI Endpoints client error.');
}