Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from crewai import Agent, Task, Crew
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
from langchain_community.tools import DuckDuckGoSearchRun, DuckDuckGoSearchResults
|
6 |
+
from crewai_tools import tool
|
7 |
+
|
8 |
+
# Define the DuckDuckGoSearch tool using the decorator for tool registration
|
9 |
+
@tool('DuckDuckGoSearch')
|
10 |
+
def search(search_query: str):
|
11 |
+
"""Search the web for information on a given topic."""
|
12 |
+
return DuckDuckGoSearchRun().run(search_query)
|
13 |
+
|
14 |
+
# Define the DuckDuckGoSearchResults tool
|
15 |
+
@tool('DuckDuckGoSearchResults')
|
16 |
+
def search_results(search_query: str):
|
17 |
+
"""Search the web for information and sources on a given topic with links and titles."""
|
18 |
+
return DuckDuckGoSearchResults().run(search_query)
|
19 |
+
|
20 |
+
def kickoff_crew(topic: str) -> dict:
|
21 |
+
"""Kickoff the research process for a given topic using CrewAI components."""
|
22 |
+
# Retrieve the API key from the environment variables
|
23 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
24 |
+
if not groq_api_key:
|
25 |
+
raise ValueError("API Key for Groq is not set in environment variables")
|
26 |
+
|
27 |
+
# Initialize the Groq large language model
|
28 |
+
groq_llm = ChatGroq(temperature=0, groq_api_key=groq_api_key, model_name="llama3-70b-8192")
|
29 |
+
|
30 |
+
# Define Agents
|
31 |
+
researcher = Agent(
|
32 |
+
role='Researcher',
|
33 |
+
goal=f'Collect detailed information on {topic}',
|
34 |
+
tools=[search, search_results],
|
35 |
+
llm=groq_llm,
|
36 |
+
backstory=(
|
37 |
+
"As a diligent researcher, you explore the depths of the internet to "
|
38 |
+
"unearth crucial information and insights on the assigned topics. "
|
39 |
+
"Attention to detail and accuracy is critical and requires that you keep a record of all the sources you use."
|
40 |
+
),
|
41 |
+
allow_delegation=False,
|
42 |
+
max_iter=5,
|
43 |
+
verbose=True
|
44 |
+
)
|
45 |
+
|
46 |
+
editor = Agent(
|
47 |
+
role='Editor',
|
48 |
+
goal='Compile and refine the information into a comprehensive report on {topic}',
|
49 |
+
llm=groq_llm,
|
50 |
+
backstory=(
|
51 |
+
"With a keen eye for detail and a strong command of language, you transform "
|
52 |
+
"raw data into polished, insightful reports that are both informative and engaging."
|
53 |
+
),
|
54 |
+
allow_delegation=False,
|
55 |
+
max_iter=3,
|
56 |
+
verbose=True
|
57 |
+
)
|
58 |
+
|
59 |
+
# Define Tasks
|
60 |
+
research_task = Task(
|
61 |
+
description=(
|
62 |
+
"Use DuckDuckGoSearch and DuckDuckGoSearchResults to gather information about {topic}. "
|
63 |
+
"Compile your findings into an initial draft. "
|
64 |
+
"Gather all the sources with title and link relevant to the topic. "
|
65 |
+
"Be sure you don't make up or invent any information."
|
66 |
+
),
|
67 |
+
expected_output='A draft report containing all relevant information about the topic and sources used',
|
68 |
+
agent=researcher
|
69 |
+
)
|
70 |
+
|
71 |
+
edit_task = Task(
|
72 |
+
description=(
|
73 |
+
"Review and refine the draft report. Organize the content, check for accuracy, "
|
74 |
+
"and enhance readability. "
|
75 |
+
"Include a section with all the sources used in the report from research_task as bullets: (title)[link]"
|
76 |
+
),
|
77 |
+
expected_output='A finalized comprehensive report on ## {topic} ##',
|
78 |
+
agent=editor,
|
79 |
+
context=[research_task]
|
80 |
+
)
|
81 |
+
|
82 |
+
# Forming the Crew
|
83 |
+
crew = Crew(
|
84 |
+
agents=[researcher, editor],
|
85 |
+
tasks=[research_task, edit_task]
|
86 |
+
)
|
87 |
+
|
88 |
+
# Kick-off the research process
|
89 |
+
result = crew.kickoff(inputs={'topic': topic})
|
90 |
+
return result
|
91 |
+
|
92 |
+
def main():
|
93 |
+
"""Set up the Gradio interface for the CrewAI Research Tool."""
|
94 |
+
with gr.Blocks() as demo:
|
95 |
+
gr.Markdown("## CrewAI Research Tool")
|
96 |
+
topic_input = gr.Textbox(label="Enter Topic", placeholder="Type here...")
|
97 |
+
submit_button = gr.Button("Start Research")
|
98 |
+
output = gr.Textbox(label="Result")
|
99 |
+
|
100 |
+
submit_button.click(
|
101 |
+
fn=kickoff_crew,
|
102 |
+
inputs=topic_input,
|
103 |
+
outputs=output
|
104 |
+
)
|
105 |
+
|
106 |
+
demo.launch()
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
main()
|