Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Apr 16, 2024
1 parent aca6bf7 commit e201435
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 55 deletions.
51 changes: 0 additions & 51 deletions cookbook/teams/journalist.py

This file was deleted.

33 changes: 33 additions & 0 deletions cookbook/teams/journalist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Journalist Team

Inspired by the fantastic work by [Matt Shumer (@mattshumer_)](https://twitter.com/mattshumer_/status/1772286375817011259).
We created an open-ended Journalist Team that uses 3 GPT-4 Assistants to write an article.
- Searcher: Finds the most relevant articles on the topic
- Writer: Writes a draft of the article
- Editor: Edits the draft to make it more coherent


### 1. Create a virtual environment

```shell
python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
```

### 2. Install libraries

```shell
pip install openai google-search-results newspaper3k lxml_html_clean phidata
```

### 3. Export `OPENAI_API_KEY`

```shell
export OPENAI_API_KEY=sk-***
```

### 4. Run the Journalist Team to generate an article

```shell
python cookbook/teams/journalist/team.py
```
Empty file.
64 changes: 64 additions & 0 deletions cookbook/teams/journalist/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from textwrap import dedent
from phi.assistant.team import Assistant
from phi.tools.serpapi_toolkit import SerpApiToolkit
from phi.tools.newspaper_toolkit import NewspaperToolkit


searcher = Assistant(
name="Searcher",
role="Searches for top URLs based on a topic",
description=dedent("""\
You are a world-class journalist for the New York Times. Given a topic, generate a list of 3 search terms
for writing an article on that topic. Then search the web for each term, analyse the results
and return the 10 most relevant URLs.
"""),
instructions=[
"Given a topic, first generate a list of 3 search terms related to that topic.",
"For each search term, `search_google` and analyze the results."
"From the results of all searcher, return the 10 most relevant URLs to the topic.",
"Remember: you are writing for the New York Times, so the quality of the sources is important.",
],
tools=[SerpApiToolkit()],
add_datetime_to_instructions=True,
)
writer = Assistant(
name="Writer",
role="Retrieves text from URLs and writes a high-quality article",
description=dedent("""\
You are a senior writer for the New York Times. Given a topic and a list of URLs,
your goal is to write a high-quality NYT-worthy article on the topic.
"""),
instructions=[
"Given a topic and a list of URLs, first read the article using `get_article_text`."
"Then write a high-quality NYT-worthy article on the topic."
"The article should be well-structured, informative, and engaging",
"Ensure the length is at least as long as a NYT cover story -- at a minimum, 15 paragraphs.",
"Ensure you provide a nuanced and balanced opinion, quoting facts where possible.",
"Remember: you are writing for the New York Times, so the quality of the article is important.",
"Focus on clarity, coherence, and overall quality.",
"Never make up facts or plagiarize. Always provide proper attribution.",
],
tools=[NewspaperToolkit()],
add_datetime_to_instructions=True,
add_chat_history_to_prompt=True,
num_history_messages=3,
)

editor = Assistant(
name="Editor",
team=[searcher, writer],
description="You are a senior NYT editor. Given a topic, your goal is to write a NYT worthy article.",
instructions=[
"Given a topic, ask the search journalist to search for the most relevant URLs for that topic.",
"Then pass a description of the topic and URLs to the writer to get a draft of the article.",
"Edit, proofread, and refine the article to ensure it meets the high standards of the New York Times.",
"The article should be extremely articulate and well written. "
"Focus on clarity, coherence, and overall quality.",
"Ensure the article is engaging and informative.",
"Remember: you are the final gatekeeper before the article is published.",
],
add_datetime_to_instructions=True,
# debug_mode=True,
markdown=True,
)
editor.print_response("Write an article about latest developments in AI.")
6 changes: 6 additions & 0 deletions phi/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def llm_task(self) -> LLMTask:

tools = self.tools
if self.team and len(self.team) > 0:
self.update_assistant_team(self.team)
if tools is None:
tools = []
for assistant_index, assistant in enumerate(self.team):
Expand Down Expand Up @@ -315,6 +316,11 @@ def get_delegation_prompt(self) -> Optional[str]:
return delegation_prompt
return None

def update_assistant_team(self, team: List["Assistant"]) -> None:
"""Update the assistant team with memory from the current assistant."""
for assistant in team:
assistant.memory = self.memory

def to_database_row(self) -> AssistantRun:
"""Create a AssistantRun for the current Assistant (to save to the database)"""

Expand Down
6 changes: 3 additions & 3 deletions phi/task/llm/llm_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ def get_system_prompt(self) -> Optional[str]:
# Add instructions for delegating tasks to another assistant
if self.delegation_prompt and self.add_delegation_instructions:
_instructions.append(
"You are the leader of a team of AI Assistants. You can either respond directly or "
"delegate tasks to the assistants below depending on their role and the tools "
"available to them."
"You are part of a team of AI Assistants. You can either respond directly or "
"delegate tasks to other assistants in your team below depending on their role and "
"the tools available to them."
)
# Add instructions for using the knowledge base
if self.add_references_to_prompt:
Expand Down
2 changes: 1 addition & 1 deletion phi/tools/newspaper_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
try:
from newspaper import Article
except ImportError:
raise ImportError("`newspaper3k` not installed.")
raise ImportError("`newspaper4k` not installed.")


class NewspaperToolkit(Toolkit):
Expand Down

0 comments on commit e201435

Please sign in to comment.