Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -33,34 +33,53 @@ HEADERS = {
|
|
33 |
}
|
34 |
|
35 |
|
36 |
-
prompt = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"""
|
|
|
|
|
38 |
|
39 |
messages = [{"role": "system", "content": prompt}]
|
|
|
|
|
|
|
|
|
40 |
chat_history = []
|
41 |
|
42 |
def ask_yc_bot(question):
|
43 |
docs = vectorstore.similarity_search(question)
|
44 |
source_name = docs[0].metadata['source']
|
45 |
context = docs[0].page_content
|
46 |
-
result = "
|
47 |
return result
|
48 |
|
49 |
|
50 |
def clear_f():
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
global messages, chat_history, context
|
53 |
context=""
|
54 |
-
prompt = """
|
55 |
-
Instruction: Твоя роль - кваліфікований співробітник саппорту у системи YouControl.
|
56 |
-
Потрібно відповісти на питання від користувача з огляду на контекст.
|
57 |
-
Контекст ми беремо з бази знань, але вона може бути не повна.
|
58 |
-
Якщо контекст не коректний, то відповідай на свій розсуд або передай запит сапорту, про контекс нічого не пишемо у відповіді.
|
59 |
-
YouControl може писатися по різному: YC,Ю-контрол,Юконтрол, Юконтроль, Юр контроль, ЮК, UControl, Ю-контроль, YOU Kontrol, YouContro.
|
60 |
-
ЗЕД - це зовнішня економічна діяльність
|
61 |
-
"""
|
62 |
messages = []
|
63 |
messages = [{"role": "system", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
chat_history.clear()
|
65 |
return ""
|
66 |
|
@@ -73,16 +92,21 @@ def query_gpt_3_5(messages, context):
|
|
73 |
return completion.choices[0].message.content
|
74 |
|
75 |
def comment_bot(slider_value, comment_text):
|
76 |
-
global messages
|
77 |
-
|
78 |
#Убираем промт из чата
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
print
|
|
|
|
|
|
|
|
|
|
|
83 |
date_d = datetime.datetime.now().date()
|
84 |
date_string = date_d.isoformat()
|
85 |
-
upload_to_airtable_log(date_string, first_element['content'],
|
86 |
|
87 |
return "" # Если функция должна что-то возвращать, замените это на нужный вывод
|
88 |
|
@@ -107,9 +131,11 @@ def upload_to_airtable_log(date, question, answer, rating, comment):
|
|
107 |
|
108 |
def respond(message, chat_history):
|
109 |
global messages
|
110 |
-
|
|
|
111 |
user_message_dict = [{"role": "user", "content": message_rag}]
|
112 |
messages.extend (user_message_dict)
|
|
|
113 |
#удаляем сообщения если, если весь диалог больше 7500, оставляем нулевой элемент массива, так как там инструкция
|
114 |
while sum(len(message["content"]) for message in messages) >= 7500:
|
115 |
if len(messages) > 1:
|
@@ -120,9 +146,10 @@ def respond(message, chat_history):
|
|
120 |
chat_history.append((message, bot_message))
|
121 |
#print("chat_histori", chat_history)
|
122 |
|
123 |
-
bot_message_dict = [{"role": "assistant", "content": bot_message}]
|
124 |
|
125 |
messages.extend (bot_message_dict)
|
|
|
126 |
print("messages", messages)
|
127 |
print ("message_rag=", message_rag)
|
128 |
print ("==========================")
|
@@ -152,7 +179,7 @@ with gr.Blocks(css=css) as demo:
|
|
152 |
radio = gr.Radio(label="Рейтинг відповіді", choices=["Нема", "1", "2", "3", "4", "5"], value="Нема")
|
153 |
comment = gr.Textbox(lines=2, label = "Коментар")
|
154 |
with gr.Row():
|
155 |
-
b2 = gr.ClearButton([radio, comment], value="Прокоментувати чат")
|
156 |
|
157 |
clear.click(clear_f)
|
158 |
submit_button.click(respond, [msg, chatbot], [msg, chatbot, context])
|
@@ -163,4 +190,4 @@ with gr.Blocks(css=css) as demo:
|
|
163 |
#b2.click(clear_f)
|
164 |
|
165 |
|
166 |
-
demo.launch(debug=True)
|
|
|
33 |
}
|
34 |
|
35 |
|
36 |
+
prompt = """
|
37 |
+
Instruction:
|
38 |
+
Твоя роль - кваліфікований співробітник саппорту у системи YouControl.
|
39 |
+
Потрібно відповісти на повідомлення від користувача з огляду на поле "мої знання".
|
40 |
+
Якщо поле "мої знання" не відповідає повідомленню, то відповідай тільки одним словом - "support".
|
41 |
+
Це дуже важливо для мого здоров'я. Питання життя і смерті.
|
42 |
+
|
43 |
+
YouControl може писатися по різному: YC,Ю-контрол,Юконтрол, Юконтроль, Юр контроль, ЮК, UControl, Ю-контроль, YOU Kontrol, YouContro.
|
44 |
+
ЗЕД - це зовнішня економічна діяльність.
|
45 |
+
омпанія YouControl не працює з Росією.
|
46 |
+
|
47 |
"""
|
48 |
+
welcome_text = "Привіт! Що ти хочеш дізнатися про YouControl?"
|
49 |
+
bot_message_dict = [{"role": "assistant", "content": welcome_text}]
|
50 |
|
51 |
messages = [{"role": "system", "content": prompt}]
|
52 |
+
messages.extend (bot_message_dict)
|
53 |
+
|
54 |
+
messages_full = [{"role": "system", "content": prompt}]
|
55 |
+
messages_full.extend (bot_message_dict)
|
56 |
chat_history = []
|
57 |
|
58 |
def ask_yc_bot(question):
|
59 |
docs = vectorstore.similarity_search(question)
|
60 |
source_name = docs[0].metadata['source']
|
61 |
context = docs[0].page_content
|
62 |
+
result = "\nПовідомлення: " + question + "\n" + "Мої знання: " + context + "\n"
|
63 |
return result
|
64 |
|
65 |
|
66 |
def clear_f():
|
67 |
+
global messages, chat_history, context, messages_full, welcome_text, prompt
|
68 |
+
|
69 |
+
#Постим в AirTable результаты
|
70 |
+
comment_bot("Авто", "Авто")
|
71 |
|
|
|
72 |
context=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
messages = []
|
74 |
messages = [{"role": "system", "content": prompt}]
|
75 |
+
bot_message_dict = [{"role": "assistant", "content": welcome_text}]
|
76 |
+
messages.extend (bot_message_dict)
|
77 |
+
|
78 |
+
messages_full = []
|
79 |
+
messages_full = [{"role": "system", "content": prompt}]
|
80 |
+
bot_message_dict = [{"role": "assistant", "content": welcome_text}]
|
81 |
+
messages_full.extend (bot_message_dict)
|
82 |
+
|
83 |
chat_history.clear()
|
84 |
return ""
|
85 |
|
|
|
92 |
return completion.choices[0].message.content
|
93 |
|
94 |
def comment_bot(slider_value, comment_text):
|
95 |
+
global messages, messages_full
|
96 |
+
|
97 |
#Убираем промт из чата
|
98 |
+
result_messages = list(messages_full)
|
99 |
+
first_element = result_messages.pop(0)
|
100 |
+
|
101 |
+
print("Содержимое messages_full \n\n")
|
102 |
+
result_airtable = ""
|
103 |
+
for message in result_messages:
|
104 |
+
print(f"{message['role']}, {message['content']}")
|
105 |
+
result_airtable = result_airtable + message['role'] + message['content']
|
106 |
+
|
107 |
date_d = datetime.datetime.now().date()
|
108 |
date_string = date_d.isoformat()
|
109 |
+
upload_to_airtable_log(date_string, first_element['content'], result_airtable, slider_value, comment_text)
|
110 |
|
111 |
return "" # Если функция должна что-то возвращать, замените это на нужный вывод
|
112 |
|
|
|
131 |
|
132 |
def respond(message, chat_history):
|
133 |
global messages
|
134 |
+
global messages_full
|
135 |
+
message_rag = ask_yc_bot(message) + "\n\n"
|
136 |
user_message_dict = [{"role": "user", "content": message_rag}]
|
137 |
messages.extend (user_message_dict)
|
138 |
+
messages_full.extend (user_message_dict)
|
139 |
#удаляем сообщения если, если весь диалог больше 7500, оставляем нулевой элемент массива, так как там инструкция
|
140 |
while sum(len(message["content"]) for message in messages) >= 7500:
|
141 |
if len(messages) > 1:
|
|
|
146 |
chat_history.append((message, bot_message))
|
147 |
#print("chat_histori", chat_history)
|
148 |
|
149 |
+
bot_message_dict = [{"role": "assistant", "content": " \n" + bot_message + "\n"}]
|
150 |
|
151 |
messages.extend (bot_message_dict)
|
152 |
+
messages_full.extend (bot_message_dict)
|
153 |
print("messages", messages)
|
154 |
print ("message_rag=", message_rag)
|
155 |
print ("==========================")
|
|
|
179 |
radio = gr.Radio(label="Рейтинг відповіді", choices=["Нема", "1", "2", "3", "4", "5"], value="Нема")
|
180 |
comment = gr.Textbox(lines=2, label = "Коментар")
|
181 |
with gr.Row():
|
182 |
+
b2 = gr.ClearButton([radio, comment], value="Прокоментувати чат")
|
183 |
|
184 |
clear.click(clear_f)
|
185 |
submit_button.click(respond, [msg, chatbot], [msg, chatbot, context])
|
|
|
190 |
#b2.click(clear_f)
|
191 |
|
192 |
|
193 |
+
demo.launch(debug=True)
|