tool call tests

This commit is contained in:
Nate 2025-01-15 20:59:59 -08:00
parent 2cc8b758d4
commit 256868e2fe
4 changed files with 90 additions and 9 deletions

8
core/index.d.ts vendored
View file

@ -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 {

View file

@ -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<CompletionOptions> = {
@ -23,7 +22,11 @@ const COMPLETION_OPTIONS: Partial<CompletionOptions> = {
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 },

View file

@ -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;

View file

@ -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) {