Skip to content
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

core[minor],openai[patch],langchain[patch]: Allow tool functions to input ToolCall / return ToolMessage #6005

Merged
merged 21 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion langchain-core/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const config = {
singletons: "singletons/index",
stores: "stores",
"structured_query": "structured_query/index",
tools: "tools",
tools: "tools/index",
"tracers/base": "tracers/base",
"tracers/console": "tracers/console",
"tracers/initialize": "tracers/initialize",
Expand Down
3 changes: 2 additions & 1 deletion langchain-core/src/callbacks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ abstract class BaseCallbackHandlerMethodsClass {
* Called at the end of a Tool run, with the tool output and the run ID.
*/
handleToolEnd?(
output: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
output: any,
runId: string,
parentRunId?: string,
tags?: string[]
Expand Down
3 changes: 2 additions & 1 deletion langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ export class CallbackManagerForToolRun
);
}

async handleToolEnd(output: string): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async handleToolEnd(output: any): Promise<void> {
await Promise.all(
this.handlers.map((handler) =>
consumeCallback(async () => {
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/language_models/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "../callbacks/manager.js";
import type { RunnableConfig } from "../runnables/config.js";
import type { BaseCache } from "../caches/base.js";
import { StructuredToolInterface } from "../tools.js";
import { StructuredToolInterface } from "../tools/index.js";
import {
Runnable,
RunnableLambda,
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/load/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export * as prompt_values from "../prompt_values.js";
export * as runnables from "../runnables/index.js";
export * as retrievers from "../retrievers/index.js";
export * as stores from "../stores.js";
export * as tools from "../tools.js";
export * as tools from "../tools/index.js";
export * as tracers__base from "../tracers/base.js";
export * as tracers__console from "../tracers/console.js";
export * as tracers__initialize from "../tracers/initialize.js";
Expand Down
2 changes: 2 additions & 0 deletions langchain-core/src/messages/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ export class AIMessageChunk extends BaseMessageChunk {
name: toolCallChunk.name ?? "",
args: parsedArgs,
id: toolCallChunk.id,
type: "tool_call",
});
} catch (e) {
invalidToolCalls.push({
name: toolCallChunk.name,
args: toolCallChunk.args,
id: toolCallChunk.id,
error: "Malformed args.",
type: "invalid_tool_call",
});
}
}
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export {
type ToolMessageFieldsWithToolCallId,
ToolMessage,
ToolMessageChunk,
type InvalidToolCall,
} from "./tool.js";
4 changes: 2 additions & 2 deletions langchain-core/src/messages/tests/message_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ describe("mergeMessageRuns", () => {
{ type: "text", text: "my favorite dish is lasagna" },
],
tool_calls: [
{ name: "blah_tool", args: { x: 2 }, id: "123" },
{ name: "blah_tool", args: { x: -10 }, id: "456" },
{ name: "blah_tool", args: { x: 2 }, id: "123", type: "tool_call" },
{ name: "blah_tool", args: { x: -10 }, id: "456", type: "tool_call" },
],
id: "baz",
}),
Expand Down
5 changes: 5 additions & 0 deletions langchain-core/src/messages/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export type ToolCall = {
args: Record<string, any>;

id?: string;

type?: "tool_call";
};

/**
Expand Down Expand Up @@ -199,13 +201,16 @@ export type ToolCallChunk = {
id?: string;

index?: number;

type?: "tool_call_chunk";
};

export type InvalidToolCall = {
name?: string;
args?: string;
id?: string;
error?: string;
type?: "invalid_tool_call";
};

export function defaultToolCallParser(
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/messages/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ function _switchTypeToMessage(
...aiChunkFields,
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
...tc,
type: "tool_call_chunk",
index: undefined,
args: JSON.stringify(tc.args),
})),
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export function convertToChunk(message: BaseMessage) {
...aiChunkFields,
tool_call_chunks: aiChunkFields.tool_calls?.map((tc) => ({
...tc,
type: "tool_call_chunk",
index: undefined,
args: JSON.stringify(tc.args),
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function parseToolCall(
const parsedToolCall: ToolCall = {
name: rawToolCall.function.name,
args: functionArgs,
type: "tool_call",
};

if (options?.returnId) {
Expand Down Expand Up @@ -104,6 +105,7 @@ export function makeInvalidToolCall(
args: rawToolCall.function?.arguments,
id: rawToolCall.id,
error: errorMsg,
type: "invalid_tool_call",
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SystemMessage,
} from "../../messages/index.js";
import { ChatGenerationChunk, GenerationChunk } from "../../outputs.js";
import { DynamicStructuredTool, DynamicTool } from "../../tools.js";
import { DynamicStructuredTool, DynamicTool } from "../../tools/index.js";
import { Document } from "../../documents/document.js";

function reverse(s: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
HumanMessage,
SystemMessage,
} from "../../messages/index.js";
import { DynamicStructuredTool, DynamicTool, tool } from "../../tools.js";
import { DynamicStructuredTool, DynamicTool, tool } from "../../tools/index.js";
import { Document } from "../../documents/document.js";
import { PromptTemplate } from "../../prompts/prompt.js";
import { GenerationChunk } from "../../outputs.js";
Expand Down
Loading
Loading