File size: 5,006 Bytes
0632cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from typing import List, Dict
import faiss
import numpy as np
import pandas as pd
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.websockets import WebSocket

from project.bot.models import MessagePair
from project.config import settings


class SearchBot:
    chat_history = []
    # is_unknown = False
    # unknown_counter = 0

    def __init__(self, memory=None):
        if memory is None:
            memory = []
        self.chat_history = memory

    async def _summarize_user_intent(self, user_query: str) -> str:
        chat_history_str = ''
        chat_history = self.chat_history[-self.unknown_counter * 2:]
        for i in chat_history:
            if i['role'] == 'user':
                chat_history_str += f"{i['role']}: {i['content']}\n"
        messages = [
            {
                'role': 'system',
                'content': f"{settings.SUMMARIZE_PROMPT}\n"
                           f"Chat history: ```{chat_history_str}```\n"
                           f"User query: ```{user_query}```"
            }
        ]
        response = await settings.OPENAI_CLIENT.chat.completions.create(
            messages=messages,
            temperature=0.1,
            n=1,
            model="gpt-3.5-turbo-0125"
        )
        user_intent = response.choices[0].message.content
        return user_intent

    @staticmethod
    def _cls_pooling(model_output):
        return model_output.last_hidden_state[:, 0]

    async def _convert_to_embeddings(self, text_list):
        encoded_input = settings.INFO_TOKENIZER(
            text_list, padding=True, truncation=True, return_tensors="pt"
        )
        encoded_input = {k: v.to(settings.device) for k, v in encoded_input.items()}
        model_output = settings.INFO_MODEL(**encoded_input)
        return self._cls_pooling(model_output).cpu().detach().numpy().astype('float32')

    @staticmethod
    async def _get_context_data(user_query: list[float]) -> list[dict]:
        radius = 30
        _, distances, indices = settings.FAISS_INDEX.range_search(user_query, radius)
        indices_distances_df = pd.DataFrame({'index': indices, 'distance': distances})
        filtered_data_df = settings.products_dataset.iloc[indices].copy()
        filtered_data_df.loc[:, 'distance'] = indices_distances_df['distance'].values
        sorted_data_df: pd.DataFrame = filtered_data_df.sort_values(by='distance').reset_index(drop=True)
        sorted_data_df = sorted_data_df.drop('distance', axis=1)
        data = sorted_data_df.head(3).to_dict(orient='records')
        return data

    @staticmethod
    async def create_context_str(context: List[Dict]) -> str:
        context_str = ''
        for i, chunk in enumerate(context):
            context_str += f'{i + 1}) {chunk["chunks"]}'
        return context_str

    async def _rag(self, context: List[Dict], query: str, session: AsyncSession, country: str):
        if context:
            context_str = await self.create_context_str(context)
            assistant_message = {"role": 'assistant', "content": context_str}
            self.chat_history.append(assistant_message)
            content = settings.PROMPT
        else:
            content = settings.EMPTY_PROMPT
        user_message = {"role": 'user', "content": query}

        self.chat_history.append(user_message)
        messages = [
            {
                'role': 'system',
                'content': content
            },
        ]
        messages = messages + self.chat_history

        stream = await settings.OPENAI_CLIENT.chat.completions.create(
            messages=messages,
            temperature=0.1,
            n=1,
            model="gpt-3.5-turbo",
            stream=True
        )
        response = ''
        async for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                chunk_content = chunk.choices[0].delta.content
                response += chunk_content
                yield response
                await asyncio.sleep(0.02)
        assistant_message = {"role": 'assistant', "content": response}
        self.chat_history.append(assistant_message)
        try:
            session.add(MessagePair(user_message=query, bot_response=response, country=country))
        except Exception as e:
            print(e)

    async def ask_and_send(self, data: Dict, websocket: WebSocket, session: AsyncSession):
        query = data['query']
        country = data['country']
        transformed_query = await self._convert_to_embeddings(query)
        context = await self._get_context_data(transformed_query)
        try:
            async for chunk in self._rag(context, query, session, country):
                await websocket.send_text(chunk)
            # await websocket.send_text('finish')
        except Exception:
            await self.emergency_db_saving(session)

    @staticmethod
    async def emergency_db_saving(session: AsyncSession):
        await session.commit()
        await session.close()