@@ -157,6 +157,105 @@ with Client() as client:
157157 print (person.name, person.age, person.city) # Alice 28 Paris
158158```
159159
160+ ### Tool Calling
161+
162+ Tool calling allows the model to call your Python functions to access real-time data, perform actions, or integrate with external systems. Tools work with a simple decorator-based API:
163+
164+ ``` python
165+ from applefoundationmodels import Client
166+
167+ with Client() as client:
168+ session = client.create_session()
169+
170+ # Register a tool with the @session.tool decorator
171+ @session.tool (description = " Get current weather for a location" )
172+ def get_weather (location : str , units : str = " celsius" ) -> str :
173+ """ Fetch weather information from your weather API."""
174+ # Your implementation here
175+ return f " Weather in { location} : 22° { units[0 ].upper()} , sunny "
176+
177+ @session.tool ()
178+ def calculate (expression : str ) -> float :
179+ """ Evaluate a mathematical expression safely."""
180+ # Your implementation here
181+ return eval (expression) # Use safe_eval in production!
182+
183+ # The model will automatically call tools when needed
184+ response = session.generate(
185+ " What's the weather in Paris and what's 15 times 23?"
186+ )
187+ print (response)
188+ # "The weather in Paris is 22°C and sunny. 15 times 23 equals 345."
189+
190+ # View the full conversation including tool calls
191+ for entry in session.transcript:
192+ print (f " { entry[' type' ]} : { entry.get(' content' , ' ' )} " )
193+ ```
194+
195+ ** Features:**
196+ - ** Automatic schema generation** from Python type hints
197+ - ** Parallel tool execution** when the model calls multiple tools
198+ - ** Full transcript access** showing all tool calls and outputs
199+ - ** Error handling** with detailed error information
200+ - ** Type-safe** with complete type annotations
201+
202+ ** Schema Extraction:**
203+
204+ The library automatically extracts JSON schemas from your Python functions:
205+
206+ ``` python
207+ @session.tool (description = " Search documentation" )
208+ def search_docs (query : str , limit : int = 10 , category : str = " all" ) -> list :
209+ """ Search the documentation database."""
210+ # Implementation...
211+ return results
212+
213+ # Automatically generates:
214+ # {
215+ # "name": "search_docs",
216+ # "description": "Search documentation",
217+ # "parameters": {
218+ # "type": "object",
219+ # "properties": {
220+ # "query": {"type": "string"},
221+ # "limit": {"type": "integer"},
222+ # "category": {"type": "string"}
223+ # },
224+ # "required": ["query"]
225+ # }
226+ # }
227+ ```
228+
229+ ** Transcript Access:**
230+
231+ View the complete conversation history including tool interactions:
232+
233+ ``` python
234+ # After generating with tools
235+ for entry in session.transcript:
236+ match entry[' type' ]:
237+ case ' prompt' :
238+ print (f " User: { entry[' content' ]} " )
239+ case ' tool_calls' :
240+ for call in entry[' tool_calls' ]:
241+ print (f " Calling tool: { call[' id' ]} " )
242+ case ' tool_output' :
243+ print (f " Tool result: { entry[' content' ]} " )
244+ case ' response' :
245+ print (f " Assistant: { entry[' content' ]} " )
246+ ```
247+
248+ ** Supported Parameter Types:**
249+
250+ Tool calling works with various parameter signatures:
251+ - No parameters
252+ - Single parameters (string, int, float, bool)
253+ - Multiple parameters with mixed types
254+ - Optional parameters with default values
255+ - Lists and nested objects
256+
257+ See ` examples/tool_calling_comprehensive.py ` for complete examples of all supported patterns.
258+
160259### Generation Parameters
161260
162261``` python
@@ -254,6 +353,10 @@ class Session:
254353 def generate_structured (prompt : str , schema : dict , ** params ) -> dict : ...
255354 async def generate_stream (prompt : str , ** params ) -> AsyncIterator[str ]: ...
256355
356+ def tool (description : str = None , name : str = None ) -> Callable: ...
357+ @ property
358+ def transcript () -> List[dict ]: ...
359+
257360 def get_history () -> List[dict ]: ...
258361 def clear_history () -> None : ...
259362 def add_message (role : str , content : str ) -> None : ...
@@ -305,6 +408,7 @@ All exceptions inherit from `FoundationModelsError`:
305408- ` GuardrailViolationError ` - Content blocked by safety filters
306409- ` ToolNotFoundError ` - Tool not registered
307410- ` ToolExecutionError ` - Tool execution failed
411+ - ` ToolCallError ` - Tool call error (validation, schema, etc.)
308412- ` UnknownError ` - Unknown error
309413
310414## Examples
@@ -313,8 +417,8 @@ See the `examples/` directory for complete working examples:
313417
314418- ` basic_chat.py ` - Simple conversation
315419- ` streaming_chat.py ` - Async streaming
316- - ` tool_calling.py ` - Tool registration (coming soon)
317420- ` structured_output.py ` - JSON schema validation
421+ - ` tool_calling_comprehensive.py ` - Complete tool calling demonstration with all parameter types
318422
319423## Development
320424
0 commit comments