Skip to content

mirascope.core.base.toolkit

The module for defining the toolkit class for LLM call tools.

Usage Documentation

Tools

BaseToolKit

Bases: BaseModel, ABC

A class for defining tools for LLM call tools.

The class should have methods decorated with @toolkit_tool to create tools.

Example:

from mirascope.core.base import BaseToolKit, toolkit_tool
from mirascope.core import openai

class BookRecommendationToolKit(BaseToolKit):
    '''A toolkit for recommending books.'''

    __namespace__: ClassVar[str | None] = 'book_tools'
    reading_level: Literal["beginner", "advanced"]

    @toolkit_tool
    def format_book(self, title: str, author: str) -> str:
        '''Returns the title and author of a book nicely formatted.

        Reading level: {self.reading_level}
        '''
        return f"{title} by {author}"

@openai.call(model="gpt-4o")
def recommend_book(genre: str, reading_level: Literal["beginner", "advanced"]):
    '''Recommend a {genre} book.'''
    toolkit = BookRecommendationToolKit(reading_level=reading_level)
    return {"tools": toolkit.create_tools()}

response = recommend_book("fantasy", "beginner")
if tool := response.tool:
    output = tool.call()
    print(output)
    #> The Name of the Wind by Patrick Rothfuss
else:
    print(response.content)
    #> Sure! I would recommend...

create_tools

create_tools() -> list[type[BaseTool]]

The method to create the tools.

Source code in mirascope/core/base/toolkit.py
def create_tools(self) -> list[type[BaseTool]]:
    """The method to create the tools."""
    tools = []
    for method, template_vars, template in self._toolkit_tool_methods:
        for var in template_vars:
            if var.startswith("self."):
                continue
            # Replace non-self template variables with escaped double brackets so
            # that templating `self` results in a future templateable string.
            template = template.replace(f"{{{var}}}", f"{{{{{var}}}}}")
        converted_method = convert_function_to_base_tool(
            method, BaseTool, template.format(self=self), self.__namespace__
        )
        for key, value in self:
            setattr(converted_method, key, value)
        tools.append(converted_method)
    return tools