Merge pull request #4564 from continuedev/nate/fix-ant-tool-use
anthropic tool call fix in openai-adapters
This commit is contained in:
commit
968ea3ad84
5 changed files with 128 additions and 2 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@continuedev/openai-adapters",
|
||||
"version": "1.0.15",
|
||||
"version": "1.0.16",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
|
|
|||
|
|
@ -71,6 +71,32 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
msgs: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
|
||||
): any[] {
|
||||
const messages = msgs.map((message) => {
|
||||
if (message.role === "tool") {
|
||||
return {
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "tool_result",
|
||||
tool_use_id: message.tool_call_id,
|
||||
content:
|
||||
typeof message.content === "string"
|
||||
? message.content
|
||||
: message.content.map((part) => part.text).join(""),
|
||||
},
|
||||
],
|
||||
};
|
||||
} else if (message.role === "assistant" && message.tool_calls) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: message.tool_calls.map((toolCall) => ({
|
||||
type: "tool_use",
|
||||
id: toolCall.id,
|
||||
name: toolCall.function?.name,
|
||||
input: JSON.parse(toolCall.function?.arguments || "{}"),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
if (!Array.isArray(message.content)) {
|
||||
return message;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,11 +86,50 @@ export class GeminiApi implements BaseLlmApi {
|
|||
}
|
||||
|
||||
const isV1API = url.includes("/v1/");
|
||||
|
||||
const toolCallIdToNameMap = new Map<string, string>();
|
||||
const contents = oaiBody.messages
|
||||
.map((msg) => {
|
||||
if (msg.role === "system" && !isV1API) {
|
||||
return null; // Don't include system message in contents
|
||||
}
|
||||
|
||||
if (msg.role == "assistant" && msg.tool_calls?.length) {
|
||||
for (const toolCall of msg.tool_calls) {
|
||||
toolCallIdToNameMap.set(toolCall.id, toolCall.function.name);
|
||||
}
|
||||
|
||||
return {
|
||||
role: "model",
|
||||
parts: msg.tool_calls.map((toolCall) => ({
|
||||
functionCall: {
|
||||
name: toolCall.function.name,
|
||||
args: JSON.parse(toolCall.function.arguments || "{}"),
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
if (msg.role === "tool") {
|
||||
return {
|
||||
role: "user",
|
||||
parts: [
|
||||
{
|
||||
functionResponse: {
|
||||
name: msg.tool_call_id,
|
||||
response: {
|
||||
name: toolCallIdToNameMap.get(msg.tool_call_id),
|
||||
content:
|
||||
typeof msg.content === "string"
|
||||
? msg.content
|
||||
: msg.content.map((part) => part.text).join(""),
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (!msg.content) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -246,9 +285,10 @@ export class GeminiApi implements BaseLlmApi {
|
|||
`models/${body.model}:streamGenerateContent?key=${this.config.apiKey}`,
|
||||
this.apiBase,
|
||||
).toString();
|
||||
const convertedBody = this._convertBody(body, apiURL);
|
||||
const resp = await customFetch(this.config.requestOptions)(apiURL, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(this._convertBody(body, apiURL)),
|
||||
body: JSON.stringify(convertedBody),
|
||||
signal,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -260,6 +260,65 @@ export function testChat(api: BaseLlmApi, model: string) {
|
|||
const parsedArgs = JSON.parse(args);
|
||||
expect(parsedArgs.name).toBe("Nate");
|
||||
});
|
||||
|
||||
test("Tool Call second message works", async () => {
|
||||
let response = "";
|
||||
for await (const chunk of api.chatCompletionStream(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
tool_calls: [
|
||||
{
|
||||
function: {
|
||||
arguments: '{"name":"Nate"}',
|
||||
name: "say_hello",
|
||||
},
|
||||
id: "tool_call_1",
|
||||
type: "function",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
tool_call_id: "tool_call_1",
|
||||
content: "Nate was greeted",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
function: {
|
||||
name: "say_hello",
|
||||
description: "Say Hello",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: {
|
||||
type: "string",
|
||||
description: "The name of the person to greet",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
type: "function",
|
||||
},
|
||||
],
|
||||
stream: true,
|
||||
model,
|
||||
},
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
console.log(JSON.stringify(chunk, null, 2));
|
||||
response += chunk.choices[0].delta.content ?? "";
|
||||
}
|
||||
|
||||
expect(response.length).toBeGreaterThan(0);
|
||||
});
|
||||
}
|
||||
|
||||
export function testCompletion(api: BaseLlmApi, model: string) {
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ export const OpenAIConfigSchema = BasePlusConfig.extend({
|
|||
z.literal("vllm"),
|
||||
z.literal("x-ai"),
|
||||
z.literal("scaleway"),
|
||||
z.literal("ncompass"),
|
||||
]),
|
||||
});
|
||||
export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue