89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import 'dotenv/config';
|
|
import OpenAI from 'openai';
|
|
|
|
const ovhAI = new OpenAI({
|
|
apiKey: process.env.OVHCLOUD_API_KEY,
|
|
baseURL: 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1'
|
|
});
|
|
|
|
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 ovhAI.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.');
|
|
}
|
|
}
|