do not split lines again in processgeminiresponse

This commit is contained in:
uinstinct 2025-10-17 07:59:31 +05:30
parent baad1e2043
commit 99d9cd00ec
3 changed files with 95 additions and 131 deletions

View file

@ -312,75 +312,57 @@ class Gemini extends BaseLLM {
}
public async *processGeminiResponse(
stream: AsyncIterable<string>,
response: Response,
): AsyncGenerator<ChatMessage> {
let buffer = "";
for await (const chunk of stream) {
buffer += chunk;
const parts = buffer.split("\n,");
let foundIncomplete = false;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
let data: GeminiChatResponse;
try {
data = JSON.parse(part) as GeminiChatResponse;
} catch (e) {
foundIncomplete = true;
continue; // yo!
}
if ("error" in data) {
throw new Error(data.error.message);
}
// In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
const textParts: MessagePart[] = [];
const toolCalls: ToolCallDelta[] = [];
for (const part of contentParts) {
if ("text" in part) {
textParts.push({ type: "text", text: part.text });
} else if ("functionCall" in part) {
toolCalls.push({
type: "function",
id: part.functionCall.id ?? uuidv4(),
function: {
name: part.functionCall.name,
arguments:
typeof part.functionCall.args === "string"
? part.functionCall.args
: JSON.stringify(part.functionCall.args),
},
});
} else {
// Note: function responses shouldn't be streamed, images not supported
console.warn("Unsupported gemini part type received", part);
}
}
const assistantMessage: AssistantChatMessage = {
role: "assistant",
content: textParts.length ? textParts : "",
};
if (toolCalls.length > 0) {
assistantMessage.toolCalls = toolCalls;
}
if (textParts.length || toolCalls.length) {
yield assistantMessage;
}
} else {
// Handle the case where the expected data structure is not found
console.warn("Unexpected response format:", data);
}
for await (const chunk of streamSse(response)) {
let data: GeminiChatResponse;
try {
data = JSON.parse(chunk) as GeminiChatResponse;
} catch (e) {
continue;
}
if (foundIncomplete) {
buffer = parts[parts.length - 1];
if ("error" in data) {
throw new Error(data.error.message);
}
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
const textParts: MessagePart[] = [];
const toolCalls: ToolCallDelta[] = [];
for (const part of contentParts) {
if ("text" in part) {
textParts.push({ type: "text", text: part.text });
} else if ("functionCall" in part) {
toolCalls.push({
type: "function",
id: part.functionCall.id ?? uuidv4(),
function: {
name: part.functionCall.name,
arguments:
typeof part.functionCall.args === "string"
? part.functionCall.args
: JSON.stringify(part.functionCall.args),
},
});
} else {
console.warn("Unsupported gemini part type received", part);
}
}
const assistantMessage: AssistantChatMessage = {
role: "assistant",
content: textParts.length ? textParts : "",
};
if (toolCalls.length > 0) {
assistantMessage.toolCalls = toolCalls;
}
if (textParts.length || toolCalls.length) {
yield assistantMessage;
}
} else {
buffer = "";
console.warn("Unexpected response format:", data);
}
}
}
@ -406,7 +388,7 @@ class Gemini extends BaseLLM {
signal,
});
for await (const chunk of this.processGeminiResponse(streamSse(response))) {
for await (const chunk of this.processGeminiResponse(response)) {
yield chunk;
}
}

View file

@ -287,7 +287,7 @@ class VertexAI extends BaseLLM {
body: JSON.stringify(body),
signal,
});
yield* this.geminiInstance.processGeminiResponse(streamSse(response));
yield* this.geminiInstance.processGeminiResponse(response);
}
private async *streamChatBison(

View file

@ -284,76 +284,58 @@ export class GeminiApi implements BaseLlmApi {
}
async *handleStreamResponse(response: any, model: string) {
let buffer = "";
let usage: UsageInfo | undefined = undefined;
for await (const chunk of streamSse(response as any)) {
buffer += chunk;
const parts = buffer.split("\n,");
let foundIncomplete = false;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
let data;
try {
data = JSON.parse(part);
} catch (e) {
foundIncomplete = true;
continue; // yo!
}
if (data.error) {
throw new Error(data.error.message);
}
// Check for usage metadata
if (data.usageMetadata) {
usage = {
prompt_tokens: data.usageMetadata.promptTokenCount || 0,
completion_tokens: data.usageMetadata.candidatesTokenCount || 0,
total_tokens: data.usageMetadata.totalTokenCount || 0,
};
}
// In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
for (const part of contentParts) {
if ("text" in part) {
yield chatChunk({
content: part.text,
model,
});
} else if ("functionCall" in part) {
yield chatChunkFromDelta({
model,
delta: {
tool_calls: [
{
index: 0,
id: part.functionCall.id ?? uuidv4(),
type: "function",
function: {
name: part.functionCall.name,
arguments: JSON.stringify(part.functionCall.args),
},
},
],
},
});
}
}
} else {
console.warn("Unexpected response format:", data);
}
let data;
try {
data = JSON.parse(chunk);
} catch (e) {
continue;
}
if (foundIncomplete) {
buffer = parts[parts.length - 1];
if (data.error) {
throw new Error(data.error.message);
}
if (data.usageMetadata) {
usage = {
prompt_tokens: data.usageMetadata.promptTokenCount || 0,
completion_tokens: data.usageMetadata.candidatesTokenCount || 0,
total_tokens: data.usageMetadata.totalTokenCount || 0,
};
}
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
for (const part of contentParts) {
if ("text" in part) {
yield chatChunk({
content: part.text,
model,
});
} else if ("functionCall" in part) {
yield chatChunkFromDelta({
model,
delta: {
tool_calls: [
{
index: 0,
id: part.functionCall.id ?? uuidv4(),
type: "function",
function: {
name: part.functionCall.name,
arguments: JSON.stringify(part.functionCall.args),
},
},
],
},
});
}
}
} else {
buffer = "";
console.warn("Unexpected response format:", data);
}
}
// Emit usage at the end if we have it
if (usage) {
yield usageChatChunk({
model,