Skip to content

Commit 7a695fc

Browse files
authored
feat: add stock_analysis & fix requirements in content_safety (#98)
2 parents 0a7477f + 0be24d9 commit 7a695fc

File tree

11 files changed

+367
-6
lines changed

11 files changed

+367
-6
lines changed

02-use-cases/beginner/callback/agent.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import asyncio
2-
31
from agentkit.apps import AgentkitAgentServerApp
42
from veadk import Agent, Runner
53
from veadk.memory import ShortTermMemory
@@ -50,5 +48,5 @@ async def main():
5048

5149

5250
if __name__ == "__main__":
53-
asyncio.run(main())
54-
# agent_server_app.run(host="0.0.0.0", port=8000)
51+
# asyncio.run(main())
52+
agent_server_app.run(host="0.0.0.0", port=8000)

02-use-cases/content_safety/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
8-
"agentkit-sdk-python==0.2.2",
8+
"agentkit-sdk-python>=0.2.0",
99
"google-adk==1.19.0",
1010
"veadk-python==0.5.0",
1111
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
veadk-python==0.5.0
22
google-adk==1.19.0
3-
agentkit-sdk-python==0.2.2
3+
agentkit-sdk-python>=0.2.0
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# 股票分析智能助手 - Stock Analysis
2+
3+
## 概述
4+
5+
这是一个基于火山引擎AgentKit构建的具备股票分析功能的智能助手。
6+
7+
## 核心功能
8+
9+
- 功能 1:股票数据分析
10+
- 功能 2:股票走势预测
11+
- 功能 3:股票投资建议
12+
13+
## Agent 能力
14+
15+
```text
16+
用户消息
17+
18+
AgentKit 运行时
19+
20+
Stock Analysis Agent
21+
├── 网页信息工具 (web_search)
22+
├── 代码执行工具 (run_code)
23+
```
24+
25+
## 目录结构说明
26+
27+
```bash
28+
stock_analysis/
29+
├── agent.py # Agent
30+
├── client.py # 测试客户端(SSE 流式调用)
31+
├── requirements.txt # Python 依赖列表 (agentkit部署时需要指定依赖文件)
32+
├── pyproject.toml # 项目配置(uv 依赖管理)
33+
├── agentkit.yaml # AgentKit 部署配置(运行agentkit config之后会自动生成)
34+
├── Dockerfile # Docker 镜像构建文件(运行agentkit config之后会自动生成)
35+
└── README.md # 项目说明文档
36+
```
37+
38+
## 本地运行
39+
40+
### 前置准备
41+
42+
**1. 开通火山方舟模型服务:**
43+
44+
- 访问 [火山方舟控制台](https://exp.volcengine.com/ark?mode=chat)
45+
- 开通模型服务
46+
47+
**2. 获取火山引擎访问凭证:**
48+
49+
- 参考 [用户指南](https://www.volcengine.com/docs/6291/65568?lang=zh) 获取 AK/SK
50+
51+
**3. 开通`web_search`工具权限:**
52+
53+
- 使用[`web_search`工具](https://www.volcengine.com/docs/85508/1650263),需提前开通并创建联网问答Agent[相应权限](https://www.volcengine.com/docs/85508/1544858)
54+
55+
**4. 创建 AgentKit 工具:**
56+
57+
- 工具类型选择:预置工具 -> AIO Sandbox
58+
59+
### 依赖安装
60+
61+
#### 1. 安装 uv 包管理器
62+
63+
```bash
64+
# macOS / Linux(官方安装脚本)
65+
curl -LsSf https://astral.sh/uv/install.sh | sh
66+
67+
# 或使用 Homebrew(macOS)
68+
brew install uv
69+
```
70+
71+
#### 2. 初始化项目依赖
72+
73+
```bash
74+
# 进入项目目录
75+
cd 02-use-cases/stock_analysis
76+
```
77+
78+
您可以通过 `pip` 工具来安装本项目依赖:
79+
80+
```bash
81+
pip install -r requirements.txt
82+
```
83+
84+
或者使用 `uv` 工具来安装本项目依赖:
85+
86+
```bash
87+
# 如果没有 `uv` 虚拟环境,可以使用命令先创建一个虚拟环境
88+
uv venv --python 3.12
89+
90+
# 使用 `pyproject.toml` 管理依赖
91+
uv sync --index-url https://pypi.tuna.tsinghua.edu.cn/simple
92+
93+
# 使用 `requirements.txt` 管理依赖
94+
uv pip install -r requirements.txt
95+
96+
# 激活虚拟环境
97+
source .venv/bin/activate
98+
```
99+
100+
### 环境准备
101+
102+
```bash
103+
# 火山方舟模型名称
104+
export MODEL_AGENT_NAME=doubao-seed-1-6-251015
105+
106+
# 火山引擎访问凭证(必需)
107+
export VOLCENGINE_ACCESS_KEY=<Your Access Key>
108+
export VOLCENGINE_SECRET_KEY=<Your Secret Key>
109+
110+
export AGENTKIT_TOOL_ID=<Your Tool ID>
111+
```
112+
113+
### 调试方法
114+
115+
#### 单线程运行:使用 VeADK Web 调试界面,调试 agent.py
116+
117+
```bash
118+
# 进入 02-use-cases 目录
119+
cd agentkit-samples/02-use-cases
120+
121+
# 启动 VeADK Web 界面
122+
veadk web --port 8080
123+
124+
# 在浏览器访问:http://127.0.0.1:8080
125+
```
126+
127+
Web 界面提供图形化对话测试环境,支持实时查看消息流和调试信息。
128+
129+
此外,还可以使用命令行测试,调试 agent.py。
130+
131+
```bash
132+
cd agentkit-samples/02-use-cases/stock_analysis
133+
134+
# 启动 Agent 服务
135+
uv run agent.py
136+
# 服务将监听 http://0.0.0.0:8000
137+
138+
# 新开终端,运行测试客户端
139+
# 需要编辑 client.py,将其中的第 14 行和第 15 行的 base_url 和 api_key 修改为 agentkit.yaml 中生成的 runtime_endpoint 和 runtime_apikey 字段
140+
uv run client.py
141+
```
142+
143+
## AgentKit 部署
144+
145+
### 前置准备
146+
147+
**重要提示**:在运行本示例之前,请先访问 [AgentKit 控制台授权页面](https://console.volcengine.com/agentkit/region:agentkit+cn-beijing/auth?projectName=default) 对所有依赖服务进行授权,确保案例能够正常执行。
148+
149+
**1. 开通火山方舟模型服务:**
150+
151+
- 访问 [火山方舟控制台](https://exp.volcengine.com/ark?mode=chat)
152+
- 开通模型服务
153+
154+
**2. 获取火山引擎访问凭证:**
155+
156+
- 参考 [用户指南](https://www.volcengine.com/docs/6291/65568?lang=zh) 获取 AK/SK
157+
158+
**3. 开通`web_search`工具权限:**
159+
160+
- 使用[`web_search`工具](https://www.volcengine.com/docs/85508/1650263),需提前开通并创建联网问答Agent[相应权限](https://www.volcengine.com/docs/85508/1544858)
161+
162+
**4. 创建 AgentKit 工具:**
163+
164+
- 工具类型选择:预置工具 -> AIO Sandbox
165+
166+
**5. 设置环境变量:**
167+
168+
```bash
169+
# 火山引擎访问凭证(必需)
170+
export VOLCENGINE_ACCESS_KEY=<Your Access Key>
171+
export VOLCENGINE_SECRET_KEY=<Your Secret Key>
172+
```
173+
174+
### AgentKit 云上部署
175+
176+
```bash
177+
cd agentkit-samples/02-use-cases/stock_analysis
178+
179+
# 配置部署参数
180+
# optional:如果 agentkit config 中不添加 --runtime_envs AGENTKIT_TOOL_ID={{your_tool_id}},可以在 AgentKit 控制台 智能体运行时 中,关键组件,选择 沙箱工具,并发布
181+
agentkit config \
182+
--agent_name stock_analysis \
183+
--entry_point 'agent.py' \
184+
--runtime_envs AGENTKIT_TOOL_ID={{your_tool_id}} \
185+
--launch_type cloud
186+
187+
# 启动云端服务
188+
agentkit launch
189+
190+
# 测试部署的 Agent
191+
agentkit invoke '帮我搜一下宁德时代近期的股票走势, 并给我一个简单的投资建议。'
192+
193+
# 或使用 client.py 连接云端服务
194+
# 需要编辑 client.py,将其中的第 14 行和第 15 行的 base_url 和 api_key 修改为 agentkit.yaml 中生成的 runtime_endpoint 和 runtime_apikey 字段
195+
uv run client.py
196+
```
197+
198+
## 示例提示词
199+
200+
以下是一些常用的提示词示例:
201+
202+
- "帮我搜一下比亚迪近期股票数据,分析一下走势"
203+
- "帮我搜一下宁德时代近期的股票走势, 并给我一个简单的投资建议"
204+
205+
## 效果展示
206+
207+
| 示例提示词 1 | 示例提示词 2 |
208+
| -------- | ------- |
209+
| 帮我搜一下比亚迪近期股票数据,分析一下走势。 | 帮我搜一下宁德时代近期的股票走势, 并给我一个简单的投资建议 |
210+
| ![示例提示词 1 截图](assets/images/prompt1.jpeg) | ![示例提示词 2 截图](assets/images/prompt2.jpeg) |
211+
212+
## 常见问题
213+
214+
无。
215+
216+
## 参考资料
217+
218+
- [VeADK 官方文档](https://volcengine.github.io/veadk-python/)
219+
- [AgentKit 开发指南](https://volcengine.github.io/agentkit-sdk-python/)
220+
- [火山方舟模型服务](https://console.volcengine.com/ark/region:ark+cn-beijing/overview?briefPage=0&briefType=introduce&type=new&projectName=default)
221+
222+
## 代码许可
223+
224+
本工程遵循 Apache 2.0 License
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from veadk import Agent
16+
from veadk.memory.short_term_memory import ShortTermMemory
17+
from veadk.tools.builtin_tools.run_code import run_code
18+
from veadk.tools.builtin_tools.web_search import web_search
19+
from agentkit.apps import AgentkitAgentServerApp
20+
21+
short_term_memory = ShortTermMemory(backend="local")
22+
23+
agent = Agent(
24+
name="data_analysis_agent",
25+
description="A data analysis for stock marketing",
26+
instruction="""
27+
You are a data analysis agent for stock marketing.
28+
Talk with user friendly. You can invoke your tools to finish user's task or question.
29+
Load memory first. In case you already have answer from memory, you can use it directly.
30+
Download the stock data thru sandbox if it is not available in memory.
31+
* If trading data is not found, download the stock trading data using run_code. You can use the Python library akshare to download relevant stock data.
32+
* After downloading, execute code through run_code to avoid installation checks each time.
33+
* You can use the web_search tool to search for relevant company operational data.
34+
* If dependency libraries are missing, install them for the sandbox using Python code.
35+
""",
36+
tools=[run_code, web_search],
37+
)
38+
39+
root_agent = agent
40+
41+
agent_server_app = AgentkitAgentServerApp(
42+
agent=agent,
43+
short_term_memory=short_term_memory,
44+
)
45+
46+
if __name__ == "__main__":
47+
agent_server_app.run(host="0.0.0.0", port=8000)
645 KB
Loading
868 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import requests
2+
import httpx
3+
import random
4+
from google.adk.cli.adk_web_server import CreateSessionRequest, RunAgentRequest
5+
from google.genai.types import Content, Part
6+
import asyncio
7+
8+
9+
if __name__ == "__main__":
10+
# Step 0: setup running configs
11+
app_name = "content_safety_app"
12+
user_id = "agentkit_user"
13+
session_id = "agentkit_sample_session"
14+
base_url = "http://127.0.0.1:8000"
15+
api_key = "test key"
16+
17+
task_num = 1
18+
19+
# Step 1: create a session
20+
def create_session():
21+
create_session_request = CreateSessionRequest(
22+
session_id=session_id + f"_{random.randint(1, 9999)}",
23+
)
24+
25+
response = requests.post(
26+
url=f"{base_url}/apps/{app_name}/users/{user_id}/sessions/{create_session_request.session_id}",
27+
headers={"Authorization": f"Bearer {api_key}"},
28+
)
29+
30+
print(f"[create session] Response from server: {response.json()}")
31+
32+
return create_session_request.session_id
33+
34+
# Step 2: run agent with SSE
35+
run_agent_request = RunAgentRequest(
36+
app_name=app_name,
37+
user_id=user_id,
38+
session_id=create_session(),
39+
new_message=Content(
40+
parts=[
41+
Part(text="帮我搜一下宁德时代近期的股票走势, 并给我一个简单的投资建议")
42+
],
43+
role="user",
44+
),
45+
stream=True,
46+
)
47+
48+
print("[run agent] Event from server:")
49+
50+
# 3. Handle streaming events
51+
async def send_request(message: str):
52+
run_agent_request = RunAgentRequest(
53+
app_name=app_name,
54+
user_id=user_id,
55+
session_id=create_session(),
56+
new_message=Content(parts=[Part(text=message)], role="user"),
57+
stream=True,
58+
)
59+
60+
with httpx.stream(
61+
"POST",
62+
f"{base_url}/run_sse",
63+
json=run_agent_request.model_dump(exclude_none=True),
64+
timeout=120,
65+
headers={"Authorization": f"Bearer {api_key}"},
66+
) as r:
67+
for line in r.iter_lines():
68+
print(line)
69+
70+
async def send_request_parallel():
71+
await send_request("帮我搜一下宁德时代近期的股票走势, 并给我一个简单的投资建议")
72+
# tasks = [
73+
# send_request("")
74+
# for _ in range(task_num)
75+
# ]
76+
# await asyncio.gather(*tasks)
77+
78+
asyncio.run(send_request_parallel())

02-use-cases/stock_analysis/config.yaml.example

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[project]
2+
name = "stock-analysis"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"agentkit-sdk-python>=0.2.0",
9+
"google-adk==1.19.0",
10+
"veadk-python==0.5.0",
11+
]

0 commit comments

Comments
 (0)