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

cloudflare[minor]: Add tool calling support #5820

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
12 changes: 12 additions & 0 deletions docs/core_docs/docs/integrations/chat/cloudflare_workersai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ import UnifiedModelParamsTooltip from "@mdx_components/unified_model_params_tool
<UnifiedModelParamsTooltip></UnifiedModelParamsTooltip>

<CodeBlock language="typescript">{Example}</CodeBlock>

### Tool calling

:::note
Tool calling is only available in `@langchain/cloudflare` version `0.0.7` and above.
:::

Cloudflare's API now supports tool calling! The below example demonstrates how to invoke and stream tool calls.

import ToolCalling from "@examples/models/chat/integration_cloudflare_tools.ts";
bracesproul marked this conversation as resolved.
Show resolved Hide resolved

<CodeBlock language="typescript">{ToolCalling}</CodeBlock>
101 changes: 101 additions & 0 deletions examples/src/models/chat/integration_cloudflare_tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ChatCloudflareWorkersAI } from "@langchain/cloudflare";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! I've reviewed the code and noticed that the recent changes explicitly access environment variables using process.env. I've flagged this for your review to ensure that the handling of environment variables aligns with best practices. Let me know if you have any questions or need further assistance!

import {
AIMessageChunk,
HumanMessage,
SystemMessage,
} from "@langchain/core/messages";
import { tool } from "@langchain/core/tools";
import { z } from "zod";

const model = new ChatCloudflareWorkersAI({
model: "@hf/nousresearch/hermes-2-pro-mistral-7b",
cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
cloudflareApiToken: process.env.CLOUDFLARE_API_TOKEN,
// Pass a custom base URL to use Cloudflare AI Gateway
// baseUrl: `https://gateway.ai.cloudflare.com/v1/{YOUR_ACCOUNT_ID}/{GATEWAY_NAME}/workers-ai/`,
});

const weatherSchema = z.object({
location: z.string().describe("The location to get the weather for"),
});
const weatherTool = tool<typeof weatherSchema>(
(input) => {
return `The weather in ${input.location} is sunny.`;
},
{
name: "get_weather",
description: "Get the weather",
}
);

const modelWithTools = model.bindTools([weatherTool]);

const inputMessages = [
new SystemMessage("You are a helpful assistant."),
new HumanMessage("What's the weather like in the North Pole?"),
];

const response = await modelWithTools.invoke(inputMessages);

console.log(response.tool_calls);

/*
[ { name: 'get_weather', args: { input: 'North Pole' } } ]
*/

const stream = await modelWithTools.stream(inputMessages);

let finalChunk: AIMessageChunk | undefined;
for await (const chunk of stream) {
console.log("chunk: ", chunk.content);
if (!finalChunk) {
finalChunk = chunk;
} else {
finalChunk = finalChunk.concat(chunk);
}
}

/*
chunk: <
bracesproul marked this conversation as resolved.
Show resolved Hide resolved
chunk: tool
chunk: _
chunk: call
chunk: >
chunk: \n
chunk: {'
chunk: arguments
chunk: ':
chunk: {'
chunk: input
chunk: ':
chunk: '
chunk: N
chunk: orth
chunk: P
chunk: ole
chunk: '},
chunk: '
chunk: name
chunk: ':
chunk: '
chunk: get
chunk: _
chunk: we
chunk: ather
chunk: '}
chunk: \n
chunk: </
chunk: tool
chunk: _
chunk: call
chunk: >
chunk: <|im_end|>
*/

console.log(finalChunk?.tool_calls);

/*
[
{ name: 'get_weather', args: { input: 'North Pole' }, id: undefined }
]
*/
6 changes: 4 additions & 2 deletions libs/langchain-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"dependencies": {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! 👋 I noticed that a new dependency "zod-to-json-schema" has been added and the existing "uuid" dependency has been updated in the "dependencies" section. This is flagged for your review to ensure it aligns with the project's dependency management strategy. Keep up the great work!

"@cloudflare/ai": "^1.0.47",
"@langchain/core": ">0.1.0 <0.3.0",
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"zod-to-json-schema": "^3.22.3"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
Expand All @@ -64,7 +65,8 @@
"release-it": "^15.10.1",
"rollup": "^4.5.2",
"ts-jest": "^29.1.0",
"typescript": "<5.2.0"
"typescript": "<5.2.0",
"zod": "^3.22.4"
},
"publishConfig": {
"access": "public"
Expand Down
Loading
Loading