Skip to content

Commit

Permalink
Workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Sep 29, 2024
1 parent ddafb15 commit 9d72264
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 694 deletions.
10 changes: 5 additions & 5 deletions cookbook/agents/basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio # noqa
from typing import Iterator # noqa
from rich.pretty import pprint # noqa
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
from phi.storage.agent.postgres import PgAgentStorage
Expand All @@ -16,7 +16,7 @@
storage=PgAgentStorage(table_name="agent_sessions", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
)

# run1: AgentResponse = agent.run("What is the stock price of NVDA") # type: ignore
# run1: RunResponse = agent.run("What is the stock price of NVDA") # type: ignore
# pprint(run1)
# print("------------*******************------------")
# print(run)
Expand All @@ -27,10 +27,10 @@
# print(m)
# print("---")

# run: AgentResponse = agent.run("What is the stock price of NVDA")
# run: RunResponse = agent.run("What is the stock price of NVDA")
# pprint(run.content)

run_stream: Iterator[AgentResponse] = agent.run(
run_stream: Iterator[RunResponse] = agent.run(
"What is the stock price of NVDA", stream=True, stream_intermediate_steps=True
)
for chunk in run_stream:
Expand All @@ -40,7 +40,7 @@


# async def main():
# run: AgentResponse = await agent.arun("What is the stock price of NVDA and TSLA")
# run: RunResponse = await agent.arun("What is the stock price of NVDA and TSLA")
# pprint(run)
# # async for chunk in await agent.arun("What is the stock price of NVDA and TSLA", stream=True):
# # print(chunk.content)
Expand Down
8 changes: 4 additions & 4 deletions cookbook/agents/image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rich.pretty import pprint # noqa
from phi.agent import Agent, AgentResponse
from phi.agent import Agent, RunResponse
from phi.model.openai import OpenAIChat

agent = Agent(
Expand All @@ -8,13 +8,13 @@
debug_mode=True,
)

# run: AgentResponse = agent.run(
# run: RunResponse = agent.run(
# "What’s in this image?",
# images=[
# "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
# ],
# ) # type: ignore
# run: AgentResponse = agent.run(
# run: RunResponse = agent.run(
# "What’s in this image?",
# images=[
# {
Expand All @@ -23,7 +23,7 @@
# }
# ],
# ) # type: ignore
run: AgentResponse = agent.run(
run: RunResponse = agent.run(
"What are in these images? Is there any difference between them?",
images=[
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
Expand Down
4 changes: 2 additions & 2 deletions cookbook/agents/intermediate_steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterator
from rich.pretty import pprint
from phi.agent import Agent, AgentResponse
from phi.agent import Agent, RunResponse
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools

Expand All @@ -10,7 +10,7 @@
markdown=True,
)

run_stream: Iterator[AgentResponse] = agent.run(
run_stream: Iterator[RunResponse] = agent.run(
"What is the stock price of NVDA", stream=True, stream_intermediate_steps=True
)
for chunk in run_stream:
Expand Down
4 changes: 2 additions & 2 deletions cookbook/agents/knowledge.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rich.pretty import pprint # noqa
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector, SearchType
Expand Down Expand Up @@ -37,5 +37,5 @@

agent.print_response("How do i make Chicken and Galangal in Coconut Milk Soup")

# run1: AgentResponse = agent.run("How to make Gluai Buat Chi?") # type: ignore
# run1: RunResponse = agent.run("How to make Gluai Buat Chi?") # type: ignore
# pprint(run1)
10 changes: 5 additions & 5 deletions cookbook/agents/structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rich.text import Text
from pydantic import BaseModel, Field

from phi.agent import Agent, AgentResponse
from phi.agent import Agent, RunResponse
from phi.model.openai import OpenAIChat

console = Console()
Expand Down Expand Up @@ -80,15 +80,15 @@ def run_agents():
# Running movie_agent_1
display_header("Running Agent with response_model=MovieScript", panel_title="Agent 1")
with console.status("Running Agent 1...", spinner="dots"):
run_movie_agent_1: AgentResponse = movie_agent_1.run("New York")
run_movie_agent_1: RunResponse = movie_agent_1.run("New York")
display_content(run_movie_agent_1.content, title="Agent 1 Response")

# Running movie_agent_2
display_header(
"Running Agent with response_model=MovieScript and structured_outputs=True", panel_title="Agent 2"
)
with console.status("Running Agent 2...", spinner="dots"):
run_movie_agent_2: AgentResponse = movie_agent_2.run("New York")
run_movie_agent_2: RunResponse = movie_agent_2.run("New York")
display_content(run_movie_agent_2.content, title="Agent 2 Response")
except Exception as e:
console.print(f"[bold red]Error occurred while running agents: {e}[/bold red]")
Expand All @@ -99,7 +99,7 @@ async def run_agents_async():
# Running movie_agent_1 asynchronously
display_header("Running Agent with response_model=MovieScript (async)", panel_title="Async Agent 1")
with console.status("Running Agent 1...", spinner="dots"):
async_run_movie_agent_1: AgentResponse = await movie_agent_1.arun("New York")
async_run_movie_agent_1: RunResponse = await movie_agent_1.arun("New York")
display_content(async_run_movie_agent_1.content, title="Async Agent 1 Response")

# Running movie_agent_2 asynchronously
Expand All @@ -108,7 +108,7 @@ async def run_agents_async():
panel_title="Async Agent 2",
)
with console.status("Running Agent 2...", spinner="dots"):
async_run_movie_agent_2: AgentResponse = await movie_agent_2.arun("New York")
async_run_movie_agent_2: RunResponse = await movie_agent_2.arun("New York")
display_content(async_run_movie_agent_2.content, title="Async Agent 2 Response")
except Exception as e:
console.print(f"[bold red]Error occurred while running async agents: {e}[/bold red]")
Expand Down
10 changes: 5 additions & 5 deletions cookbook/agents/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio # noqa
from typing import Iterator # noqa
from rich.pretty import pprint # noqa
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
from phi.storage.agent.postgres import PgAgentStorage
Expand All @@ -16,7 +16,7 @@
storage=PgAgentStorage(table_name="agent_sessions", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
)

# run1: AgentResponse = agent.run("What is the stock price of NVDA") # type: ignore
# run1: RunResponse = agent.run("What is the stock price of NVDA") # type: ignore
# pprint(run1)
# print("------------*******************------------")
# print(run)
Expand All @@ -27,10 +27,10 @@
# print(m)
# print("---")

# run: AgentResponse = agent.run("What is the stock price of NVDA")
# run: RunResponse = agent.run("What is the stock price of NVDA")
# pprint(run.content)

# run_stream: Iterator[AgentResponse] = agent.run(
# run_stream: Iterator[RunResponse] = agent.run(
# "What is the stock price of NVDA", stream=True, stream_intermediate_steps=True
# )
# for chunk in run_stream:
Expand All @@ -41,7 +41,7 @@

async def main():
await agent.aprint_response("What is the stock price of NVDA and TSLA")
# run: AgentResponse = await agent.arun("What is the stock price of NVDA and TSLA")
# run: RunResponse = await agent.arun("What is the stock price of NVDA and TSLA")
# pprint(run)
# async for chunk in await agent.arun("What is the stock price of NVDA and TSLA", stream=True):
# print(chunk.content)
Expand Down
4 changes: 2 additions & 2 deletions cookbook/providers/openai/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools

Expand All @@ -10,7 +10,7 @@
)

# Get the response in a variable
# run: AgentResponse = agent.run("What is the stock price of NVDA and TSLA")
# run: RunResponse = agent.run("What is the stock price of NVDA and TSLA")
# print(run.content)

# Print the response on the terminal
Expand Down
4 changes: 2 additions & 2 deletions cookbook/providers/openai/agent_stream.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Iterator # noqa
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools

Expand All @@ -11,7 +11,7 @@
)

# Get the response in a variable
# run_response: Iterator[AgentResponse] = agent.run("What is the stock price of NVDA and TSLA", stream=True)
# run_response: Iterator[RunResponse] = agent.run("What is the stock price of NVDA and TSLA", stream=True)
# for chunk in run_response:
# print(chunk.content)

Expand Down
4 changes: 2 additions & 2 deletions cookbook/providers/openai/basic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat

agent = Agent(model=OpenAIChat(id="gpt-4o"), instructions=["Respond in a southern tone"], markdown=True)

# Get the response in a variable
# run: AgentResponse = agent.run("Explain simulation theory")
# run: RunResponse = agent.run("Explain simulation theory")
# print(run.content)

# Print the response on the terminal
Expand Down
4 changes: 2 additions & 2 deletions cookbook/providers/openai/basic_stream.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Iterator # noqa
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat

agent = Agent(model=OpenAIChat(id="gpt-4o"), instructions=["Respond in a southern tone"], markdown=True)

# Get the response in a variable
# run_response: Iterator[AgentResponse] = agent.run("Explain simulation theory", stream=True)
# run_response: Iterator[RunResponse] = agent.run("Explain simulation theory", stream=True)
# for chunk in run_response:
# print(chunk.content)

Expand Down
4 changes: 2 additions & 2 deletions cookbook/providers/openai/structured_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
from rich.pretty import pprint # noqa
from pydantic import BaseModel, Field
from phi.agent import Agent, AgentResponse # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.openai import OpenAIChat


Expand All @@ -25,7 +25,7 @@ class MovieScript(BaseModel):
)

# Get the response in a variable
# run: AgentResponse = movie_agent.run("New York")
# run: RunResponse = movie_agent.run("New York")
# pprint(run.content)

movie_writer.print_response("New York")
71 changes: 57 additions & 14 deletions cookbook/workflows/news_article.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from textwrap import dedent
from typing import Optional, Iterator

from pydantic import BaseModel, Field

from phi.agent import Agent, AgentResponse
from phi.agent import Agent, RunResponse
from phi.workflow import Workflow
from phi.tools.duckduckgo import DuckDuckGo
from phi.utils.pprint import pprint_agent_response_stream
from phi.tools.newspaper4k import Newspaper4k
from phi.utils.pprint import pprint_run_response
from phi.utils.log import logger


Expand All @@ -27,22 +29,63 @@ class WriteNewsReport(Workflow):
response_model=NewsArticles,
)

writer: Agent = Agent()
writer: Agent = Agent(
name="Writer",
tools=[Newspaper4k()],
description="You are a Senior NYT Editor and your task is to write a NYT cover story due tomorrow.",
instructions=[
"You will be provided with news articles and their links.",
"Carefully read each article and think about the contents",
"Then generate a final New York Times worthy article in the <article_format> provided below.",
"Break the article into sections and provide key takeaways at the end.",
"Make sure the title is catchy and engaging.",
"Give the section relevant titles and provide details/facts/processes in each section."
"Ignore articles that you cannot read or understand.",
"REMEMBER: you are writing for the New York Times, so the quality of the article is important.",
],
expected_output=dedent("""\
An engaging, informative, and well-structured article in the following format:
<article_format>
## Engaging Article Title
def run(self, topic: str) -> Iterator[AgentResponse]:
### 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
- [Title](url)
- [Title](url)
- [Title](url)
</article_format>
"""),
)

