| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """``@function_tool`` examples loaded by the tests. |
| |
| Includes both sync and async functions to exercise the dispatcher's |
| ``asyncio.iscoroutinefunction`` branch. |
| """ |
|
|
| import asyncio |
|
|
| from verl.tools.function_tool import function_tool |
|
|
|
|
| @function_tool |
| def get_weather(city: str) -> dict: |
| """Get the current weather for a city. |
| |
| Args: |
| city: The city to look up, e.g. "Tokyo" or "San Francisco". |
| """ |
| |
| |
| |
| table = { |
| "tokyo": {"temperature_c": 17.3, "condition": "drizzle"}, |
| "san francisco": {"temperature_c": 14.8, "condition": "fog"}, |
| "new york": {"temperature_c": 21.6, "condition": "sunny"}, |
| } |
| return table.get(city.lower(), {"temperature_c": -273.15, "condition": "unknown"}) |
|
|
|
|
| @function_tool |
| def calculator(expression: str) -> str: |
| """Evaluate an arithmetic expression and return the result. |
| |
| Supports +, -, *, /, **, parentheses, and unary minus. Use this for any |
| numerical computation instead of doing mental arithmetic. |
| |
| Args: |
| expression: A Python-style arithmetic expression, e.g. "(3+4)*5". |
| """ |
| import ast |
| import operator as op |
|
|
| ops = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.USub: op.neg} |
|
|
| def _eval(node): |
| if isinstance(node, ast.Constant) and isinstance(node.value, int | float): |
| return node.value |
| if isinstance(node, ast.BinOp): |
| return ops[type(node.op)](_eval(node.left), _eval(node.right)) |
| if isinstance(node, ast.UnaryOp): |
| return ops[type(node.op)](_eval(node.operand)) |
| raise ValueError(f"unsupported node: {ast.dump(node)}") |
|
|
| try: |
| return str(_eval(ast.parse(expression, mode="eval").body)) |
| except Exception as e: |
| return f"ERROR: {e}" |
|
|
|
|
| @function_tool |
| async def fetch_url(url: str) -> str: |
| """Fetch the contents of a URL (async). |
| |
| This is a stubbed example that demonstrates ``async def`` tools; the |
| real implementation would use ``aiohttp`` or ``httpx``. |
| |
| Args: |
| url: The URL to fetch. |
| """ |
| |
| |
| await asyncio.sleep(0) |
| return f"<stub body for {url}>" |
|
|