feat: add support for gemini thought signature
This commit is contained in:
parent
6b7111cc6c
commit
28805d0564
2 changed files with 72 additions and 8 deletions
|
|
@ -266,18 +266,35 @@ class Gemini extends BaseLLM {
|
|||
? [{ text: msg.content }]
|
||||
: msg.content.map(this.continuePartToGeminiPart),
|
||||
};
|
||||
if (msg.toolCalls) {
|
||||
msg.toolCalls.forEach((toolCall) => {
|
||||
|
||||
if (msg.toolCalls && msg.toolCalls.length) {
|
||||
msg.toolCalls.forEach((toolCall, index) => {
|
||||
if (toolCall.function?.name) {
|
||||
const signatureForCall = (toolCall as any)?.extra_content
|
||||
?.google?.thought_signature;
|
||||
|
||||
let thoughtSignature: string | undefined;
|
||||
if (index === 0) {
|
||||
if (typeof signatureForCall === "string") {
|
||||
thoughtSignature = signatureForCall;
|
||||
} else {
|
||||
// Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures
|
||||
// for histories that were not generated by Gemini or are missing signatures.
|
||||
thoughtSignature = "skip_thought_signature_validator";
|
||||
}
|
||||
}
|
||||
|
||||
assistantMsg.parts.push({
|
||||
functionCall: {
|
||||
name: toolCall.function.name,
|
||||
args: safeParseToolCallArgs(toolCall),
|
||||
},
|
||||
...(thoughtSignature && { thoughtSignature }),
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return assistantMsg;
|
||||
}
|
||||
return {
|
||||
|
|
@ -370,6 +387,7 @@ class Gemini extends BaseLLM {
|
|||
if ("text" in part) {
|
||||
textParts.push({ type: "text", text: part.text });
|
||||
} else if ("functionCall" in part) {
|
||||
const thoughtSignature = (part as any)?.thoughtSignature;
|
||||
toolCalls.push({
|
||||
type: "function",
|
||||
id: part.functionCall.id ?? uuidv4(),
|
||||
|
|
@ -380,6 +398,13 @@ class Gemini extends BaseLLM {
|
|||
? part.functionCall.args
|
||||
: JSON.stringify(part.functionCall.args),
|
||||
},
|
||||
...(thoughtSignature && {
|
||||
extra_content: {
|
||||
google: {
|
||||
thought_signature: thoughtSignature,
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
// Note: function responses shouldn't be streamed, images not supported
|
||||
|
|
|
|||
|
|
@ -143,9 +143,25 @@ export class GeminiApi implements BaseLlmApi {
|
|||
|
||||
return {
|
||||
role: "model" as const,
|
||||
parts: msg.tool_calls.map((toolCall) => {
|
||||
// Type guard for function tool calls
|
||||
parts: msg.tool_calls.map((toolCall, index) => {
|
||||
if (toolCall.type === "function" && "function" in toolCall) {
|
||||
let thoughtSignature: string | undefined;
|
||||
if (index === 0) {
|
||||
const rawSignature = (toolCall as any)?.extra_content?.google
|
||||
?.thought_signature;
|
||||
|
||||
if (
|
||||
typeof rawSignature === "string" &&
|
||||
rawSignature.length > 0
|
||||
) {
|
||||
thoughtSignature = rawSignature;
|
||||
} else {
|
||||
// Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures
|
||||
// for histories that were not generated by Gemini or are missing signatures.
|
||||
thoughtSignature = "skip_thought_signature_validator";
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
functionCall: {
|
||||
id: includeToolCallIds ? toolCall.id : undefined,
|
||||
|
|
@ -155,12 +171,12 @@ export class GeminiApi implements BaseLlmApi {
|
|||
`Call: ${toolCall.function.name} ${toolCall.id}`,
|
||||
),
|
||||
},
|
||||
...(thoughtSignature && { thoughtSignature }),
|
||||
};
|
||||
} else {
|
||||
throw new Error(
|
||||
`Unsupported tool call type in Gemini: ${toolCall.type}`,
|
||||
);
|
||||
}
|
||||
throw new Error(
|
||||
`Unsupported tool call type in Gemini: ${toolCall.type}`,
|
||||
);
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
|
@ -328,11 +344,27 @@ export class GeminiApi implements BaseLlmApi {
|
|||
if (contentParts) {
|
||||
for (const part of contentParts) {
|
||||
if ("text" in part) {
|
||||
const thoughtSignature = (part as any)?.thoughtSignature;
|
||||
if (thoughtSignature) {
|
||||
yield chatChunkFromDelta({
|
||||
model,
|
||||
delta: {
|
||||
role: "assistant",
|
||||
extra_content: {
|
||||
google: {
|
||||
thought_signature: thoughtSignature,
|
||||
},
|
||||
},
|
||||
} as any,
|
||||
});
|
||||
}
|
||||
|
||||
yield chatChunk({
|
||||
content: part.text,
|
||||
model,
|
||||
});
|
||||
} else if ("functionCall" in part) {
|
||||
const thoughtSignature = (part as any)?.thoughtSignature;
|
||||
yield chatChunkFromDelta({
|
||||
model,
|
||||
delta: {
|
||||
|
|
@ -345,6 +377,13 @@ export class GeminiApi implements BaseLlmApi {
|
|||
name: part.functionCall.name,
|
||||
arguments: JSON.stringify(part.functionCall.args),
|
||||
},
|
||||
...(thoughtSignature && {
|
||||
extra_content: {
|
||||
google: {
|
||||
thought_signature: thoughtSignature,
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue