forked from nishadika1994/crs-agent-bricks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crs_chatbot.py
260 lines (218 loc) Β· 8.04 KB
/
crs_chatbot.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# main.py
import openai
from openai_config import API_KEY, WEBSITE_CONTENT
from autogen import AssistantAgent, GroupChatManager, UserProxyAgent, config_list_from_json
from autogen.agentchat import GroupChat
import streamlit as st
import re
import autogen
import panel as pn
import json
# Set OpenAI API key
openai.api_key = API_KEY
my_variable = ""
config_list = [
{
"api_base": "http://localhost:1234/v1",
"api_type": "open_ai",
"api_key": "sk-",
}
]
config_list = config_list_from_json(
env_or_file="OAI_CONFIG_LIST.json",
file_location=".",
)
llm_config = {"config_list": config_list, "seed": 42, "request_timeout": 600,
"temperature": 0,}
def extract_links_and_images(text):
# Extract URLs from the text using a simple regex
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
return urls
def is_question_about_website(user_input):
# Add keywords that indicate a question related to the website
website_keywords = ["features", "information", "details", "content", "product"]
# Check if any of the keywords are present in the user's input
return any(keyword in user_input.lower() for keyword in website_keywords)
admin = UserProxyAgent(
name="admin",
human_input_mode="ALWAYS",
system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""",
llm_config=llm_config,
code_execution_config=False,
)
Marketing = AssistantAgent(
name="Marketing",
llm_config=llm_config,
system_message="Marketing. Develop and implement effective strategies to promote and market our SaaS product based on market trends and customer needs."
)
Sales = AssistantAgent(
name="Sales",
llm_config=llm_config,
system_message="Sales. Implement approved marketing and sales strategies to attract customers and drive revenue for our SaaS solution."
)
Planner = AssistantAgent(
name="Planner",
llm_config=llm_config,
system_message="""Planner. Develop comprehensive plans for marketing, sales, and product development. Iterate based on feedback from admin, critic, and teams involved. Clearly outline tasks for Marketing, Sales, Product, and address feedback effectively."""
)
Product = AssistantAgent(
name="Product",
llm_config=llm_config,
system_message="Product. Ensure the accurate implementation of specifications for the SaaS-based product, adhering to approved plans and meeting customer expectations."
)
Builder = AssistantAgent(
name="Builder",
llm_config=llm_config,
system_message="""Builder. Focus on the technical aspects of the SaaS product. Collaborate with the Product team to ensure the implementation aligns with technical specifications. Provide insights into the development process, technology stack, and address technical challenges."""
)
critic = AssistantAgent(
name="critic",
system_message="""critic. Thoroughly review plans and claims from other agents. Offer constructive feedback to enhance the quality of marketing, sales, product, and technical strategies. Ensure plans include verifiable information and reliable sources.""",
llm_config=llm_config,
)
groupchat = GroupChat(
agents=[Sales,Marketing,Product,Planner,critic],
messages=[],
max_round=500,
)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)
avatar = {admin.name:"π¨βπΌ", Marketing.name:"π©βπ»", Sales.name:"π©βπ¬", Planner.name:"π", Product.name:"π ", critic.name:'π'}
def ask_question(prompt):
# Make an API call to OpenAI GPT-3
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=150
)
print(response) # Print the raw response for debugging
answer = response.choices[0].text.strip()
answer = answer.lstrip('\n')
# answer = response
# answer = re.sub(r'/\w+ ', '/', answer)
# Extract links and images from the answer
media_urls = extract_links_and_images(answer)
if media_urls:
answer += "\n" + "\n".join([f'<a href="{url}" target="_blank">{url}</a>' for url in media_urls])
return answer
def main():
# Streamlit CSS
st.markdown(
"""
<style>
.chat-container {
max-width: 400px;
max-hight: 300px;
margin: 50px auto;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
background-color: #f4f4f4;
flex-grow: 1;
}
.chat-header {
background-color: #c41212;
color: #fff;
padding: 10px;
text-align: center;
font-size: 20px;
font-weight: 10px;
}
.chat-messages {
max-height: 300px;
overflow-y: auto;
padding: 10px;
}
.message {
margin-bottom: 10px;
}
.message.sender {
text-align: right;
}
.message.sender .message-body {
background-color: #c41212;
color: #fff;
border-radius: 10px;
padding: 8px 12px;
display: inline-block;
}
.message.receiver .message-body {
background-color: #ddd;
border-radius: 10px;
padding: 8px 12px;
display: inline-block;
}
.message-body {
max-width: 70%;
word-wrap: break-word;
}
.input-container {
display: flex;
justify-content: flex-end;
align-items: center;
position: fixed;
bottom: 0;
left: 30%;
width: 40%;
padding: 10px;
background-color: #eee;
border-top: 1px solid #ccc;
}
.input-container input {
width: 80%;
padding: 8px;
box-sizing: border-box;
border: none;
border-radius: 5px;
}
.input-container button {
width: 18%;
padding: 8px;
box-sizing: border-box;
border: none;
border-radius: 5px;
background-color: #c41212;
color: #fff;
cursor: pointer;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
<div class="chat-header">CSR Virtual Assistant</div>
<div class="chat-messages">
""",
unsafe_allow_html=True,
)
user_input = st.chat_input("Say something")
# Initialize session state to store the conversation history
if "conversation_history" not in st.session_state:
st.session_state.conversation_history = []
if user_input:
# Concatenate user's input with the previous conversation
prompt = "\n".join([interaction['text'] for interaction in st.session_state.conversation_history])
prompt += f"\nUser: {user_input}\nWebsite Content: {WEBSITE_CONTENT}"
# Ask a question using OpenAI GPT-3
answer = ask_question(prompt)
# Store the conversation history
st.session_state.conversation_history.append({"speaker": "You", "text": user_input})
st.session_state.conversation_history.append({"speaker": "Assistant", "text": answer})
# Clear user_input after processing by setting it to an empty string
user_input = ""
# Display the conversation messages
for interaction in st.session_state.conversation_history:
if interaction['speaker'] == "You":
st.markdown(f'<div class="message sender"><div class="message-body">You: {interaction["text"]}</div></div>', unsafe_allow_html=True)
elif interaction['speaker'] == "Assistant":
st.markdown(f'<div class="message receiver"><div class="message-body">Assistant: {interaction["text"]}</div></div>', unsafe_allow_html=True)
st.markdown(
"""
</div>
</div>
""",
unsafe_allow_html=True,
)
if __name__ == "__main__":
main()