Upload 2 files
Browse files- Agent.py +115 -0
- requirements.txt +4 -0
Agent.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from crewai import Crew
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
import os
|
| 5 |
+
from textwrap import dedent
|
| 6 |
+
from crewai import Agent
|
| 7 |
+
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
|
| 8 |
+
from crewai import Task
|
| 9 |
+
|
| 10 |
+
# Initialize the LLM
|
| 11 |
+
llm = ChatGroq(
|
| 12 |
+
api_key="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",
|
| 13 |
+
verbose=True,
|
| 14 |
+
model="llama3-8b-8192"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Set environment variable for API key
|
| 18 |
+
os.environ["SERPER_API_KEY"] = "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"
|
| 19 |
+
|
| 20 |
+
# Define the agents and tasks
|
| 21 |
+
def web_researcher_agent(topic):
|
| 22 |
+
return Agent(
|
| 23 |
+
role="Expert web researcher",
|
| 24 |
+
goal=dedent(f"""Your goal is to search on the web and scrape websites for relevant high-quality content about the given topic.
|
| 25 |
+
Use the tools provided to search on web and scrape website.
|
| 26 |
+
Topic: {topic}"""),
|
| 27 |
+
tools=[ScrapeWebsiteTool(), SerperDevTool()],
|
| 28 |
+
backstory=dedent("""You are proficient at searching for specific topics on the web, selecting those that provide
|
| 29 |
+
best value and information."""),
|
| 30 |
+
verbose=True,
|
| 31 |
+
allow_delegation=False,
|
| 32 |
+
llm=llm
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
def writer_agent(topic):
|
| 36 |
+
return Agent(
|
| 37 |
+
role="Expert Writer",
|
| 38 |
+
goal=dedent(f"""Your goal is to write high-quality content about the given topic.
|
| 39 |
+
Topic: {topic}"""),
|
| 40 |
+
tools=[ScrapeWebsiteTool(), SerperDevTool()],
|
| 41 |
+
backstory=dedent("""You are proficient at writing high-quality content about the given topic."""),
|
| 42 |
+
verbose=True,
|
| 43 |
+
allow_delegation=False,
|
| 44 |
+
llm=llm
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
def web_researcher_task(topic):
|
| 48 |
+
return Task(
|
| 49 |
+
description=dedent(f"""Get valuable and high-quality information from the web about a given topic.
|
| 50 |
+
Your goal is to extract high-quality information from the web about a given topic.
|
| 51 |
+
Topic: {topic}"""),
|
| 52 |
+
expected_output=dedent(f"""The expected output should be well-formatted information about the given topic,
|
| 53 |
+
only high-quality information related to the topic.
|
| 54 |
+
Topic: {topic}"""),
|
| 55 |
+
agent=web_researcher_agent(topic)
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
def writer_task(topic):
|
| 59 |
+
return Task(
|
| 60 |
+
description=dedent(f"""Using information extracted by the web researcher agent,
|
| 61 |
+
your task is to write very detailed and lengthy content on the given topic.
|
| 62 |
+
Topic: {topic}"""),
|
| 63 |
+
expected_output=dedent(f"""The expected output should be well-formatted and flawless content on the given topic.
|
| 64 |
+
Topic: {topic}"""),
|
| 65 |
+
agent=writer_agent(topic)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Streamlit UI
|
| 69 |
+
st.header("Multi-Agent Chat System")
|
| 70 |
+
|
| 71 |
+
# Initialize chat history
|
| 72 |
+
if 'chat_history' not in st.session_state:
|
| 73 |
+
st.session_state.chat_history = []
|
| 74 |
+
|
| 75 |
+
# Function to format messages with HTML
|
| 76 |
+
def format_message(role, message):
|
| 77 |
+
if role == 'User':
|
| 78 |
+
return f"<div style='padding:10px;'><b style='color: #007bff;'>User:</b> {message}</div>"
|
| 79 |
+
elif role == 'System':
|
| 80 |
+
return f"<div style='padding:10px;'><b style='color: #dc3545;'>System:</b> {message}</div>"
|
| 81 |
+
|
| 82 |
+
# Display chat history
|
| 83 |
+
for entry in st.session_state.chat_history:
|
| 84 |
+
st.markdown(format_message(entry['role'], entry['message']), unsafe_allow_html=True)
|
| 85 |
+
|
| 86 |
+
# Input and buttons
|
| 87 |
+
topic = st.text_input("Enter the topic:")
|
| 88 |
+
|
| 89 |
+
col1, col2 = st.columns([2, 1])
|
| 90 |
+
|
| 91 |
+
with col1:
|
| 92 |
+
if st.button("Generate") and topic:
|
| 93 |
+
# Add user input to chat history
|
| 94 |
+
st.session_state.chat_history.append({'role': 'User', 'message': topic})
|
| 95 |
+
|
| 96 |
+
# Create and run Crew with agents and tasks
|
| 97 |
+
crew = Crew(
|
| 98 |
+
agents=[web_researcher_agent(topic), writer_agent(topic)],
|
| 99 |
+
tasks=[web_researcher_task(topic), writer_task(topic)],
|
| 100 |
+
verbose=True,
|
| 101 |
+
)
|
| 102 |
+
result = crew.kickoff()
|
| 103 |
+
|
| 104 |
+
# Add system response to chat history
|
| 105 |
+
st.session_state.chat_history.append({'role': 'System', 'message': result})
|
| 106 |
+
|
| 107 |
+
# Display updated chat history
|
| 108 |
+
for entry in st.session_state.chat_history:
|
| 109 |
+
st.markdown(format_message(entry['role'], entry['message']), unsafe_allow_html=True)
|
| 110 |
+
|
| 111 |
+
with col2:
|
| 112 |
+
if st.button("Clear Chat"):
|
| 113 |
+
st.session_state.chat_history = []
|
| 114 |
+
st.session_state['dummy_var'] = not st. session_state.get('dummy_var', False)
|
| 115 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
crewai==0.41.1
|
| 2 |
+
crewai_tools==0.4.26
|
| 3 |
+
langchain_groq==0.1.9
|
| 4 |
+
streamlit==1.37.1
|