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 chat completion call at client side #2041

Merged
merged 29 commits into from
Apr 20, 2024

Conversation

cy948
Copy link
Contributor

@cy948 cy948 commented Apr 14, 2024

💻 变更类型 | Change Type

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

🔀 变更说明 | Description of Change

detail:

  • ♻️ src/libs/agent-runtime/AgentRuntime.ts:从 src/app/api/chat/agentRuntime.ts 中将 AgentRuntime 代码抽离,去掉服务端逻辑后得到该 lib; ❇️ chat 方法现在接受两个参数:payload:ChatStreamPayloadoptions?:ChatCompetitionOptions;
  • 🧪 src/libs/agent-runtime/AgentRuntime.test.ts:为上述模块的测试文件,主要测试 AgentRuntime 类能否初始化及正常入参;
  • ♻️ ✨ src/app/api/chat/agentRuntime.ts:新增方法 getLlmOptionsFromPayload 用于处理服务端参数、新增方法 initializeWithUserPayload 用于调用 src/libs/agent-runtime/AgentRuntime.ts 中的 AgentRuntime,并调用 getLlmOptionsFromPayload 进行参数处理;❇️L485: 新增了方法 createTraceOptions 用于构建 trace 设置。
  • 🧪 src/app/api/chat/agentRuntime.test.ts:为上述新增方法 initializeWithUserPayload, getLlmOptionsFromPayload 补充单元测试,主要测试:
    • initializeWithUserPayload 在空 payload 下能否正常使用环境变量填补必要的参数,完成 AgentRuntime instance 初始化;
    • initializeWithUserPayload 在非空 payload 下能否正常传参,完成 AgentRuntime instance 初始化;
  • ♻️ src/app/api/chat/[provider]/route.ts:使用 src/app/api/chat/agentRuntime.ts: initializeWithUserPayload 创建服务端 AgentRuntime instance;❇️根据是否启用trace条件调用chat方法。
  • 🧪 src/app/api/chat/[provider]/route.test.ts:测试对象变更,src/app/api/chat/agentRuntime.ts -> src/libs/agent-runtime/AgentRuntime.ts ;❇️L113:115,新 route.ts 中若用户不开启 trace,chat 方法中没有第二个参数,故移除了对第二个参数的expect
  • src/services/chat.ts:新增方法 initializeWithClientStore 用于创建客户端 AgentRuntime instance;❇️移除了 L:178fetchOnClient 方法的 trace 头构建;
  • 🧪 src/services/__tests__/chat.test.ts:新增针对上述方法 initializeWithClientStore 的测试;❗ZhiPu AI 在初始化中,generateApiToken 方法报错,只能强制mock该方法结果以通过测试,日后待排查。
  • 🌐 src/locales/default/setting.ts:新增 fetchOnClient 的翻译,❗ i18n 待 review 后生成;

📝 补充信息 | Additional Information

  • src/services/chat.ts:L271:使用用户 llm 设置决定是否使用 client 请求;
  • src/services/chat.ts:L85:目前仅在客户端设置解除 openai 的 sdk 的 browser 端限制,其他 sdk 可能也存在 browser 端限制,目前依靠其他 provider contributor 测试后进行调整;

Copy link

vercel bot commented Apr 14, 2024

Someone is attempting to deploy a commit to the LobeHub Team on Vercel.

A member of the Team first needs to authorize it.

@lobehubbot
Copy link
Member

👍 @cy948

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.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link
Contributor

@arvinxx arvinxx left a comment

Choose a reason for hiding this comment

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

Agent Runtime 里应该还是有一些 server 端的逻辑,估计还是需要一些改造。

  1. 从 server 取环境变量的逻辑需要抽离出来;
  2. trace 的部分可能不需要有,或者要改成通过 api 去调用;

src/services/chat.ts Outdated Show resolved Hide resolved
src/services/chat.ts Outdated Show resolved Hide resolved
src/services/chat.ts Outdated Show resolved Hide resolved
@cy948 cy948 force-pushed the feat/agent-runtime-in-browser branch from c3c8148 to f57c7e1 Compare April 15, 2024 14:40
Copy link

