Tool Use
Function Calling — OpenAI-Format auf jeden EU-Provider übersetzt.
CleverRouter mapt das OpenAI-Tool-Format auf das jeweilige Provider-Schema (Mistral, Anthropic, Bedrock). Du nutzt denselben Request-Body wie bei OpenAI — wir übersetzen.
Tool-Loop
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.CLEVERROUTER_API_KEY!,
baseURL: 'https://cleverouter.eu/v1',
});
const tools = [
{
type: 'function' as const,
function: {
name: 'get_weather',
description: 'Aktuelles Wetter für eine Stadt.',
parameters: {
type: 'object',
properties: {
city: { type: 'string' },
unit: { type: 'string', enum: ['c', 'f'], default: 'c' },
},
required: ['city'],
},
},
},
];
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: 'user', content: 'Wie ist das Wetter in Paris?' },
];
const first = await client.chat.completions.create({
model: 'mistral/mistral-large-2',
messages,
tools,
});
const toolCalls = first.choices[0]?.message.tool_calls;
if (toolCalls?.length) {
messages.push(first.choices[0].message);
for (const call of toolCalls) {
const args = JSON.parse(call.function.arguments);
const result = await getWeather(args.city, args.unit);
messages.push({
role: 'tool',
tool_call_id: call.id,
content: JSON.stringify(result),
});
}
const final = await client.chat.completions.create({
model: 'mistral/mistral-large-2',
messages,
});
console.log(final.choices[0]?.message.content);
}Parallele Tool-Calls
Mistral und Anthropic erzeugen häufig parallele Tool-Calls. Sammle alle, führe sie parallel aus, häng alle Resultate gemeinsam an — nicht sequentiell.
Streaming + Tools
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta;
if (delta?.tool_calls) {
// partial arguments — sammeln bis "finish_reason: tool_calls"
}
}