File size: 24,906 Bytes
af9408a
 
 
 
 
 
 
 
 
035ad05
 
 
 
 
390d742
48129e9
 
b05c4ca
67a469e
 
48129e9
 
 
 
 
af9408a
 
 
23d01e6
af9408a
035ad05
7a9c7fe
035ad05
af9408a
035ad05
af9408a
 
b90d069
af9408a
 
035ad05
48129e9
035ad05
48129e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a9587f
48129e9
 
e656d6e
48129e9
 
 
67a469e
 
 
48129e9
4f47415
035ad05
 
4f47415
035ad05
 
 
 
 
4f47415
035ad05
 
 
f4fd216
 
 
035ad05
 
 
 
 
 
 
b594666
5444866
035ad05
 
 
 
 
5444866
035ad05
 
 
5444866
035ad05
 
 
5444866
035ad05
 
 
 
 
 
 
4f47415
77946a4
035ad05
 
 
67a469e
035ad05
77946a4
67a469e
48129e9
 
67a469e
c8e2f88
 
 
67a469e
 
77946a4
 
48129e9
035ad05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9408a
4f47415
035ad05
 
af9408a
035ad05
 
 
 
 
 
 
 
2abdfa5
 
 
 
e11fc23
2abdfa5
035ad05
1545f62
 
 
 
 
 
 
 
 
 
48129e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67a469e
 
 
 
 
 
 
 
 
 
 
48129e9
 
 
 
 
 
 
 
 
 
af9408a
4f47415
 
 
c8e2f88
af9408a
c8e2f88
7a9c7fe
1545f62
67a469e
 
 
77946a4
 
 
af9408a
48129e9
 
 
af9408a
4f47415
af9408a
77946a4
 
390d742
77946a4
 
390d742
f76934b
48129e9
 
 
035ad05
390d742
7721a62
 
 
035ad05
48129e9
67a469e
48129e9
 
035ad05
 
 
 
 
 
7721a62
 
 
 
 
67a469e
 
 
 
7721a62
 
f4fd216
 
 
 
 
 
 
 
 
 
 
390d742
77946a4
390d742
67a469e
d3245c3
f4fd216
 
773d31d
67a469e
48129e9
67a469e
48129e9
a7d541f
d3245c3
67a469e
 
d3245c3
67a469e
d3245c3
035ad05
67a469e
390d742
48129e9
 
67a469e
2e6c125
48129e9
 
 
67a469e
 
 
 
 
 
 
 
c8e2f88
67a469e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8e2f88
67a469e
48129e9
2e6c125
5444866
ec8cd19
035ad05
 
5444866
035ad05
5444866
390d742
 
67a469e
c8e2f88
 
 
035ad05
c8e2f88
5444866
48129e9
cc07797
48129e9
5444866
48129e9
 
 
 
 
 
5444866
 
 
 
 
 
 
 
 
 
 
48129e9
5444866
 
 
 
 
 
 
 
 
48129e9
 
 
 
7f7f148
5444866
 
 
48129e9
5444866
 
 
48129e9
 
67a469e
 
 
c6f292a
 
 
e11fc23
 
f63d7ba
 
e11fc23
2a73dc5
 
 
f63d7ba
c8e2f88
2a73dc5
 
 
e11fc23
c6f292a
e11fc23
d462e1a
952ca4e
e2fe64b
e11fc23
 
 
 
 
 
 
 
 
 
c8e2f88
e11fc23
4f47415
 
 
b90d069
5d4faf8
4f47415
 
48129e9
4f47415
 
 
 
 
af9408a
77946a4
af9408a
 
 
 
 
c8e2f88
 
af9408a
4f47415
 
 
 
 
67a469e
 
 
 
 
 
 
 
 
 
 
 
 
 
c8e2f88
 
 
fda10d4
c8e2f88
 
 
fda10d4
c8e2f88
 
 
 
 
 
 
 
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
from fastembed.sparse.bm25 import Bm25
from fastembed.late_interaction import LateInteractionTextEmbedding
from pydantic import BaseModel, Field
from langchain_core.load import dumpd, dumps, load, loads
from langchain.schema import Document

dense_embedding_model = import_embedding()

late_interaction_embedding_model = LateInteractionTextEmbedding("colbert-ir/colbertv2.0")
bm25_embedding_model = Bm25("Qdrant/bm25", language="turkish")

AZURE_OPENAI_KEY = os.getenv('azure_api')
os.environ['AZURE_OPENAI_KEY'] = AZURE_OPENAI_KEY
openai.api_version = "2024-08-01-preview" # change it with your own version
openai.azure_endpoint = os.getenv('azure_endpoint')