codecov bot commented Apr 16, 2024

Codecov Report

Attention: Patch coverage is 94.50355% with 31 lines in your changes are missing coverage. Please review.

Project coverage is 92.81%. Comparing base (b3a7329) to head (8234092).

Files Patch % Lines
src/services/chat.ts 85.81% 21 Missing ⚠️
src/app/api/chat/[provider]/route.ts 38.46% 8 Missing ⚠️
src/libs/agent-runtime/AgentRuntime.ts 98.95% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2041      +/-   ##
==========================================
+ Coverage   92.77%   92.81%   +0.04%     
==========================================
  Files         265      266       +1     
  Lines       15086    15361     +275     
  Branches     1062     1783     +721     
==========================================
+ Hits        13996    14258     +262     
- Misses       1090     1103      +13     

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

src/app/api/chat/[provider]/route.test.ts Show resolved Hide resolved
src/app/api/chat/agentRuntime.ts Outdated Show resolved Hide resolved
src/app/api/chat/agentRuntime.ts Outdated Show resolved Hide resolved
src/app/api/chat/agentRuntime.ts Outdated Show resolved Hide resolved
src/libs/agent-runtime/AgentRuntime.ts Show resolved Hide resolved
@lobehub lobehub deleted a comment from lobehubbot Apr 19, 2024
src/app/api/chat/[provider]/route.test.ts Outdated Show resolved Hide resolved
src/app/api/chat/agentRuntime.ts Outdated Show resolved Hide resolved
@arvinxx arvinxx force-pushed the feat/agent-runtime-in-browser branch from fbc2741 to ec5be8c Compare April 19, 2024 17:09
Copy link

vercel bot commented Apr 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lobe-chat-community ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 20, 2024 4:15am

@arvinxx arvinxx merged commit 9f5858f into lobehub:main Apr 20, 2024
6 checks passed
@lobehubbot
Copy link
Member

❤️ Great PR @cy948 ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.
项目的成长离不开用户反馈和贡献,感谢您的贡献! 如果您对 LobeHub 开发者社区感兴趣,请加入我们的 discord,然后私信 @arvinxx@canisminor1990。他们会邀请您加入我们的私密开发者频道。我们将会讨论关于 Lobe Chat 的开发,分享和讨论全球范围内的 AI 消息。

github-actions bot pushed a commit that referenced this pull request Apr 20, 2024
## [Version 0.148.0](v0.147.22...v0.148.0)
<sup>Released on **2024-04-20**</sup>

#### ✨ Features

