feat: auto-append /v1 to embedding_api_base in OpenAI embedding provider#6863
feat: auto-append /v1 to embedding_api_base in OpenAI embedding provider#6863Izayoi9 wants to merge 1 commit intoAstrBotDevs:masterfrom
Conversation
…er (AstrBotDevs#6855) When users configure `embedding_api_base` without the `/v1` suffix, the OpenAI SDK does not auto-complete it, causing request path errors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求解决了 OpenAI SDK 在处理 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider whether this
/v1auto-append logic should be shared with other OpenAI-compatible providers (chat, completions, etc.) so that API base handling is consistent across the codebase. - The unconditional appending of
/v1might break OpenAI-compatible endpoints that intentionally omit the version (or use a different path); you may want to gate this behavior behind a config flag or limit it to known OpenAI hosts.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider whether this `/v1` auto-append logic should be shared with other OpenAI-compatible providers (chat, completions, etc.) so that API base handling is consistent across the codebase.
- The unconditional appending of `/v1` might break OpenAI-compatible endpoints that intentionally omit the version (or use a different path); you may want to gate this behavior behind a config flag or limit it to known OpenAI hosts.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request modifies the OpenAI embedding source to ensure the API base URL consistently ends with /v1. A review comment points out that the current string manipulation approach might incorrectly handle URLs with query parameters, suggesting the use of urllib.parse for a more robust solution.
| if api_base and not api_base.endswith("/v1") and not api_base.endswith("/v1/"): | ||
| api_base = api_base.rstrip("/") + "/v1" |
There was a problem hiding this comment.
当前使用字符串操作的实现方式,在处理带有查询参数的 URL 时可能会出错。例如,如果 embedding_api_base 被设置为 https://example.com/api?key=123,代码会将其错误地更改为 https://example.com/api?key=123/v1,这是一个无效的 URL。
建议使用 urllib.parse 来处理 URL,这样可以确保只修改路径部分,而不会影响 URL 的其他部分(如查询参数),使逻辑更加健壮。
if api_base:
from urllib.parse import urlparse, urlunparse
parsed = urlparse(api_base)
if not parsed.path.rstrip("/").endswith("/v1"):
new_path = parsed.path.rstrip("/") + "/v1"
api_base = urlunparse(parsed._replace(path=new_path))
Fixes #6855
当用户配置
embedding_api_base时未填写/v1后缀,OpenAI SDK 不会自动补全,导致请求路径错误(如https://api.openai.com/embeddings而非https://api.openai.com/v1/embeddings)。Modifications / 改动点
在
astrbot/core/provider/sources/openai_embedding_source.py中,初始化api_base后增加判断:若 URL 不以/v1或/v1/结尾,则自动补全/v1。This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
修改前,用户配置
embedding_api_base为https://api.openai.com时,实际请求路径为https://api.openai.com/embeddings,返回 404。修改后,自动补全为
https://api.openai.com/v1,请求路径正确为https://api.openai.com/v1/embeddings。Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
New Features: