A tool is a Python function that your agents can use to accomplish tasks. Tools extend an agent’s capabilities beyond their built-in knowledge and abilities, allowing them to interact with external systems, perform calculations, or access specific information.Tools can be:
Simple utility functions for common operations
Complex data processing operations
API calls to external services
File system operations
Database queries
System commands
Custom business logic
Here’s a basic example of a tool for rolling dice:
Copy
Ask AI
import marvinimport randomdef roll_die() -> int: """Roll a 6-sided die.""" return random.randint(1, 6)rolls = marvin.run("Roll a die four times", tools=[roll_die])print(rolls)
Any Python function can be used as a tool. The function’s name, docstring, and type hints help agents understand what the tool does and how to use it:
Copy
Ask AI
from typing import Annotatedfrom pydantic import FieldLocation = Annotated[str, Field(description="The 5 or 9-digit zip code")]Temperature = Annotated[float, Field(description="The temperature in Fahrenheit")]def get_weather(location: Location) -> Temperature: """Fetch weather data for the specified location.""" # Implementation details...
Tools can be provided to tasks or agents. When a tool is provided to a task, any agent working on that task will have access to the tool. When a tool is provided to an agent, the agent can use the tool in any task it is assigned to:
Copy
Ask AI
import marvindef search_database(query: str) -> list: """Search the product database for items matching the query.""" # Implementation details...# Provide tools to a tasktask = marvin.Task( instructions="Find products matching 'blue shirt'", tools=[search_database])# Or provide tools to an agentagent = marvin.Agent( name="ProductBot", tools=[search_database])