Skip to content

Commit

Permalink
fix: improve gemini stream response handling and URL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Dec 13, 2024
1 parent c5ad25c commit c1030c5
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/adapter/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export class GeminiAdapter extends BaseAdapter {
topK: 40,
topP: 0.95,
maxOutputTokens: 8192,
responseMimeType: "text/plain"
}
};
}

public getTextGenerationUrl(_apiUrl: string): string {
const operationName = this.isStreamEnabled() ? 'streamGenerateContent' : 'generateContent';
return `${this.config.baseUrl}/${this.getModel()}:${operationName}`;
const baseUrl = `${this.config.baseUrl}/${this.getModel()}:${operationName}`;
return this.isStreamEnabled() ? `${baseUrl}?alt=sse` : baseUrl;
}

public parseResponse(response: HttpResponse<GeminiResponse | OpenAiChatCompletion>): string {
Expand Down Expand Up @@ -115,15 +115,23 @@ export class GeminiAdapter extends BaseAdapter {
targetText: string
): string {
try {
const cleanedText = streamData.text.startsWith(',')
? streamData.text.slice(1)
: streamData.text;
let cleanedText = streamData.text;

// Remove "data: " prefix if present
if (cleanedText.startsWith('data: ')) {
cleanedText = cleanedText.slice(5);
}
// Remove leading comma if present
if (cleanedText.startsWith(',')) {
cleanedText = cleanedText.slice(1);
}

const parsedChunk = JSON.parse(cleanedText);
const text = parsedChunk.candidates?.[0]?.content?.parts?.[0]?.text;

if (text) {
targetText += text;

query.onStream({
result: {
from: query.detectFrom,
Expand Down

0 comments on commit c1030c5

Please sign in to comment.