Skip to content

Commit

Permalink
add standard test for openai formatted tools
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jun 12, 2024
1 parent 21a2a36 commit b755752
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ChatTogetherAIStandardUnitTests extends ChatModelUnitTests<
constructor() {
super({
Cls: ChatTogetherAI,
chatModelHasToolCalling: false,
chatModelHasStructuredOutput: false,
chatModelHasToolCalling: true,
chatModelHasStructuredOutput: true,
constructorArgs: {},
});
process.env.TOGETHER_AI_API_KEY = "test";
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain-standard-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"@jest/globals": "^29.5.0",
"@langchain/core": "workspace:*",
"zod": "^3.22.4"
"zod": "^3.22.4",
"zod-to-json-schema": "^3.23.0"
},
"devDependencies": {
"@langchain/scripts": "workspace:*",
Expand Down
39 changes: 39 additions & 0 deletions libs/langchain-standard-tests/src/integration_tests/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BaseChatModelsTestsFields,
RecordStringAny,
} from "../base.js";
import { zodToJsonSchema } from "zod-to-json-schema";

Check failure on line 18 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

`zod-to-json-schema` import should occur before import of `../base.js`

const adderSchema = /* #__PURE__ */ z
.object({
Expand Down Expand Up @@ -385,6 +386,37 @@ export abstract class ChatModelIntegrationTests<
expect([1, 2].includes(resultStringContent.parsed.b)).toBeTruthy();
}

async testBindToolsWithOpenAIFormattedTools() {
if (!this.chatModelHasToolCalling) {
console.log("Test requires tool calling. Skipping...");
return;
}

const model = new this.Cls(this.constructorArgs);
if (!model.bindTools) {
throw new Error(
"bindTools undefined. Cannot test OpenAI formatted tool calls."
);
}
const modelWithTools = model.bindTools([{
type: "function",
function: {
name: "math_addition",
description: adderSchema.description,
parameters: zodToJsonSchema(adderSchema)
}
}]);

const result: AIMessage = await modelWithTools.invoke("What is 1 + 2");
expect(result).toBeInstanceOf(AIMessage);
expect(result.tool_calls).toHaveLength(1);
if (!result.tool_calls) {
throw new Error("result.tool_calls is undefined");
}
const { tool_calls } = result;
expect(tool_calls[0].name).toBe("math_addition");
}

/**
* Run all unit tests for the chat model.
* Each test is wrapped in a try/catch block to prevent the entire test suite from failing.
Expand Down Expand Up @@ -471,6 +503,13 @@ export abstract class ChatModelIntegrationTests<
console.error("testWithStructuredOutputIncludeRaw failed", e);
}

try {
await this.testBindToolsWithOpenAIFormattedTools();
} catch (e: any) {
allTestsPassed = false;
console.error("testBindToolsWithOpenAIFormattedTools failed", e);
}

return allTestsPassed;
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10258,6 +10258,7 @@ __metadata:
ts-jest: ^29.1.0
typescript: ^5.4.5
zod: ^3.22.4
zod-to-json-schema: ^3.23.0
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -38392,6 +38393,15 @@ __metadata:
languageName: node
linkType: hard

"zod-to-json-schema@npm:^3.23.0":
version: 3.23.0
resolution: "zod-to-json-schema@npm:3.23.0"
peerDependencies:
zod: ^3.23.3
checksum: 56f220f06687b41602478cf19f9fbf04488a450c0e47e6cd6c1dc3b6729e2b1c75f742a52a16cbb11bcdf1ff7b2bf2043dfff59f3784d6ac8ecfa562ce035e21
languageName: node
linkType: hard

"zod@npm:^3.22.3, zod@npm:^3.22.4":
version: 3.22.4
resolution: "zod@npm:3.22.4"
Expand Down

0 comments on commit b755752

Please sign in to comment.