|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from dataclasses import dataclass |
16 | 15 | 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 |
17 | 19 |
|
18 | 20 |
|
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.""" |
22 | 24 |
|
23 | 25 | func_name: str |
24 | | - func_args: dict[str, Any] |
| 26 | + func_args: dict[str, Any] = Field(default_factory=dict) |
25 | 27 |
|
26 | 28 |
|
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 | + """ |
30 | 36 |
|
31 | 37 | input: str |
32 | 38 | output: Union[str, FunctionCallExample] |
33 | 39 |
|
34 | 40 |
|
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 | + ) |
44 | 48 | if isinstance(ex.output, str): |
45 | | - lines.append(f"- Output: {ex.output}") |
| 49 | + output_parts = [types.Part.from_text(text=ex.output)] |
46 | 50 | 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