mirascope.core.openai.tool
Module tool
The OpenAITool
class for easy tool usage with OpenAI LLM calls.
Usage
Class GenerateOpenAIStrictToolJsonSchema
Bases:
GenerateJsonSchemaNoTitlesClass OpenAIToolConfig
A tool configuration for OpenAI-specific features.
Bases:
ToolConfigAttributes
Name | Type | Description |
---|---|---|
strict | bool | - |
Class OpenAITool
A class for defining tools for OpenAI LLM calls.
Example:
from mirascope.core import prompt_template
from mirascope.core.openai import openai_call
def format_book(title: str, author: str) -> str:
return f"{title} by {author}"
@openai_call("gpt-4o-mini", tools=[format_book])
def recommend_book(genre: str) -> str:
return f"Recommend a {genre} book"
response = recommend_book("fantasy")
if tool := response.tool: # returns an `OpenAITool` instance
print(tool.call())
Bases:
BaseToolAttributes
Name | Type | Description |
---|---|---|
tool_call | SkipJsonSchema[ChatCompletionMessageToolCall] | - |
Function tool_schema
Constructs a JSON Schema tool schema from the BaseModel
schema defined.
Example:
from mirascope.core.openai import OpenAITool
def format_book(title: str, author: str) -> str:
return f"{title} by {author}"
tool_type = OpenAITool.type_from_fn(format_book)
print(tool_type.tool_schema()) # prints the OpenAI-specific tool schema
Parameters
Name | Type | Description |
---|---|---|
cls | Any | - |
Returns
Type | Description |
---|---|
ChatCompletionToolParam | - |
Function from_tool_call
Constructs an OpenAITool
instance from a tool_call
.
Parameters
Name | Type | Description |
---|---|---|
cls | Any | - |
tool_call | ChatCompletionMessageToolCall | The OpenAI tool call from which to construct this tool instance. |
allow_partial= False | bool | Whether to allow partial JSON data. |
Returns
Type | Description |
---|---|
OpenAITool | - |