Skip to content

Commit 0c11473

Browse files
committed
chore: reconstruct cli
1 parent 0d1b92a commit 0c11473

26 files changed

+226
-20
lines changed

config.yaml.full

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ database:
9898

9999

100100
# [optional] for prompt optimization in cli/app
101-
agent_pilot:
101+
prompt_pilot:
102102
api_key:
103103

104104

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ dependencies = [
2121
"opentelemetry-exporter-otlp>=1.35.0",
2222
"opentelemetry-instrumentation-logging>=0.56b0",
2323
"wrapt>=1.17.2", # For patching built-in functions
24-
"typer>=0.16.0", # For command-line implementation
2524
]
2625

2726
[project.scripts]
28-
veadk = "veadk.cli.main:app"
27+
veadk = "veadk.cli.cli:veadk"
2928

3029
[project.optional-dependencies]
3130
database = [

veadk/cli/cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import click
2+
3+
from veadk.cli.cli_deploy import deploy
4+
from veadk.cli.cli_init import init
5+
from veadk.cli.cli_prompt import prompt
6+
7+
8+
@click.group()
9+
def veadk():
10+
"""Volcengine ADK command line tools"""
11+
pass
12+
13+
14+
veadk.add_command(deploy)
15+
veadk.add_command(prompt)
16+
veadk.add_command(init)
17+
18+
if __name__ == "__main__":
19+
veadk()

veadk/cli/cli_deploy.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import click
2+
3+
4+
@click.command()
5+
@click.option(
6+
"--access-key",
7+
default=None,
8+
help="Volcengine access key",
9+
)
10+
@click.option(
11+
"--secret-key",
12+
default=None,
13+
help="Volcengine secret key",
14+
)
15+
@click.option("--name", help="Expected Volcengine FaaS application name")
16+
@click.option("--path", default=".", help="Local project path")
17+
def deploy(access_key: str, secret_key: str, name: str, path: str) -> None:
18+
"""Deploy a user project to Volcengine FaaS application."""
19+
from pathlib import Path
20+
21+
from veadk.config import getenv
22+
from veadk.integrations.ve_faas.ve_faas import VeFaaS
23+
24+
if not access_key:
25+
access_key = getenv("VOLCENGINE_ACCESS_KEY")
26+
if not secret_key:
27+
secret_key = getenv("VOLCENGINE_SECRET_KEY")
28+
29+
user_proj_abs_path = Path(path).resolve()
30+
31+
ve_faas = VeFaaS(access_key=access_key, secret_key=secret_key)
32+
ve_faas.deploy(name=name, path=str(user_proj_abs_path))

veadk/cli/cli_init.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from typing import Any
2+
3+
import click
4+
5+
6+
def _set_variable_in_file(file_path: str, setting_values: dict):
7+
import ast
8+
9+
with open(file_path, "r", encoding="utf-8") as f:
10+
source_code = f.read()
11+
12+
tree = ast.parse(source_code)
13+
14+
class VariableTransformer(ast.NodeTransformer):
15+
def visit_Assign(self, node: ast.Assign):
16+
for target in node.targets:
17+
if isinstance(target, ast.Name) and target.id in setting_values:
18+
node.value = ast.Constant(value=setting_values[target.id])
19+
return node
20+
21+
transformer = VariableTransformer()
22+
new_tree = transformer.visit(tree)
23+
ast.fix_missing_locations(new_tree)
24+
new_source_code = ast.unparse(new_tree)
25+
26+
with open(file_path, "w", encoding="utf-8") as f:
27+
f.write(new_source_code)
28+
29+
click.echo("Your project has beed created.")
30+
31+
32+
def _render_prompts() -> dict[str, Any]:
33+
vefaas_application_name = click.prompt(
34+
"Volcengine FaaS application name", default="veadk-cloud-agent"
35+
)
36+
37+
gateway_name = click.prompt(
38+
"Volcengine gateway instance name", default="", show_default=True
39+
)
40+
41+
gateway_service_name = click.prompt(
42+
"Volcengine gateway service name", default="", show_default=True
43+
)
44+
45+
gateway_upstream_name = click.prompt(
46+
"Volcengine gateway upstream name", default="", show_default=True
47+
)
48+
49+
deploy_mode_options = {
50+
"1": "A2A/MCP Server",
51+
"2": "VeADK Studio",
52+
"3": "VeADK Web / Google ADK Web",
53+
}
54+
55+
click.echo("Choose a deploy mode:")
56+
for key, value in deploy_mode_options.items():
57+
click.echo(f" {key}. {value}")
58+
59+
deploy_mode = click.prompt(
60+
"Enter your choice", type=click.Choice(deploy_mode_options.keys())
61+
)
62+
63+
return {
64+
"VEFAAS_APPLICATION_NAME": vefaas_application_name,
65+
"GATEWAY_NAME": gateway_name,
66+
"GATEWAY_SERVICE_NAME": gateway_service_name,
67+
"GATEWAY_UPSTREAM_NAME": gateway_upstream_name,
68+
"USE_STUDIO": deploy_mode == deploy_mode_options["2"],
69+
"USE_ADK_WEB": deploy_mode == deploy_mode_options["3"],
70+
}
71+
72+
73+
@click.command()
74+
def init() -> None:
75+
"""Init a veadk project that can be deployed to Volcengine VeFaaS."""
76+
import shutil
77+
from pathlib import Path
78+
79+
import veadk.integrations.ve_faas as vefaas
80+
81+
cwd = Path.cwd()
82+
local_dir_name = click.prompt("Directory name", default="veadk-cloud-proj")
83+
target_dir_path = cwd / local_dir_name
84+
85+
if target_dir_path.exists():
86+
click.confirm(
87+
f"Directory '{target_dir_path}' already exists, do you want to overwrite it",
88+
abort=True,
89+
)
90+
shutil.rmtree(target_dir_path)
91+
92+
setting_values = _render_prompts()
93+
94+
template_dir_path = Path(vefaas.__file__).parent / "template"
95+
shutil.copytree(template_dir_path, target_dir_path)
96+
_set_variable_in_file(target_dir_path / "deploy.py", setting_values)

veadk/cli/cli_prompt.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import click
2+
3+
4+
@click.command()
5+
@click.option(
6+
"--path", default=".", help="Agent file path with global variable `agent=...`"
7+
)
8+
@click.option("--feedback", default=None, help="Suggestions for prompt optimization")
9+
@click.option("--api-key", default=None, help="API Key of PromptPilot")
10+
@click.option(
11+
"--model-name",
12+
default="doubao-1.5-pro-32k-250115",
13+
help="Model name for prompt optimization",
14+
)
15+
def prompt(path: str, feedback: str, api_key: str, model_name: str) -> None:
16+
"""Optimize agent system prompt from a local file."""
17+
from pathlib import Path
18+
19+
from veadk.agent import Agent
20+
from veadk.config import getenv
21+
from veadk.integrations.ve_prompt_pilot.ve_prompt_pilot import VePromptPilot
22+
from veadk.utils.misc import load_module_from_file
23+
24+
module_name = "agents_for_prompt_pilot"
25+
module_abs_path = Path(path).resolve()
26+
27+
module = load_module_from_file(
28+
module_name=module_name, file_path=str(module_abs_path)
29+
)
30+
31+
# get all global variables from module
32+
globals_in_module = vars(module)
33+
34+
agents = []
35+
for global_variable_name, global_variable_value in globals_in_module.items():
36+
if isinstance(global_variable_value, Agent):
37+
agent = global_variable_value
38+
agents.append(agent)
39+
40+
if len(agents) > 0:
41+
click.echo(f"Found {len(agents)} agents in {module_abs_path}")
42+
43+
if not api_key:
44+
api_key = getenv("PROMPT_PILOT_API_KEY")
45+
ve_prompt_pilot = VePromptPilot(api_key)
46+
ve_prompt_pilot.optimize(
47+
agents=agents, feedback=feedback, model_name=model_name
48+
)
49+
else:
50+
click.echo(f"No agents found in {module_abs_path}")

veadk/cli/cli_web.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def web():
2+
pass
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)