Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: Support Cloudflare Workers AI #2966

Closed
wants to merge 48 commits into from
Closed

Conversation

sxjeru
Copy link
Contributor

@sxjeru sxjeru commented Jun 21, 2024

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • ⚡️ perf
  • 📝 docs

🔀 变更说明 | Description of Change

#3402 代替。

参考 #2804 完成,目前已可对话,剩余事项如下:

  • 补全模型信息;
  • 增加 test;
  • 支持在设置中修改 Account id,而不是直接从 env 获得。

尝试了文档中列出的所有模型,将其中能用中文正常交流的模型都加入列表了。
由于 beta 模型目前可免费使用,所以基本所有模型都是 enabled 状态。

📝 补充信息 | Additional Information

// ref https://developers.cloudflare.com/workers-ai/models/#text-generation
// api https://developers.cloudflare.com/workers-ai/configuration/open-ai-compatibility

sxjeru added 12 commits May 31, 2024 22:37
This commit adds support for Cloudflare as a model provider. It includes changes to the `ModelProvider` enum, the `UserKeyVaults` interface, the `getServerGlobalConfig` function, the `DEFAULT_LLM_CONFIG` constant, the `getLLMConfig` function, the `AgentRuntime` class, and the `DEFAULT_MODEL_PROVIDER_LIST` constant.
Copy link

vercel bot commented Jun 21, 2024

@sxjeru is attempting to deploy a commit to the LobeHub Pro Team on Vercel.

A member of the Team first needs to authorize it.

@lobehubbot
Copy link
Member

👍 @sxjeru

Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

@arvinxx
Copy link
Contributor

arvinxx commented Jun 21, 2024

Rebase下main

src/config/llm.ts Outdated Show resolved Hide resolved
src/libs/agent-runtime/cloudflare/index.ts Outdated Show resolved Hide resolved
@arvinxx
Copy link
Contributor

arvinxx commented Jun 21, 2024

有不少删了之前代码的地方。要revert回来

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


There are many places where the previous code has been deleted. To revert back

src/config/modelProviders/cloudflare.ts Outdated Show resolved Hide resolved
}

export const LobeCloudflareAI = LobeOpenAICompatibleFactory({
baseURL: `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/ai/v1`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要用 llmEnv.XXX

Copy link
Contributor Author

@sxjeru sxjeru Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换成 llmEnv.CLOUDFLARE_ACCOUNT_ID 之后报下述错误:
`Error: ❌ Attempted to access a server-side environment variable on the client

请教一下有什么办法能让设置页的 Cloudflare Account ID 同步到此处 baseURL?
配置项写在 src/app/api/chat/agentRuntime.ts

case ModelProvider.Cloudflare: {
      const { CLOUDFLARE_API_KEY, CLOUDFLARE_ACCOUNT_ID } = getLLMConfig();
      const apiKey = apiKeyManager.pick(payload?.apiKey || CLOUDFLARE_API_KEY);
      const accountID = payload.apiKey && payload.cloudflareAccountID ? payload.cloudflareAccountID : CLOUDFLARE_ACCOUNT_ID;
      return { accountID, apiKey };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accountId 作为参数传给 server端。然后 server 端的 LobeCloudflareAI 初始化时候 baseURL 的逻辑变成 accountId 拼接的?

Copy link

codecov bot commented Jun 24, 2024

Codecov Report

Attention: Patch coverage is 43.29897% with 220 lines in your changes missing coverage. Please review.

Project coverage is 93.45%. Comparing base (920de7c) to head (65c0bd2).
Report is 1 commits behind head on main.

Files Patch % Lines
src/libs/agent-runtime/cloudflare/index.ts 12.21% 194 Missing ⚠️
src/app/api/chat/agentRuntime.ts 9.09% 10 Missing ⚠️
src/services/_auth.ts 22.22% 7 Missing ⚠️
src/services/chat.ts 14.28% 6 Missing ⚠️
src/libs/agent-runtime/AgentRuntime.ts 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2966      +/-   ##
==========================================
- Coverage   94.21%   93.45%   -0.77%     
==========================================
  Files         403      405       +2     
  Lines       25657    26038     +381     
  Branches     2751     2034     -717     
==========================================
+ Hits        24174    24335     +161     
- Misses       1483     1703     +220     
Flag Coverage Δ
app 93.45% <43.29%> (-0.77%) ⬇️
server 69.52% <41.17%> (-1.17%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Gotta reabse

@BrandonStudio
Copy link
Contributor

@sxjeru Could you have some discussions in #3186 ?

@sxjeru sxjeru marked this pull request as draft July 25, 2024 15:57
@sxjeru sxjeru marked this pull request as ready for review July 27, 2024 04:28
@BrandonStudio
Copy link
Contributor

@sxjeru If you are ready, please merge my commits at sxjeru#38

@BrandonStudio
Copy link
Contributor

@sxjeru Will you write test or shall I?

@sxjeru
Copy link
Contributor Author

sxjeru commented Aug 5, 2024

@BrandonStudio If you have free time, please do it.

能者多劳

@BrandonStudio
Copy link
Contributor

OK. I will continue working on it. Please close this PR, and I will start a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants