| 66 | } |
| 67 | |
| 68 | export class GeminiApi implements BaseLlmApi { |
| 69 | apiBase: string = "https://generativelanguage.googleapis.com/v1beta/"; |
| 70 | private genAI: GoogleGenAI; |
| 71 | |
| 72 | static maxStopSequences = 5; |
| 73 | |
| 74 | constructor(protected config: GeminiConfig) { |
| 75 | this.apiBase = config.apiBase ?? this.apiBase; |
| 76 | // Create GoogleGenAI with native fetch to avoid pollution |
| 77 | // from Vercel AI SDK packages that can break stream handling |
| 78 | this.genAI = withNativeFetch( |
| 79 | () => |
| 80 | new GoogleGenAI({ |
| 81 | apiKey: this.config.apiKey, |
| 82 | }), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | private _oaiPartToGeminiPart( |
| 87 | part: |
| 88 | | OpenAI.Chat.Completions.ChatCompletionContentPart |
| 89 | | OpenAI.Chat.Completions.ChatCompletionContentPartRefusal, |
| 90 | ): GeminiChatContentPart { |
| 91 | switch (part.type) { |
| 92 | case "refusal": |
| 93 | return { |
| 94 | text: part.refusal, |
| 95 | }; |
| 96 | case "text": |
| 97 | return { |
| 98 | text: part.text, |
| 99 | }; |
| 100 | case "input_audio": |
| 101 | throw new Error("Unsupported part type: input_audio"); |
| 102 | case "image_url": |
| 103 | default: |
| 104 | return { |
| 105 | inlineData: { |
| 106 | mimeType: "image/jpeg", |
| 107 | data: (part as ChatCompletionContentPartImage).image_url?.url.split( |
| 108 | ",", |
| 109 | )[1], |
| 110 | }, |
| 111 | }; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public _convertBody( |
| 116 | oaiBody: ChatCompletionCreateParams, |
| 117 | isV1API: boolean, |
| 118 | includeToolCallIds: boolean, |
| 119 | ) { |
| 120 | const generationConfig: any = {}; |
| 121 | |
| 122 | if (oaiBody.top_p) { |
| 123 | generationConfig.topP = oaiBody.top_p; |
| 124 | } |
| 125 | if (oaiBody.temperature !== undefined && oaiBody.temperature !== null) { |
nothing calls this directly
no outgoing calls
no test coverage detected