Skip to content

Commit

Permalink
v2.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Oct 18, 2024
1 parent 4aca737 commit 846cf55
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 114 deletions.
4 changes: 2 additions & 2 deletions cookbook/agents/generate_image.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.dalle import DalleTools
from phi.tools.dalle import Dalle

agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DalleTools()],
tools=[Dalle()],
markdown=True,
debug_mode=True,
instructions=[
Expand Down
4 changes: 2 additions & 2 deletions cookbook/agents/generate_video.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.video_gen import VideoGenTools
from phi.tools.models_labs import ModelsLabs

agent = Agent(
name="Video Generation Agent",
agent_id="video-generation-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[VideoGenTools()],
tools=[ModelsLabs()],
markdown=True,
debug_mode=True,
show_tool_calls=True,
Expand Down
126 changes: 126 additions & 0 deletions cookbook/playground/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""Run `pip install openai sqlalchemy pypdf duckduckgo-search yfinance exa_py lancedb tantivy` to install dependencies."""

from textwrap import dedent
from datetime import datetime

from phi.agent import Agent
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.model.openai import OpenAIChat
from phi.playground import Playground, serve_playground_app
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.dalle import Dalle
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.exa import ExaTools
from phi.tools.yfinance import YFinanceTools
from phi.vectordb.lancedb import LanceDb, SearchType

lance_db_uri = "tmp/lancedb"
db_session_storage_file: str = "agents.db"

web_agent = Agent(
name="Web Agent",
role="Search the web for information",
agent_id="web-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGo()],
instructions=["Break down the users request into 2-3 different searches.", "Always include sources"],
storage=SqlAgentStorage(table_name="web_agent", db_file=db_session_storage_file),
add_history_to_messages=True,
num_history_responses=5,
add_datetime_to_instructions=True,
markdown=True,
)

finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
agent_id="finance-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
instructions=["Always use tables to display data"],
storage=SqlAgentStorage(table_name="finance_agent", db_file=db_session_storage_file),
add_history_to_messages=True,
num_history_responses=5,
add_datetime_to_instructions=True,
markdown=True,
)

image_agent = Agent(
name="Image Agent",
role="Generate images given a prompt",
agent_id="image-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[Dalle(model="dall-e-3", size="1792x1024", quality="hd", style="vivid")],
storage=SqlAgentStorage(table_name="image_agent", db_file=db_session_storage_file),
add_history_to_messages=True,
add_datetime_to_instructions=True,
markdown=True,
)

research_agent = Agent(
name="Research Agent",
role="Write research reports for the New York Times",
agent_id="research-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[ExaTools(start_published_date=datetime.now().strftime("%Y-%m-%d"), type="keyword")],
description=(
"You are a Research Agent that has the special skill of writing New York Times worthy articles. "
"If you can directly respond to the user, do so. If the user asks for a report or provides a topic, follow the instructions below."
),
instructions=[
"For the provided topic, run 3 different searches.",
"Read the results carefully and prepare a NYT worthy article.",
"Focus on facts and make sure to provide references.",
],
expected_output=dedent("""\
Your articles should be engaging, informative, well-structured and in markdown format. They should follow the following structure:
## Engaging Article Title
### Overview
{give a brief introduction of the article and why the user should read this report}
{make this section engaging and create a hook for the reader}
### Section 1
{break the article into sections}
{provide details/facts/processes in this section}
... more sections as necessary...
### Takeaways
{provide key takeaways from the article}
### References
- [Reference 1](link)
- [Reference 2](link)
"""),
storage=SqlAgentStorage(table_name="research_agent", db_file=db_session_storage_file),
add_history_to_messages=True,
add_datetime_to_instructions=True,
markdown=True,
)

recipes_knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=LanceDb(table_name="thai_recipes", uri=lance_db_uri, search_type=SearchType.vector),
)

recipe_agent = Agent(
name="Thai Recipes Agent",
agent_id="thai-recipes-agent",
model=OpenAIChat(id="gpt-4o"),
description="You are an expert at Thai Recipes and have a knowledge base full of special Thai recipes.",
instructions=["Always search your knowledge base first for the recipe."],
knowledge=recipes_knowledge_base,
storage=SqlAgentStorage(table_name="thai_recipe_agent", db_file=db_session_storage_file),
add_history_to_messages=True,
add_datetime_to_instructions=True,
markdown=True,
)

app = Playground(agents=[web_agent, finance_agent, image_agent, research_agent, recipe_agent]).get_app()

if __name__ == "__main__":
# Load the knowledge base: Comment out after first run
recipes_knowledge_base.load(upsert=True)
serve_playground_app("demo:app", reload=True)
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
app = Playground(agents=[finance_agent, research_agent]).get_app()

if __name__ == "__main__":
serve_playground_app("serve_ollama:app", port=8118, reload=True)
serve_playground_app("ollama:app", port=8118, reload=True)
73 changes: 0 additions & 73 deletions cookbook/playground/serve_8118.py

This file was deleted.

12 changes: 6 additions & 6 deletions cookbook/playground/serve.py → cookbook/playground/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Run `pip install yfinance exa_py` to install dependencies."""
"""Run `pip install openai yfinance exa_py` to install dependencies."""

from textwrap import dedent
from datetime import datetime
Expand All @@ -11,16 +11,16 @@
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector, SearchType
from phi.playground import Playground, serve_playground_app
from phi.tools.video_gen import VideoGenTools
from phi.tools.dalle import DalleTools
from phi.tools.models_labs import ModelsLabs
from phi.tools.dalle import Dalle

db_url: str = "postgresql+psycopg://ai:ai@localhost:5532/ai"

video_gen_agent = Agent(
name="Video Gen Agent",
agent_id="video-gen-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[VideoGenTools()],
tools=[ModelsLabs()],
markdown=True,
debug_mode=True,
show_tool_calls=True,
Expand Down Expand Up @@ -56,7 +56,7 @@
name="Dalle Agent",
agent_id="dalle-agent",
model=OpenAIChat(id="gpt-4o"),
tools=[DalleTools()],
tools=[Dalle()],
markdown=True,
debug_mode=True,
)
Expand Down Expand Up @@ -129,4 +129,4 @@
if __name__ == "__main__":
# Load the knowledge base: Comment out after first run
# recipe_knowledge_base.load(upsert=True)
serve_playground_app("serve:app", reload=True)
serve_playground_app("test:app", reload=True)
27 changes: 15 additions & 12 deletions phi/tools/dalle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from phi.tools import Toolkit
from typing import Optional
from os import getenv
from typing import Optional, Literal

from phi.tools import Toolkit
from phi.utils.log import logger


Expand All @@ -10,33 +11,34 @@
raise ImportError("`openai` not installed. Please install using `pip install openai`")


class DalleTools(Toolkit):
class Dalle(Toolkit):
def __init__(
self,
model: str = "dall-e-3",
size: str = "1280x720",
quality: str = "standard",
n: int = 1,
size: Optional[Literal["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"]] = "1024x1024",
quality: Literal["standard", "hd"] = "standard",
style: Literal["vivid", "natural"] = "vivid",
api_key: Optional[str] = None,
):
super().__init__(name="dalle")

self.model = model
self.n = n
self.size = size
self.quality = quality
self.n = n

self.style = style
self.api_key = api_key or getenv("OPENAI_API_KEY")
if not self.api_key:
logger.error("OPENAI_API_KEY not set. Please set the OPENAI_API_KEY environment variable.")

self.register(self.generate_image)

def generate_image(self, prompt: str) -> str:
"""Use this function to generate an image using DALL-E.
"""Use this function to generate an image given a prompt.
Args:
prompt (str): The prompt to generate an image from.
prompt (str): A text description of the desired image.
Returns:
str: The URL of the generated image, or an error message.
Expand All @@ -48,11 +50,12 @@ def generate_image(self, prompt: str) -> str:
client = OpenAI(api_key=self.api_key)
logger.info(f"Generating image for prompt: {prompt}")
response = client.images.generate(
model=self.model,
prompt=prompt,
size=self.size, # type: ignore
quality=self.quality, # type: ignore
model=self.model,
n=self.n,
quality=self.quality,
size=self.size,
style=self.style,
)
return response.data[0].url or "Error: No image URL returned"
except Exception as e:
Expand Down
Loading

0 comments on commit 846cf55

Please sign in to comment.