File size: 2,908 Bytes
e16cbb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from crewai import Agent, Task, Crew
from langchain_groq import ChatGroq
import streamlit as st

# Initialize the LLM for the Marketing Research Agent
llm = ChatGroq(
    groq_api_key="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",
    model_name="llama3-70b-8192",  # Replace with the actual Marketing Research model name
)

# Define the Marketing Research Agent with a specific goal
marketing_agent = Agent(
    role='Marketing Research Agent',
    goal='Provide in-depth insights and analysis on marketing trends, strategies, consumer behavior, and market research.',
    backstory=(
        "You are a Marketing Research Agent, skilled in gathering and analyzing information on market trends, "
        "consumer behavior, competitive landscape, and marketing strategies. Your role is to answer marketing-related questions "
        "with a detailed, data-driven approach, and strictly limit responses to marketing research only."
    ),
    verbose=True,
    llm=llm,
)

def process_question_with_agent(question):
    # Describe the task for the agent
    task_description = f"Research and provide a detailed answer to the marketing question: '{question}'"
    
    # Define the task for the agent to generate a response to the question
    research_task = Task(
        description=task_description,
        agent=marketing_agent,
        human_input=False,
        expected_output="Answer related to marketing research"  # Placeholder for expected output
    )

    # Instantiate the crew with the defined agent and task
    crew = Crew(
        agents=[marketing_agent],
        tasks=[research_task],
        verbose=2,
    )

    # Get the crew to work on the task and return the result
    result = crew.kickoff()
    
    return result

# Set the title of your app with Markdown
st.markdown("<h1 style='text-align: center;'>Marketing Research Chatbot</h1>", unsafe_allow_html=True)

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# React to user input
if prompt := st.chat_input("Ask a marketing research question:"):
    # Display user message in chat message container
    st.chat_message("user").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Get the response from the Marketing Research Agent
    with st.spinner("Processing..."):
        response = process_question_with_agent(prompt)
    
    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        st.markdown(response)
    
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})