This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import 'dotenv/config';
|
||||
import { generateText, tool, jsonSchema } from 'ai';
|
||||
import type { JSONSchema7 } from 'json-schema';
|
||||
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;
|
||||
@@ -24,43 +28,61 @@ export async function makeRequest<T extends BaseTool>(
|
||||
toolDef: T
|
||||
): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
const { steps } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4.5',
|
||||
const completion = await ovhAI.chat.completions.create({
|
||||
model: 'Meta-Llama-3_3-70B-Instruct',
|
||||
temperature: 1,
|
||||
tools: {
|
||||
[toolDef.name]: tool({
|
||||
description: toolDef.input_schema.description || '',
|
||||
inputSchema: jsonSchema(toolDef.input_schema as JSONSchema7),
|
||||
execute: async args => args
|
||||
})
|
||||
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 }
|
||||
},
|
||||
toolChoice: {
|
||||
type: 'tool',
|
||||
toolName: toolDef.name
|
||||
},
|
||||
prompt
|
||||
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 toolCalls = steps.flatMap(step => step.toolCalls);
|
||||
const message = completion.choices[0]?.message;
|
||||
|
||||
if (!toolCalls || toolCalls.length === 0) {
|
||||
throw new Error('No tool calls found in response');
|
||||
if (!message?.tool_calls || message.tool_calls.length === 0) {
|
||||
throw new Error('No function call found in response');
|
||||
}
|
||||
|
||||
const typedCall = toolCalls[0] as unknown as {
|
||||
toolName: string;
|
||||
input: Record<string, unknown>;
|
||||
};
|
||||
const toolCall = message.tool_calls[0];
|
||||
|
||||
if (typedCall.toolName !== toolDef.name) {
|
||||
if (toolCall.function.name !== toolDef.name) {
|
||||
throw new Error(
|
||||
`Expected tool ${toolDef.name} but got ${typedCall.toolName}`
|
||||
`Expected tool ${toolDef.name} but got ${toolCall.function.name}`
|
||||
);
|
||||
}
|
||||
|
||||
return typedCall.input;
|
||||
const result = JSON.parse(toolCall.function.arguments);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Error making request:', error);
|
||||
throw Error('Vercel AI Gateway client error.');
|
||||
throw Error('OVH AI Endpoints client error.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user