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: add custom stream handle support for LobeOpenAICompatibleFactory #5039

Merged
merged 18 commits into from
Dec 29, 2024

Conversation

hezhijie0327
Copy link
Contributor

@hezhijie0327 hezhijie0327 commented Dec 16, 2024

💻 变更类型 | Change Type

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

🔀 变更说明 | Description of Change

  1. 为 LobeOpenAICompatibleFactory 引入 handleStreamhandleTransformResponseToStream 接口,用于应用自定义流处理
  2. Qwen 切回 LobeOpenAICompatibleFactory
    a. 对语言模型默认启用内置联网搜索能力,引入 enable_search (印象里之前有个 Issue 提了这个 feat,过会儿翻下) [Request] 增加通义千问打开联网搜索的开关 #4877
    b. 对 frequency_penalty presence_penalty temperature top_p 进行范围限制
    - ref: https://help.aliyun.com/zh/dashscope/developer-reference/tongyi-qianwen-vl-plus-api
    - ref: https://help.aliyun.com/zh/dashscope/developer-reference/qwen-api
功能 效果
Qwen 互联网搜索 imageimage
多模态 image
工具调用 image
  1. 补齐之前 Spark 缺少的 function call 的实现
    a. 通过改造实现了 Spark 的 Function Call 功能(整体工具调用效果不是特别好,存在限制)
    b. 支持流式及非流式对话
    c. 试了下工具调用支持流式,但是除了 max-32k 模型支持工具回调外,其余均不支持(curl 调用测试也为空;max-32k 存在相同 payload,上一秒还能出内容,再次执行直接返回空的问题)
Function Name 描述
transformSparkStream 处理流式
transformSparkResponseToStream 处理非流式

Spark 的工具调用流很怪,返回的是对象不是数组,通过魔改 OpenAI 流处理对非标准返回强制转换为数组


PS: 给讯飞开了工单,将近三个多月无进展
image

📝 补充信息 | Additional Information

模块 效果
对话 image
工具调用 image

非流式响应流

{
    "code": 0,
    "message": "Success",
    "sid": "cha000b95d2@dx193cef49db1b894532",
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "",
                "tool_calls": {
                    "type": "function",
                    "function": {
                        "arguments": "{\"city\":\"Shanghai\"}",
                        "name": "realtime-weather____fetchCurrentWeather"
                    }
                }
            },
            "index": 0
        }
    ],
    "usage": {
        "prompt_tokens": 8,
        "completion_tokens": 0,
        "total_tokens": 8
    }
}

流式输出响应流

{
    "code": 0,
    "message": "Success",
    "sid": "cha000b6cd0@dx193cef628319a4b532",
    "id": "cha000b6cd0@dx193cef628319a4b532",
    "created": 1734344060,
    "choices": [
        {
            "delta": {
                "role": "assistant",
                "content": "",
                "tool_calls": {
                    "type": "function",
                    "function": {
                        "arguments": "{\"city\":\"Shanghai\"}",
                        "name": "realtime-weather____fetchCurrentWeather"
                    }
                }
            },
            "index": 0
        }
    ],
    "usage": {
        "prompt_tokens": 8,
        "completion_tokens": 0,
        "total_tokens": 8
    }
}

Copy link

vercel bot commented Dec 16, 2024

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

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Dec 16, 2024
@lobehubbot
Copy link
Member

👍 @hezhijie0327

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

@dosubot dosubot bot added the 💄 Design Design an style | 样式问题 label Dec 16, 2024
Copy link

codecov bot commented Dec 16, 2024

Codecov Report

Attention: Patch coverage is 87.19212% with 26 lines in your changes missing coverage. Please review.

Project coverage is 92.16%. Comparing base (50e0dc4) to head (cd6237e).
Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
src/libs/agent-runtime/utils/streams/spark.ts 82.83% 23 Missing ⚠️
src/libs/agent-runtime/qwen/index.ts 93.61% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5039      +/-   ##
==========================================
- Coverage   92.20%   92.16%   -0.05%     
==========================================
  Files         568      569       +1     
  Lines       43395    43463      +68     
  Branches     2706     3890    +1184     
==========================================
+ Hits        40012    40056      +44     
- Misses       3383     3407      +24     
Flag Coverage Δ
app 92.16% <87.19%> (-0.05%) ⬇️
server 97.66% <ø> (ø)

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.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Dec 16, 2024
@hezhijie0327 hezhijie0327 changed the title 💄 style: add function call support for Spark ✨ feat: add custom stream handle support for LobeOpenAICompatibleFactory Dec 21, 2024
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Dec 21, 2024
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Dec 21, 2024
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Dec 21, 2024
@hezhijie0327
Copy link
Contributor Author

@arvinxx 改造好了

  1. 顺带把 Qwen 也切换回 LobeOpenAICompatibleFactory 并引入 handleStream: QwenAIStream 处理自定义流
  2. 为 Qwen Chat 模型引入 enable_search 默认启用内建联网功能
  3. 为 Qwen 引入 QwenLegacyModels 数组,用于处理部分模型不支持 presence_penalty

@lobehubbot
Copy link
Member

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


@arvinxx has been transformed

  1. By the way, switch Qwen back to LobeOpenAICompatibleFactory and introduce handleStream: QwenAIStream to process custom streams
  2. Introduce enable_search to the Qwen Chat model to enable the built-in networking function by default
  3. Introduce the QwenLegacyModels array to Qwen to handle some models that do not support presence_penalty

Copy link

vercel bot commented Dec 29, 2024

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

Name Status Preview Comments Updated (UTC)
lobe-chat-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 29, 2024 3:55am

@arvinxx arvinxx merged commit ea7e732 into lobehub:main Dec 29, 2024
6 of 8 checks passed
@lobehubbot
Copy link
Member

❤️ Great PR @hezhijie0327 ❤️

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 Dec 29, 2024
## [Version&nbsp;1.42.0](v1.41.0...v1.42.0)
<sup>Released on **2024-12-29**</sup>

#### ✨ Features

- **misc**: Add custom stream handle support for LobeOpenAICompatibleFactory.

<br/>

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

#### What's improved

* **misc**: Add custom stream handle support for LobeOpenAICompatibleFactory, closes [#5039](#5039) ([ea7e732](ea7e732))

</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 1.42.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@hezhijie0327 hezhijie0327 deleted the spark_tool branch December 29, 2024 05:46
github-actions bot pushed a commit to bentwnghk/lobe-chat that referenced this pull request Dec 29, 2024
## [Version&nbsp;1.84.0](v1.83.0...v1.84.0)
<sup>Released on **2024-12-29**</sup>

#### ✨ Features

- **misc**: Add custom stream handle support for LobeOpenAICompatibleFactory.

<br/>

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

#### What's improved

* **misc**: Add custom stream handle support for LobeOpenAICompatibleFactory, closes [lobehub#5039](https://github.com/bentwnghk/lobe-chat/issues/5039) ([ea7e732](ea7e732))

</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
💄 Design Design an style | 样式问题 released size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants