Skip to content

Commit

Permalink
fix-agent-output-model-phi-1236
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Sep 19, 2024
1 parent 814903f commit dcdda91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Empty file added cookbook/providers/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions cookbook/providers/openai/structured_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import List
from pydantic import BaseModel, Field
from rich.pretty import pprint
from phi.agent import Agent
from phi.model.openai import OpenAIChat


class MovieScript(BaseModel):
setting: str = Field(..., description="Provide a nice setting for a blockbuster movie.")
ending: str = Field(..., description="Ending of the movie. If not available, provide a happy ending.")
genre: str = Field(
..., description="Genre of the movie. If not available, select action, thriller or romantic comedy."
)
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!")


movie_agent = Agent(
model=OpenAIChat(model="gpt-4o"),
description="You help people write movie scripts.",
output_model=MovieScript,
# debug_mode=True,
)

pprint(movie_agent.run("New York"))
4 changes: 2 additions & 2 deletions phi/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def run(
try:
structured_output = None
try:
structured_output = self.output_model.model_validate(run_response.content)
structured_output = self.output_model.model_validate(json.loads(run_response.content))
except ValidationError:
# Check if response starts with ```json
if run_response.content.startswith("```json"):
Expand Down Expand Up @@ -1362,7 +1362,7 @@ async def arun(
try:
structured_output = None
try:
structured_output = self.output_model.model_validate_json(run_response.content)
structured_output = self.output_model.model_validate(json.loads(run_response.content))
except ValidationError:
# Check if response starts with ```json
if run_response.content.startswith("```json"):
Expand Down
4 changes: 3 additions & 1 deletion phi/playground/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def agent_get():
),
enable_rag=agent.enable_rag,
tools=formatted_tools,
memory={"name": agent.memory.db.__class__.__name__} if agent.memory and agent.memory.db else None,
memory={"name": agent.memory.db.__class__.__name__}
if agent.memory and agent.memory.db
else None,
storage={"name": agent.storage.__class__.__name__} if agent.storage else None,
knowledge={"name": agent.knowledge.__class__.__name__} if agent.knowledge else None,
description=agent.description,
Expand Down

0 comments on commit dcdda91

Please sign in to comment.