def run(self, topic: str) -> Iterator[RunResponse]:
logger.info(f"Researching articles on: {topic}")
# research: AgentResponse = self.researcher.run(topic)
# if research.content and isinstance(research.content, NewsArticles) and research.content.articles:
# logger.info(f"Research identified {len(research.content.articles)} articles.")
# else:
# logger.error("No articles found.")
# return
research: RunResponse = self.researcher.run(topic)
if research and research.content and isinstance(research.content, NewsArticles) and research.content.articles:
logger.info(f"Researcher identified {len(research.content.articles)} articles.")
else:
yield RunResponse(
run_id=self.run_id,
content=f"Sorry could not find any articles on the topic: {topic}",
)
return

logger.info("Reading each article and writing a report.")
# writer.print_response(research.content.model_dump_json(indent=2), markdown=True, show_message=False)
yield from self.writer.run(f"write 1 sentence on {topic}", stream=True)
yield from self.writer.run(research.content.model_dump_json(indent=2), stream=True)


# Run workflow
story = WriteNewsReport(debug_mode=False).run(topic="avocado toast")
pprint_agent_response_stream(story, markdown=True, show_time=True)
story = WriteNewsReport(debug_mode=False).run(topic="IBM Hashicorp Acquisition")
# Print the response
pprint_run_response(story, markdown=True, show_time=True)
Loading

0 comments on commit 9d72264

Please sign in to comment.