model = "gpt-4o-mini" # 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(client, query, collection_name, CAR_ID):

    dense_query_vector = list(dense_embedding_model.embed_documents([query]))[0]
    sparse_query_vector = list(bm25_embedding_model.query_embed(query))[0]
    late_query_vector = list(late_interaction_embedding_model.query_embed(query))[0].tolist()
    
    prefetch = [
        models.Prefetch(
            query=dense_query_vector,
            using="sfr-mistral",
            limit=30,
            ),
        models.Prefetch(
            query=models.SparseVector(**sparse_query_vector.as_object()),
            using="bm25",
            limit=30,
            ),
        models.Prefetch(
            query=late_query_vector,
            using="colbertv2.0",
            limit=30,
            ),
            ]
    
    results = client.query_points(
        collection_name,
        prefetch=prefetch,
        query=models.FusionQuery(
            fusion=models.Fusion.RRF,
        ),
        with_payload=True,
        query_filter=models.Filter(
                must=[
                    models.FieldCondition(key="car_id", match=models.MatchValue(value=CAR_ID))
                    ]),
                  
        limit=10,
    )
    # retrieved_chunks = [doc.payload for doc in results.points]

    retrieved_chunks = [Document(metadata=doc.payload["metadata"], page_content=doc.payload["page_content"]) for doc in results.points]
    return retrieved_chunks

## 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,
    tool_choice="required",
    temperature=0.0
)
    return completion

