Skip to content

standard-tests[minor]: Improve prompting to force model to call tool #6004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions libs/langchain-standard-tests/src/integration_tests/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { z } from "zod";
import { StructuredTool } from "@langchain/core/tools";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
BaseChatModelsTests,
BaseChatModelsTestsFields,
Expand All @@ -37,6 +38,14 @@
}
}

const MATH_ADDITION_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages([
[
"system",
"You are bad at math and must ALWAYS call the {toolName} function.",
],
["human", "What is the sum of 1836281973 and 19973286?"],
]);

interface ChatModelIntegrationTestsFields<
CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions,
OutputMessageType extends BaseMessageChunk = BaseMessageChunk,
Expand Down Expand Up @@ -228,11 +237,11 @@
new ToolMessage(functionResult, functionId, functionName),
];

const resultStringContent = await modelWithTools.invoke(
const result = await modelWithTools.invoke(
messagesStringContent,
callOptions
);
expect(resultStringContent).toBeInstanceOf(this.invokeResponseType);
expect(result).toBeInstanceOf(this.invokeResponseType);
}

/**
Expand Down Expand Up @@ -334,11 +343,11 @@
new HumanMessage("What is 3 + 4"),
];

const resultStringContent = await modelWithTools.invoke(
const result = await modelWithTools.invoke(
messagesStringContent,
callOptions
);
expect(resultStringContent).toBeInstanceOf(this.invokeResponseType);
expect(result).toBeInstanceOf(this.invokeResponseType);
}

async testWithStructuredOutput() {
Expand All @@ -353,13 +362,17 @@
"withStructuredOutput undefined. Cannot test tool message histories."
);
}
const modelWithTools = model.withStructuredOutput(adderSchema);
const modelWithTools = model.withStructuredOutput(adderSchema, {
name: "math_addition",
});

const resultStringContent = await modelWithTools.invoke("What is 1 + 2");
expect(resultStringContent.a).toBeDefined();
expect([1, 2].includes(resultStringContent.a)).toBeTruthy();
expect(resultStringContent.b).toBeDefined();
expect([1, 2].includes(resultStringContent.b)).toBeTruthy();
const result = await MATH_ADDITION_PROMPT.pipe(modelWithTools).invoke({
toolName: "math_addition",
});
expect(result.a).toBeDefined();
expect(typeof result.a).toBe("number");
expect(result.b).toBeDefined();
expect(typeof result.b).toBe("number");
}

async testWithStructuredOutputIncludeRaw() {
Expand All @@ -376,14 +389,17 @@
}
const modelWithTools = model.withStructuredOutput(adderSchema, {
includeRaw: true,
name: "math_addition",
});

const resultStringContent = await modelWithTools.invoke("What is 1 + 2");
expect(resultStringContent.raw).toBeInstanceOf(this.invokeResponseType);
expect(resultStringContent.parsed.a).toBeDefined();
expect([1, 2].includes(resultStringContent.parsed.a)).toBeTruthy();
expect(resultStringContent.parsed.b).toBeDefined();
expect([1, 2].includes(resultStringContent.parsed.b)).toBeTruthy();
const result = await MATH_ADDITION_PROMPT.pipe(modelWithTools).invoke({
toolName: "math_addition",
});
expect(result.raw).toBeInstanceOf(this.invokeResponseType);
expect(result.parsed.a).toBeDefined();
expect(typeof result.parsed.a).toBe("number");
expect(result.parsed.b).toBeDefined();
expect(typeof result.parsed.b).toBe("number");
}

async testBindToolsWithOpenAIFormattedTools() {
Expand All @@ -409,7 +425,11 @@
},
]);

const result: AIMessage = await modelWithTools.invoke("What is 1 + 2");
const result: AIMessage = await MATH_ADDITION_PROMPT.pipe(
modelWithTools
).invoke({
toolName: "math_addition",
});
expect(result.tool_calls).toHaveLength(1);
if (!result.tool_calls) {
throw new Error("result.tool_calls is undefined");
Expand All @@ -429,49 +449,49 @@

try {
await this.testInvoke();
} catch (e: any) {

Check warning on line 452 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testInvoke failed", e);
}

try {
await this.testStream();
} catch (e: any) {

Check warning on line 459 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testStream failed", e);
}

try {
await this.testBatch();
} catch (e: any) {

Check warning on line 466 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testBatch failed", e);
}

try {
await this.testConversation();
} catch (e: any) {

Check warning on line 473 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testConversation failed", e);
}

try {
await this.testUsageMetadata();
} catch (e: any) {

Check warning on line 480 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testUsageMetadata failed", e);
}

try {
await this.testUsageMetadataStreaming();
} catch (e: any) {

Check warning on line 487 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testUsageMetadataStreaming failed", e);
}

try {
await this.testToolMessageHistoriesStringContent();
} catch (e: any) {

Check warning on line 494 in libs/langchain-standard-tests/src/integration_tests/chat_models.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
allTestsPassed = false;
console.error("testToolMessageHistoriesStringContent failed", e);
}
Expand Down
Loading