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

cohere[minor]: Add support for tool calling cohere #5810

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
8 changes: 8 additions & 0 deletions docs/core_docs/docs/integrations/chat/cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ import StatefulChatExample from "@examples/models/chat/cohere/stateful_conversat
You can see the LangSmith traces from this example [here](https://smith.langchain.com/public/8e67b05a-4e63-414e-ac91-a91acf21b262/r) and [here](https://smith.langchain.com/public/50fabc25-46fe-4727-a59c-7e4eb0de8e70/r)
:::

### Tools

The Cohere API supports tool calling, along with multi-hop-tool calling. The following example demonstrates how to call tools:

import ToolCallingExample from "@examples/models/chat/cohere/tool_calling.ts";

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

### RAG

Cohere also comes out of the box with RAG support.
Expand Down
1 change: 0 additions & 1 deletion examples/src/models/chat/cohere/chat_cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ChatPromptTemplate } from "@langchain/core/prompts";

const model = new ChatCohere({
apiKey: process.env.COHERE_API_KEY, // Default
model: "command", // Default
});
const prompt = ChatPromptTemplate.fromMessages([
["ai", "You are a helpful assistant"],
Expand Down
1 change: 0 additions & 1 deletion examples/src/models/chat/cohere/chat_stream_cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StringOutputParser } from "@langchain/core/output_parsers";

const model = new ChatCohere({
apiKey: process.env.COHERE_API_KEY, // Default
model: "command", // Default
});
const prompt = ChatPromptTemplate.fromMessages([
["ai", "You are a helpful assistant"],
Expand Down
1 change: 0 additions & 1 deletion examples/src/models/chat/cohere/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HumanMessage } from "@langchain/core/messages";

const model = new ChatCohere({
apiKey: process.env.COHERE_API_KEY, // Default
model: "command", // Default
});

const response = await model.invoke(
Expand Down
1 change: 0 additions & 1 deletion examples/src/models/chat/cohere/rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HumanMessage } from "@langchain/core/messages";

const model = new ChatCohere({
apiKey: process.env.COHERE_API_KEY, // Default
model: "command", // Default
});

const documents = [
Expand Down
1 change: 0 additions & 1 deletion examples/src/models/chat/cohere/stateful_conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HumanMessage } from "@langchain/core/messages";

const model = new ChatCohere({
apiKey: process.env.COHERE_API_KEY, // Default
model: "command", // Default
});

const conversationId = `demo_test_id-${Math.random()}`;
Expand Down
57 changes: 57 additions & 0 deletions examples/src/models/chat/cohere/tool_calling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ChatCohere } from "@langchain/cohere";
Copy link

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! 🚀

import { HumanMessage } from "@langchain/core/messages";
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.bindTools(tools);

const messages = [new HumanMessage("What is the magic function of number 5?")];
const response = await modelWithTools.invoke(messages);
/*
AIMessage {
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 }
}
*/
1 change: 1 addition & 0 deletions libs/langchain-cohere/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"arrow-body-style": 0,
camelcase: 0,
"class-methods-use-this": 0,
"import/extensions": [2, "ignorePackages"],
Expand Down
7 changes: 5 additions & 2 deletions libs/langchain-cohere/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
"author": "LangChain",
"license": "MIT",
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" 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"
"@langchain/core": ">=0.2.14 <0.3.0",
"cohere-ai": "^7.10.5",
"uuid": "^10.0.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.1"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
Loading
Loading