retrieval_functions = [
    {
        "type": "function",
        "function":{
            "name": "get_section_titles",
            "description": """Use this function to get the section, subsection and subsubsection titles from a user manual table of content.""",
            "parameters": {
                "type": "object",
                "properties": {
                    "section_title": {
                        "type": "string",
                        "description": "Title of the section in the table of content",
                    },
                    "sub_section_title": {
                        "type": "string",
                        "description": "Title of the subsection in the table of content",
                    },
                    "sub_sub_section_title": {
                        "type": "string",
                        "description": "Title of the subsubsection in the table of content",
                    }
                },
                "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:
        content = doc_section_content["TableOfContents"][section_title][sub_section_title][sub_sub_section_title]["content"]
        pages = doc_section_content["TableOfContents"][section_title][sub_section_title][sub_sub_section_title]["pages"]

        response = Document(metadata={"page": pages,
                                "section_title": section_title, 
                                "sub_section_title": sub_section_title, 
                                "sub_sub_section_title": sub_sub_section_title}, 
                                page_content = 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

class GradeDocuments(BaseModel):
    """Binary score for relevance check on retrieved documents."""

    binary_score: str = Field(description="Documents are relevant to the question, 'yes' or 'no'")

def grade_document_with_openai(document: str, question: str) -> GradeDocuments:
    system_message = """
    You are a grader assessing relevance of a retrieved document to a user question. 
    Consider the following when making your assessment: 
    - Does the document directly or indiretly address the user's question? 
    - Does it provide information or context that is pertinent to the question? 
    - Does it discuss relevant risks, benefits, recommendations, or considerations related to the question? 
    If the document contains keyword(s) or semantic meaning related or partially related to the question, grade it as relevant. 
    Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question. 
    """
    # """
    # You are a grader assessing relevance of a retrieved document to a user question.
    # Consider the following when making your assessment:
    #     - Does the documents encapsulate the car related feature mentioned the question?
    #     - Does the documents directly or indirectly address the user's question?
    #     - Does it provide information or context that is pertinent to the question?
    #     - Does it discuss relevant risks, benefits, recommendations, or considerations related to the question?
    # If the documents contains keyword(s) or semantic meaning related or partially related to the question, grade it as relevant
    # Give a binary score 'yes' or 'no' to indicate whether the documents is relevant to the question.
    # """
    
    response = client.chat.completions.create(
            model=model, 
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": f'Retrieved document: \n\n {document} \n\n User question: {question}'}
            ]
        )

    score = response.choices[0].message.content
    return GradeDocuments(binary_score=score.strip())
    
liked_state = gr.State(None)
last_interaction = gr.State(None)

def chat(question, manual, history, liked, graded_doc_history):
    history = history or []
    graded_doc_history = graded_doc_history or []
    
    conv = format_chat_prompt(history)
    # print("History: ", history) 
    # print("CONV: ", conv)
    # print("Graded_doc_history: ", graded_doc_history) 
    manual_list = {"Toyota_Corolla_2024_TR": -8580416610875007536, 
                   "Renault_Clio_2024_TR":-5514489544983735006, 
                   "Fiat_Egea_2024_TR":-2026113796962100812}
    
    collection_list = {"Toyota_Corolla_2024_TR": "HYBRID_TOYOTA_MANUAL_COLLECTION_EMBED3", 
                    "Renault_Clio_2024_TR": "HYBRID_RENAULT_MANUAL_COLLECTION_EMBED3", 
                    "Fiat_Egea_2024_TR": "HYBRID_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))

    start_time = time.time()

    for i in range(3):
        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- Sorulan soru bir arızaya işaret ediyor olabilir mi?
        4- Bu konu başlıkları kullanım kılavuzu içindekiler tablosu başlıkları ile alakalı mı?
        5- 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)
        
        first_hop_response = chat_gpt(prompt=f"Soru: {question}", 
                                    history=[{"role": "system", "content": f"{first_hop}"}], 
                                    tools=retrieval_functions)

        tool_calls = first_hop_response.choices[-1].message.tool_calls
        arg_list = []
    
        if tool_calls:
            for tool_call in tool_calls:
              function_name = tool_call.function.name
              args = json.loads(tool_call.function.arguments)
              arg_list.append(args)
              print(f"Will call {function_name} with arguments {args}")
            break
        else:
            print("No tool calls")
      
    print("First_hop:--- %s seconds ---" % (time.time() - start_time))
    
    path = "Contents/" + manual + ".json"

    start_time = time.time()
    documents = []
    for i in arg_list:
        documents.append(get_section_content(str(i['section_title']), str(i['sub_section_title']), str(i['sub_sub_section_title']), content_path=path))
        # break  

    # print("Documents:", documents)  
    CAR_ID = manual_list[manual]
    
    documents_db = retriever_db(qclient, question, collection_name, CAR_ID)

    for i in documents_db: 
        documents.append(i)
           
    print("Retriever:--- %s seconds ---" % (time.time() - start_time))

    # print("Documents: ", documents)
    start_time = time.time()
    graded_doc = []
    for r in documents:
    
        grade = grade_document_with_openai(r, question)
    
        if grade.binary_score == "yes":
            graded_doc.append(r)
            # graded_doc_history.append(r) 
            print(r.metadata["page"])  

    print("GRADED DOC:", graded_doc)   
    print("GRADED DOC HISTORY", graded_doc_history)  
    # if not graded_doc:
    #     documents = []
    #     CAR_ID = manual_list[manual]
    
    #     documents_db = retriever_db(qclient, question, collection_name, CAR_ID)
        
    #     for i in documents_db:
    #         documents.append(i)
        
    #     for r in documents:
        
    #         grade = grade_document_with_openai(r, question)
        
    #         if grade.binary_score == "yes":
    #             graded_doc.append(r)
    #             graded_doc_history.append(r)

    print("Onaylanan Doküman Sayısı:", len(graded_doc))
    # print(graded_doc_history)       
    print("Grade Doc:--- %s seconds ---" % (time.time() - start_time))
    

    start_time = time.time()
    if get_lead_result(question).lower() == 'yes':
        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.
        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. Sorunun tipine göre OtoBakım servislerini ziyaret edebileceklerini söylemelisin.' 
        print('Lead: no')
    print("Lead Check:--- %s seconds ---" % (time.time() - start_time))

    start_time = time.time()

    marka = manual.split("_")[0]
    model = manual.split("_")[1]
    yıl = manual.split("_")[2]
    prompt = f"""
    Sen, yalnızca {yıl} {marka} {model} marka araba ile ilgili sorunlara 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 eğer dokümanlarda (yani kullanım kılavuzunda) yeterli bilgi varsa doğru, yardımcı, net ve yorum yapmadan cevaplar vermektir.
    Temel nezaket etkileşimlerine uygun ve kibar bir şekilde yanıt vermek için tasarlandın.
    
    Soruları yanıtlarken aşağıdaki adımları izle: \n
        - Dokümanlar soruyla ilgiliyse, soruyu yanıtlamak için dokümanlardan yararlan. 
        - Sorulara cevap verirken sana sağlanan bilgilerdeki uyarılara, tehlikelere vurgu yap ve öne çıkar.
        - Soruları yanıtlarken yorum yapma, kişisel görüşlerini belirtme ve önceki bilgilerini kullanma.
        - Dokümandakiler dışında terim ve bilgileri kullanma.
        - Eğer dokümanlarda bir işlemin nasıl yapıldığı adım adım anlatılıyorsa, bu adımları direkt şekilde ekle.
        - Kullanıcıya doğrudan cevap ver.
        - Cevaplar kısa ama anlamlı ve yeterli olsun. 
        - Cevabın sonunda kullanılan kaynakları listelerken şu kurallara uy:
            1. Başlık yapısı tamamen aynı olan kaynakları (section_title -> sub_section_title -> sub_sub_section_title) tek bir referans altında birleştir.
            2. Başlık boş olsa bile mutlaka referansta göster
            3. Boş başlıklar için -> işaretinden sonra boşluk bırak
            3. Her başlık yapısı sadece bir kez yazılmalı
            4. Aynı başlığa ait tüm sayfa numaralarını tek bir "Sayfa:" satırında göster
            5. Sayfa numaralarını şu kurallara göre formatla:
                - Ardışık sayfalar için tire kullan (örn: **-**)
                - Ardışık olmayan sayfalar için virgül kullan (örn: **, **, **)
                - Tekrar eden sayfa numaralarını eleme
            6. Format şu şekilde olmalı:
                Kaynaklar:
                a. [section_title -> sub_section_title -> sub_sub_section_title] // dolu başlık örneği
                    Sayfa: X-Z ya da A, B, ...  
                b. Sayfa: K, L, ...  // boş başlık örneği       
                .
                .
                .

    Ek yönerge: {lead}

    Son Kontrol:
        - Cevabın doğruluğunu ve tamlığını kontrol et.
        - Gereksiz bilgi veya yorum olup olmadığını kontrol et.
        - Referansların doğru eklendiğinden emin ol.

    Sorulan soru bir önceki soru ile bağlantılı olabilir. Bu durumda önceki soruyla yeni soruyu beraber değerlendir.
    Kullanıcı detaylı bilgi, daha çok, başka vb. ifadeler kullanmışsa, daha detaylı bilgi ver.
    Eğer Dokümanlar boş ise ve Geçmiş Dokümanlar soru ile alakasızsa: sorulan soruyla uyumlu bir şekilde, kullanım kılavuzunda bu bilginin olmadığını, soruyu neden yanıtlayamadığını belirt ve soru hakkında yorum yapma.
    Soru çok genel ise, spesifik bilgi iste.

    Geçmiş Dokümanlar: {graded_doc_history}
    Dokümanlar: {graded_doc}
    """

    for r in graded_doc:
        graded_doc_history.append(r) 

    conv.append({"role": "system", "content": f"{prompt}"})
    final_response = chat_gpt_nofn(prompt=f"Soru: {question}", history=conv)
    # final_response = chat_gpt_nofn(prompt=prompt, history=conv)
    
    partial_response = ""
    print("Answer:--- %s seconds ---" % (time.time() - start_time))
    
    for chunk in final_response:
        try:
            if chunk.choices[0].delta.content is not None:
                partial_response += chunk.choices[0].delta.content
        
                yield partial_response, history + [(question, partial_response)], graded_doc_history
        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, graded_doc_history

def save_last_interaction(feedback):
    if last_interaction.value:
        DatabaseOperations.save_user_history_demo(
            qclient,
            "USER_COLLECTION_EMBED3_v3",
            last_interaction.value["question"],
            last_interaction.value["response"],
            dense_embedding_model,
            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()
    graded_doc_history = gr.State([])
    clear = gr.ClearButton(components=[textbox, chatbot, graded_doc_history], 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, graded_doc_history):
    #     save_last_interaction("N/A")  # Save previous interaction before starting a new one        
    #     chat_generator = chat(question, manual, history, liked_state.value, graded_doc_history)
    #     final_response = ""
    #     final_history = history

    #     for partial_response, updated_history, doc_history in chat_generator:
    #         final_response += partial_response
    #         final_history = updated_history
    #         yield "", final_history
    #     return "", final_history
    
    def gradio_chat(question, manual, history, graded_doc_history):
        save_last_interaction("N/A")
        chat_generator = chat(question, manual, history, liked_state.value, graded_doc_history)
        final_history = history
        final_doc_history = graded_doc_history or []
        
        for partial_response, updated_history, doc_history in chat_generator:
            final_history = updated_history
            final_doc_history = doc_history
            yield "", final_history, final_doc_history
        
        return "", final_history, final_doc_history

    textbox.submit(gradio_chat, 
                  [textbox, manual, chatbot, graded_doc_history], 
                  [textbox, chatbot, graded_doc_history])
    chatbot.like(handle_like, None, None)
demo.queue()    
demo.launch()