-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
cohere[minor]: Add support for tool calling cohere #5810
Changes from 1 commit
36f86ea
6de2f8e
8d98b04
2442d5a
100e7ef
1f7f7e1
bb5d9a3
77d3f46
834addc
827cfc0
f7debe4
457d6fa
3ff35b8
41c8210
aa82799
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ChatCohere } from "@langchain/cohere"; | ||
import { HumanMessage, ToolMessage } from "@langchain/core/messages"; | ||
import { convertToCohereTool } from "@langchain/core/utils/function_calling"; | ||
import { z } from "zod"; | ||
import { DynamicStructuredTool } from "@langchain/core/tools"; | ||
|
||
const model = new ChatCohere({ | ||
apiKey: process.env.COHERE_API_KEY, // Default | ||
}); | ||
|
||
const magicFunctionTool = new DynamicStructuredTool({ | ||
name: "magic_function", | ||
description: "Apply a magic function to the input number", | ||
schema: z.object({ | ||
num: z.number().describe("The number to apply the magic function for"), | ||
}), | ||
func: async ({ num }) => { | ||
return `The magic function of ${num} is ${num + 5}`; | ||
}, | ||
}); | ||
|
||
const tools = [magicFunctionTool]; | ||
const modelWithTools = model.bind({ | ||
tools: tools.map(convertToCohereTool), | ||
}); | ||
|
||
let messages = [new HumanMessage("What is the magic function of number 5?")]; | ||
const response = await modelWithTools.invoke( | ||
messages, | ||
); | ||
/** | ||
response: AIMessage { | ||
lc_serializable: true, | ||
lc_kwargs: { | ||
content: 'I will use the magic_function tool to answer this question.', | ||
additional_kwargs: { | ||
response_id: 'd0b189e5-3dbf-493c-93f8-99ed4b01d96d', | ||
generationId: '8982a68f-c64c-48f8-bf12-0b4bea0018b6', | ||
chatHistory: [Array], | ||
finishReason: 'COMPLETE', | ||
meta: [Object], | ||
toolCalls: [Array] | ||
}, | ||
tool_calls: [ [Object] ], | ||
usage_metadata: { input_tokens: 920, output_tokens: 54, total_tokens: 974 }, | ||
invalid_tool_calls: [], | ||
response_metadata: {} | ||
}, | ||
lc_namespace: [ 'langchain_core', 'messages' ], | ||
content: 'I will use the magic_function tool to answer this question.', | ||
name: undefined, | ||
additional_kwargs: { | ||
response_id: 'd0b189e5-3dbf-493c-93f8-99ed4b01d96d', | ||
generationId: '8982a68f-c64c-48f8-bf12-0b4bea0018b6', | ||
chatHistory: [ [Object], [Object] ], | ||
finishReason: 'COMPLETE', | ||
meta: { apiVersion: [Object], billedUnits: [Object], tokens: [Object] }, | ||
toolCalls: [ [Object] ] | ||
}, | ||
response_metadata: { | ||
estimatedTokenUsage: { completionTokens: 54, promptTokens: 920, totalTokens: 974 }, | ||
response_id: 'd0b189e5-3dbf-493c-93f8-99ed4b01d96d', | ||
generationId: '8982a68f-c64c-48f8-bf12-0b4bea0018b6', | ||
chatHistory: [ [Object], [Object] ], | ||
finishReason: 'COMPLETE', | ||
meta: { apiVersion: [Object], billedUnits: [Object], tokens: [Object] }, | ||
toolCalls: [ [Object] ] | ||
}, | ||
tool_calls: [ | ||
{ | ||
name: 'magic_function', | ||
args: [Object], | ||
id: '4ec98550-ba9a-4043-adfe-566230e5' | ||
} | ||
], | ||
invalid_tool_calls: [], | ||
usage_metadata: { input_tokens: 920, output_tokens: 54, total_tokens: 974 } | ||
} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
"ES2022.Object", | ||
"DOM" | ||
], | ||
"module": "ES2020", | ||
"module": "NodeNext", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you revert these? Should not be in this PR. |
||
"moduleResolution": "nodenext", | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", | ||
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", | ||
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", | ||
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", | ||
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\chat_models.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert |
||
"test:standard:unit": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.test.ts --testTimeout 100000 --maxWorkers=50%", | ||
"test:standard:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", | ||
"test:standard": "yarn test:standard:unit && yarn test:standard:int", | ||
|
@@ -36,7 +36,8 @@ | |
"license": "MIT", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! I noticed that a new dependency "zod" has been added to the "dependencies" section, which is a change in the project's dependencies. This comment is to flag the change for maintainers to review. Great work! |
||
"dependencies": { | ||
"@langchain/core": ">=0.2.5 <0.3.0", | ||
"cohere-ai": "^7.10.5" | ||
"cohere-ai": "^7.10.5", | ||
"zod": "^3.23.8" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.5.0", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! 👋 This is a friendly flag to highlight that the recent code change explicitly accesses an environment variable using
process.env
. This is an important point for maintainers to review. Keep up the great work! 🚀