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 04be7a66c0
Some checks failed
Deploy / lint-build-deploy (push) Failing after 6s
fix: use lazy loading
2026-01-19 20:19:54 +01:00

96 lines
2.4 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>;
}
export async function makeRequest<T extends BaseTool>(
prompt: string,
toolDef: T
): Promise<Record<string, unknown>> {
try {
const completion = await getClient().chat.completions.create({
model: 'Meta-Llama-3_3-70B-Instruct',
temperature: 1,
max_tokens: 16000,
tools: [
{
type: 'function',
function: {
name: toolDef.name,
description: toolDef.input_schema.description || '',
parameters: {
type: 'object',
properties: toolDef.input_schema.properties,
required: toolDef.input_schema.required
? [...toolDef.input_schema.required]
: undefined
}
}
}
],
tool_choice: {
type: 'function',
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.'
},
{
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('Error making request:', error);
throw Error('OVH AI Endpoints client error.');
}
}