Create crew.py
Browse files- lab/crew.py +98 -0
lab/crew.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Agent, Task, Crew, Process
|
2 |
+
from extra_tools import search_wikipedia, scrap_webpage
|
3 |
+
from pydantic import BaseModel, Field
|
4 |
+
from typing import List
|
5 |
+
from typing_extensions import TypedDict
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
class Paragraph(TypedDict):
|
10 |
+
sub_header: str
|
11 |
+
paragraph: str
|
12 |
+
|
13 |
+
class Essay(BaseModel):
|
14 |
+
header: str = Field(..., description="The header of the essay")
|
15 |
+
entry: str = Field(..., description="The entry of the essay")
|
16 |
+
paragraphs: List[Paragraph] = Field(..., description="The paragraphs of the essay")
|
17 |
+
conclusion: str = Field(..., description="The conclusion of the essay")
|
18 |
+
seo_keywords: List[str] = Field(..., description="The SEO keywords of the essay")
|
19 |
+
|
20 |
+
class CrewClass:
|
21 |
+
"""Essay Writing Crew Class"""
|
22 |
+
def __init__(self, llm):
|
23 |
+
self.llm = llm
|
24 |
+
|
25 |
+
self.researcher = Agent(
|
26 |
+
role="Content Researcher",
|
27 |
+
goal="Research accurate content on {topic}",
|
28 |
+
backstory="You're researching content to write an essay about the topic: {topic}."
|
29 |
+
"You collect information that helps the audience learn something and make informed decisions."
|
30 |
+
"Your work is the basis for the Content Writer to write an article on this topic.",
|
31 |
+
verbose=True,
|
32 |
+
llm=self.llm,
|
33 |
+
)
|
34 |
+
|
35 |
+
self.writer = Agent(
|
36 |
+
role="Content Writer",
|
37 |
+
goal="Write insightful and factually accurate "
|
38 |
+
"opinion piece about the provided topic",
|
39 |
+
backstory="You're working on a writing a new opinion piece about the provided topic."
|
40 |
+
"You base your writing on the work of the Content Researcher, who provides an outline and relevant context about the topic."
|
41 |
+
"You follow the main objectives and direction of the outline, as provide by the Content Researcher."
|
42 |
+
"You also provide objective and impartial insights and back them up with information provide by the Content Researcher.",
|
43 |
+
verbose=True,
|
44 |
+
|
45 |
+
llm=self.llm
|
46 |
+
)
|
47 |
+
self.editor = Agent(
|
48 |
+
role="Content Editor",
|
49 |
+
goal="Edit a given essay to align with the writing style of the organization.",
|
50 |
+
backstory="You are an editor who receives an essay from the Content Writer."
|
51 |
+
"Your goal is to review the essay to ensure that it follows best practices, provides balanced viewpoints"
|
52 |
+
"when providing opinions or assertions,and also avoids major controversial topics or opinions when possible.",
|
53 |
+
verbose=True,
|
54 |
+
llm=self.llm
|
55 |
+
)
|
56 |
+
|
57 |
+
self.research = Task(
|
58 |
+
description=(
|
59 |
+
"1. Prioritize the latest trends, key players, and noteworthy news on {topic}.\n"
|
60 |
+
"2. Identify the target audience, considering their interests and pain points.\n"
|
61 |
+
"3. Research a detailed content outline including an introduction, key points, and a conclusion.\n"
|
62 |
+
"4. Include SEO keywords and relevant data or sources."
|
63 |
+
),
|
64 |
+
expected_output="A comprehensive document with an outline, audience analysis, SEO keywords, and resources.",
|
65 |
+
tools = [search_wikipedia, scrap_webpage],
|
66 |
+
agent=self.researcher,
|
67 |
+
)
|
68 |
+
|
69 |
+
self.write = Task(
|
70 |
+
description=(
|
71 |
+
"1. Use the content to craft a compelling essay.\n"
|
72 |
+
"2. Incorporate SEO keywords naturally.\n"
|
73 |
+
"3. Sections/Subtitles are properly named in an engaging manner.\n"
|
74 |
+
"4. Ensure the essay is structured with an engaging introduction, insightful body, and a summarizing conclusion.\n"
|
75 |
+
"5. Proofread for grammatical errors and alignment with the brand's voice.\n"
|
76 |
+
"6. Pick a suitable header\n"
|
77 |
+
),
|
78 |
+
expected_output="A well-written essay in markdown format, ready for publication, each section should have 2 or 3 paragraphs.",
|
79 |
+
context=[self.research],
|
80 |
+
agent=self.writer,
|
81 |
+
)
|
82 |
+
|
83 |
+
self.edit = Task(
|
84 |
+
description="Proofread the given essay for grammatical errors and alignment with the brand's voice.",
|
85 |
+
expected_output="A well-written essay in required format, ready for publication, each section should have 2 or 3 paragraphs.",
|
86 |
+
output_json = Essay,
|
87 |
+
context=[self.write],
|
88 |
+
agent=self.editor
|
89 |
+
)
|
90 |
+
|
91 |
+
def kickoff(self,*args):
|
92 |
+
return Crew(
|
93 |
+
agents=[self.researcher, self.writer, self.editor],
|
94 |
+
tasks=[self.research, self.write, self.edit],
|
95 |
+
process=Process.sequential,
|
96 |
+
verbose=True,
|
97 |
+
memory=True
|
98 |
+
).kickoff(*args)
|