|
| 1 | +# FunctionTool Specifications |
| 2 | + |
| 3 | +FunctionTool is the utility allowing developers to provide functions within their code and invoke during streaming or running. |
| 4 | + |
| 5 | +## Example of Function |
| 6 | + |
| 7 | +Here is an example of a function: |
| 8 | +```python |
| 9 | +def fetch_weather(location: str) -> str: |
| 10 | + """ |
| 11 | + Fetches the weather information for the specified location. |
| 12 | + |
| 13 | + :param location (str): The location to fetch weather for. |
| 14 | + :return: Weather information as a JSON string. |
| 15 | + :rtype: str |
| 16 | + """ |
| 17 | + # In a real-world scenario, you'd integrate with a weather API. |
| 18 | + mock_weather_data = {"New York": "Sunny, 25°C", "London": "Cloudy, 18°C", "Tokyo": "Rainy, 22°C"} |
| 19 | + weather = mock_weather_data.get(location, "Weather data not available for this location.") |
| 20 | + weather_json = json.dumps({"weather": weather}) |
| 21 | + return weather_json |
| 22 | +``` |
| 23 | + |
| 24 | +Here is an example to attach this function definition to create_agent |
| 25 | + |
| 26 | +```python |
| 27 | +functions = FunctionTool({fetch_weather}) |
| 28 | + |
| 29 | +agent = agents_client.create_agent( |
| 30 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 31 | + name="my-assistant", |
| 32 | + instructions="You are a helpful assistant", |
| 33 | + tools=functions.definitions, |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +To verify that the SDK parsed the docstring properly, you can print the definition: |
| 38 | + |
| 39 | +```python |
| 40 | +[print(json.dumps(tool.as_dict(), indent=4)) for tool in functions.definitions] |
| 41 | +``` |
| 42 | + |
| 43 | +Alternatively user can check the tools property in newly created agent: |
| 44 | + |
| 45 | +```python |
| 46 | +[print(json.dumps(tool.as_dict(), indent=4)) for tool in agent.tools if tool.type == "function"] |
| 47 | +``` |
| 48 | + |
| 49 | +The terminal will display the definition as below: |
| 50 | + |
| 51 | +```json |
| 52 | +[ |
| 53 | + { |
| 54 | + "type": "function", |
| 55 | + "function": { |
| 56 | + "name": "fetch_weather", |
| 57 | + "description": "Fetches the weather information for the specified location.", |
| 58 | + "parameters": { |
| 59 | + "type": "object", |
| 60 | + "properties": { |
| 61 | + "location": { |
| 62 | + "type": "string", |
| 63 | + "description": "The location to fetch weather for." |
| 64 | + } |
| 65 | + }, |
| 66 | + "required": [ |
| 67 | + "location" |
| 68 | + ] |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +] |
| 73 | +``` |
| 74 | + |
| 75 | +## Requirements for FunctionTool |
| 76 | + |
| 77 | +To ensure `FunctionTool` operates correctly and generates accurate function definitions that agents can reliably call, adhere to the following standards: |
| 78 | + |
| 79 | +1. **Type Annotations** |
| 80 | + - All function parameters and return types should be explicitly type-annotated using Python's type hinting. |
| 81 | + |
| 82 | +2. **Structured Docstrings** |
| 83 | + - Utilize a consistent docstring format similar to the example above (see also related agent samples in this repository). |
| 84 | + - Include clear descriptions for each function and parameter. |
| 85 | + |
| 86 | +3. **Supported Types** |
| 87 | + |
| 88 | + `FunctionTool` maps common Python types to their JSON Schema equivalents, ensuring accurate representation without complex type details: |
| 89 | + |
| 90 | + - **Strings and Numbers** |
| 91 | + - `str` → `string` |
| 92 | + - `int` → `integer` |
| 93 | + - `float` → `number` |
| 94 | + - `bool` → `boolean` |
| 95 | + |
| 96 | + - **Collections** |
| 97 | + - `list` → `array` |
| 98 | + - `dict` → `object` |
| 99 | + |
| 100 | + - **Nullable Types** |
| 101 | + - `Optional[type]` includes `null` |
0 commit comments