feat(models): add tool use support to OpenAIClient

This commit is contained in:
William Valentin
2026-02-05 17:44:04 -08:00
parent 36c1cfc768
commit 96ade25e98
2 changed files with 72 additions and 6 deletions
+24 -2
View File
@@ -33,15 +33,36 @@ export class OpenAIClient implements ModelClient {
messages.push({ role: msg.role, content: msg.content });
}
const response = await this.client.chat.completions.create({
// Build params, conditionally including tools
const params: OpenAI.ChatCompletionCreateParamsNonStreaming = {
model: this.model,
max_tokens: request.maxTokens ?? this.defaultMaxTokens,
messages,
});
};
if (request.tools && request.tools.length > 0) {
params.tools = request.tools.map(t => ({
type: 'function' as const,
function: {
name: t.name,
description: t.description,
parameters: t.input_schema as OpenAI.FunctionParameters,
},
}));
}
const response = await this.client.chat.completions.create(params);
const choice = response.choices[0];
const content = choice?.message?.content ?? '';
// Parse tool_calls from the response if present
const toolCalls = choice?.message?.tool_calls?.map((tc: OpenAI.ChatCompletionMessageToolCall) => ({
id: tc.id,
name: tc.function.name,
args: JSON.parse(tc.function.arguments),
})) ?? [];
return {
content,
stopReason: choice?.finish_reason ?? 'stop',
@@ -49,6 +70,7 @@ export class OpenAIClient implements ModelClient {
inputTokens: response.usage?.prompt_tokens ?? 0,
outputTokens: response.usage?.completion_tokens ?? 0,
},
...(toolCalls.length > 0 ? { toolCalls } : {}),
};
}
}