Spaces:
Sleeping
Sleeping
File size: 17,468 Bytes
af9408a 035ad05 390d742 af9408a 035ad05 af9408a 035ad05 af9408a b90d069 af9408a 035ad05 4f47415 035ad05 4f47415 035ad05 4f47415 035ad05 4f47415 77946a4 035ad05 77946a4 035ad05 af9408a 4f47415 035ad05 af9408a 035ad05 2abdfa5 e11fc23 2abdfa5 035ad05 1545f62 af9408a 4f47415 af9408a 952ca4e 1545f62 77946a4 af9408a 77946a4 af9408a 4f47415 af9408a 77946a4 390d742 77946a4 390d742 af9408a 390d742 4f47415 af9408a 035ad05 390d742 035ad05 c097cf4 035ad05 390d742 77946a4 390d742 035ad05 77946a4 2552d13 035ad05 77946a4 035ad05 390d742 035ad05 390d742 035ad05 390d742 035ad05 495ffd2 035ad05 d462e1a 035ad05 390d742 035ad05 cc07797 035ad05 77946a4 035ad05 77946a4 035ad05 cc07797 035ad05 aad6296 035ad05 77946a4 035ad05 e11fc23 b03ad18 e11fc23 3ee94b9 390d742 3ee94b9 af9408a e11fc23 2a73dc5 004e928 2a73dc5 e11fc23 d462e1a 952ca4e e11fc23 4f47415 b90d069 035ad05 4f47415 af9408a 77946a4 af9408a 4f47415 017448f 4f47415 4c418a7 d462e1a fda10d4 017448f e11fc23 4f47415 e11fc23 4f47415 af9408a |
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
import uuid
from embedding_loader import *
from initialize_db import QdrantClientInitializer
from pdf_loader import PDFLoader
from IPython.display import display, Markdown
import gradio as gr
from langchain_core.messages import HumanMessage, AIMessage
from langchain.memory import ConversationBufferMemory
from langchain_core.chat_history import InMemoryChatMessageHistory
from qdrant_client import QdrantClient, models
from db_operations import DatabaseOperations
from openai import AzureOpenAI
import json
from qdrant_client.http import models as rest
import time
embeddings = import_embedding()
AZURE_OPENAI_KEY = os.getenv('azure_api')
os.environ['AZURE_OPENAI_KEY'] = AZURE_OPENAI_KEY
openai.api_version = "2024-02-15-preview" # change it with your own version
openai.azure_endpoint = os.getenv('azure_endpoint')
model = "gpt4o" # deployment name on Azure OPENAI Studio
client = AzureOpenAI(azure_endpoint = openai.azure_endpoint,
api_key=AZURE_OPENAI_KEY,
api_version=openai.api_version)
obj_qdrant = QdrantClientInitializer()
qclient = obj_qdrant.initialize_db()
obj_loader = PDFLoader()
# -----
def retriever_db(db, CAR_ID):
retriever = db.as_retriever(search_kwargs={'k': 4}, filter=rest.Filter(
must=[
models.FieldCondition(key="car_id", match=models.MatchValue(value=CAR_ID))
]
))
return retriever
## new version
def chat_gpt(prompt=None, history=[], model=model, client=client, tools=[None]):
if prompt is None:
messages = history
else:
history.append({"role": "user", "content": f"{prompt}"})
messages = history
completion = client.chat.completions.create(
model=model,
messages=messages,
tools=tools
)
return completion
retrieval_functions = [
{
"type": "function",
"function":{
"name": "get_section_content",
"description": """Use this function to get the contents of a particular section of a user manual.""",
"parameters": {
"type": "object",
"properties": {
"section_title": {
"type": "string",
"description": "Title of the section in the user manual",
},
"sub_section_title": {
"type": "string",
"description": "Title of the subsection in the user manual",
},
"sub_sub_section_title": {
"type": "string",
"description": "Title of the subsubsection in the user manual",
}
},
"required": ["section_title", "sub_section_title", "sub_sub_section_title"],
}
}
}
]
def get_section_content(section_title, sub_section_title, sub_sub_section_title, content_path):
with open(content_path, "r") as file:
doc_section_content = json.loads(file.read())
response = None
try:
response = doc_section_content["TableOfContents"][section_title][sub_section_title][sub_sub_section_title]["content"]
except:
pass
return response
def get_lead_result(question):
hizmet_listesi = {"Bakım": """Check-Up, Periyodik Bakım, Aks Değişimi, Amortisör Değişimi, Amortisör Takozu Değişimi, Baskı Balata Değişimi, Benzin Filtresi Değişimi,
Debriyaj Balatası Değişimi, Direksiyon Kutusu Değişimi, Dizel Araç Bakımı, Egzoz Muayenesi, Fren Kaliperi Değişimi, El Freni Teli Değişimi,
Fren Balatası Değişimi, Fren Disk Değişimi, Hava Filtresi Değişimi, Helezon Yay Değişimi, Kampana Fren Balatası Değişimi,
Kızdırma Bujisi Değişimi, Rot Başı Değişimi, Rot Kolu Değişimi, Rotil Değişimi, Silecek Değişimi, Süspansiyon, Triger Kayışı Değişimi,
Triger Zinciri Değişimi, V Kayışı Değişimi, Yağ Filtresi Değişimi, Yakıt Filtresi Değişimi, Havayastığı Değişimi""",
"Yağ ve Sıvılar": """Şanzıman Yağı Değişimi, Dizel Araçlarda Yağ Değişimi, Yağ Değişimi, Fren Hidrolik Değişimi, Antifriz Değişimi,""",
"Akü": """Akü Şarj Etme, Akü Değişimi""",
"Klima": """Oto Klima Kompresörü Tamiri, Oto Klima Tamiri, Araç Klima Temizliği, Araç Klima Bakteri Temizliği, Klima Gazı Dolumu, Klima Dezenfeksiyonu, Polen Filtresi Değişimi""",
"Elektrik": """Servis Uyarı Lambası Sıfırlama,Buji Kablosu Değişimi, Arıza Tespit, Göstergelerin Kontrolü, Far Ayarı ve Ampul Değişimi, Buji Değişimi, Sigorta Değişimi""",
"Lastik/ Jant": """Lastik Jant Satış, Lastik Değişimi, Balans Ayarı, Rot Ayarı, Rotasyon, Lastik Tamiri, Hava Kontrolü, Nitrojen Dolumu, Supap Değişimi, Lastik Saklama (Lastik Oteli), Jant Sökme Takma,""",
"Diğer": """Cam Tamiri""",
"Hibrit Araçlar": "Hibrit Araç Aküsü"}
lead_functions = [
{
"type": "function",
"function": {
"name": "grade_service_relevance",
"description": "Grade the relevance of services to a user question",
"parameters": {
"type": "object",
"properties": {
"binary_score": {
"type": "string",
"description": "Services are relevant to the question, 'yes' or 'no'",
"enum": ["yes", "no"]
}
},
"required": ["binary_score"]
}
}
}
]
# System message
system_message = """Soruyu cevaplarken:
1- Önce soruyu düşün.
2- Kullanıcının sorduğu soru, hizmet listesinde sunulan hizmetlerle alakalı mı?
Alakalı ise "yes", değilse "no" olarak cevap ver."""
def service_grader_relevance(hizmet_listesi: str, question: str) -> dict:
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": f"Provided services: \n\n {hizmet_listesi} \n\n User question: {question}"}
],
tools=lead_functions,
tool_choice={"type": "function", "function": {"name": "grade_service_relevance"}}
)
tool_call = completion.choices[0].message.tool_calls[0]
return json.loads(tool_call.function.arguments)
result = service_grader_relevance(hizmet_listesi, question)
return result['binary_score']
def chat_gpt_nofn(prompt=None, history=[], model=model, client=client):
if prompt is None:
messages = history
else:
history.append({"role": "user", "content": f"{prompt}"})
messages = history
completion = client.chat.completions.create(
model=model,
messages=messages,
stream=True)
return completion
def format_chat_prompt(chat_history):
prompt = []
print(chat_history)
for turn in chat_history:
user_message, ai_message = turn
prompt.append({"role": "user", "content": user_message})
prompt.append({"role": "assistant", "content": ai_message})
return prompt
liked_state = gr.State(None)
last_interaction = gr.State(None)
def chat(question, manual, history, liked):
history = history or []
conv = format_chat_prompt(history)
manual_list = {"Toyota_Corolla_2024_TR": -8580416610875007536,
"Renault_Clio_2024_TR":-5514489544983735006,
"Fiat_Egea_2024_TR":-2026113796962100812}
collection_list = {"Toyota_Corolla_2024_TR": "TOYOTA_MANUAL_COLLECTION_EMBED3",
"Renault_Clio_2024_TR": "RENAULT_MANUAL_COLLECTION_EMBED3",
"Fiat_Egea_2024_TR": "FIAT_MANUAL_COLLECTION_EMBED3"}
collection_name = collection_list[manual]
toc_name = "ToC_" + manual + ".txt"
start_time = time.time()
with open("ToCs/" + toc_name, "r") as file:
content = json.loads(file.read())
print("ToCs:--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
db = obj_loader.load_from_database(embeddings=embeddings, collection_name=collection_name)
print("DB Load:--- %s seconds ---" % (time.time() - start_time))
CAR_ID = manual_list[manual]
retriever = retriever_db(db, CAR_ID)
start_time = time.time()
first_hop = f"""Soruyu cevaplarken:
1- Önce soruyu düşün.
2- Kullanıcının sorduğu sorunun konu başlıkları neler olabilir?
3- Bu konu başlıkları kullanım kılavuzu içindekiler tablosu başlıkları ile alakalı mı?
4- Alakalı olabilecek tüm başlıkları türet.
Buna göre, aşağıda vereceğim kullanım kılavuzu içindekiler tablosu (başlıklar) bilgisini kullanarak bu içeriğe erişmek için uygun fonksiyonları üret.
Eğer herhangi bir içeriğe ulaşamazsan, bunu belir ve sorunun cevabı hakkında yorum yapma.
Kullanım Kılavuzu İçindekiler Tablosu:
{content}
"""
# conv = [{"role": "system", "content": f"{first_hop}"}]
conv.append({"role": "system", "content": f"{first_hop}"})
first_hop_response = chat_gpt(prompt=f"Soru: {question}", history=conv, tools=retrieval_functions)
conv.append(first_hop_response.choices[-1].message)
print("First_hop:--- %s seconds ---" % (time.time() - start_time))
path = "Contents/" + manual + ".json"
start_time = time.time()
i = 0
while(i < 2):
tool_calls = first_hop_response.choices[-1].message.tool_calls
if tool_calls:
for tool_call in tool_calls:
function_name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"Will call {function_name} with arguments {args}")
if function_name == 'get_section_content':
addition = get_section_content(str(args['section_title']), str(args['sub_section_title']), str(args['sub_sub_section_title']), content_path=path)
if not addition:
addition = ' '
conv.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": addition
})
first_hop_response = chat_gpt(prompt=None, history=conv, tools=retrieval_functions)
else:
conv.append(first_hop_response.choices[-1].message)
break
i += 1
print("Second_hop:--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
documents = retriever.invoke(question)
print("Retriever:--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
if get_lead_result(question).lower() == 'yes':
lead = """ Cevabın sonunda, "Nasıl devam edeceğinizden emin değilseniz." ifadesiyle başlayarak, kullanıcılara hızlı ve uygun fiyatlı çözümler için OtoBakım mağazalarını ziyaret etmelerini teklif etmelisin."""
print('Lead: yes')
else:
lead = 'Her zaman tamirci yerine geçen "Servis", "Yetkili Servis", ""... servisi", ""... yetkili servisi", "bakım" gibi ifadeler yerine yalnızca "OtoBakım Servisi" yazmalısın.'
print('Lead: no')
print("Lead Check:--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
prompt = f"""
Sen, yalnızca araba sorunları ve araç bilgilerine odaklanan, ARVI adında uzman bir asistansın.
Amacın, araba sorunları, bakım, onarımlar, teknik özellikler ve diğer araçla ilgili konularla ilgili sorulara doğru, yardımcı ve net cevaplar vermektir.
Ayrıca, temel nezaket etkileşimlerine uygun ve kibar bir şekilde yanıt vermek için tasarlandın. İşte yönergeler:
1. Araba Sorunları ve Araç Bilgileri:
- Araba sorunları, teşhis, onarımlar, bakım ve araç teknik özellikleri ile ilgili soruları her zaman yanıtla.
- Soruları yanıtlarken yorum yapma ve kişisel görüşlerini belirtme.
2. Referanslar:
- Bir soruyu yanıtlarken, eğer aşağıdaki ya da sorunun içindeki içerikten faydalandıysan, cevabın sonunda içeriğin sayfa numarasını ve bölümünü referans olarak ekle.
- Aynı referansı tekrar etme.
Lead: {lead} \n
Dokümanlarda ve sorunun içinde verilen tüm bilgilere dayanarak, aşağıdaki soruyu kısaca yanıtla: \n
Sorulara cevap verirken sana sağlanan bilgilerdeki uyarılara, tehlikelere vurgu yap. \n
Soru: {question} \n
Dokümanlar: {documents}
Elde ettiğin bilgiler soru ile ilgili görünmüyorsa cevap veremeyeceğini belirt.
Kullanıcıya doğrudan cevap ver. \n
Yeni fonksiyon çağırma. \n
Soru çok genel ise, spesifik bilgi iste. \n
Eğer sorunun cevabına ulaşamadıysan, bu soruya cevap veremeyeceğini belirt.
Kesinlikle cevaplar üzerine yorum yapma ve bilgi dağarcığını kullanma
Referans verme örneği:
Ref-1:
Ref-2:
...
"""
#final_response = chat_gpt_nofn(prompt=prompt, history=conv)
#response = final_response.choices[-1].message.content
#conv.append(final_response.choices[-1].message)
#history.append((question, response))
#print("Answer:--- %s seconds ---" % (time.time() - start_time))
# Store the last interaction without saving to the database yet
#last_interaction.value = {
# "question": question,
# "response": response,
# "manual": manual,
# "point_id": uuid.uuid4().hex
#}
#return '', history
final_response = chat_gpt_nofn(prompt=prompt, history=conv)
partial_response = ""
for chunk in final_response:
try:
if chunk.choices[0].delta.content is not None:
partial_response += chunk.choices[0].delta.content
print("Answer:--- %s seconds ---" % (time.time() - start_time))
yield partial_response, history + [(question, partial_response)]
except:
pass
response = partial_response
conv.append({"role": "user", "content": prompt})
conv.append({"role": "assistant", "content": response})
history.append((question, response))
print("Answer:--- %s seconds ---" % (time.time() - start_time))
# Store the last interaction without saving to the database yet
last_interaction.value = {
"question": question,
"response": response,
"manual": manual,
"point_id": uuid.uuid4().hex
}
yield response, history
def save_last_interaction(feedback):
if last_interaction.value:
DatabaseOperations.save_user_history_demo(
qclient,
"USER_COLLECTION_EMBED3_v2",
last_interaction.value["question"],
last_interaction.value["response"],
embeddings,
last_interaction.value["point_id"],
last_interaction.value["manual"],
feedback
)
last_interaction.value = None
manual_list = ["Toyota_Corolla_2024_TR", "Renault_Clio_2024_TR", "Fiat_Egea_2024_TR"]
with gr.Blocks() as demo:
chatbot = gr.Chatbot(height=600)
manual = gr.Dropdown(label="Kullanım Kılavuzları", value="Toyota_Corolla_2024_TR", choices=manual_list)
textbox = gr.Textbox()
clear = gr.ClearButton(components=[textbox, chatbot], value='Clear console')
def handle_like(data: gr.LikeData):
liked_state.value = data.liked
if liked_state.value is not None:
feedback = "LIKE" if liked_state.value else "DISLIKE"
save_last_interaction(feedback)
#def gradio_chat(question, manual, history):
# save_last_interaction("N/A") # Save previous interaction before starting a new one
# return chat(question, manual, history, liked_state.value)
def gradio_chat(question, manual, history):
save_last_interaction("N/A") # Save previous interaction before starting a new one
history.append((question, ""))
yield "", history
chat_generator = chat(question, manual, history, liked_state.value)
final_response = ""
final_history = history
for partial_response, updated_history in chat_generator:
final_response += partial_response
final_history = updated_history
yield "", final_history
return "", final_history
textbox.submit(gradio_chat, [textbox, manual, chatbot], [textbox, chatbot])
chatbot.like(handle_like, None, None)
demo.queue()
demo.launch()
|