Skip to content

Commit

Permalink
perf: gemini 支持cloudflare ai gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Oct 14, 2024
1 parent e756436 commit 2e649eb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions src/agent/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ export class Gemini implements ChatAgent {
if (onStream !== null) {
console.warn('Stream mode is not supported');
}
const url = `${context.GOOGLE_COMPLETIONS_API}${context.GOOGLE_COMPLETIONS_MODEL}:${
// 暂时不支持stream模式
// onStream ? 'streamGenerateContent' : 'generateContent'
'generateContent'
}?key=${context.GOOGLE_API_KEY}`;
const mode = 'generateContent'; // onStream ? 'streamGenerateContent' : 'generateContent'
const url = `${context.GOOGLE_COMPLETIONS_API}${context.GOOGLE_COMPLETIONS_MODEL}:${mode}`;

const contentsTemp = [...history || [], { role: 'user', content: message }];
if (prompt) {
Expand All @@ -62,7 +59,8 @@ export class Gemini implements ChatAgent {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
'x-goog-api-key': context.GOOGLE_API_KEY,
} as Record<string, string>,
body: JSON.stringify({ contents }),
});
const data = await resp.json() as any;
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class WorkersConfig {
export class GeminiConfig {
// Google Gemini API Key
GOOGLE_API_KEY: string | null = null;
// Google Gemini API
// Google Gemini API: Cloudflare AI gateway: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models
GOOGLE_COMPLETIONS_API = 'https://generativelanguage.googleapis.com/v1beta/models/';
// Google Gemini Model
GOOGLE_COMPLETIONS_MODEL = 'gemini-pro';
Expand Down

0 comments on commit 2e649eb

Please sign in to comment.