File size: 2,456 Bytes
22cff0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#getting replies

import time
import logging
from config import TOP_K, MAX_RETRIEVED_WORDS
from graphs import create_graphs
from vector_store import retrieve_chunks
from summarizer import summarize_text

logger = logging.getLogger(__name__)

def chat_answer(message, history, embedding_model, index, chunks, summarizer):

    try:
        context = " ".join(
            str(h["content"])
            for h in history[-3:]
            if h["role"] == "user"
        )

        # for h in history[-3:]  means loop through each message (h)
        # if h["role"] == "user" means keep only user messages (ignore assistant replies)
        # h["content"] will extract the actual text of the user question
        # then create a list of those questions and joins them

        full_query = context + " " + message

        t1 = time.time()
        retrieved_chunks = retrieve_chunks(
            full_query,
            embedding_model,
            index,
            chunks,
            TOP_K
        )
        t2 = time.time()

        answer = " ".join(retrieved_chunks)
        answer = " ".join(answer.split()[:MAX_RETRIEVED_WORDS])

        summary = summarize_text(answer, summarizer)
        t3 = time.time()

        retrieved_len = len(answer.split())
        summary_len = len(summary.split())

        #to fetch time it takes for evry step in the pipleine
        stage_times = {
            "Retrieve": t2 - t1,
            "Summarize": t3 - t2
        }

        chunk_lengths = [len(c.split()) for c in chunks]

        g1, g2, g3 = create_graphs(
            chunk_lengths,
            retrieved_len,
            summary_len,
            stage_times
        )

        metrics = f"""

                        Retrieved words: {retrieved_len}

                        Summary words: {summary_len}

                        Compression ratio: {round(summary_len / max(retrieved_len,1), 3)}



                        Retrieval time: {round(stage_times['Retrieve'],3)}s

                        Summarization time: {round(stage_times['Summarize'],3)}s

                        """

        return summary, metrics, g1, g2, g3

    except Exception as e:
        logger.exception("Chatbot error")
        return "Error occurred. Please try again.", "", None, None, None #get the 5 things (summary, metrics, g1, g2, g3) from return statement or give None for properly handling error