-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(cohere): add support from Reasoning/Vision/Translate models #8597
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
Open
maxbrunet
wants to merge
1
commit into
continuedev:main
Choose a base branch
from
maxbrunet:feat/cohere-2025-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+999
−40
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,10 @@ import { | |
| Chunk, | ||
| CompletionOptions, | ||
| LLMOptions, | ||
| MessageContent, | ||
| } from "../../index.js"; | ||
| import { renderChatMessage, stripImages } from "../../util/messageContent.js"; | ||
| import { BaseLLM } from "../index.js"; | ||
| import { DEFAULT_REASONING_TOKENS } from "../constants.js"; | ||
|
|
||
| class Cohere extends BaseLLM { | ||
| static providerName = "cohere"; | ||
|
|
@@ -19,7 +19,6 @@ class Cohere extends BaseLLM { | |
|
|
||
| private _convertMessages(msgs: ChatMessage[]): any[] { | ||
| const messages = []; | ||
| let lastToolPlan: MessageContent | undefined; | ||
| for (const m of msgs) { | ||
| if (!m.content) { | ||
| continue; | ||
|
|
@@ -48,36 +47,44 @@ class Cohere extends BaseLLM { | |
| }); | ||
| break; | ||
| case "thinking": | ||
| lastToolPlan = m.content; | ||
| messages.push({ | ||
| role: "assistant", | ||
| content: [ | ||
| { | ||
| type: "thinking", | ||
| thinking: m.content, | ||
| }, | ||
| ], | ||
| }); | ||
| break; | ||
| case "assistant": | ||
| if (m.toolCalls) { | ||
| if (!lastToolPlan) { | ||
| throw new Error("No tool plan found"); | ||
| } | ||
| messages.push({ | ||
| let msg: any; | ||
| if (messages.at(-1)?.content[0]?.thinking) { | ||
| msg = messages.pop(); | ||
| } else { | ||
| msg = { | ||
| 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, | ||
| content: [], | ||
| }; | ||
| } | ||
|
|
||
| if (m.toolCalls) { | ||
| msg.tool_calls = m.toolCalls.map((toolCall) => ({ | ||
| id: toolCall.id, | ||
| type: "function", | ||
| function: { | ||
| name: toolCall.function?.name, | ||
| arguments: toolCall.function?.arguments, | ||
| }, | ||
| })); | ||
| } else { | ||
| msg.content.push({ | ||
| type: "text", | ||
| text: m.content, | ||
| }); | ||
| lastToolPlan = undefined; | ||
| break; | ||
| } | ||
| messages.push({ | ||
| role: m.role, | ||
| content: m.content, | ||
| }); | ||
|
|
||
| messages.push(msg); | ||
| break; | ||
| case "system": | ||
| messages.push({ | ||
|
|
@@ -110,6 +117,15 @@ class Cohere extends BaseLLM { | |
| stop_sequences: options.stop?.slice(0, Cohere.maxStopSequences), | ||
| frequency_penalty: options.frequencyPenalty, | ||
| presence_penalty: options.presencePenalty, | ||
| thinking: options.reasoning | ||
| ? { | ||
| type: "enabled" as const, | ||
| token_budget: | ||
| options.reasoningBudgetTokens ?? DEFAULT_REASONING_TOKENS, | ||
| } | ||
| : // Reasoning is enabled by default for models that support it. | ||
| // https://docs.cohere.com/reference/chat-stream#request.body.thinking | ||
| { type: "disabled" as const }, | ||
| tools: options.tools?.map((tool) => ({ | ||
| type: "function", | ||
| function: { | ||
|
|
@@ -159,14 +175,17 @@ class Cohere extends BaseLLM { | |
|
|
||
| if (options.stream === false) { | ||
| const data = await resp.json(); | ||
| for (const content of data.message.content) { | ||
| if (content.thinking) { | ||
| yield { role: "thinking", content: content.thinking }; | ||
| continue; | ||
| } | ||
| yield { role: "assistant", content: content.text }; | ||
| } | ||
| 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, | ||
| }; | ||
| if (data.message.tool_plan) { | ||
| yield { role: "thinking", content: data.message.tool_plan }; | ||
| } | ||
| yield { | ||
| role: "assistant", | ||
| content: "", | ||
|
|
@@ -181,7 +200,6 @@ class Cohere extends BaseLLM { | |
| }; | ||
| return; | ||
| } | ||
| yield { role: "assistant", content: data.message.content[0].text }; | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -192,16 +210,20 @@ class Cohere extends BaseLLM { | |
| switch (value.type) { | ||
| // https://docs.cohere.com/v2/docs/streaming#content-delta | ||
| case "content-delta": | ||
| if (value.delta.message.content.thinking) { | ||
|
Collaborator
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. @maxbrunet just to double check can content deltas also have text that would be lost here? or is it thinking OR text? |
||
| yield { | ||
| role: "thinking", | ||
| content: value.delta.message.content.thinking, | ||
| }; | ||
| break; | ||
| } | ||
| yield { | ||
| role: "assistant", | ||
| 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, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Wrapping assistant content in a
{type: "text"}block assigns arrays ofMessageParts to thetextfield, corrupting structured assistant replies when they are already multi-part.Prompt for AI agents
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.
@maxbrunet could you check this feedback?