File size: 13,216 Bytes
abafbcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from vanna.base import VannaBase
from pinecone import Pinecone
from climateqa.engine.embeddings import get_embeddings_function
import pandas as pd
import hashlib

class MyCustomVectorDB(VannaBase):
    
    """
    VectorDB class for storing and retrieving vectors from Pinecone.
    
    args : 
        config (dict) : Configuration dictionary containing the Pinecone API key and the index name :
            - pc_api_key (str) : Pinecone API key
            - index_name (str) : Pinecone index name
            - top_k (int) : Number of top results to return (default = 2)
            
    """
    
    def __init__(self,config):
        super().__init__(config = config)
        try : 
            self.api_key = config.get('pc_api_key')
            self.index_name = config.get('index_name')
        except : 
            raise Exception("Please provide the Pinecone API key and the index name")
        
        self.pc = Pinecone(api_key = self.api_key)
        self.index = self.pc.Index(self.index_name)
        self.top_k = config.get('top_k', 2)
        self.embeddings = get_embeddings_function()
        
        
    def check_embedding(self, id, namespace):
        fetched = self.index.fetch(ids = [id], namespace = namespace)
        if fetched['vectors'] == {}: 
            return False
        return True
    
    def generate_hash_id(self, data: str) -> str:
        """
        Generate a unique hash ID for the given data.
        
        Args:
            data (str): The input data to hash (e.g., a concatenated string of user attributes).
        
        Returns:
            str: A unique hash ID as a hexadecimal string.
        """
        
        data_bytes = data.encode('utf-8')
        hash_object = hashlib.sha256(data_bytes)
        hash_id = hash_object.hexdigest()
        
        return hash_id
    
    def add_ddl(self, ddl: str, **kwargs) -> str:
        id = self.generate_hash_id(ddl) + '_ddl'
        
        if self.check_embedding(id, 'ddl'):
            print(f"DDL having id {id} already exists")
            return id
        
        self.index.upsert(
            vectors = [(id, self.embeddings.embed_query(ddl), {'ddl': ddl})],
            namespace = 'ddl'
        )
        
        return id

    def add_documentation(self, doc: str, **kwargs) -> str:
        id = self.generate_hash_id(doc) + '_doc'
        
        if self.check_embedding(id, 'documentation'):
            print(f"Documentation having id {id} already exists")
            return id
        
        self.index.upsert(
            vectors = [(id, self.embeddings.embed_query(doc), {'doc': doc})],
            namespace = 'documentation'
        )
        
        return id

    def add_question_sql(self, question: str, sql: str, **kwargs) -> str:
        id = self.generate_hash_id(question) + '_sql'
        
        if self.check_embedding(id, 'question_sql'):
            print(f"Question-SQL pair having id {id} already exists")
            return id
        
        self.index.upsert(
            vectors = [(id, self.embeddings.embed_query(question + sql), {'question': question, 'sql': sql})],
            namespace = 'question_sql'
        )
        
        return id

    def get_related_ddl(self, question: str, **kwargs) -> list:
        res = self.index.query(
            vector=self.embeddings.embed_query(question),
            top_k=self.top_k,
            namespace='ddl',
            include_metadata=True
        )
        
        return [match['metadata']['ddl'] for match in res['matches']]

    def get_related_documentation(self, question: str, **kwargs) -> list:
        res = self.index.query(
            vector=self.embeddings.embed_query(question),
            top_k=self.top_k,
            namespace='documentation',
            include_metadata=True
        )
        
        return [match['metadata']['doc'] for match in res['matches']]

    def get_similar_question_sql(self, question: str, **kwargs) -> list:
        res = self.index.query(
            vector=self.embeddings.embed_query(question),
            top_k=self.top_k,
            namespace='question_sql',
            include_metadata=True
        )
        
        return [(match['metadata']['question'], match['metadata']['sql']) for match in res['matches']]

    def get_training_data(self, **kwargs) -> pd.DataFrame:
        
        list_of_data = []
        
        namespaces = ['ddl', 'documentation', 'question_sql']
        
        for namespace in namespaces:
            
            data = self.index.query(
            top_k=10000,
            namespace=namespace,
            include_metadata=True,
            include_values=False
            )
            
            for match in data['matches']:
                list_of_data.append(match['metadata'])
                
        return pd.DataFrame(list_of_data)
            


    def remove_training_data(self, id: str, **kwargs) -> bool:
        if id.endswith("_ddl"):
            self.Index.delete(ids=[id], namespace="_ddl")
            return True
        if id.endswith("_sql"):
            self.index.delete(ids=[id], namespace="_sql")
            return True
        
        if id.endswith("_doc"):
            self.Index.delete(ids=[id], namespace="_doc")
            return True
        
        return False
    
    def generate_embedding(self, text, **kwargs):
        # Implement the method here
        pass


    def get_sql_prompt(
            self,
            initial_prompt : str,
            question: str,
            question_sql_list: list,
            ddl_list: list,
            doc_list: list,
            **kwargs,
        ):
            """
            Example:
            ```python
            vn.get_sql_prompt(
                question="What are the top 10 customers by sales?",
                question_sql_list=[{"question": "What are the top 10 customers by sales?", "sql": "SELECT * FROM customers ORDER BY sales DESC LIMIT 10"}],
                ddl_list=["CREATE TABLE customers (id INT, name TEXT, sales DECIMAL)"],
                doc_list=["The customers table contains information about customers and their sales."],
            )

            ```

            This method is used to generate a prompt for the LLM to generate SQL.

            Args:
                question (str): The question to generate SQL for.
                question_sql_list (list): A list of questions and their corresponding SQL statements.
                ddl_list (list): A list of DDL statements.
                doc_list (list): A list of documentation.

            Returns:
                any: The prompt for the LLM to generate SQL.
            """

            if initial_prompt is None:
                initial_prompt = f"You are a {self.dialect} expert. " + \
                "Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. "

            initial_prompt = self.add_ddl_to_prompt(
                initial_prompt, ddl_list, max_tokens=self.max_tokens
            )

            if self.static_documentation != "":
                doc_list.append(self.static_documentation)

            initial_prompt = self.add_documentation_to_prompt(
                initial_prompt, doc_list, max_tokens=self.max_tokens
            )

            # initial_prompt = self.add_sql_to_prompt(
            #     initial_prompt, question_sql_list, max_tokens=self.max_tokens
            # )


            initial_prompt += (
                "===Response Guidelines \n"
                "1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n"
                "2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n"
                "3. If the provided context is insufficient, please give a sql query based on your knowledge and the context provided. \n"
                "4. Please use the most relevant table(s). \n"
                "5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n"
                f"6. Ensure that the output SQL is {self.dialect}-compliant and executable, and free of syntax errors. \n"
                f"7. Add a description of the table in the result of the sql query, if relevant. \n"
                "8 Make sure to include the relevant KPI in the SQL query. The query should return impactfull data \n"
                # f"8. If a set of latitude,longitude is provided, make a intermediate query to find the nearest value in the table and replace the coordinates in the sql query. \n"
                # "7. Add a description of the table in the result of the sql query."
                # "7. If the question is about a specific latitude, longitude, query an interval of 0.3 and keep only the first set of coordinate. \n"
                # "7. Table names should be included in the result of the sql query. Use for example Mean_winter_temperature AS table_name in the query \n"
            )


            message_log = [self.system_message(initial_prompt)]

            for example in question_sql_list:
                if example is None:
                    print("example is None")
                else:
                    if example is not None and "question" in example and "sql" in example:
                        message_log.append(self.user_message(example["question"]))
                        message_log.append(self.assistant_message(example["sql"]))

            message_log.append(self.user_message(question))

            return message_log
        

