From cfea0215a73d8af25c8b9d00fa894124dc318a00 Mon Sep 17 00:00:00 2001 From: tangou Date: Thu, 25 Dec 2025 00:15:36 +0800 Subject: [PATCH 1/3] feat(agent_example): create prompt example to instruction --- veadk/agent.py | 12 ++++++++ veadk/prompts/prompt_example.py | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 veadk/prompts/prompt_example.py diff --git a/veadk/agent.py b/veadk/agent.py index 82e3eb1e..0c9818e2 100644 --- a/veadk/agent.py +++ b/veadk/agent.py @@ -56,6 +56,7 @@ from veadk.utils.patches import patch_asyncio, patch_tracer from veadk.utils.misc import check_litellm_version from veadk.version import VERSION +from veadk.prompts.prompt_example import AgentExample, format_examples patch_tracer() patch_asyncio() @@ -140,6 +141,8 @@ class Agent(LlmAgent): enable_authz: bool = False + examples: list[AgentExample] = Field(default_factory=list) + def model_post_init(self, __context: Any) -> None: super().model_post_init(None) # for sub_agents init @@ -255,6 +258,15 @@ def model_post_init(self, __context: Any) -> None: else: self.before_agent_callback = check_agent_authorization + if self.examples: + base = ( + self.instruction + if isinstance(self.instruction, str) + else DEFAULT_INSTRUCTION + ) + self.instruction = base + format_examples(self.examples) + print(self.instruction) + if self.prompt_manager: self.instruction = self.prompt_manager.get_prompt diff --git a/veadk/prompts/prompt_example.py b/veadk/prompts/prompt_example.py new file mode 100644 index 00000000..12e5d7be --- /dev/null +++ b/veadk/prompts/prompt_example.py @@ -0,0 +1,51 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dataclasses import dataclass +from typing import Any, Union + + +@dataclass +class FunctionCallExample: + """Function call output example.""" + + func_name: str + func_args: dict[str, Any] + + +@dataclass +class AgentExample: + """Input/output example for agent instruction.""" + + input: str + output: Union[str, FunctionCallExample] + + +def format_examples(examples: list[AgentExample]) -> str: + """Format examples as natural language string.""" + if not examples: + return "" + + lines = ["\n\n# Input/Output Example"] + for i, ex in enumerate(examples, 1): + lines.append(f"\nExample {i}:") + lines.append(f"- Input: {ex.input}") + if isinstance(ex.output, str): + lines.append(f"- Output: {ex.output}") + else: + lines.append( + f"- Output: Call function `{ex.output.func_name}` with arguments {ex.output.func_args}" + ) + + return "\n".join(lines) From 39dbe15e746c8d79cacbcdd04664dc99b15fa7b0 Mon Sep 17 00:00:00 2001 From: tangou Date: Thu, 25 Dec 2025 11:18:20 +0800 Subject: [PATCH 2/3] feat(agent_example): create prompt example to instruction --- veadk/agent.py | 20 ++++++------- veadk/prompts/prompt_example.py | 53 +++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/veadk/agent.py b/veadk/agent.py index 0c9818e2..5defd0f3 100644 --- a/veadk/agent.py +++ b/veadk/agent.py @@ -56,7 +56,11 @@ from veadk.utils.patches import patch_asyncio, patch_tracer from veadk.utils.misc import check_litellm_version from veadk.version import VERSION -from veadk.prompts.prompt_example import AgentExample, format_examples +from veadk.prompts.prompt_example import ( + ExampleTool, + _convert_to_adk_examples, + AgentExample, +) patch_tracer() patch_asyncio() @@ -258,18 +262,14 @@ def model_post_init(self, __context: Any) -> None: else: self.before_agent_callback = check_agent_authorization - if self.examples: - base = ( - self.instruction - if isinstance(self.instruction, str) - else DEFAULT_INSTRUCTION - ) - self.instruction = base + format_examples(self.examples) - print(self.instruction) - if self.prompt_manager: self.instruction = self.prompt_manager.get_prompt + if self.examples: + adk_examples = _convert_to_adk_examples(self.examples) + self.tools.append(ExampleTool(adk_examples)) + logger.info(f"Added {len(self.examples)} examples to agent") + logger.info(f"VeADK version: {VERSION}") logger.info(f"{self.__class__.__name__} `{self.name}` init done.") diff --git a/veadk/prompts/prompt_example.py b/veadk/prompts/prompt_example.py index 12e5d7be..e400d947 100644 --- a/veadk/prompts/prompt_example.py +++ b/veadk/prompts/prompt_example.py @@ -12,40 +12,47 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dataclasses import dataclass from typing import Any, Union +from pydantic import BaseModel, Field +from google.genai import types +from google.adk.examples.example import Example -@dataclass -class FunctionCallExample: - """Function call output example.""" +# === Add these classes before the Agent class === +class FunctionCallExample(BaseModel): + """Represents a function call output in an example.""" func_name: str - func_args: dict[str, Any] + func_args: dict[str, Any] = Field(default_factory=dict) -@dataclass -class AgentExample: - """Input/output example for agent instruction.""" +class AgentExample(BaseModel): + """A few-shot example for the agent. + + Attributes: + input: User input text. + output: Expected output - text string or function call. + """ input: str output: Union[str, FunctionCallExample] -def format_examples(examples: list[AgentExample]) -> str: - """Format examples as natural language string.""" - if not examples: - return "" - - lines = ["\n\n# Input/Output Example"] - for i, ex in enumerate(examples, 1): - lines.append(f"\nExample {i}:") - lines.append(f"- Input: {ex.input}") +def _convert_to_adk_examples(examples: list[AgentExample]) -> list[Example]: + """Convert AgentExample list to ADK Example list.""" + result = [] + for ex in examples: + input_content = types.Content( + role="user", parts=[types.Part.from_text(text=ex.input)] + ) if isinstance(ex.output, str): - lines.append(f"- Output: {ex.output}") + output_parts = [types.Part.from_text(text=ex.output)] else: - lines.append( - f"- Output: Call function `{ex.output.func_name}` with arguments {ex.output.func_args}" - ) - - return "\n".join(lines) + output_parts = [ + types.Part.from_function_call( + name=ex.output.func_name, args=ex.output.func_args + ) + ] + output_content = [types.Content(role="model", parts=output_parts)] + result.append(Example(input=input_content, output=output_content)) + return result From 3cb749f0249d57b27c8e1ea522dff4923a8103d9 Mon Sep 17 00:00:00 2001 From: tangou Date: Thu, 25 Dec 2025 11:25:38 +0800 Subject: [PATCH 3/3] feat(agent_example): create prompt example to instruction --- veadk/prompts/prompt_example.py | 1 - 1 file changed, 1 deletion(-) diff --git a/veadk/prompts/prompt_example.py b/veadk/prompts/prompt_example.py index e400d947..8943c983 100644 --- a/veadk/prompts/prompt_example.py +++ b/veadk/prompts/prompt_example.py @@ -18,7 +18,6 @@ from google.adk.examples.example import Example -# === Add these classes before the Agent class === class FunctionCallExample(BaseModel): """Represents a function call output in an example."""