Skip to content

Commit 39dbe15

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

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

veadk/agent.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
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
59+
from veadk.prompts.prompt_example import (
60+
ExampleTool,
61+
_convert_to_adk_examples,
62+
AgentExample,
63+
)
6064

6165
patch_tracer()
6266
patch_asyncio()
@@ -258,18 +262,14 @@ def model_post_init(self, __context: Any) -> None:
258262
else:
259263
self.before_agent_callback = check_agent_authorization
260264

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-
270265
if self.prompt_manager:
271266
self.instruction = self.prompt_manager.get_prompt
272267

268+
if self.examples:
269+
adk_examples = _convert_to_adk_examples(self.examples)
270+
self.tools.append(ExampleTool(adk_examples))
271+
logger.info(f"Added {len(self.examples)} examples to agent")
272+
273273
logger.info(f"VeADK version: {VERSION}")
274274

275275
logger.info(f"{self.__class__.__name__} `{self.name}` init done.")

veadk/prompts/prompt_example.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,47 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from dataclasses import dataclass
1615
from typing import Any, Union
16+
from pydantic import BaseModel, Field
17+
from google.genai import types
18+
from google.adk.examples.example import Example
1719

1820

19-
@dataclass
20-
class FunctionCallExample:
21-
"""Function call output example."""
21+
# === Add these classes before the Agent class ===
22+
class FunctionCallExample(BaseModel):
23+
"""Represents a function call output in an example."""
2224

2325
func_name: str
24-
func_args: dict[str, Any]
26+
func_args: dict[str, Any] = Field(default_factory=dict)
2527

2628

27-
@dataclass
28-
class AgentExample:
29-
"""Input/output example for agent instruction."""
29+
class AgentExample(BaseModel):
30+
"""A few-shot example for the agent.
31+
32+
Attributes:
33+
input: User input text.
34+
output: Expected output - text string or function call.
35+
"""
3036

3137
input: str
3238
output: Union[str, FunctionCallExample]
3339

3440

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}")
41+
def _convert_to_adk_examples(examples: list[AgentExample]) -> list[Example]:
42+
"""Convert AgentExample list to ADK Example list."""
43+
result = []
44+
for ex in examples:
45+
input_content = types.Content(
46+
role="user", parts=[types.Part.from_text(text=ex.input)]
47+
)
4448
if isinstance(ex.output, str):
45-
lines.append(f"- Output: {ex.output}")
49+
output_parts = [types.Part.from_text(text=ex.output)]
4650
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)
51+
output_parts = [
52+
types.Part.from_function_call(
53+
name=ex.output.func_name, args=ex.output.func_args
54+
)
55+
]
56+
output_content = [types.Content(role="model", parts=output_parts)]
57+
result.append(Example(input=input_content, output=output_content))
58+
return result

0 commit comments

Comments
 (0)