feat: v3.0 Function Calling模式 - LLM自主调用工具
This commit is contained in:
@@ -382,5 +382,200 @@ class LLMService:
|
||||
yield {"type": "content", "text": buffer}
|
||||
|
||||
|
||||
async def chat_with_tools(
|
||||
self,
|
||||
messages: List[Dict],
|
||||
provider_config: dict,
|
||||
agent_config: dict,
|
||||
tools: List[Dict] = None,
|
||||
enable_thinking: bool = True,
|
||||
images: List[Dict] = None
|
||||
) -> Tuple[str, Optional[str], Optional[List[Dict]]]:
|
||||
"""
|
||||
支持Function Calling的对话
|
||||
|
||||
Args:
|
||||
messages: 对话历史
|
||||
provider_config: LLM Provider配置
|
||||
agent_config: Agent配置
|
||||
tools: 工具定义列表(OpenAI Function Calling格式)
|
||||
enable_thinking: 是否启用思考
|
||||
images: 图片数据列表
|
||||
|
||||
Returns:
|
||||
Tuple[str, Optional[str], Optional[List[Dict]]]: (回复内容, 思考过程, 工具调用记录)
|
||||
"""
|
||||
api_base = provider_config.get('api_base')
|
||||
api_key = provider_config.get('api_key')
|
||||
model = agent_config.get('model_override') or provider_config.get('default_model', 'auto')
|
||||
supports_function_calling = provider_config.get('supports_function_calling', False)
|
||||
|
||||
max_tokens = provider_config.get('max_tokens', 4096)
|
||||
temperature = agent_config.get('temperature_override') or provider_config.get('temperature', 0.7)
|
||||
|
||||
# 如果不支持Function Calling,直接调用普通chat
|
||||
if not supports_function_calling or not tools:
|
||||
response, thinking = await self.chat(messages, provider_config, agent_config, enable_thinking, images)
|
||||
return response, thinking, None
|
||||
|
||||
# 构建消息
|
||||
final_messages = messages.copy()
|
||||
system_prompt = agent_config.get('system_prompt', '你是一个有用的AI助手。')
|
||||
if final_messages and final_messages[0]['role'] != 'system':
|
||||
final_messages.insert(0, {"role": "system", "content": system_prompt})
|
||||
|
||||
# 处理图片(多模态)
|
||||
if images and len(images) > 0:
|
||||
for i in range(len(final_messages) - 1, -1, -1):
|
||||
if final_messages[i]['role'] == 'user':
|
||||
original_text = final_messages[i]['content']
|
||||
multimodal_content = [{"type": "text", "text": original_text if original_text else "请描述这张图片"}]
|
||||
for img in images:
|
||||
multimodal_content.append({
|
||||
"type": "image_url",
|
||||
"image_url": {"url": img['data']}
|
||||
})
|
||||
final_messages[i]['content'] = multimodal_content
|
||||
break
|
||||
|
||||
# 第一次调用:让LLM决定是否调用工具
|
||||
url = f"{api_base.rstrip('/')}/chat/completions"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"model": model,
|
||||
"messages": final_messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens,
|
||||
"tools": tools # 传入工具定义
|
||||
}
|
||||
|
||||
logger.info(f"Function Calling调用: url={url}, model={model}, tools={len(tools)}")
|
||||
|
||||
tool_calls_record = [] # 记录工具调用
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(url, headers=headers, json=payload)
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.error(f"API返回错误: status={response.status_code}, body={response.text[:500]}")
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
if 'choices' not in data or len(data['choices']) == 0:
|
||||
raise ValueError("API响应格式错误:缺少choices")
|
||||
|
||||
message = data['choices'][0]['message']
|
||||
|
||||
# 检查是否有工具调用
|
||||
if 'tool_calls' in message and message['tool_calls']:
|
||||
logger.info(f"LLM请求调用工具: {len(message['tool_calls'])} 个")
|
||||
|
||||
# 将LLM的工具调用消息添加到历史
|
||||
final_messages.append({
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": message['tool_calls']
|
||||
})
|
||||
|
||||
# 记录工具调用
|
||||
for tc in message['tool_calls']:
|
||||
tool_calls_record.append({
|
||||
"id": tc['id'],
|
||||
"name": tc['function']['name'],
|
||||
"arguments": json.loads(tc['function']['arguments'])
|
||||
})
|
||||
|
||||
# 返回工具调用记录,由调用方执行工具
|
||||
return None, None, tool_calls_record
|
||||
|
||||
# 没有工具调用,直接返回内容
|
||||
content = message.get('content', '')
|
||||
|
||||
# 处理思考内容(如果有)
|
||||
thinking_content = None
|
||||
# 这里可以添加思考内容提取逻辑
|
||||
|
||||
return content, thinking_content, None
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(f"HTTP错误: {e.response.status_code}, {e.response.text}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Function Calling调用异常: {type(e).__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def chat_with_tool_results(
|
||||
self,
|
||||
messages: List[Dict],
|
||||
provider_config: dict,
|
||||
agent_config: dict,
|
||||
tool_results: List[Dict],
|
||||
enable_thinking: bool = True
|
||||
) -> Tuple[str, Optional[str]]:
|
||||
"""
|
||||
第二阶段调用:将工具执行结果返回给LLM
|
||||
|
||||
Args:
|
||||
messages: 对话历史(包含工具调用和结果)
|
||||
provider_config: LLM Provider配置
|
||||
agent_config: Agent配置
|
||||
tool_results: 工具执行结果 [{"tool_call_id": "xxx", "content": "..."}]
|
||||
|
||||
Returns:
|
||||
Tuple[str, Optional[str]]: (回复内容, 思考过程)
|
||||
"""
|
||||
api_base = provider_config.get('api_base')
|
||||
api_key = provider_config.get('api_key')
|
||||
model = agent_config.get('model_override') or provider_config.get('default_model', 'auto')
|
||||
max_tokens = provider_config.get('max_tokens', 4096)
|
||||
temperature = agent_config.get('temperature_override') or provider_config.get('temperature', 0.7)
|
||||
|
||||
# 将工具结果添加到消息历史
|
||||
final_messages = messages.copy()
|
||||
for result in tool_results:
|
||||
final_messages.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": result['tool_call_id'],
|
||||
"content": result['content']
|
||||
})
|
||||
|
||||
# 调用LLM生成最终回复
|
||||
url = f"{api_base.rstrip('/')}/chat/completions"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"model": model,
|
||||
"messages": final_messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens
|
||||
}
|
||||
|
||||
logger.info(f"工具结果返回LLM: url={url}, model={model}")
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(url, headers=headers, json=payload)
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.error(f"API返回错误: status={response.status_code}")
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
content = data['choices'][0]['message']['content']
|
||||
|
||||
return content, None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"工具结果调用异常: {e}")
|
||||
raise
|
||||
|
||||
|
||||
# 全局实例
|
||||
llm_service = LLMService()
|
||||
Reference in New Issue
Block a user