mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 21:30:01 +08:00
检查bot是否运行正常 / bot check (push) Canceled after 0s
CodeQL Code Security Analysis / Analyze (python) (push) Canceled after 0s
Ruff Lint / Ruff Lint (push) Canceled after 0s
Sequential Lint and Type Check / ruff-call (push) Canceled after 0s
Release Drafter / Update Release Draft (push) Canceled after 0s
Force Sync to Aliyun / sync (push) Canceled after 0s
Update Version / update-version (push) Canceled after 0s
Sequential Lint and Type Check / pyright-call (push) Canceled after 0s
* ✨ feat(llm): 新增 Grok 适配器及 API 密钥重置指令 - 新增 Grok 适配器并支持原生推特检索工具 `x_search` - 优化 DeepSeek 适配器以迁移至 Responses 协议,增强多模态支持 - 在 LLM 管理插件中新增 `llm reset` 子命令以重置 Key 状态 - 增强 MCP 连接异常处理及流关闭容错能力 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from typing import Any
|
|
|
|
from zhenxun.services.ai.core.models import ModelCapabilities
|
|
from zhenxun.services.ai.llm.adapters.handlers.openai_handlers import (
|
|
OpenAIResponsesTextHandler,
|
|
ResponsesToolSerializer,
|
|
)
|
|
from zhenxun.services.ai.llm.adapters.openai import OpenAIResponsesAdapter
|
|
|
|
|
|
class GrokToolSerializer(ResponsesToolSerializer):
|
|
"""Grok 专属工具序列化器"""
|
|
|
|
def serialize_server_tools(
|
|
self, tools: list[Any], capabilities: ModelCapabilities
|
|
) -> list[dict[str, Any]]:
|
|
res = []
|
|
for t in tools:
|
|
type_id = getattr(t, "type_id", "unknown")
|
|
if type_id not in capabilities.supported_native_tools:
|
|
continue
|
|
|
|
if type_id == "web_search":
|
|
res.append({"type": "web_search"})
|
|
elif type_id == "x_search":
|
|
payload = {"type": "x_search"}
|
|
if getattr(t, "allowed_x_handles", None):
|
|
payload["allowed_x_handles"] = t.allowed_x_handles
|
|
if getattr(t, "excluded_x_handles", None):
|
|
payload["excluded_x_handles"] = t.excluded_x_handles
|
|
res.append(payload)
|
|
elif type_id == "code_execution":
|
|
res.append({"type": "code_interpreter"})
|
|
elif type_id == "file_search":
|
|
res.append({"type": "file_search"})
|
|
return res
|
|
|
|
|
|
class GrokTextHandler(OpenAIResponsesTextHandler):
|
|
"""Grok 文本处理器"""
|
|
|
|
def __init__(self, api_type: str = "grok"):
|
|
super().__init__(api_type=api_type)
|
|
self.serializer = GrokToolSerializer(api_type=api_type)
|
|
|
|
|
|
class GrokAdapter(OpenAIResponsesAdapter):
|
|
"""xAI Grok API 适配器"""
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.text_handler = GrokTextHandler(api_type=self.api_type)
|
|
|
|
@property
|
|
def api_type(self) -> str:
|
|
return "grok"
|
|
|
|
@property
|
|
def supported_api_types(self) -> list[str]:
|
|
return ["grok"]
|