forked from Avaiga/demo-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (241 loc) · 9.28 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os
import sys
import time
from taipy.gui import Gui, State, notify
import openai
import ollama
import chromadb
from dotenv import load_dotenv
chromadb_client = None
ollama_client = None
collection = None
client = None
context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI assistant. How can I help you today? "
conversation = {
"Conversation": ["Who are you?", "Hi! I am JanSamvad AI assistant. How can I help you today?"]
}
current_user_message = ""
past_conversations = []
selected_conv = None
selected_row = [1]
def on_init(state: State) -> None:
"""
Initialize the app.
Args:
- state: The current state of the app.
"""
state.context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI assistant. How can I help you today? "
state.conversation = {
"Conversation": ["Who are you?", "Hi! I am JanSamvad AI assistant. How can I help you today?"]
}
state.current_user_message = ""
state.past_conversations = []
state.selected_conv = None
state.selected_row = [1]
state.collection = state.chromadb_client.get_collection(name="docs")
def request(state: State, prompt: str) -> str:
"""
Send a prompt to the JanSamvad AI assistant API and return the response.
Args:
- state: The current state of the app.
- prompt: The prompt to send to the API.
Returns:
The response from the API.
"""
response = state.client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"{prompt}",
}
],
model="gpt-4-turbo-preview",
)
return response.choices[0].message.content
def retrieve_context(state: State, prompt: str) -> str:
# generate an embedding for the prompt and retrieve the most relevant doc
prompt_embedding = state.ollama_client.embeddings(
prompt=prompt,
model='phi:latest'
)
# get relevant document
rag_contexts = state.collection.query(
query_embeddings=[prompt_embedding["embedding"]],
n_results=2
)
# build the rag context string
rag_context_str = "\n".join(rag_contexts['documents'][0])
return rag_context_str
def ollama_request(state: State, prompt: str) -> str:
"""
Send a prompt to the ollama API and return the response.
Args:
- state: The current state of the app.
- prompt: The prompt to send to the API.
Returns:
The response from the API.
"""
# retrieve the rag context first for the input prompt
rag_context_str = retrieve_context(state, prompt)
#ollama_client = ollama.Client(host='http://ollama:11434')
response = state.ollama_client.chat(
messages=[
{
'role': 'user',
'content': f"Using this data: {rag_context_str}. Respond to this prompt: {prompt}",
}
],
model='phi:latest',
stream=False
)
answer = response['message']['content'].replace("\n", "")
if answer in ['', ' ']:
answer = "Please add more specific detail about the query!"
return answer
# send message v2 version for streaming
def send_message_v2(state: State) -> None:
notify(state, "info", "Sending message and streaming...")
# update context mehtod
state.context += f"Human: \n {state.current_user_message}\n\n AI:"
prompt = state.context
# answer = request(state, state.context).replace("\n", "")
rag_context_str = retrieve_context(state, prompt)
# use the rag context generate llm output in stream fashion
stream = state.ollama_client.chat(
messages=[
{
'role': 'user',
'content': f"Using this data: {rag_context_str}. Respond to this prompt: {prompt}",
}
],
model='phi:latest',
stream=True
)
# using stream generator stream the result
conv = state.conversation._dict.copy()
# append current user message and empty string as last message as dummy
conv["Conversation"] += [state.current_user_message, '']
answer = ""
for chunk in stream:
msg_chunks = chunk['message']['content']
answer += msg_chunks
#print(msg_chunks, end='', flush=True)
conv["Conversation"][-1] = [answer] # update the last message with the stream chunk message
state.context += answer
state.selected_row = [len(state.conversation["Conversation"]) + 1]
# update contex is complete
# set the current user message to empty string
state.current_user_message = ""
state.conversation = conv
notify(state, "success", "Stream response received!")
def update_context(state: State) -> None:
"""
Update the context with the user's message and the AI's response.
Args:
- state: The current state of the app.
"""
state.context += f"Human: \n {state.current_user_message}\n\n AI:"
# answer = request(state, state.context).replace("\n", "")
answer = ollama_request(state, state.context)
state.context += answer
state.selected_row = [len(state.conversation["Conversation"]) + 1]
return answer
def send_message(state: State) -> None:
"""
Send the user's message to the API and update the context.
Args:
- state: The current state of the app.
"""
notify(state, "info", "Sending message...")
answer = update_context(state)
conv = state.conversation._dict.copy()
conv["Conversation"] += [state.current_user_message, answer]
state.current_user_message = ""
state.conversation = conv
notify(state, "success", "Response received!")
def style_conv(state: State, idx: int, row: int) -> str:
"""
Apply a style to the conversation table depending on the message's author.
Args:
- state: The current state of the app.
- idx: The index of the message in the table.
- row: The row of the message in the table.
Returns:
The style to apply to the message.
"""
if idx is None:
return None
elif idx % 2 == 0:
return "user_message"
else:
return "gpt_message"
def on_exception(state, function_name: str, ex: Exception) -> None:
"""
Catches exceptions and notifies user in Taipy GUI
Args:
state (State): Taipy GUI state
function_name (str): Name of function where exception occured
ex (Exception): Exception
"""
notify(state, "error", f"An error occured in {function_name}: {ex}")
def reset_chat(state: State) -> None:
"""
Reset the chat by clearing the conversation.
Args:
- state: The current state of the app.
"""
state.past_conversations = state.past_conversations + [
[len(state.past_conversations), state.conversation]
]
state.conversation = {
"Conversation": ["Who are you?", "Hi! I am JanSamvad AI assistant. How can I help you today?"]
}
def tree_adapter(item: list) -> [str, str]:
"""
Converts element of past_conversations to id and displayed string
Args:
item: element of past_conversations
Returns:
id and displayed string
"""
identifier = item[0]
if len(item[1]["Conversation"]) > 3:
return (identifier, item[1]["Conversation"][2][:50] + "...")
return (item[0], "Empty conversation")
def select_conv(state: State, var_name: str, value) -> None:
"""
Selects conversation from past_conversations
Args:
state: The current state of the app.
var_name: "selected_conv"
value: [[id, conversation]]
"""
state.conversation = state.past_conversations[value[0][0]][1]
state.context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI assistant. How can I help you today? "
for i in range(2, len(state.conversation["Conversation"]), 2):
state.context += f"Human: \n {state.conversation['Conversation'][i]}\n\n AI:"
state.context += state.conversation["Conversation"][i + 1]
state.selected_row = [len(state.conversation["Conversation"]) + 1]
past_prompts = []
page = """
<|layout|columns=300px 1|
<|part|class_name=sidebar|
# JanSamvad **AI**{: .color-primary} # {: .logo-text}
<|New Conversation|button|class_name=fullwidth plain|id=reset_app_button|on_action=reset_chat|>
### Previous activities ### {: .h5 .mt2 .mb-half}
<|{selected_conv}|tree|lov={past_conversations}|class_name=past_prompts_list|multiple|adapter=tree_adapter|on_change=select_conv|>
|>
<|part|class_name=p2 align-item-bottom table|
<|{conversation}|table|style=style_conv|show_all|selected={selected_row}|rebuild|>
<|part|class_name=card mt1|
<|{current_user_message}|input|label=Write your message here...|on_action=send_message|class_name=fullwidth|change_delay=-1|>
|>
|>
|>
"""
if __name__ == "__main__":
load_dotenv()
client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))
chromadb_client = chromadb.HttpClient(host="chromadb-vecdb", port=8000)
ollama_client = ollama.Client(host='http://ollama:11434')
Gui(page).run(debug=True, dark_mode=True, use_reloader=True, title="💬 Taipy Chat")