mirascope.core.base.toolkit | Mirascope
MirascopeLilypad

mirascope.core.base.toolkit

Module toolkit

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

Usage

Attribute P

Type: ParamSpec('P')

Function is_toolkit_tool

Parameters

NameTypeDescription
method() => Any | BaseTool-

Returns

TypeDescription
bool-

Class ToolKitToolMethod

Bases:

NamedTuple

Attributes

NameTypeDescription
method() => str-
template_varslist[str]-
templatestr-

Class BaseToolKit

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...

Bases: BaseModel, ABC

Attributes

NameTypeDescription
model_configConfigDict(arbitrary_types_allowed=True)-

Function create_tools

The method to create the tools.

Parameters

NameTypeDescription
selfAny-

Returns

TypeDescription
list[type[BaseTool]]-

Function toolkit_tool

Parameters

NameTypeDescription
method(_BaseToolKitT, P) => str | type[_BaseToolT]-

Returns

TypeDescription
(_BaseToolKitT, P) => str | type[_BaseToolT]-