- **misc**: Support chat completion call at client side.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Support chat completion call at client side, closes [#2041](#2041) ([9f5858f](9f5858f))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

🎉 This PR is included in version 0.148.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit to bentwnghk/lobe-chat that referenced this pull request Apr 20, 2024
## [Version&nbsp;1.28.0](v1.27.20...v1.28.0)
<sup>Released on **2024-04-20**</sup>

#### ✨ Features

- **misc**: Support chat completion call at client side.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Support chat completion call at client side, closes [lobehub#2041](https://github.com/bentwnghk/lobe-chat/issues/2041) ([9f5858f](9f5858f))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@cy948 cy948 deleted the feat/agent-runtime-in-browser branch April 20, 2024 15:51
TheNameIsNigel pushed a commit to TheNameIsNigel/lobe-chat that referenced this pull request May 15, 2024
* ✨ feat: support chat completion call at client side

* 🐛 fix: disable agent-runtime in browser feature when user set
endpoint only

- 🐛 fix: this feature only enable when user set both apikey and
  endpoint in one specific llm setting

* ♻️ refactor: refactor agentruntime

* ♻️ refactor: add fetchOnClient

* 🚨 chore: eslint Circular Dependencies issue

* 💄 feat: Added UI to control fetchOnClient

* ✅ test: Passed chat provider test

* ♻️ refactor: remove redundant export

* ✅ test: add for agentRuntime

* 💄 style: remove tag for llm config

* ✅ test: add for agentRuntime init

* ♻️ refactor: fetchOnClient

* ✅ test: add for clinet agentRuntime init

* ♻️ refactor: for write tests easily

* ✅ test: for client AgentRuntime

* 🌐 i18n: fetch on client

* ✅ test: agentruntime

* ♻️ refactor: don't trace on client

* 📝 docs: update annotation for new methods

* 🐛fix: error not catch at client

* ♻️ refactor: extract trace at chat()

* ✅ test: for new chat method

* ♻️ refactor: -lines make stats better

* ♻️ refactor: remove deprecate

* ♻️  refactor: remove deprecate class AgentRuntime
* ♻️  refactor: fix circular dependence from AgentRuntime
* ✅ test: pass tests

* ♻️ refactor: reformat import urls

* ♻️ refactor: rename initAgentRuntimeWithUserPayload

* 💄 style: improve config

* 🌐 style: add i18n

* 💄 style: client 默认显示 ollama
TheNameIsNigel pushed a commit to TheNameIsNigel/lobe-chat that referenced this pull request May 15, 2024
## [Version&nbsp;0.148.0](lobehub/lobe-chat@v0.147.22...v0.148.0)
<sup>Released on **2024-04-20**</sup>

#### ✨ Features

- **misc**: Support chat completion call at client side.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Support chat completion call at client side, closes [lobehub#2041](lobehub#2041) ([9f5858f](lobehub@9f5858f))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@jinjianming
Copy link

docker 部署如何开启默认使用客户端模式呢

@lobehubbot
Copy link
Member

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


How to enable docker deployment to use client mode by default

@cy948
Copy link
Contributor Author

cy948 commented Jul 11, 2024

docker 部署如何开启默认使用客户端模式呢

目前“是否默认开启客户端模式”功能由社区统一配置,暂时不能由环境变量进行配置。

@lobehubbot
Copy link
Member

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


How to enable the default client mode for docker deployment

Currently, the "whether to enable client mode by default" function is uniformly configured by the community and cannot be configured by environment variables for the time being.

ipoly pushed a commit to ipoly/lobe-chat that referenced this pull request Jul 29, 2024
* ✨ feat: support chat completion call at client side

* 🐛 fix: disable agent-runtime in browser feature when user set
endpoint only

- 🐛 fix: this feature only enable when user set both apikey and
  endpoint in one specific llm setting

* ♻️ refactor: refactor agentruntime

* ♻️ refactor: add fetchOnClient

* 🚨 chore: eslint Circular Dependencies issue

* 💄 feat: Added UI to control fetchOnClient

* ✅ test: Passed chat provider test

* ♻️ refactor: remove redundant export

* ✅ test: add for agentRuntime

* 💄 style: remove tag for llm config

* ✅ test: add for agentRuntime init

* ♻️ refactor: fetchOnClient

* ✅ test: add for clinet agentRuntime init

* ♻️ refactor: for write tests easily

* ✅ test: for client AgentRuntime

* 🌐 i18n: fetch on client

* ✅ test: agentruntime

* ♻️ refactor: don't trace on client

* 📝 docs: update annotation for new methods

* 🐛fix: error not catch at client

* ♻️ refactor: extract trace at chat()

* ✅ test: for new chat method

* ♻️ refactor: -lines make stats better

* ♻️ refactor: remove deprecate

* ♻️  refactor: remove deprecate class AgentRuntime
* ♻️  refactor: fix circular dependence from AgentRuntime
* ✅ test: pass tests

* ♻️ refactor: reformat import urls

* ♻️ refactor: rename initAgentRuntimeWithUserPayload

* 💄 style: improve config

* 🌐 style: add i18n

* 💄 style: client 默认显示 ollama
ipoly pushed a commit to ipoly/lobe-chat that referenced this pull request Jul 29, 2024
## [Version&nbsp;0.148.0](lobehub/lobe-chat@v0.147.22...v0.148.0)
<sup>Released on **2024-04-20**</sup>

#### ✨ Features

- **misc**: Support chat completion call at client side.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Support chat completion call at client side, closes [lobehub#2041](lobehub#2041) ([9f5858f](lobehub@9f5858f))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants