mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-05 23:56:37 +08:00
feat: 增强Gemini API tools参数处理
- 修改GeminiRequest模型,使tools字段支持单个工具对象或工具对象列表 - 在gemini_chat_service中添加类型转换逻辑,确保tools始终以列表形式处理 - 提高API的灵活性和兼容性
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from typing import List, Optional, Dict, Any, Literal
|
from typing import List, Optional, Dict, Any, Literal, Union
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ class GeminiContent(BaseModel):
|
|||||||
|
|
||||||
class GeminiRequest(BaseModel):
|
class GeminiRequest(BaseModel):
|
||||||
contents: List[GeminiContent] = []
|
contents: List[GeminiContent] = []
|
||||||
tools: Optional[List[Dict[str, Any]]] = []
|
tools: Optional[Union[List[Dict[str, Any]], Dict[str, Any]]] = []
|
||||||
safetySettings: Optional[List[SafetySetting]] = None
|
safetySettings: Optional[List[SafetySetting]] = None
|
||||||
generationConfig: Optional[GenerationConfig] = None
|
generationConfig: Optional[GenerationConfig] = None
|
||||||
systemInstruction: Optional[SystemInstruction] = None
|
systemInstruction: Optional[SystemInstruction] = None
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ def _build_tools(model: str, payload: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|||||||
|
|
||||||
tool = dict()
|
tool = dict()
|
||||||
if payload and isinstance(payload, dict) and "tools" in payload:
|
if payload and isinstance(payload, dict) and "tools" in payload:
|
||||||
|
if payload.get("tools") and isinstance(payload.get("tools"), dict):
|
||||||
|
payload["tools"] = [payload.get("tools")]
|
||||||
items = payload.get("tools", [])
|
items = payload.get("tools", [])
|
||||||
if items and isinstance(items, list):
|
if items and isinstance(items, list):
|
||||||
tool.update(_merge_tools(items))
|
tool.update(_merge_tools(items))
|
||||||
|
|||||||
Reference in New Issue
Block a user