diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts index 1f810723a..254afd6fa 100644 --- a/core/llm/llms/Gemini.ts +++ b/core/llm/llms/Gemini.ts @@ -1,5 +1,5 @@ +import { v4 as uuidv4 } from "uuid"; import { - AssistantChatMessage, ChatMessage, CompletionOptions, LLMOptions, @@ -7,7 +7,6 @@ import { TextMessagePart, ToolCallDelta, } from "../../index.js"; -import { findLast } from "../../util/findLast.js"; import { renderChatMessage } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; import { streamResponse } from "../stream.js"; @@ -205,20 +204,24 @@ class Gemini extends BaseLLM { options: CompletionOptions, isV1API: boolean, ): GeminiChatRequestBody { + const toolCallIdToNameMap = new Map(); + messages.forEach((msg) => { + if (msg.role === "assistant" && msg.toolCalls) { + msg.toolCalls.forEach((call) => { + if (call.id && call.function?.name) { + toolCallIdToNameMap.set(call.id, call.function.name); + } + }); + } + }); + const body: GeminiChatRequestBody = { contents: messages .filter((msg) => !(msg.role === "system" && isV1API)) .map((msg) => { if (msg.role === "tool") { - let fn_name = ""; - const lastToolCallMessage = findLast( - messages, - (msg) => "toolCalls" in msg && msg.toolCalls?.[0]?.function?.name, - ) as AssistantChatMessage; - if (lastToolCallMessage) { - fn_name = lastToolCallMessage.toolCalls![0]!.function!.name!; - } - if (!fn_name) { + let functionName = toolCallIdToNameMap.get(msg.toolCallId); + if (!functionName) { console.warn( "Sending tool call response for unidentified tool call", ); @@ -228,7 +231,8 @@ class Gemini extends BaseLLM { parts: [ { functionResponse: { - name: fn_name || "unknown", + id: msg.toolCallId, + name: functionName || "unknown", response: { output: msg.content, // "output" key is opinionated - not all functions will output objects }, @@ -358,7 +362,7 @@ class Gemini extends BaseLLM { type: "toolCall", data: { type: "function", - id: "", // Not supported by gemini + id: part.functionCall.id ?? uuidv4(), function: { name: part.functionCall.name, arguments: diff --git a/core/llm/llms/gemini-types.ts b/core/llm/llms/gemini-types.ts index d37fad121..355d1c942 100644 --- a/core/llm/llms/gemini-types.ts +++ b/core/llm/llms/gemini-types.ts @@ -175,50 +175,67 @@ export function convertContinueToolToGeminiFunction( return fn; } +export type GeminiTextContentPart = { + text: string; +}; + +export type GeminiInlineDataContentPart = { + inlineData: { + mimeType: string; + data: string; + }; +}; + +export type GeminiFunctionCallContentPart = { + functionCall: { + id?: string; + name: string; + args: JSONSchema7Object; + }; +}; + +export type GeminiFunctionResponseContentPart = { + functionResponse: { + id?: string; + name: string; + response: JSONSchema7Object; + }; +}; + +export type GeminiFileDataContentPart = { + fileData: { + fileUri: string; + mimeType: string; // See possible values here: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#filedata + }; +}; + +export type GeminiExecutableCodeContentPart = { + executableCode: { + language: "PYTHON" | "LANGUAGE_UNSPECIFIED"; + code: string; + }; +}; + +export type GeminiCodeExecutionResultContentPart = { + codeExecutionResult: { + outcome: + | "OUTCOME_UNSPECIFIED" + | "OUTCOME_OK" + | "OUTCOME_FAILED" + | "OUTCOME_DEADLINE_EXCEEDED"; + output: string; + }; +}; + export type GeminiChatContentPart = - | { - text: string; - } - | { - inlineData: { - mimeType: string; - data: string; - }; - } - | { - functionCall: { - name: string; - args: JSONSchema7Object; - }; - } - | { - functionResponse: { - name: string; - response: JSONSchema7Object; - }; - } - | { - fileData: { - fileUri: string; - mimeType: string; // See possible values here: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#filedata - }; - } - | { - executableCode: { - language: "PYTHON" | "LANGUAGE_UNSPECIFIED"; - code: string; - }; - } - | { - codeExecutionResult: { - outcome: - | "OUTCOME_UNSPECIFIED" - | "OUTCOME_OK" - | "OUTCOME_FAILED" - | "OUTCOME_DEADLINE_EXCEEDED"; - output: string; - }; - }; + | GeminiTextContentPart + | GeminiInlineDataContentPart + | GeminiFunctionCallContentPart + | GeminiFunctionResponseContentPart + | GeminiFileDataContentPart + | GeminiExecutableCodeContentPart + | GeminiCodeExecutionResultContentPart; + export interface GeminiChatContent { role?: "user" | "model"; parts: GeminiChatContentPart[]; diff --git a/packages/openai-adapters/package-lock.json b/packages/openai-adapters/package-lock.json index 1fa921da5..08d52d3b9 100644 --- a/packages/openai-adapters/package-lock.json +++ b/packages/openai-adapters/package-lock.json @@ -16,6 +16,7 @@ "json-schema": "^0.4.0", "node-fetch": "^3.3.2", "openai": "^4.76.0", + "uuid": "^11.1.0", "zod": "^3.24.2" }, "devDependencies": { @@ -4356,6 +4357,18 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/uuid": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.0.tgz", + "integrity": "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/esm/bin/uuid" + } + }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", diff --git a/packages/openai-adapters/package.json b/packages/openai-adapters/package.json index 478b50f0d..82ba2509b 100644 --- a/packages/openai-adapters/package.json +++ b/packages/openai-adapters/package.json @@ -19,6 +19,7 @@ "json-schema": "^0.4.0", "node-fetch": "^3.3.2", "openai": "^4.76.0", + "uuid": "^11.1.0", "zod": "^3.24.2" }, "devDependencies": { diff --git a/packages/openai-adapters/src/apis/Gemini.ts b/packages/openai-adapters/src/apis/Gemini.ts index 336dee644..ab76a28d0 100644 --- a/packages/openai-adapters/src/apis/Gemini.ts +++ b/packages/openai-adapters/src/apis/Gemini.ts @@ -14,6 +14,7 @@ import { Model, } from "openai/resources/index"; +import { v4 as uuidv4 } from "uuid"; import { GeminiConfig } from "../types.js"; import { chatChunk, @@ -23,6 +24,8 @@ import { } from "../util.js"; import { convertOpenAIToolToGeminiFunction, + GeminiChatContent, + GeminiChatContentPart, GeminiToolFunctionDeclaration, } from "../util/gemini-types.js"; import { @@ -51,9 +54,15 @@ export class GeminiApi implements BaseLlmApi { } private _oaiPartToGeminiPart( - part: OpenAI.Chat.Completions.ChatCompletionContentPart, - ) { + part: + | OpenAI.Chat.Completions.ChatCompletionContentPart + | OpenAI.Chat.Completions.ChatCompletionContentPartRefusal, + ): GeminiChatContentPart { switch (part.type) { + case "refusal": + return { + text: part.refusal, + }; case "text": return { text: part.text, @@ -91,7 +100,15 @@ export class GeminiApi implements BaseLlmApi { const isV1API = url.includes("/v1/"); const toolCallIdToNameMap = new Map(); - const contents = oaiBody.messages + oaiBody.messages.forEach((msg) => { + if (msg.role === "assistant" && msg.tool_calls) { + msg.tool_calls.forEach((call) => { + toolCallIdToNameMap.set(call.id, call.function.name); + }); + } + }); + + const contents: (GeminiChatContent | null)[] = oaiBody.messages .map((msg) => { if (msg.role === "system" && !isV1API) { return null; // Don't include system message in contents @@ -103,9 +120,10 @@ export class GeminiApi implements BaseLlmApi { } return { - role: "model", + role: "model" as const, parts: msg.tool_calls.map((toolCall) => ({ functionCall: { + id: toolCall.id, name: toolCall.function.name, args: JSON.parse(toolCall.function.arguments || "{}"), }, @@ -114,14 +132,15 @@ export class GeminiApi implements BaseLlmApi { } if (msg.role === "tool") { + const functionName = toolCallIdToNameMap.get(msg.tool_call_id); return { - role: "user", + role: "user" as const, parts: [ { functionResponse: { - name: msg.tool_call_id, + id: msg.tool_call_id, + name: functionName ?? "unknown", response: { - name: toolCallIdToNameMap.get(msg.tool_call_id), content: typeof msg.content === "string" ? msg.content @@ -138,12 +157,12 @@ export class GeminiApi implements BaseLlmApi { } return { - role: msg.role === "assistant" ? "model" : "user", + role: + msg.role === "assistant" ? ("model" as const) : ("user" as const), parts: typeof msg.content === "string" ? [{ text: msg.content }] - : // @ts-ignore - msg.content.map(this._oaiPartToGeminiPart), + : msg.content.map(this._oaiPartToGeminiPart), }; }) .filter((c) => c !== null); @@ -283,7 +302,7 @@ export class GeminiApi implements BaseLlmApi { tool_calls: [ { index: 0, - id: "", // Not supported by Gemini + id: part.functionCall.id ?? uuidv4(), type: "function", function: { name: part.functionCall.name, diff --git a/packages/openai-adapters/src/util/gemini-types.ts b/packages/openai-adapters/src/util/gemini-types.ts index d1921302e..44f752f40 100644 --- a/packages/openai-adapters/src/util/gemini-types.ts +++ b/packages/openai-adapters/src/util/gemini-types.ts @@ -175,50 +175,67 @@ export function convertOpenAIToolToGeminiFunction( return fn; } +export type GeminiTextContentPart = { + text: string; +}; + +export type GeminiInlineDataContentPart = { + inlineData: { + mimeType: string; + data: string; + }; +}; + +export type GeminiFunctionCallContentPart = { + functionCall: { + id?: string; + name: string; + args: JSONSchema7Object; + }; +}; + +export type GeminiFunctionResponseContentPart = { + functionResponse: { + id?: string; + name: string; + response: JSONSchema7Object; + }; +}; + +export type GeminiFileDataContentPart = { + fileData: { + fileUri: string; + mimeType: string; // See possible values here: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#filedata + }; +}; + +export type GeminiExecutableCodeContentPart = { + executableCode: { + language: "PYTHON" | "LANGUAGE_UNSPECIFIED"; + code: string; + }; +}; + +export type GeminiCodeExecutionResultContentPart = { + codeExecutionResult: { + outcome: + | "OUTCOME_UNSPECIFIED" + | "OUTCOME_OK" + | "OUTCOME_FAILED" + | "OUTCOME_DEADLINE_EXCEEDED"; + output: string; + }; +}; + export type GeminiChatContentPart = - | { - text: string; - } - | { - inlineData: { - mimeType: string; - data: string; - }; - } - | { - functionCall: { - name: string; - args: JSONSchema7Object; - }; - } - | { - functionResponse: { - name: string; - response: JSONSchema7Object; - }; - } - | { - fileData: { - fileUri: string; - mimeType: string; // See possible values here: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#filedata - }; - } - | { - executableCode: { - language: "PYTHON" | "LANGUAGE_UNSPECIFIED"; - code: string; - }; - } - | { - codeExecutionResult: { - outcome: - | "OUTCOME_UNSPECIFIED" - | "OUTCOME_OK" - | "OUTCOME_FAILED" - | "OUTCOME_DEADLINE_EXCEEDED"; - output: string; - }; - }; + | GeminiTextContentPart + | GeminiInlineDataContentPart + | GeminiFunctionCallContentPart + | GeminiFunctionResponseContentPart + | GeminiFileDataContentPart + | GeminiExecutableCodeContentPart + | GeminiCodeExecutionResultContentPart; + export interface GeminiChatContent { role?: "user" | "model"; parts: GeminiChatContentPart[];