Skip to content

Commit

Permalink
chore: supports Gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
FYLSen committed Dec 13, 2024
1 parent f40939c commit 99b4753
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 756 deletions.
16 changes: 15 additions & 1 deletion cf-worker-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ class AIService {
this.provider = env.AI_PROVIDER
this.model = env.AI_MODEL
this.apiKey = env.AI_API_KEY
this.apiEndpoint = env.AI_ENDPOINT || this.getDefaultEndpoint()
this.maxContentLength = env.MAX_CONTENT_LENGTH || 10000
this.language = language
this.promptTemplate = env.PROMPT_TEMPLATE || this.getDefaultPromptTemplate()
this.apiEndpoint = env.AI_ENDPOINT || this.getDefaultEndpoint()
}

getDefaultEndpoint() {
Expand All @@ -476,6 +476,8 @@ class AIService {
return 'https://api.openai.com/v1/chat/completions'
case 'anthropic':
return 'https://api.anthropic.com/v1/messages'
case 'google':
return 'https://generativelanguage.googleapis.com/v1beta/models/${this.model}:generateContent?key=${this.apiKey}'
default:
throw new Error(`No default endpoint for provider: ${this.provider}`)
}
Expand Down Expand Up @@ -521,6 +523,10 @@ The summary MUST be:
}

createMessages(content) {
if (this.provider === 'google') {
return { system_instruction: { parts: { text: this.promptTemplate }},
contents: { parts: { text: content }}}
}
return [
{ role: 'system', content: this.promptTemplate },
{ role: 'user', content: content },
Expand All @@ -545,6 +551,13 @@ The summary MUST be:
},
headers: { 'anthropic-version': '2023-06-01' },
},
google: {
body: {
messages,
generationConfig: { maxOutputTokens: 1024 },
},
headers: {},
},
}

const config = apiConfigs[this.provider]
Expand Down Expand Up @@ -578,6 +591,7 @@ The summary MUST be:
const extractors = {
openai: (data) => data.choices[0].message.content,
anthropic: (data) => data.content[0].text,
google: (data) => data.candidates[0].content.parts[0].text,
}

const extractor = extractors[this.provider]
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This Cloudflare Worker provides an API endpoint for summarizing web articles usi
## Features

- Summarizes web articles from allowed domains
- Supports multiple AI providers (OpenAI, Anthropic, Cloudflare AI)
- Supports multiple AI providers (OpenAI, Anthropic, Google, Cloudflare AI)
- Generates summaries in multiple languages
- Caches summaries to reduce API calls and improve response times
- Implements rate limiting to prevent abuse
Expand All @@ -23,7 +23,7 @@ This Cloudflare Worker provides an API endpoint for summarizing web articles usi

The following environment variables need to be set:

- `AI_PROVIDER`: The AI service provider to use (openai, anthropic, or cloudflare)
- `AI_PROVIDER`: The AI service provider to use (openai, anthropic, google, or cloudflare)
- `AI_MODEL`: The specific AI model to use (e.g., gpt-3.5-turbo for OpenAI)
- `AI_API_KEY`: Your API key for the chosen AI provider
- `AI_ENDPOINT`: (Optional) Custom endpoint URL for the AI API
Expand Down Expand Up @@ -155,6 +155,7 @@ The worker supports multiple AI providers:

- OpenAI
- Anthropic
- Google
- Cloudflare AI

## Long Article Handling
Expand Down
7 changes: 4 additions & 3 deletions readme_zh.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# AI 网页文章摘要 Cloudflare Worker

这个 Cloudflare Worker 提供了一个 API 端点,用于使用各种 AI 提供商来summarize网络文章。它支持 OpenAI、Anthropic 和 Cloudflare 的 AI 服务。该 worker 可以生成多语言摘要,并包括缓存和速率限制等功能。
这个 Cloudflare Worker 提供了一个 API 端点,用于使用各种 AI 提供商来summarize网络文章。它支持 OpenAI、Anthropic、Google 和 Cloudflare 的 AI 服务。该 worker 可以生成多语言摘要,并包括缓存和速率限制等功能。

## 功能

- 摘要化来自允许域名的网络文章
- 支持多个 AI 提供商(OpenAI、Anthropic、Cloudflare AI)
- 支持多个 AI 提供商(OpenAI、Anthropic、Google、Cloudflare AI)
- 生成多语言摘要
- 缓存摘要以减少 API 调用并提高响应时间
- 实现速率限制以防止滥用
Expand All @@ -23,7 +23,7 @@

需要设置以下环境变量:

- `AI_PROVIDER`:要使用的 AI 服务提供商(openai、anthropic 或 cloudflare)
- `AI_PROVIDER`:要使用的 AI 服务提供商(openai、anthropic、google 或 cloudflare)
- `AI_MODEL`:要使用的特定 AI 模型(例如,OpenAI 的 gpt-3.5-turbo)
- `AI_API_KEY`:您选择的 AI 提供商的 API 密钥
- `AI_ENDPOINT`:(可选)AI API 的自定义端点 URL
Expand Down Expand Up @@ -155,6 +155,7 @@ worker 支持多个 AI 提供商:

- OpenAI
- Anthropic
- Google
- Cloudflare AI

## 长文章处理
Expand Down
Loading

0 comments on commit 99b4753

Please sign in to comment.