Skip to content
Open
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
8 changes: 7 additions & 1 deletion astrbot/core/provider/sources/openai_embedding_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self, provider_config: dict, provider_settings: dict) -> None:
http_client=http_client,
)
self.model = provider_config.get("embedding_model", "text-embedding-3-small")
# 一次性构建并缓存 embedding 请求参数,避免每次调用都执行 SiliconFlow 检测并刷 WARN 日志
self._cached_kwargs = self._build_embedding_kwargs()

async def get_embedding(self, text: str) -> list[float]:
"""获取文本的嵌入"""
Expand All @@ -66,7 +68,11 @@ async def get_embeddings(self, text: list[str]) -> list[list[float]]:
return [item.embedding for item in embeddings.data]

def _embedding_kwargs(self) -> dict:
"""构建嵌入请求的可选参数"""
"""返回启动时构建并缓存的 embedding 请求参数"""
return self._cached_kwargs

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.

medium

Returning a direct reference to the internal mutable dictionary self._cached_kwargs can lead to unintended side effects if any caller or subclass modifies the returned dictionary. It is safer to return a copy of the dictionary to ensure the cached state remains immutable from the outside.

Suggested change
def _embedding_kwargs(self) -> dict:
"""构建嵌入请求的可选参数"""
"""返回启动时构建并缓存的 embedding 请求参数"""
return self._cached_kwargs
def _embedding_kwargs(self) -> dict:
"""返回启动时构建并缓存的 embedding 请求参数"""
return self._cached_kwargs.copy()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. Applied in commit 227ff74 — _embedding_kwargs() now returns self._cached_kwargs.copy() to prevent external mutation of the cached dict.


def _build_embedding_kwargs(self) -> dict:
"""构建嵌入请求的可选参数。仅在 __init__ 时调用一次,SiliconFlow 检测与 WARN 日志只触发一次。"""
kwargs = {}
if "embedding_dimensions" in self.provider_config:
try:
Expand Down