----------------------
To pass a Pydantic BaseModel as tool input in LlamaIndex, you can use the
FunctionTool
class from
llama_index.core.tools
. Here's a step-by-step guide:
- Define your Pydantic BaseModel. For example, a
Song
model:
from pydantic import BaseModel
class Song(BaseModel):
"""A song with name and artist"""
name: str
artist: str
- Define a function that takes the fields of your BaseModel as arguments and returns an instance of your BaseModel. For example, a
generate_song
function:
def generate_song(name: str, artist: str) -> Song:
"""Generates a song with provided name and artist."""
return Song(name=name, artist=artist)
- Create a
FunctionTool
from your function using the from_defaults
method:
from llama_index.core.tools import FunctionTool
tool = FunctionTool.from_defaults(fn=generate_song)
- Use the
predict_and_call
method of your LLM (either Upstage
or OpenAI
) to call the tool with your input:
from llama_index.llms.upstage import Upstage
llm = Upstage()
response = llm.predict_and_call([tool], "Generate a song")
In this example, "Generate a song" is the input to the LLM, which will be passed to the
generate_song
function. The function will return a
Song
object, which is an instance of the Pydantic BaseModel.
You can find more details in the
LlamaIndex documentation () and
GitHub examples ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------