Ollama Python Library 0.4 Release Notes
Ollama Python library 0.4 enables developers to provide Python functions directly as tools for LLMs, removing the need to manually define JSON schemas. This update streamlines the integration of external capabilities—such as mathematical operations or API requests—into model workflows.
Direct Python Function Integration as Tools
Developers can now pass actual Python function references into the tools field of the ollama.chat method. This allows the model to request the execution of specific functions based on the user's prompt.
Implementation Workflow
Integrating a function as a tool follows a three-step process:
- Function Definition: Define a standard Python function. For optimal model performance, developers should annotate parameter and return value types and include Google-style docstrings to provide the model with necessary context.
- Passing the Tool: Include the function reference in the
toolslist when callingollama.chat. - Execution: Iterate through
response.message.tool_callsto identify the requested function and execute it using the arguments provided by the model.
Support for External Libraries
This functionality extends to existing Python libraries and SDKs. For example, the request function from the requests library can be passed as a tool, allowing the model to trigger HTTP requests to fetch external web content.
Automated JSON Schema Generation
Ollama Python library 0.4 automates the creation of the tool definitions required by the model. Previously, these JSON schemas had to be provided manually.
The library now utilizes Pydantic and docstring parsing to automatically generate the JSON schema from the Python function's signature and documentation. This process extracts the function name, description, and parameter types (e.g., mapping Python int to JSON integer) to create a valid tool definition for the LLM.
Additional Library Enhancements
Beyond function calling improvements, the 0.4 release introduces the following updates:
- Full Typing Support: The library now features comprehensive typing support, enabling direct object access while preserving backward compatibility with existing functionality.
- Updated Examples: New and updated implementation examples are available on the official Ollama Python GitHub repository.
Sources
Related
- Dispatch
- Dispatch
- Project
- Dispatch
- Dispatch