|
| 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