# def get_sql_prompt(
#         self,
#         initial_prompt : str,
#         question: str,
#         question_sql_list: list,
#         ddl_list: list,
#         doc_list: list,
#         **kwargs,
#     ):
#         """
#         Example:
#         ```python
#         vn.get_sql_prompt(
#             question="What are the top 10 customers by sales?",
#             question_sql_list=[{"question": "What are the top 10 customers by sales?", "sql": "SELECT * FROM customers ORDER BY sales DESC LIMIT 10"}],
#             ddl_list=["CREATE TABLE customers (id INT, name TEXT, sales DECIMAL)"],
#             doc_list=["The customers table contains information about customers and their sales."],
#         )

#         ```

#         This method is used to generate a prompt for the LLM to generate SQL.

#         Args:
#             question (str): The question to generate SQL for.
#             question_sql_list (list): A list of questions and their corresponding SQL statements.
#             ddl_list (list): A list of DDL statements.
#             doc_list (list): A list of documentation.

#         Returns:
#             any: The prompt for the LLM to generate SQL.
#         """

#         if initial_prompt is None:
#             initial_prompt = f"You are a {self.dialect} expert. " + \
#             "Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. "

#         initial_prompt = self.add_ddl_to_prompt(
#             initial_prompt, ddl_list, max_tokens=self.max_tokens
#         )

#         if self.static_documentation != "":
#             doc_list.append(self.static_documentation)

#         initial_prompt = self.add_documentation_to_prompt(
#             initial_prompt, doc_list, max_tokens=self.max_tokens
#         )

#         initial_prompt += (
#             "===Response Guidelines \n"
#             "1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n"
#             "2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n"
#             "3. If the provided context is insufficient, please explain why it can't be generated. \n"
#             "4. Please use the most relevant table(s). \n"
#             "5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n"
#             f"6. Ensure that the output SQL is {self.dialect}-compliant and executable, and free of syntax errors. \n"
#         )

#         message_log = [self.system_message(initial_prompt)]

#         for example in question_sql_list:
#             if example is None:
#                 print("example is None")
#             else:
#                 if example is not None and "question" in example and "sql" in example:
#                     message_log.append(self.user_message(example["question"]))
#                     message_log.append(self.assistant_message(example["sql"]))

#         message_log.append(self.user_message(question))

#         return message_log