✨ add tool support to Cohere
This commit is contained in:
parent
be62db43ce
commit
c0174b6074
3 changed files with 134 additions and 0 deletions
|
|
@ -4,6 +4,7 @@ import {
|
|||
Chunk,
|
||||
CompletionOptions,
|
||||
LLMOptions,
|
||||
MessageContent,
|
||||
} from "../../index.js";
|
||||
import { renderChatMessage, stripImages } from "../../util/messageContent.js";
|
||||
import { BaseLLM } from "../index.js";
|
||||
|
|
@ -18,6 +19,7 @@ class Cohere extends BaseLLM {
|
|||
|
||||
private _convertMessages(msgs: ChatMessage[]): any[] {
|
||||
const messages = [];
|
||||
let lastToolPlan: MessageContent | undefined;
|
||||
for (const m of msgs) {
|
||||
if (!m.content) {
|
||||
continue;
|
||||
|
|
@ -29,7 +31,33 @@ class Cohere extends BaseLLM {
|
|||
content: m.content,
|
||||
});
|
||||
break;
|
||||
case "thinking":
|
||||
lastToolPlan = m.content;
|
||||
break;
|
||||
case "assistant":
|
||||
if (m.toolCalls) {
|
||||
if (!lastToolPlan) {
|
||||
throw new Error("No tool plan found");
|
||||
}
|
||||
messages.push({
|
||||
role: m.role,
|
||||
tool_calls: m.toolCalls.map((toolCall) => ({
|
||||
id: toolCall.id,
|
||||
type: "function",
|
||||
function: {
|
||||
name: toolCall.function?.name,
|
||||
arguments: toolCall.function?.arguments,
|
||||
},
|
||||
})),
|
||||
// Ideally the tool plan would be in this message, but it is
|
||||
// split in another, usually the previous, this one's content is
|
||||
// a space.
|
||||
// tool_plan: m.content,
|
||||
tool_plan: lastToolPlan,
|
||||
});
|
||||
lastToolPlan = undefined;
|
||||
break;
|
||||
}
|
||||
messages.push({
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
|
|
@ -41,6 +69,13 @@ class Cohere extends BaseLLM {
|
|||
content: stripImages(m.content),
|
||||
});
|
||||
break;
|
||||
case "tool":
|
||||
messages.push({
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
tool_call_id: m.toolCallId,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
@ -59,6 +94,14 @@ class Cohere extends BaseLLM {
|
|||
stop_sequences: options.stop?.slice(0, Cohere.maxStopSequences),
|
||||
frequency_penalty: options.frequencyPenalty,
|
||||
presence_penalty: options.presencePenalty,
|
||||
tools: options.tools?.map((tool) => ({
|
||||
type: "function",
|
||||
function: {
|
||||
name: tool.function.name,
|
||||
parameters: tool.function.parameters,
|
||||
description: tool.function.description,
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -100,10 +143,34 @@ class Cohere extends BaseLLM {
|
|||
|
||||
if (options.stream === false) {
|
||||
const data = await resp.json();
|
||||
if (data.message.tool_calls) {
|
||||
yield {
|
||||
// Use the "thinking" role for `tool_plan`, since there is no such
|
||||
// role in the Cohere API at the moment and it is a "a
|
||||
// chain-of-thought style reflection".
|
||||
role: "thinking",
|
||||
content: data.message.tool_plan,
|
||||
};
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: data.message.tool_calls.map((toolCall: any) => ({
|
||||
id: toolCall.id,
|
||||
type: "function",
|
||||
function: {
|
||||
name: toolCall.function?.name,
|
||||
arguments: toolCall.function?.arguments,
|
||||
},
|
||||
})),
|
||||
};
|
||||
return;
|
||||
}
|
||||
yield { role: "assistant", content: data.message.content[0].text };
|
||||
return;
|
||||
}
|
||||
|
||||
let lastToolUseId: string | undefined;
|
||||
let lastToolUseName: string | undefined;
|
||||
for await (const value of streamSse(resp)) {
|
||||
// https://docs.cohere.com/v2/docs/streaming#stream-events
|
||||
switch (value.type) {
|
||||
|
|
@ -114,6 +181,57 @@ class Cohere extends BaseLLM {
|
|||
content: value.delta.message.content.text,
|
||||
};
|
||||
break;
|
||||
// https://docs.cohere.com/reference/chat-stream#request.body.messages.assistant.tool_plan
|
||||
case "tool-plan-delta":
|
||||
// Use the "thinking" role for `tool_plan`, since there is no such
|
||||
// role in the Cohere API at the moment and it is a "a
|
||||
// chain-of-thought style reflection".
|
||||
yield {
|
||||
role: "thinking",
|
||||
content: value.delta.message.tool_plan,
|
||||
};
|
||||
break;
|
||||
case "tool-call-start":
|
||||
lastToolUseId = value.delta.message.tool_calls.id;
|
||||
lastToolUseName = value.delta.message.tool_calls.function.name;
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: [
|
||||
{
|
||||
id: lastToolUseId,
|
||||
type: "function",
|
||||
function: {
|
||||
name: lastToolUseName,
|
||||
arguments: value.delta.message.tool_calls.function.arguments,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
break;
|
||||
case "tool-call-delta":
|
||||
if (!lastToolUseId || !lastToolUseName) {
|
||||
throw new Error("No tool use found");
|
||||
}
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: [
|
||||
{
|
||||
id: lastToolUseId,
|
||||
type: "function",
|
||||
function: {
|
||||
name: lastToolUseName,
|
||||
arguments: value.delta.message.tool_calls.function.arguments,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
break;
|
||||
case "tool-call-end":
|
||||
lastToolUseId = undefined;
|
||||
lastToolUseName = undefined;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,6 +109,19 @@ describe("PROVIDER_TOOL_SUPPORT", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("cohere", () => {
|
||||
const supportsFn = PROVIDER_TOOL_SUPPORT["cohere"];
|
||||
|
||||
it("should return true for Command models", () => {
|
||||
expect(supportsFn("command-r")).toBe(true);
|
||||
expect(supportsFn("command-a")).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for other models", () => {
|
||||
expect(supportsFn("c4ai-aya-expanse-32b")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("gemini", () => {
|
||||
const supportsFn = PROVIDER_TOOL_SUPPORT["gemini"];
|
||||
|
||||
|
|
|
|||
|
|
@ -71,6 +71,9 @@ export const PROVIDER_TOOL_SUPPORT: Record<string, (model: string) => boolean> =
|
|||
|
||||
return false;
|
||||
},
|
||||
cohere: (model) => {
|
||||
return model.toLowerCase().startsWith("command");
|
||||
},
|
||||
gemini: (model) => {
|
||||
// All gemini models support function calling
|
||||
return model.toLowerCase().includes("gemini");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue