A simple toy framework to enable tool usage with LLMs (like Ollama's models). It supports automatic discovery and registration of tools defined using decorators.
- Auto-discovery of tool functions (class methods or standalone)
- Decorator-based registration using
@tool_method - Supports structured tool calling with arguments and response parsing
- Easy integration with Ollama's chat interface
Create tool functions in the tools/ directory. Use the @tool_method decorator to register them:
For example you can define a WeatherTool api like below
from main.registry.tool_registry import ToolRegistry as tools
@tools.tool_method(name="get_weather_for_location", description="Get weather for a given location")
def get_weather_for_location(location: str) -> str:
return f"The weather in {location} is sunny."Use the LlmTooling interface to simplify initialization and tool usage.
from ollama import chat
from main import LlmTooling
# Initialize or register all tools
LlmTooling.init()
messages = [{"role": "user", "content": "What's the weather in Paris?"}]
response = chat(
model="llama3.2:3b",
messages=messages,
tools=LlmTooling.get_all_tools_as_callable() # Get all available tools
)
followup = LlmTooling.parse_and_call_tool_if_available(response) # Depending on the LLM response, call the requried tool if needed
if followup:
# Send followup to LLM for continuation
final_response = chat(
model="llama3.2:3b",
messages=followup,
tools=LlmTooling.get_all_tools_as_callable()
)
print(final_response)@tools.tool_method(name="tool_name", description="What the tool does")
def your_tool_function(arg1: str, arg2: int) -> str:
return "result"- LLM chooses to call a tool (via
tool_callsin response) parse_and_call_tool_if_availableruns the function- Result is sent back to the LLM using the appropriate message format
- Only functions marked with
@tool_methodare registered - Both class methods and standalone functions are supported
MIT