Files
agentUniverse/docs/guidebook/zh/实践应用/Python自动执行案例.md
2025-07-20 17:14:13 +08:00

76 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Python自动执行案例
## 案例说明
本案例基于ReactAgentTemplate搭建了一个简单可以自动生成python代码并执行的案例。
该案例基于千问大模型和`google_search`功能,使用前需要您在环境变量中配置`DASHSCOPE_API_KEY``SERPER_API_KEY`
## 快速开始
### 配置API密钥
比如在agentUniverse管理私有密钥配置的文件`custom_key.toml`中配置密钥信息讨论组默认使用qwen作为基座模型serper作为google search工具
```toml
[KEY_LIST]
# serper google search key
SERPER_API_KEY='xxx'
# openai api key
DASHSCOPE_API_KEY='xxx'
```
### 创建Agent
```yaml
info:
name: 'demo_react_agent'
description: 'react agent'
profile:
prompt_version: qwen_react_agent.cn
llm_model:
name: 'qwen_llm'
model_name: 'qwen-max'
stop: 'Observation'
temperature: 0.1
action:
tool:
- 'google_search_tool'
- 'python_runner'
knowledge:
- 'law_knowledge'
memory:
name: 'demo_memory'
metadata:
type: 'AGENT'
module: 'agentuniverse.agent.template.react_agent_template'
class: 'ReActAgentTemplate'
```
这里使用了google_search_tool与python_runner两个工具相关工具代码链接如下
- [google_search_tool](../../../../examples/sample_apps/react_agent_app/intelligence/agentic/tool/google_search_tool.yaml)
- [python_runner](../../../../examples/sample_apps/react_agent_app/intelligence/agentic/tool/python_runner.yaml)
### 代码运行
1. 测试用例运行
直接使用测试代码运行[test_case](../../../../examples/sample_apps/react_agent_app/intelligence/test/test_react_agent.py)
2. 接口运行
配置好相关key之后启动web服务使用下面的curl进行测试
```shell
curl --location --request POST 'http://localhost:8888/service_run' \
--header 'Cookie: spanner=fQ47DxJmWYzf8rKDhs69LExySZYZFUiVXt2T4qEYgj0' \
--header 'Content-Type: application/json' \
--data-raw '{
"service_id": "demo_react_service",
"params": {
"input": "请帮我生成一段python代码可以计算三数之和"
}
}'
```
### 执行结果
![test_case](../../_picture/react_demo_step.png)
图片中react一共执行了三步
第一步模型根据问题给出了一段python代码并交给python_runner工具执行但是执行失败了失败原因是没有使用print打印执行结果。
第二步模型意识到错误主动修改了代码再次使用python_runner工具执行执行成功。
第三步,模型将执行成功的代码告诉给用户。
### 注意
受限于模型能力问题建议使用qwen-max模型进行测试