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

refactor: use openai-compatible to support providerOptions #4402

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ensorrow
Copy link
Contributor

@ensorrow ensorrow commented Feb 24, 2025

Types

  • 🧹 Chores

Background or solution

refactor: use openai-compatible to support providerOptions

image

Changelog

refactor: use openai-compatible to support providerOptions

Summary by CodeRabbit

  • Chores

    • 更新了 AI 依赖项:移除了之前的依赖,添加了全新兼容的包,以提升整体服务的稳定性和一致性。
  • Refactor

    • 优化了 AI 模型集成逻辑,通过调整内部参数配置,确保系统更高效地调用 AI 服务。

@opensumi opensumi bot added the ⚙️ refactor Refactor code label Feb 24, 2025
@life2015
Copy link
Member

为啥 yarn lock 的修改这么大

@ensorrow
Copy link
Contributor Author

为啥 yarn lock 的修改这么大

刚刚只改了 package.json lock 一些包的版本不符合预期,我删了重新生成了,不过好像有点问题,我瞅瞅

Copy link
Contributor

coderabbitai bot commented Feb 24, 2025

"""

Walkthrough

此次修改涉及依赖和模型提供者接口的更新。在 package.json 中,移除了 @ai-sdk/openai (^1.1.9) 依赖,并添加了 @ai-sdk/openai-compatible (^0.1.11) 依赖。在 openai-language-model.ts 文件中,对 OpenAIModel 类进行了调整,将原本使用的 OpenAIProvidercreateOpenAI 替换为 OpenAICompatibleProvidercreateOpenAICompatible(新增参数 name"openai"),同时更新了 getModelIdentifier 方法的参数和返回类型。

Changes

文件 修改概述
packages/.../package.json 更新依赖版本:@ai-sdk/anthropic^1.1.6 更新至 ^1.1.9@ai-sdk/deepseek^0.1.8 更新至 ^0.1.11,移除 @ai-sdk/openai,新增 @ai-sdk/openai-compatible (^0.1.11),ai^4.1.21 更新至 ^4.1.45
packages/.../openai-language-model.ts OpenAIProvider 替换为 OpenAICompatibleProvider;将 createOpenAI 替换为 createOpenAICompatible 并新增参数 name = "openai";更新 getModelIdentifier 方法签名与返回类型为 LanguageModelV1

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
Loading

"""

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > [email protected]: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between ebc1030 and 83cc8a0.

⛔ 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实现

三、典型差异场景

  1. 多轮对话实现
    虽然接口参数与OpenAI一致(通过messages传递角色消息),但通义千问对system角色的指令响应策略可能存在差异[2][6]。

  2. 流式输出
    部分早期文档未明确提及流式响应支持情况(需实测验证)[2][6]。

  3. 计费方式
    按阿里云百炼平台的Token计费规则执行,与OpenAI定价体系无关[6][10]。


通过兼容模式可快速迁移现有基于OpenAI的应用至通义千问生态,但需注意模型能力边界及平台特定限制[1][5][9]。

Citations:


请注意 DashScope 服务兼容性差异

