|
|
@@ -1275,6 +1275,70 @@ print(f"数学专用Agent结果: {math_result}")
|
|
|
<img src="https://raw.githubusercontent.com/datawhalechina/Hello-Agents/main/docs/images/7-figures/table-02.png" alt="" width="90%"/>
|
|
|
</div>
|
|
|
|
|
|
+### 7.4.5 FunctionCallAgent
|
|
|
+
|
|
|
+FunctionCallAgent是hello-agents在0.1.16之后引入的Agent,它基于OpenAI原生函数调用机制的Agent,展示了如何使用OpenAI的函数调用机制来构建Agent。
|
|
|
+它支持以下功能:
|
|
|
+_build_tool_schemas:通过工具的description构建OpenAI的function calling schema
|
|
|
+_extract_message_content:从OpenAI的响应中提取文本
|
|
|
+_parse_function_call_arguments:解析模型返回的JSON字符串参数
|
|
|
+_convert_parameter_types:转换参数类型
|
|
|
+
|
|
|
+这些功能可以使其具备原生的OpenAI Functioncall的能力,对比使用prompt约束的方式,具备更强的鲁棒性。
|
|
|
+```python
|
|
|
+def _invoke_with_tools(self, messages: list[dict[str, Any]], tools: list[dict[str, Any]], tool_choice: Union[str, dict], **kwargs):
|
|
|
+ """调用底层OpenAI客户端执行函数调用"""
|
|
|
+ client = getattr(self.llm, "_client", None)
|
|
|
+ if client is None:
|
|
|
+ raise RuntimeError("HelloAgentsLLM 未正确初始化客户端,无法执行函数调用。")
|
|
|
+
|
|
|
+ client_kwargs = dict(kwargs)
|
|
|
+ client_kwargs.setdefault("temperature", self.llm.temperature)
|
|
|
+ if self.llm.max_tokens is not None:
|
|
|
+ client_kwargs.setdefault("max_tokens", self.llm.max_tokens)
|
|
|
+
|
|
|
+ return client.chat.completions.create(
|
|
|
+ model=self.llm.model,
|
|
|
+ messages=messages,
|
|
|
+ tools=tools,
|
|
|
+ tool_choice=tool_choice,
|
|
|
+ **client_kwargs,
|
|
|
+ )
|
|
|
+#内部逻辑是对Openai 原生的functioncall作再封装
|
|
|
+from openai import OpenAI
|
|
|
+client = OpenAI()
|
|
|
+
|
|
|
+tools = [
|
|
|
+ {
|
|
|
+ "type": "function",
|
|
|
+ "function": {
|
|
|
+ "name": "get_current_weather",
|
|
|
+ "description": "Get the current weather in a given location",
|
|
|
+ "parameters": {
|
|
|
+ "type": "object",
|
|
|
+ "properties": {
|
|
|
+ "location": {
|
|
|
+ "type": "string",
|
|
|
+ "description": "The city and state, e.g. San Francisco, CA",
|
|
|
+ },
|
|
|
+ "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
|
|
|
+ },
|
|
|
+ "required": ["location"],
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+]
|
|
|
+messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
|
|
|
+completion = client.chat.completions.create(
|
|
|
+ model="gpt-5",
|
|
|
+ messages=messages,
|
|
|
+ tools=tools,
|
|
|
+ tool_choice="auto"
|
|
|
+)
|
|
|
+
|
|
|
+print(completion)
|
|
|
+```
|
|
|
+
|
|
|
## 7.5 工具系统
|
|
|
|
|
|
本节内容将在前面构建的Agent基础架构上,深入探讨工具系统的设计与实现。我们将从基础设施建设开始,逐步深入到自定义开发设计。本节的学习目标围绕以下三个核心方面展开:
|