Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions astrbot/core/provider/sources/openai_embedding_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, provider_config: dict, provider_settings: dict) -> None:
api_base = provider_config.get(
"embedding_api_base", "https://api.openai.com/v1"
).strip()
if api_base and not api_base.endswith("/v1") and not api_base.endswith("/v1/"):
api_base = api_base.rstrip("/") + "/v1"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

当前使用字符串操作的实现方式,在处理带有查询参数的 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))

logger.info(f"[OpenAI Embedding] {provider_id} Using API Base: {api_base}")
self.client = AsyncOpenAI(
api_key=provider_config.get("embedding_api_key"),
Expand Down