From 256868e2fe60276934eeb75db1376df87b33958b Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 15 Jan 2025 20:59:59 -0800 Subject: [PATCH] tool call tests --- core/index.d.ts | 8 +++ core/llm/llm.test.ts | 84 ++++++++++++++++++++++++++++---- core/llm/llms/Anthropic.ts | 6 +++ core/llm/openaiTypeConverters.ts | 1 + 4 files changed, 90 insertions(+), 9 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index 13cd7dc65..518e301d9 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -860,6 +860,13 @@ export interface Tool { uri?: string; } +interface ToolChoice { + type: "function"; + function: { + name: string; + }; +} + export interface BaseCompletionOptions { temperature?: number; topP?: number; @@ -877,6 +884,7 @@ export interface BaseCompletionOptions { stream?: boolean; prediction?: Prediction; tools?: Tool[]; + toolChoice?: ToolChoice; } export interface ModelCapability { diff --git a/core/llm/llm.test.ts b/core/llm/llm.test.ts index 8c4c82c25..701be7be3 100644 --- a/core/llm/llm.test.ts +++ b/core/llm/llm.test.ts @@ -1,14 +1,13 @@ import * as dotenv from "dotenv"; -import { CompletionOptions } from ".."; +import { AssistantChatMessage, CompletionOptions } from ".."; +import { BaseLLM } from "."; import Anthropic from "./llms/Anthropic"; import Gemini from "./llms/Gemini"; import Mistral from "./llms/Mistral"; import OpenAI from "./llms/OpenAI"; -import { BaseLLM } from "."; - dotenv.config(); const COMPLETION_OPTIONS: Partial = { @@ -23,7 +22,11 @@ const COMPLETION_OPTIONS: Partial = { function testLLM( llm: BaseLLM, - { skip, testFim }: { skip?: boolean; testFim?: boolean }, + { + skip, + testFim, + testToolCall, + }: { skip?: boolean; testFim?: boolean; testToolCall?: boolean }, ) { if (skip) { return; @@ -82,6 +85,66 @@ function testLLM( return; }); } + + if (testToolCall) { + test("Tool Call works", async () => { + let args = ""; + let isFirstChunk = true; + for await (const chunk of llm.streamChat( + [{ role: "user", content: "Hi, my name is Nate." }], + new AbortController().signal, + { + tools: [ + { + displayTitle: "Say Hello", + function: { + name: "say_hello", + description: "Say Hello", + parameters: { + type: "object", + properties: { + name: { + type: "string", + description: "The name of the person to greet", + }, + }, + }, + }, + type: "function", + wouldLikeTo: "Say hello", + readonly: true, + }, + ], + toolChoice: { + type: "function", + function: { + name: "say_hello", + }, + }, + }, + )) { + const typedChunk = chunk as AssistantChatMessage; + if (!typedChunk.toolCalls) { + continue; + } + const toolCall = typedChunk.toolCalls[0]; + args += toolCall.function?.arguments ?? ""; + + expect(chunk.role).toBe("assistant"); + expect(chunk.content).toBe(""); + expect(typedChunk.toolCalls).toHaveLength(1); + + if (isFirstChunk) { + isFirstChunk = false; + expect(toolCall.id).toBeDefined(); + expect(toolCall.function!.name).toBe("say_hello"); + } + } + + const parsedArgs = JSON.parse(args); + expect(parsedArgs.name).toBe("Nate"); + }); + } }); } @@ -91,12 +154,15 @@ describe("LLM", () => { model: "claude-3-5-sonnet-latest", apiKey: process.env.ANTHROPIC_API_KEY, }), - { skip: false }, - ); - testLLM( - new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "gpt-3.5-turbo" }), - { skip: false }, + { + skip: false, + testToolCall: true, + }, ); + testLLM(new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "gpt-4o" }), { + skip: false, + testToolCall: true, + }); testLLM( new OpenAI({ apiKey: process.env.OPENAI_API_KEY, model: "o1-preview" }), { skip: false }, diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index 234c16860..df527d579 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -30,6 +30,12 @@ class Anthropic extends BaseLLM { description: tool.function.description, input_schema: tool.function.parameters, })), + tool_choice: options.toolChoice + ? { + type: "tool", + name: options.toolChoice.function.name, + } + : undefined, }; return finalOptions; diff --git a/core/llm/openaiTypeConverters.ts b/core/llm/openaiTypeConverters.ts index 057219beb..839efda4e 100644 --- a/core/llm/openaiTypeConverters.ts +++ b/core/llm/openaiTypeConverters.ts @@ -80,6 +80,7 @@ export function toChatBody( stream: options.stream ?? true, stop: options.stop, prediction: options.prediction, + tool_choice: options.toolChoice, }; if (options.tools?.length) {