File size: 4,580 Bytes
b454ea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
from crewai import Crew
from langchain_groq import ChatGroq
import os
from textwrap import dedent
from crewai import Agent
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
from crewai import Task

# Initialize the LLM
llm = ChatGroq(
    api_key="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",
    verbose=True,
    model="llama3-8b-8192"
)

# Set environment variable for API key
os.environ["SERPER_API_KEY"] = "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"

# Define the agents and tasks
def web_researcher_agent(topic):
    return Agent(
        role="Expert web researcher",
        goal=dedent(f"""Your goal is to search on the web and scrape websites for relevant high-quality content about the given topic.

                     Use the tools provided to search on web and scrape website.

                     Topic: {topic}"""),
        tools=[ScrapeWebsiteTool(), SerperDevTool()],
        backstory=dedent("""You are proficient at searching for specific topics on the web, selecting those that provide

                            best value and information."""),
        verbose=True,
        allow_delegation=False,
        llm=llm
    )

def writer_agent(topic):
    return Agent(
        role="Expert Writer",
        goal=dedent(f"""Your goal is to write high-quality content about the given topic.

                     Topic: {topic}"""),
        tools=[ScrapeWebsiteTool(), SerperDevTool()],
        backstory=dedent("""You are proficient at writing high-quality content about the given topic."""),
        verbose=True,
        allow_delegation=False,
        llm=llm
    )

def web_researcher_task(topic):
    return Task(
        description=dedent(f"""Get valuable and high-quality information from the web about a given topic.

                              Your goal is to extract high-quality information from the web about a given topic.

                              Topic: {topic}"""),
        expected_output=dedent(f"""The expected output should be well-formatted information about the given topic,

                                  only high-quality information related to the topic.

                                  Topic: {topic}"""),
        agent=web_researcher_agent(topic)
    )

def writer_task(topic):
    return Task(
        description=dedent(f"""Using information extracted by the web researcher agent,

                              your task is to write very detailed and lengthy content on the given topic.

                              Topic: {topic}"""),
        expected_output=dedent(f"""The expected output should be well-formatted and flawless content on the given topic.

                                  Topic: {topic}"""),
        agent=writer_agent(topic)
    )

# Streamlit UI
st.header("Multi-Agent Chat System")

# Initialize chat history
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

# Function to format messages with HTML
def format_message(role, message):
    if role == 'User':
        return f"<div style='padding:10px;'><b style='color: #007bff;'>User:</b> {message}</div>"
    elif role == 'System':
        return f"<div style='padding:10px;'><b style='color: #dc3545;'>System:</b> {message}</div>"

# Display chat history
for entry in st.session_state.chat_history:
    st.markdown(format_message(entry['role'], entry['message']), unsafe_allow_html=True)

# Input and buttons
topic = st.text_input("Enter the topic:")

col1, col2 = st.columns([2, 1])

with col1:
    if st.button("Generate") and topic:
        # Add user input to chat history
        st.session_state.chat_history.append({'role': 'User', 'message': topic})

        # Create and run Crew with agents and tasks
        crew = Crew(
            agents=[web_researcher_agent(topic), writer_agent(topic)],
            tasks=[web_researcher_task(topic), writer_task(topic)],
            verbose=True,
        )
        result = crew.kickoff()

        # Add system response to chat history
        st.session_state.chat_history.append({'role': 'System', 'message': result})

        # Display updated chat history
        for entry in st.session_state.chat_history:
            st.markdown(format_message(entry['role'], entry['message']), unsafe_allow_html=True)

with col2:
    if st.button("Clear Chat"):
       st.session_state.chat_history = []
       st.session_state['dummy_var'] = not st.          session_state.get('dummy_var', False)