Skip to content

Commit cfea021

Browse files
committed
feat(agent_example): create prompt example to instruction
1 parent a47a11f commit cfea021

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

veadk/agent.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from veadk.utils.patches import patch_asyncio, patch_tracer
5757
from veadk.utils.misc import check_litellm_version
5858
from veadk.version import VERSION
59+
from veadk.prompts.prompt_example import AgentExample, format_examples
5960

6061
patch_tracer()
6162
patch_asyncio()
@@ -140,6 +141,8 @@ class Agent(LlmAgent):
140141

141142
enable_authz: bool = False
142143

144+
examples: list[AgentExample] = Field(default_factory=list)
145+
143146
def model_post_init(self, __context: Any) -> None:
144147
super().model_post_init(None) # for sub_agents init
145148

@@ -255,6 +258,15 @@ def model_post_init(self, __context: Any) -> None:
255258
else:
256259
self.before_agent_callback = check_agent_authorization
257260

261+
if self.examples:
262+
base = (
263+
self.instruction
264+
if isinstance(self.instruction, str)
265+
else DEFAULT_INSTRUCTION
266+
)
267+
self.instruction = base + format_examples(self.examples)
268+
print(self.instruction)
269+
258270
if self.prompt_manager:
259271
self.instruction = self.prompt_manager.get_prompt
260272

veadk/prompts/prompt_example.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 dataclasses import dataclass
16+
from typing import Any, Union
17+
18+
19+
@dataclass
20+
class FunctionCallExample:
21+
"""Function call output example."""
22+
23+
func_name: str
24+
func_args: dict[str, Any]
25+
26+
27+
@dataclass
28+
class AgentExample:
29+
"""Input/output example for agent instruction."""
30+
31+
input: str
32+
output: Union[str, FunctionCallExample]
33+
34+
35+
def format_examples(examples: list[AgentExample]) -> str:
36+
"""Format examples as natural language string."""
37+
if not examples:
38+
return ""
39+
40+
lines = ["\n\n# Input/Output Example"]
41+
for i, ex in enumerate(examples, 1):
42+
lines.append(f"\nExample {i}:")
43+
lines.append(f"- Input: {ex.input}")
44+
if isinstance(ex.output, str):
45+
lines.append(f"- Output: {ex.output}")
46+
else:
47+
lines.append(
48+
f"- Output: Call function `{ex.output.func_name}` with arguments {ex.output.func_args}"
49+
)
50+
51+
return "\n".join(lines)

0 commit comments

Comments
 (0)