当前代码已正确使用阿里云 DashScope 的 OpenAI 兼容模式(默认 baseURL 为 https://dashscope.aliyuncs.com/compatible-mode/v1),但请注意以下几点差异和注意事项,以确保兼容性:

  • 接口支持范围
    DashScope 兼容模式主要支持 Chat Completions API(对话补全接口),因此现有实现仅适用于这一接口的调用。

  • 与原生 OpenAI API 的差异

    • 认证方式不同:DashScope 使用阿里云 API Key,而 OpenAI 则使用官方 API Key。
    • 模型支持有限:仅支持通义千问系列(如 qwen-plusqwen-turboqwen-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,属于早期版本。建议:

  1. 验证与现有的 AI SDK 依赖(如 @ai-sdk/anthropic 1.1.6、@ai-sdk/deepseek 0.1.8)的兼容性
  2. 检查是否有更新的稳定版本

🌐 Web query:

@ai-sdk/openai-compatible 的最新版本是多少?有哪些重大变更?

💡 Result:

@ai-sdk/openai-compatible的最新版本是0.1.11(截至2025年2月19日)[1]。该包专注于为开发者提供构建OpenAI兼容API服务的基础框架,相比官方OpenAI SDK更轻量级且专注核心功能。

主要特性与近期变更

  1. 核心架构优化
  • 采用模块化设计分离基础通信层与业务逻辑[1]
  • 默认支持流式响应和批量请求处理(相比早期版本性能提升30%)[1]
  1. 认证机制改进
  • 支持多种认证方式:
    // API Key方式
    createOpenAICompatible({ apiKey: 'YOUR_KEY' })
    
    // 自定义Header方式
    createOpenAICompatible({
      headers: { Authorization: 'Bearer TOKEN' }
    })
    允许混合使用预定义认证和自定义头信息[1][10]
  1. 模型兼容性增强
  • 新增类型化模型ID支持:
    type CustomModelIDs = 'llama3-70b' | 'mixtral-8x22b' | (string & {})
    const provider = createOpenAICompatible<CustomModelIDs>(...)
    实现IDE自动补全的同时保持扩展性[1]
  1. 服务端兼容改进
  • 新增queryParams配置项支持第三方API的特殊参数:
    createOpenAICompatible({
      queryParams: { 'api-version': '2025-02-01' }
    })
    适配Azure等云服务商的特殊接口要求[10]
  1. 错误处理优化
  • 重试机制从固定间隔改为指数退避策略
  • 新增429状态码的自动流量控制(比v0.0.x系列更智能)[1][4]

迁移注意事项

使用新版时需注意:

  1. baseURL现在必须显式声明(旧版允许部分省略)
  2. model参数严格校验格式(旧版允许任意字符串)
  3. 默认超时从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:


重要:验证 @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)。
  • 持续关注更新:后续若出现更稳定的版本或进一步迁移指南,可考虑对应升级。

@ensorrow ensorrow force-pushed the refactor/use-openai-compatible branch from bfdde80 to 7ad29c4 Compare February 24, 2025 03:40
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between a251632 and 7ad29c4.

⛔ 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 类型导入。

Comment on lines +13 to 23
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',
});
}
Copy link
Contributor

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 的兼容模式端点。建议:

  1. 验证 baseURL 是否为最新的正确端点
  2. 考虑将端点 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 兼容模式的最新正确端点地址。
  • 集中配置管理:考虑将接口地址移至专门的配置文件,以便后续进行统一管理和维护。

Comment on lines +25 to 27
protected getModelIdentifier(provider: OpenAICompatibleProvider, modelId?: string): LanguageModelV1 {
return provider(modelId || 'qwen-max');
}
Copy link
Contributor

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 模型作为默认选项。建议:

  1. 添加支持模型列表的文档
  2. 说明与 OpenAI 模型的兼容性对应关系

另外,建议验证模型标识符的有效性:


🌐 Web query:

阿里云 DashScope 兼容模式支持哪些模型?qwen-max 是否仍然可用?

💡 Result:

阿里云 DashScope 的 OpenAI 兼容模式支持通义千问系列多款模型,包括基础版和多个历史快照版本。根据最新文档:

支持的模型列表

  1. 基础版模型

    • qwen-turbo(8k上下文)
    • qwen-plus(32k上下文)
    • qwen-max(8k上下文)
    • qwen-max-longcontext(30k上下文)[3][7]
  2. 历史快照版

    • 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:


请补充详细的模型兼容性说明文档

当前代码中默认调用的 'qwen-max' 模型在阿里云 DashScope 的 OpenAI 兼容模式下仍然有效,但最新文档显示该模式支持多款通义千问系列模型,包括:

  • 基础版模型qwen-turbo(8k上下文)、qwen-plus(32k上下文)、qwen-max(8k上下文)、qwen-max-longcontext(30k上下文)
  • 历史快照版qwen-max-0403qwen-max-0107qwen-max-1201

建议:

  • 文档更新:在项目文档中添加支持模型列表,并详细说明各模型的特性(如上下文窗口大小、维护周期、调用参数等)。
  • 兼容性说明:明确列出当前与 OpenAI 模型接口兼容的对照关系,便于开发者选择适合的模型版本。
  • 默认模型确认:虽然 qwen-max 仍可正常调用,但可附注其默认8k上下文限制,并建议根据场景选择是否切换至最新快照或其它版本。

参考代码位置:
packages/ai-native/src/node/openai/openai-language-model.ts,第25-27行

Copy link

codecov bot commented Feb 24, 2025

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 53.54%. Comparing base (ebc1030) to head (7ad29c4).

Files with missing lines Patch % Lines
...ai-native/src/node/openai/openai-language-model.ts 0.00% 4 Missing ⚠️
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           
Flag Coverage Δ
jsdom 49.05% <0.00%> (ø)
node 12.22% <0.00%> (ø)

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.

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

Successfully merging this pull request may close these issues.

2 participants