diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index a9d9e5e96..1021d81b5 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -740,6 +740,7 @@ CONFIG_METADATA_2 = { "api_base": "https://api.openai.com/v1", "timeout": 120, "model_config": {"model": "gpt-4o-mini", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], "hint": "也兼容所有与 OpenAI API 兼容的服务。", @@ -755,6 +756,7 @@ CONFIG_METADATA_2 = { "api_base": "", "timeout": 120, "model_config": {"model": "gpt-4o-mini", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -768,6 +770,7 @@ CONFIG_METADATA_2 = { "api_base": "https://api.x.ai/v1", "timeout": 120, "model_config": {"model": "grok-2-latest", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "xai_native_search": False, "modalities": ["text", "image", "tool_use"], @@ -799,6 +802,7 @@ CONFIG_METADATA_2 = { "key": ["ollama"], # ollama 的 key 默认是 ollama "api_base": "http://localhost:11434/v1", "model_config": {"model": "llama3.1-8b", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -813,6 +817,7 @@ CONFIG_METADATA_2 = { "model_config": { "model": "llama-3.1-8b", }, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -829,6 +834,7 @@ CONFIG_METADATA_2 = { "model": "gemini-1.5-flash", "temperature": 0.4, }, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -870,6 +876,7 @@ CONFIG_METADATA_2 = { "api_base": "https://api.deepseek.com/v1", "timeout": 120, "model_config": {"model": "deepseek-chat", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "tool_use"], }, @@ -883,6 +890,7 @@ CONFIG_METADATA_2 = { "api_base": "https://api.302.ai/v1", "timeout": 120, "model_config": {"model": "gpt-4.1-mini", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -899,6 +907,7 @@ CONFIG_METADATA_2 = { "model": "deepseek-ai/DeepSeek-V3", "temperature": 0.4, }, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -915,6 +924,7 @@ CONFIG_METADATA_2 = { "model": "deepseek/deepseek-r1", "temperature": 0.4, }, + "custom_headers": {}, "custom_extra_body": {}, }, "小马算力": { @@ -930,6 +940,7 @@ CONFIG_METADATA_2 = { "model": "kimi-k2-instruct-0905", "temperature": 0.7, }, + "custom_headers": {}, "custom_extra_body": {}, }, "优云智算": { @@ -944,6 +955,7 @@ CONFIG_METADATA_2 = { "model_config": { "model": "moonshotai/Kimi-K2-Instruct", }, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -957,6 +969,7 @@ CONFIG_METADATA_2 = { "timeout": 120, "api_base": "https://api.moonshot.cn/v1", "model_config": {"model": "moonshot-v1-8k", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -972,6 +985,8 @@ CONFIG_METADATA_2 = { "model_config": { "model": "glm-4-flash", }, + "custom_headers": {}, + "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, "Dify": { @@ -1028,6 +1043,7 @@ CONFIG_METADATA_2 = { "timeout": 120, "api_base": "https://api-inference.modelscope.cn/v1", "model_config": {"model": "Qwen/Qwen3-32B", "temperature": 0.4}, + "custom_headers": {}, "custom_extra_body": {}, "modalities": ["text", "image", "tool_use"], }, @@ -1040,6 +1056,7 @@ CONFIG_METADATA_2 = { "key": [], "api_base": "https://api.fastgpt.in/api/v1", "timeout": 60, + "custom_headers": {}, "custom_extra_body": {}, }, "Whisper(API)": { @@ -1321,6 +1338,12 @@ CONFIG_METADATA_2 = { "render_type": "checkbox", "hint": "模型支持的模态。如所填写的模型不支持图像,请取消勾选图像。", }, + "custom_headers": { + "description": "自定义添加请求头", + "type": "dict", + "items": {}, + "hint": "此处添加的键值对将被合并到 OpenAI SDK 的 default_headers 中,用于自定义 HTTP 请求头。值必须为字符串。", + }, "custom_extra_body": { "description": "自定义请求体参数", "type": "dict", diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index 076afc40f..823287b6f 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -43,14 +43,23 @@ class ProviderOpenAIOfficial(Provider): self.api_keys: list = super().get_keys() self.chosen_api_key = self.api_keys[0] if len(self.api_keys) > 0 else None self.timeout = provider_config.get("timeout", 120) + self.custom_headers = provider_config.get("custom_headers", {}) if isinstance(self.timeout, str): self.timeout = int(self.timeout) + + if not isinstance(self.custom_headers, dict) or not self.custom_headers: + self.custom_headers = None + else: + for key in self.custom_headers: + self.custom_headers[key] = str(self.custom_headers[key]) + # 适配 azure openai #332 if "api_version" in provider_config: # 使用 azure api self.client = AsyncAzureOpenAI( api_key=self.chosen_api_key, api_version=provider_config.get("api_version", None), + default_headers=self.custom_headers, base_url=provider_config.get("api_base", ""), timeout=self.timeout, ) @@ -59,6 +68,7 @@ class ProviderOpenAIOfficial(Provider): self.client = AsyncOpenAI( api_key=self.chosen_api_key, base_url=provider_config.get("api_base", None), + default_headers=self.custom_headers, timeout=self.timeout, )