Skip to content

Commit

Permalink
Handle more advanced CoT tool use in Anthropic (#45)
Browse files Browse the repository at this point in the history
Per [this
guide](https://docs.anthropic.com/claude/docs/tool-use#chain-of-thought-tool-use),
Anthropic might return multiple messages, expressing chain of thought
and tool use. This change generalizes the conversion slightly so that we
use the first text response and the first tool call.
  • Loading branch information
ankrgyl committed Apr 6, 2024
1 parent 4a9f1d9 commit b87c35a
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/proxy/src/providers/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export function anthropicCompletionToOpenAICompletion(
completion: AnthropicCompletion,
isFunction: boolean,
): ChatCompletion {
const firstText = completion.content.find((c) => c.type === "text");
const firstTool = completion.content.find((c) => c.type === "tool_use");
return {
id: completion.id,
choices: [
Expand All @@ -125,29 +127,26 @@ export function anthropicCompletionToOpenAICompletion(
message: {
role: "assistant",
// Anthropic inserts extra whitespace at the beginning of the completion
content:
completion.content[0].type == "text"
? completion.content[0].text.trimStart()
: null,
content: firstText ? firstText.text.trimStart() : null,
tool_calls: isFunction
? undefined
: completion.content[0].type == "tool_use"
: firstTool
? [
{
id: completion.content[0].id,
id: firstTool.id,
type: "function",
function: {
name: completion.content[0].name,
arguments: JSON.stringify(completion.content[0].input),
name: firstTool.name,
arguments: JSON.stringify(firstTool.input),
},
},
]
: undefined,
function_call:
isFunction && completion.content[0].type == "tool_use"
isFunction && firstTool
? {
name: completion.content[0].name,
arguments: JSON.stringify(completion.content[0].input),
name: firstTool.name,
arguments: JSON.stringify(firstTool.input),
}
: undefined,
},
Expand Down

0 comments on commit b87c35a

Please sign in to comment.