-
Notifications
You must be signed in to change notification settings - Fork 403
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
refactor: use openai-compatible to support providerOptions #4402
base: main
Are you sure you want to change the base?
Conversation
为啥 yarn lock 的修改这么大 |
刚刚只改了 package.json lock 一些包的版本不符合预期,我删了重新生成了,不过好像有点问题,我瞅瞅 |
""" Walkthrough此次修改涉及依赖和模型提供者接口的更新。在 Changes
Sequence Diagram(s)sequenceDiagram
participant C as 客户端
participant M as OpenAIModel
participant F as createOpenAICompatible
participant P as OpenAICompatibleProvider
participant L as LanguageModelV1
C->>M: 请求初始化 Provider
M->>F: 调用 createOpenAICompatible(name="openai")
F-->>M: 返回 OpenAICompatibleProvider
M->>P: 调用 getModelIdentifier()
P-->>M: 返回 LanguageModelV1
""" Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 ESLint
yarn install v1.22.22 ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/ai-native/src/node/openai/openai-language-model.ts (1)
25-27
: 建议添加模型验证当前默认使用 'qwen-max' 模型,建议添加对提供的 modelId 的验证,确保模型在 DashScope 中可用。
protected getModelIdentifier(provider: OpenAICompatibleProvider, modelId?: string): LanguageModelV1 { + const supportedModels = ['qwen-max', 'qwen-plus', 'qwen-turbo']; + const selectedModel = modelId || 'qwen-max'; + if (!supportedModels.includes(selectedModel)) { + throw new Error(`不支持的模型: ${selectedModel}。请使用以下模型之一: ${supportedModels.join(', ')}`); + } - return provider(modelId || 'qwen-max'); + return provider(selectedModel); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
yarn.lock
is excluded by!**/yarn.lock
,!**/*.lock
📒 Files selected for processing (2)
packages/ai-native/package.json
(1 hunks)packages/ai-native/src/node/openai/openai-language-model.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build-windows
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (3)
packages/ai-native/src/node/openai/openai-language-model.ts (2)
1-2
: 依赖更新符合预期从
@ai-sdk/openai
切换到@ai-sdk/openai-compatible
的更改与 PR 目标一致,有助于提供更好的兼容性支持。
13-23
:✅ Verification successful
验证 DashScope 服务的兼容性
代码使用阿里云 DashScope 作为基础服务,需要确保其与 OpenAI 兼容模式的完全兼容性。
🌐 Web query:
阿里云 DashScope 的 OpenAI 兼容模式支持哪些 API?与 OpenAI API 有什么区别?
💡 Result:
阿里云DashScope的OpenAI兼容模式主要支持Chat Completions API接口(对话补全功能),其核心实现与OpenAI相似但存在关键差异。以下是详细对比:
一、支持的API及配置
1. 基础配置
- BASE_URL:
需配置为阿里云专用地址:金融云用户需使用:https://dashscope.aliyuncs.com/compatible-mode/v1
https://dashscope-finance.aliyuncs.com/compatible-mode/v1
- HTTP端点:
调用对话补全接口时需访问:POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
2. 代码示例(Python)
from openai import OpenAI client = OpenAI( api_key="YOUR_DASHSCOPE_API_KEY", base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" ) response = client.chat.completions.create( model="qwen-plus", # 指定通义千问系列模型 messages=[ {'role': 'user', 'content': '你好'} ] )3. 支持的通义千问模型
模型名称 适用场景 qwen-turbo
通用对话 qwen-plus
复杂任务处理 qwen-vl
多模态视觉问答
二、与原生OpenAI API的区别
对比维度 DashScope OpenAI兼容模式 原生OpenAI API 认证方式 使用阿里云API Key( DASHSCOPE_API_KEY
)[1][2][5]OpenAI官方API Key 模型范围 仅限通义千问系列(如qwen-turbo/qwen-plus)[1][5][9] GPT系列(如gpt-3.5/gpt-4) 部署环境 支持公有云/金融云双环境配置[5][9] 仅全球统一服务 错误码体系 需参考阿里云特定错误码文档[5][6] OpenAI独立错误码系统 多模态支持 VL/Audio模型需切换专用endpoint[5][9] DALL·E/Whisper等通过独立API实现
三、典型差异场景
多轮对话实现
虽然接口参数与OpenAI一致(通过messages
传递角色消息),但通义千问对system
角色的指令响应策略可能存在差异[2][6]。流式输出
部分早期文档未明确提及流式响应支持情况(需实测验证)[2][6]。计费方式
按阿里云百炼平台的Token计费规则执行,与OpenAI定价体系无关[6][10]。
通过兼容模式可快速迁移现有基于OpenAI的应用至通义千问生态,但需注意模型能力边界及平台特定限制[1][5][9]。
Citations:
- 1: https://help.aliyun.com/zh/model-studio/developer-reference/compatibility-of-openai-with-dashscope
- 2: https://help.aliyun.com/zh/model-studio/user-guide/text-generation/
- 3: https://www.explinks.com/blog/lingji-ai-model-code-example/
- 4: https://blog.csdn.net/u013524655/article/details/144751904
- 5: https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api
- 6: https://blog.csdn.net/lovechris00/article/details/136938486
- 7: 无法用dashscope model server使用OpenAI SDK modelscope/modelscope-agent#493
- 8: https://github.com/fruitbars/simple-one-api
- 9: https://help.aliyun.com/zh/model-studio/developer-reference/qwen-vl-compatible-with-openai
- 10: https://dashscope.aliyun.com
请注意 DashScope 服务兼容性差异
当前代码已正确使用阿里云 DashScope 的 OpenAI 兼容模式(默认 baseURL 为
https://dashscope.aliyuncs.com/compatible-mode/v1
),但请注意以下几点差异和注意事项,以确保兼容性:
接口支持范围
DashScope 兼容模式主要支持 Chat Completions API(对话补全接口),因此现有实现仅适用于这一接口的调用。与原生 OpenAI API 的差异
- 认证方式不同:DashScope 使用阿里云 API Key,而 OpenAI 则使用官方 API Key。
- 模型支持有限:仅支持通义千问系列(如
qwen-plus
、qwen-turbo
、qwen-vl
),与 GPT 系列模型存在差异。- 错误码体系、计费方式及部分功能(如流式输出、多模态支持)的实现可能存在不同。
配置扩展建议
如需支持金融云环境,请留意可能需要将baseURL
调整为https://dashscope-finance.aliyuncs.com/compatible-mode/v1
。总体来看,代码实现符合 DashScope 兼容模式的基本要求,但建议在文档或代码注释中添加上述信息,以便在未来遇到接口差异或功能限制时能迅速定位问题。
packages/ai-native/package.json (1)
24-24
:❓ Verification inconclusive
验证依赖版本兼容性
新添加的
@ai-sdk/openai-compatible
包版本为 0.1.11,属于早期版本。建议:
- 验证与现有的 AI SDK 依赖(如 @ai-sdk/anthropic 1.1.6、@ai-sdk/deepseek 0.1.8)的兼容性
- 检查是否有更新的稳定版本
🌐 Web query:
@ai-sdk/openai-compatible 的最新版本是多少?有哪些重大变更?
💡 Result:
@ai-sdk/openai-compatible
的最新版本是0.1.11(截至2025年2月19日)[1]。该包专注于为开发者提供构建OpenAI兼容API服务的基础框架,相比官方OpenAI SDK更轻量级且专注核心功能。主要特性与近期变更
- 核心架构优化
- 采用模块化设计分离基础通信层与业务逻辑[1]
- 默认支持流式响应和批量请求处理(相比早期版本性能提升30%)[1]
- 认证机制改进
- 支持多种认证方式:
允许混合使用预定义认证和自定义头信息[1][10]// API Key方式 createOpenAICompatible({ apiKey: 'YOUR_KEY' }) // 自定义Header方式 createOpenAICompatible({ headers: { Authorization: 'Bearer TOKEN' } })
- 模型兼容性增强
- 新增类型化模型ID支持:
实现IDE自动补全的同时保持扩展性[1]type CustomModelIDs = 'llama3-70b' | 'mixtral-8x22b' | (string & {}) const provider = createOpenAICompatible<CustomModelIDs>(...)
- 服务端兼容改进
- 新增
queryParams
配置项支持第三方API的特殊参数:适配Azure等云服务商的特殊接口要求[10]createOpenAICompatible({ queryParams: { 'api-version': '2025-02-01' } })
- 错误处理优化
- 重试机制从固定间隔改为指数退避策略
- 新增429状态码的自动流量控制(比v0.0.x系列更智能)[1][4]
迁移注意事项
使用新版时需注意:
baseURL
现在必须显式声明(旧版允许部分省略)model
参数严格校验格式(旧版允许任意字符串)- 默认超时从30s缩短为10s(可通过
fetch
配置覆盖)[1][10]建议通过类型检查逐步迁移:
// legacy code const oldProvider = createOpenAICompatible() // modern approach const newProvider = createOpenAICompatible<ModelTypes>({ baseURL: 'https://api.example.com/v2' // required now })Citations:
- 1: https://www.npmjs.com/package/@ai-sdk/openai-compatible
- 2: https://discuss.ai.google.dev/t/openai-compatible-endpoint-changed/51842
- 3: https://blog.csdn.net/2301_76946366/article/details/136972043
- 4: https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation
- 5: https://sdk.vercel.ai/providers/ai-sdk-providers/openai
- 6: https://learn.microsoft.com/zh-cn/azure/ai-services/openai/api-version-deprecation
- 7: https://www.npmjs.com/package/@ai-sdk/openai
- 8: https://hadxu.github.io/post/2024-04-19-OpenAI-compatible-API/
- 9: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Zm2ycv77m
- 10: https://sdk.vercel.ai/providers/openai-compatible-providers
重要:验证 @ai-sdk/openai-compatible 依赖及其配置兼容性
当前
packages/ai-native/package.json
中新增的@ai-sdk/openai-compatible
版本为0.1.11
,经查询该版本为最新发布版本(截至2025年2月),并引入了核心架构优化、认证机制改进、模型兼容性增强、错误处理优化等多项更新。建议:
- 依赖兼容性验证:确认该版本与其他 AI SDK 依赖(如
@ai-sdk/anthropic
1.1.6、@ai-sdk/deepseek
0.1.8)的兼容性。- 配置调整确认:检查是否需要调整调用配置,例如新版本对
baseURL
的强制要求、model
参数的严格校验以及默认超时配置的变化(从30s缩短为10s)。- 持续关注更新:后续若出现更稳定的版本或进一步迁移指南,可考虑对应升级。
bfdde80
to
7ad29c4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
yarn.lock
is excluded by!**/yarn.lock
,!**/*.lock
📒 Files selected for processing (2)
packages/ai-native/package.json
(2 hunks)packages/ai-native/src/node/openai/openai-language-model.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/ai-native/package.json
⏰ Context from checks skipped due to timeout of 90000ms (8)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: build (macos-latest, 20.x)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: build-windows
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (1)
packages/ai-native/src/node/openai/openai-language-model.ts (1)
1-2
: 导入声明更新正确!成功引入了
@ai-sdk/openai-compatible
包中的兼容性组件,这与依赖更新保持一致。同时添加了必要的LanguageModelV1
类型导入。
protected initializeProvider(options: IAIBackServiceOption): OpenAICompatibleProvider { | ||
const apiKey = options.apiKey; | ||
if (!apiKey) { | ||
throw new Error(`Please provide OpenAI API Key in preferences (${AINativeSettingSectionsId.OpenaiApiKey})`); | ||
} | ||
return createOpenAI({ | ||
return createOpenAICompatible({ | ||
apiKey, | ||
baseURL: options.baseURL || 'https://dashscope.aliyuncs.com/compatible-mode/v1', | ||
name: 'openai', | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
请验证 DashScope 端点配置
代码实现从标准 OpenAI 切换到了阿里云 DashScope 的兼容模式端点。建议:
- 验证 baseURL 是否为最新的正确端点
- 考虑将端点 URL 移至配置文件中
🏁 Script executed:
#!/bin/bash
# 验证端点配置
curl -I https://dashscope.aliyuncs.com/compatible-mode/v1
Length of output: 471
请更新 DashScope 端点配置
目前代码通过以下方式设置了 DashScope 的接口地址:
baseURL: options.baseURL || 'https://dashscope.aliyuncs.com/compatible-mode/v1'
然而经过测试,该端点返回了 HTTP 404 错误,说明当前 URL 可能已失效或配置错误。建议:
- 核对接口文档:确认阿里云 DashScope 兼容模式的最新正确端点地址。
- 集中配置管理:考虑将接口地址移至专门的配置文件,以便后续进行统一管理和维护。
protected getModelIdentifier(provider: OpenAICompatibleProvider, modelId?: string): LanguageModelV1 { | ||
return provider(modelId || 'qwen-max'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
建议添加模型兼容性文档
当前实现使用了阿里云的 Qwen-Max 模型作为默认选项。建议:
- 添加支持模型列表的文档
- 说明与 OpenAI 模型的兼容性对应关系
另外,建议验证模型标识符的有效性:
🌐 Web query:
阿里云 DashScope 兼容模式支持哪些模型?qwen-max 是否仍然可用?
💡 Result:
阿里云 DashScope 的 OpenAI 兼容模式支持通义千问系列多款模型,包括基础版和多个历史快照版本。根据最新文档:
支持的模型列表
-
基础版模型:
qwen-turbo
(8k上下文)qwen-plus
(32k上下文)qwen-max
(8k上下文)qwen-max-longcontext
(30k上下文)[3][7]
-
历史快照版:
qwen-max-0403
(2024年4月3日快照)qwen-max-0107
(2024年1月7日快照)qwen-max-1201
(2023年12月1日快照)[3]
qwen-max 的可用性
qwen-max
目前仍可正常调用,但需注意:
- 该模型的默认上下文窗口为8k tokens[3]
- 阿里云会定期发布新快照版(如
qwen-max-0403
),旧快照版会在新版本发布后保留1个月维护期[3][8] - 建议优先使用最新快照版以获得更稳定的表现
调用注意事项
需通过指定以下参数调用:
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1" # OpenAI兼容接口地址
api_key = "sk-xxx" # 从阿里云控制台获取的API Key[2][7]
若需要超长上下文支持(30k tokens),应选择专有模型qwen-max-longcontext
[3]。对于视觉任务则需要切换至通义千问VL系列模型[5][8]。
Citations:
- 1: https://blog.csdn.net/avideointerfaces/article/details/136471520
- 2: https://help.aliyun.com/zh/model-studio/user-guide/text-generation/
- 3: https://blog.csdn.net/u012917925/article/details/137993551
- 4: https://help.aliyun.com/zh/model-studio/user-guide/embedding
- 5: https://help.aliyun.com/zh/model-studio/user-guide/vision/
- 6: https://help.aliyun.com/zh/model-studio/billing-for-model-studio
- 7: https://help.aliyun.com/zh/model-studio/developer-reference/compatibility-of-openai-with-dashscope
- 8: https://help.aliyun.com/zh/model-studio/user-guide/long-context-qwen-long
- 9: https://www.explinks.com/api/scd20240802571808624111
- 10: https://www.explinks.com/blog/ua-how-to-call-aliyun-tongyi-modelscope-api/
请补充详细的模型兼容性说明文档
当前代码中默认调用的 'qwen-max'
模型在阿里云 DashScope 的 OpenAI 兼容模式下仍然有效,但最新文档显示该模式支持多款通义千问系列模型,包括:
- 基础版模型:
qwen-turbo
(8k上下文)、qwen-plus
(32k上下文)、qwen-max
(8k上下文)、qwen-max-longcontext
(30k上下文) - 历史快照版:
qwen-max-0403
、qwen-max-0107
、qwen-max-1201
建议:
- 文档更新:在项目文档中添加支持模型列表,并详细说明各模型的特性(如上下文窗口大小、维护周期、调用参数等)。
- 兼容性说明:明确列出当前与 OpenAI 模型接口兼容的对照关系,便于开发者选择适合的模型版本。
- 默认模型确认:虽然
qwen-max
仍可正常调用,但可附注其默认8k上下文限制,并建议根据场景选择是否切换至最新快照或其它版本。
参考代码位置:
packages/ai-native/src/node/openai/openai-language-model.ts
,第25-27行
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4402 +/- ##
=======================================
Coverage 53.54% 53.54%
=======================================
Files 1652 1652
Lines 101904 101904
Branches 22049 22041 -8
=======================================
Hits 54567 54567
Misses 39382 39382
Partials 7955 7955
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Types
Background or solution
refactor: use openai-compatible to support providerOptions
Changelog
refactor: use openai-compatible to support providerOptions
Summary by CodeRabbit
Chores
Refactor