agentai icon indicating copy to clipboard operation
agentai copied to clipboard

Validate pydantic models if declared

Open adivik2000 opened this issue 2 years ago • 0 comments

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.

adivik2000 avatar Sep 13 '23 17:09 adivik2000