agentai
agentai copied to clipboard
Validate pydantic models if declared
This PR addresses the following issue.
Say we have a Pydantic Model like
class GameEntry(BaseModel):
"""Entry for the Name, Place, Animal, Thing game."""
letter: str = Field(..., description="The starting letter for the entries.")
name: str = Field(..., description="A name that starts with the given letter.")
place: str = Field(..., description="A place that starts with the given letter.")
animal: str = Field(..., description="An animal that starts with the given letter.")
thing: str = Field(..., description="A thing that starts with the given letter.")
A function like this
def player_says(entry: GameEntry):
f"Entry for letter {entry.letter} is: ({entry.name}, {entry.place}, {entry.animal}, {entry.thing})"
A typical function call with the class instance would return this perfectly but when we're using openai function calling, this would throw an attribute error as the function_arguments that we parse and use is a dict and not a pydantic model. Hence added a small functionality that validates the arguments with the models if defined and calls(in chat_complete_execute_fn) with the respective classes. The function can be now used as is, without any modification or extra validation in the function.
Took help of GPT for this one. Open to feedback.