doc:修复第七章 tiny-agent
This commit is contained in:
@@ -51,14 +51,15 @@ class Agent:
|
|||||||
stream=False,
|
stream=False,
|
||||||
)
|
)
|
||||||
if response.choices[0].message.tool_calls:
|
if response.choices[0].message.tool_calls:
|
||||||
|
self.messages.append({"role": "assistant", "content": response.choices[0].message.content})
|
||||||
# 处理工具调用
|
# 处理工具调用
|
||||||
tool_list = []
|
tool_list = []
|
||||||
for tool_call in response.choices[0].message.tool_calls:
|
for tool_call in response.choices[0].message.tool_calls:
|
||||||
# 处理工具调用并将结果添加到消息列表中
|
# 处理工具调用并将结果添加到消息列表中
|
||||||
self.messages.append(self.handle_tool_call(tool_call))
|
self.messages.append(self.handle_tool_call(tool_call))
|
||||||
tool_list.append(tool_call.function.name)
|
tool_list.append([tool_call.function.name, tool_call.function.arguments])
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print("调用工具:", tool_list)
|
print("调用工具:", response.choices[0].message.content, tool_list)
|
||||||
# 再次获取模型的完成响应,这次包含工具调用的结果
|
# 再次获取模型的完成响应,这次包含工具调用的结果
|
||||||
response = self.client.chat.completions.create(
|
response = self.client.chat.completions.create(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ def add(a: float, b: float):
|
|||||||
:param b: 第二个浮点数。
|
:param b: 第二个浮点数。
|
||||||
:return: 两个浮点数的和。
|
:return: 两个浮点数的和。
|
||||||
"""
|
"""
|
||||||
return a + b
|
return str(a + b)
|
||||||
|
|
||||||
def mul(a: float, b: float):
|
def mul(a: float, b: float):
|
||||||
"""
|
"""
|
||||||
@@ -26,7 +26,7 @@ def mul(a: float, b: float):
|
|||||||
:param b: 第二个浮点数。
|
:param b: 第二个浮点数。
|
||||||
:return: 两个浮点数的积。
|
:return: 两个浮点数的积。
|
||||||
"""
|
"""
|
||||||
return a * b
|
return str(a * b)
|
||||||
|
|
||||||
def compare(a: float, b: float):
|
def compare(a: float, b: float):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -488,7 +488,7 @@ def add(a: float, b: float):
|
|||||||
:param b: 第二个浮点数。
|
:param b: 第二个浮点数。
|
||||||
:return: 两个浮点数的和。
|
:return: 两个浮点数的和。
|
||||||
"""
|
"""
|
||||||
return a + b
|
return str(a + b)
|
||||||
|
|
||||||
def compare(a: float, b: float):
|
def compare(a: float, b: float):
|
||||||
"""
|
"""
|
||||||
@@ -511,7 +511,7 @@ def count_letter_in_string(a: str, b: str):
|
|||||||
:param b: 要统计的字母。
|
:param b: 要统计的字母。
|
||||||
:return: 字母在字符串中出现的次数。
|
:return: 字母在字符串中出现的次数。
|
||||||
"""
|
"""
|
||||||
return a.count(b)
|
return str(a.count(b))
|
||||||
|
|
||||||
# ... (可能还有其他工具函数)
|
# ... (可能还有其他工具函数)
|
||||||
```
|
```
|
||||||
@@ -560,7 +560,7 @@ SYSREM_PROMPT = """
|
|||||||
class Agent:
|
class Agent:
|
||||||
def __init__(self, client: OpenAI, model: str = "Qwen/Qwen2.5-32B-Instruct", tools: List=[], verbose : bool = True):
|
def __init__(self, client: OpenAI, model: str = "Qwen/Qwen2.5-32B-Instruct", tools: List=[], verbose : bool = True):
|
||||||
self.client = client
|
self.client = client
|
||||||
self.tools = tools # 存储可用的工具函数列表
|
self.tools = tools
|
||||||
self.model = model
|
self.model = model
|
||||||
self.messages = [
|
self.messages = [
|
||||||
{"role": "system", "content": SYSREM_PROMPT},
|
{"role": "system", "content": SYSREM_PROMPT},
|
||||||
@@ -568,20 +568,17 @@ class Agent:
|
|||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
|
||||||
def get_tool_schema(self) -> List[Dict[str, Any]]:
|
def get_tool_schema(self) -> List[Dict[str, Any]]:
|
||||||
# 使用 utils.function_to_json 获取所有工具的 JSON Schema
|
# 获取所有工具的 JSON 模式
|
||||||
return [function_to_json(tool) for tool in self.tools]
|
return [function_to_json(tool) for tool in self.tools]
|
||||||
|
|
||||||
def handle_tool_call(self, tool_call):
|
def handle_tool_call(self, tool_call):
|
||||||
# 处理来自模型的工具调用请求
|
# 处理工具调用
|
||||||
function_name = tool_call.function.name
|
function_name = tool_call.function.name
|
||||||
function_args = tool_call.function.arguments
|
function_args = tool_call.function.arguments
|
||||||
function_id = tool_call.id
|
function_id = tool_call.id
|
||||||
|
|
||||||
# 动态执行工具函数
|
|
||||||
# 注意:实际应用中应添加更严格的安全检查
|
|
||||||
function_call_content = eval(f"{function_name}(**{function_args})")
|
function_call_content = eval(f"{function_name}(**{function_args})")
|
||||||
|
|
||||||
# 返回工具执行结果给模型
|
|
||||||
return {
|
return {
|
||||||
"role": "tool",
|
"role": "tool",
|
||||||
"content": function_call_content,
|
"content": function_call_content,
|
||||||
@@ -589,37 +586,37 @@ class Agent:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def get_completion(self, prompt) -> str:
|
def get_completion(self, prompt) -> str:
|
||||||
# 主对话逻辑
|
|
||||||
self.messages.append({"role": "user", "content": prompt})
|
self.messages.append({"role": "user", "content": prompt})
|
||||||
|
|
||||||
# 第一次调用模型,传入工具 Schema
|
# 获取模型的完成响应
|
||||||
response = self.client.chat.completions.create(
|
response = self.client.chat.completions.create(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=self.messages,
|
messages=self.messages,
|
||||||
tools=self.get_tool_schema(),
|
tools=self.get_tool_schema(),
|
||||||
stream=False,
|
stream=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 检查模型是否请求调用工具
|
# 检查模型是否调用了工具
|
||||||
if response.choices[0].message.tool_calls:
|
if response.choices[0].message.tool_calls:
|
||||||
|
self.messages.append({"role": "assistant", "content": response.choices[0].message.content})
|
||||||
|
# 处理工具调用
|
||||||
tool_list = []
|
tool_list = []
|
||||||
# 处理所有工具调用请求
|
|
||||||
for tool_call in response.choices[0].message.tool_calls:
|
for tool_call in response.choices[0].message.tool_calls:
|
||||||
# 执行工具并将结果添加到消息历史中
|
# 处理工具调用并将结果添加到消息列表中
|
||||||
self.messages.append(self.handle_tool_call(tool_call))
|
self.messages.append(self.handle_tool_call(tool_call))
|
||||||
tool_list.append(tool_call.function.name)
|
tool_list.append([tool_call.function.name, tool_call.function.arguments])
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print("调用工具:", tool_list)
|
print("调用工具:", response.choices[0].message.content, tool_list)
|
||||||
|
# 再次获取模型的完成响应,这次包含工具调用的结果
|
||||||
# 第二次调用模型,传入工具执行结果
|
|
||||||
response = self.client.chat.completions.create(
|
response = self.client.chat.completions.create(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=self.messages,
|
messages=self.messages,
|
||||||
tools=self.get_tool_schema(), # 再次传入 Schema 可能有助于模型理解上下文
|
tools=self.get_tool_schema(),
|
||||||
stream=False,
|
stream=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 将最终的助手回复添加到消息历史
|
# 将模型的完成响应添加到消息列表中
|
||||||
self.messages.append({"role": "assistant", "content": response.choices[0].message.content})
|
self.messages.append({"role": "assistant", "content": response.choices[0].message.content})
|
||||||
return response.choices[0].message.content
|
return response.choices[0].message.content
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user