-
-
Notifications
You must be signed in to change notification settings - Fork 239
Upgrade Cohere integration #883
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
Draft
andreibondarev
wants to merge
1
commit into
main
Choose a base branch
from
cohere-1.0.1
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.
Draft
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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
module Langchain | ||
class Assistant | ||
module LLM | ||
module Adapters | ||
class Cohere < Base | ||
# Build the chat parameters for the Cohere LLM | ||
# | ||
# @param messages [Array] The messages | ||
# @param instructions [String] The system instructions | ||
# @param tools [Array] The tools to use | ||
# @param tool_choice [String] The tool choice | ||
# @param parallel_tool_calls [Boolean] Whether to make parallel tool calls | ||
# @return [Hash] The chat parameters | ||
def build_chat_params( | ||
messages:, | ||
instructions:, | ||
tools:, | ||
tool_choice:, | ||
parallel_tool_calls: | ||
) | ||
Langchain.logger.warn "WARNING: `parallel_tool_calls:` is not supported by Cohere currently" | ||
Langchain.logger.warn "WARNING: `tool_choice:` is not supported by Cohere currently" | ||
|
||
params = {messages: messages} | ||
if tools.any? | ||
params[:tools] = build_tools(tools) | ||
end | ||
params | ||
end | ||
|
||
# Build a Cohere message | ||
# | ||
# @param role [String] The role of the message | ||
# @param content [String] The content of the message | ||
# @param image_url [String] The image URL | ||
# @param tool_calls [Array] The tool calls | ||
# @param tool_call_id [String] The tool call ID | ||
# @return [Messages::CohereMessage] The Cohere message | ||
def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) | ||
Langchain.logger.warn "Image URL is not supported by Cohere" if image_url | ||
|
||
Messages::CohereMessage.new(role: role, content: content, tool_calls: tool_calls, tool_call_id: tool_call_id) | ||
end | ||
|
||
# Extract the tool call information from the Cohere tool call hash | ||
# | ||
# @param tool_call [Hash] The tool call hash | ||
# @return [Array] The tool call information | ||
def extract_tool_call_args(tool_call:) | ||
tool_call_id = tool_call.dig("id") | ||
|
||
function_name = tool_call.dig("function", "name") | ||
tool_name, method_name = function_name.split("__") | ||
|
||
tool_arguments = tool_call.dig("function", "arguments") | ||
tool_arguments = if tool_arguments.is_a?(Hash) | ||
Langchain::Utils::HashTransformer.symbolize_keys(tool_arguments) | ||
else | ||
JSON.parse(tool_arguments, symbolize_names: true) | ||
end | ||
|
||
[tool_call_id, tool_name, method_name, tool_arguments] | ||
end | ||
|
||
# Build the tools for the Cohere LLM | ||
def build_tools(tools) | ||
tools.map { |tool| tool.class.function_schemas.to_openai_format }.flatten | ||
end | ||
|
||
# Get the allowed assistant.tool_choice values for Cohere | ||
def allowed_tool_choices | ||
["auto", "none"] | ||
end | ||
|
||
# Get the available tool names for Cohere | ||
def available_tool_names(tools) | ||
build_tools(tools).map { |tool| tool.dig(:function, :name) } | ||
end | ||
|
||
def tool_role | ||
Messages::CohereMessage::TOOL_ROLE | ||
end | ||
|
||
def support_system_message? | ||
Messages::CohereMessage::ROLES.include?("system") | ||
end | ||
|
||
private | ||
|
||
def build_tool_choice(choice) | ||
case choice | ||
when "auto" | ||
choice | ||
else | ||
{"type" => "function", "function" => {"name" => choice}} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module Langchain | ||
class Assistant | ||
module Messages | ||
class CohereMessage < Base | ||
# OpenAI uses the following roles: | ||
ROLES = [ | ||
"system", | ||
"assistant", | ||
"user", | ||
"tool" | ||
].freeze | ||
|
||
TOOL_ROLE = "tool" | ||
|
||
# Initialize a new OpenAI message | ||
# | ||
# @param role [String] The role of the message | ||
# @param content [String] The content of the message | ||
# @param tool_calls [Array<Hash>] The tool calls made in the message | ||
# @param tool_call_id [String] The ID of the tool call | ||
def initialize(role:, content: nil, tool_calls: [], tool_call_id: nil) | ||
raise ArgumentError, "Role must be one of #{ROLES.join(", ")}" unless ROLES.include?(role) | ||
raise ArgumentError, "Tool calls must be an array of hashes" unless tool_calls.is_a?(Array) && tool_calls.all? { |tool_call| tool_call.is_a?(Hash) } | ||
|
||
@role = role | ||
# Some Tools return content as a JSON hence `.to_s` | ||
@content = content.to_s | ||
@tool_calls = tool_calls | ||
@tool_call_id = tool_call_id | ||
end | ||
|
||
# Convert the message to an OpenAI API-compatible hash | ||
# | ||
# @return [Hash] The message as an OpenAI API-compatible hash | ||
def to_hash | ||
{}.tap do |h| | ||
h[:role] = role | ||
h[:content] = content if content # Content is nil for tool calls | ||
h[:tool_calls] = tool_calls if tool_calls.any? | ||
h[:tool_call_id] = tool_call_id if tool_call_id | ||
end | ||
end | ||
|
||
# Check if the message came from an LLM | ||
# | ||
# @return [Boolean] true/false whether this message was produced by an LLM | ||
def llm? | ||
assistant? | ||
end | ||
|
||
# Check if the message came from an LLM | ||
# | ||
# @return [Boolean] true/false whether this message was produced by an LLM | ||
def assistant? | ||
role == "assistant" | ||
end | ||
|
||
# Check if the message are system instructions | ||
# | ||
# @return [Boolean] true/false whether this message are system instructions | ||
def system? | ||
role == "system" | ||
end | ||
|
||
# Check if the message is a tool call | ||
# | ||
# @return [Boolean] true/false whether this message is a tool call | ||
def tool? | ||
role == "tool" | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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.
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.
Consider providing a default value for the 'parallel_tool_calls' parameter to avoid a potential ArgumentError when it is not supplied.
Copilot uses AI. Check for mistakes.