Skip to content

Commit c824486

Browse files
committed
feat: add cli kb
1 parent 425104f commit c824486

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

docs/content/90.cli/1.overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ VeADK 提供如下命令便捷您的操作:
1414
| `veadk prompt` | 优化智能体系统提示词 | 借助火山引擎 PromptPilot 产品 |
1515
| `veadk web` | 支持长短期记忆、知识库的前端调试界面 | 兼容 Google ADK web |
1616
| `veadk eval` | 支持不同后端的评测 | 评测后端包括 `adk``deepeval`,评测数据集源包括 Google ADK 评测集格式文件,以及 Tracing 文件 |
17+
| `veadk kb` | 知识库相关操作 | 向知识库添加本地文件或目录 |

docs/content/90.cli/2.commands.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,45 @@ veadk web --session_service_uri="mysql+pymysql://{user}:{password}@{host}/{datab
8484
火山引擎 Secret Key
8585
::
8686
::
87+
88+
## 知识库操作
89+
90+
您可以通过 `veadk kb add` 命令来向您的知识库添加本地文件或者目录。例如,准备如下目录:
91+
92+
::code-tree{default-value="knowledges/id.txt"}
93+
94+
```txt [knowledges/id.txt]
95+
My id is 20250101.
96+
```
97+
98+
::
99+
100+
之后,将 `knowledges/id.txt` 中的内容存入到 OpenSearch 知识库中:
101+
102+
```bash
103+
# 知识库的相关配置将会从环境变量中读取
104+
veadk kb add --backend opensearch --app_name=cmd_app --path=./knowledges
105+
```
106+
107+
可使用如下代码测试:
108+
109+
```python
110+
import asyncio
111+
112+
from veadk import Agent, Runner
113+
from veadk.knowledgebase import KnowledgeBase
114+
115+
app_name = "cmd_app"
116+
117+
knowledgebase = KnowledgeBase(backend="opensearch", app_name=app_name)
118+
119+
agent = Agent(knowledgebase=knowledgebase)
120+
121+
runner = Runner(agent=agent, app_name=app_name)
122+
123+
response = asyncio.run(
124+
runner.run(messages="Please help me to fetch my ID from my knowledgebase")
125+
)
126+
127+
print(response) # Your ID is 20250101.
128+
```

veadk/cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from veadk.cli.cli_deploy import deploy
1919
from veadk.cli.cli_eval import eval
2020
from veadk.cli.cli_init import init
21+
from veadk.cli.cli_kb import kb
2122
from veadk.cli.cli_pipeline import pipeline
2223
from veadk.cli.cli_prompt import prompt
2324
from veadk.cli.cli_web import web
@@ -39,6 +40,7 @@ def veadk():
3940
veadk.add_command(web)
4041
veadk.add_command(pipeline)
4142
veadk.add_command(eval)
43+
veadk.add_command(kb)
4244

4345
if __name__ == "__main__":
4446
veadk()

veadk/cli/cli_kb.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 pathlib import Path
16+
from typing import Literal
17+
18+
import click
19+
20+
21+
@click.command()
22+
@click.option(
23+
"--backend",
24+
type=click.Choice(
25+
["local", "opensearch", "viking", "redis"],
26+
case_sensitive=False,
27+
),
28+
required=True,
29+
)
30+
@click.option(
31+
"--app_name",
32+
default="",
33+
help="`app_name` for init your knowledgebase",
34+
)
35+
@click.option(
36+
"--index",
37+
default="",
38+
help="Knowledgebase index",
39+
)
40+
@click.option(
41+
"--path",
42+
required=True,
43+
help="Knowledge file or directory path",
44+
)
45+
def add(
46+
backend: Literal["local", "opensearch", "viking", "redis"],
47+
app_name: str,
48+
index: str,
49+
path: str,
50+
):
51+
"""Add files to knowledgebase"""
52+
_path = Path(path)
53+
assert _path.exists(), f"Path {path} not exists. Please check your input."
54+
55+
from veadk.knowledgebase import KnowledgeBase
56+
57+
knowledgebase = KnowledgeBase(backend=backend, app_name=app_name, index=index)
58+
59+
if _path.is_file():
60+
knowledgebase.add_from_files(files=[path])
61+
elif _path.is_dir():
62+
knowledgebase.add_from_directory(directory=path)
63+
else:
64+
raise RuntimeError(
65+
"Unsupported knowledgebase file type, only support a single file and a directory."
66+
)
67+
68+
69+
@click.group()
70+
def kb():
71+
"""VeADK Knowledgebase management"""
72+
pass
73+
74+
75+
kb.add_command(add)

0 commit comments

Comments
 (0)