File size: 18,776 Bytes
1871bfe
ecbd714
 
 
1871bfe
ecbd714
1871bfe
 
ecbd714
 
 
1871bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecbd714
 
 
 
 
 
 
 
 
a3ccc70
36cb8de
ecbd714
 
 
 
 
 
5adc727
 
 
ecbd714
 
1871bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58c2aca
1871bfe
 
 
 
 
 
58c2aca
1871bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58c2aca
 
1871bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecbd714
 
1871bfe
 
 
ecbd714
 
1871bfe
 
 
 
 
 
e97bd62
1871bfe
 
ecbd714
 
 
1871bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00cad52
1871bfe
 
 
 
ecbd714
 
 
 
 
 
 
1871bfe
e97bd62
 
1871bfe
 
 
58c2aca
1871bfe
58c2aca
 
1871bfe
ecbd714
 
 
 
 
 
 
 
 
1871bfe
ecbd714
1871bfe
 
ecbd714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1871bfe
ecbd714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36cb8de
 
ecbd714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datetime
import asyncio
from concurrent.futures import ThreadPoolExecutor

from typing import Any, List, Dict, Union
from pydantic import Extra

import wandb
from wandb.sdk.data_types.trace_tree import Trace

import pinecone
import google.generativeai as genai

from llama_index import (
    ServiceContext,
    PromptHelper,
    VectorStoreIndex
)
from llama_index.vector_stores import PineconeVectorStore
from llama_index.storage.storage_context import StorageContext
from llama_index.node_parser import SimpleNodeParser
from llama_index.text_splitter import TokenTextSplitter
from llama_index.embeddings.base import BaseEmbedding
from llama_index.llms import (
    CustomLLM,
    CompletionResponse,
    CompletionResponseGen,
    LLMMetadata,
)
from llama_index.llms.base import llm_completion_callback

from llama_index.evaluation import SemanticSimilarityEvaluator
from llama_index.embeddings import SimilarityMode

import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p', level=logging.INFO)
logger = logging.getLogger('llm')

prompt_template = """
[System]
    You are in a role play of Gerard Lee. Gerard is a data enthusiast and humble about his success.
    Reply in no more than 5 complete sentences unless [User Query] requests to elaborate. Using content from [Context] only without prior knowledge except referring to [History] for seamless conversatation.

[History]
    {context_history}

[Context]
    {context_from_index}

[User Query]
    {user_query}
"""

class LlamaIndexPaLMEmbeddings(BaseEmbedding, extra=Extra.allow):
    def __init__(
        self,
        model_name: str = 'models/embedding-gecko-001',
        **kwargs: Any,
    ) -> None:
        super().__init__(**kwargs)
        self._model_name = model_name

    @classmethod
    def class_name(cls) -> str:
        return 'PaLMEmbeddings'

    def gen_embeddings(self, text: str) -> List[float]:
        return genai.generate_embeddings(self._model_name, text)

    def _get_query_embedding(self, query: str) -> List[float]:
        embeddings = self.gen_embeddings(query)
        return embeddings['embedding']

    def _get_text_embedding(self, text: str) -> List[float]:
        embeddings = self.gen_embeddings(text)
        return embeddings['embedding']

    def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
        embeddings = [
            self.gen_embeddings(text)['embedding'] for text in texts
        ]
        return embeddings
    
    async def _aget_query_embedding(self, query: str) -> List[float]:
        return self._get_query_embedding(query)

    async def _aget_text_embedding(self, text: str) -> List[float]:
        return self._get_text_embedding(text)
    
class LlamaIndexPaLMText(CustomLLM, extra=Extra.allow):
    def __init__(
        self,
        model_name: str = 'models/text-bison-001',
        model_kwargs: dict = {},
        context_window: int = 8196,
        num_output: int = 1024,
        **kwargs: Any,
    ) -> None:
        super().__init__(**kwargs)
        self._model_name = model_name
        self._model_kwargs = model_kwargs
        self._context_window = context_window
        self._num_output = num_output
        
    @property
    def metadata(self) -> LLMMetadata:
        """Get LLM metadata."""
        return LLMMetadata(
            context_window=self._context_window,
            num_output=self._num_output,
            model_name=self._model_name
        )

    def gen_texts(self, prompt):
            logging.debug(f"prompt: {prompt}")
            response = genai.generate_text(
                model=self._model_name,
                prompt=prompt,
                safety_settings=[
                    {
                        'category': genai.types.HarmCategory.HARM_CATEGORY_UNSPECIFIED,
                        'threshold': genai.types.HarmBlockThreshold.BLOCK_NONE,
                    },
                ],
                **self._model_kwargs
            )
            logging.debug(f"response:\n{response}")
            return response.candidates[0]['output']

    @llm_completion_callback()
    def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
        text = self.gen_texts(prompt)
        return CompletionResponse(text=text)

    @llm_completion_callback()
    def stream_complete(self, prompt: str, **kwargs: Any) -> CompletionResponseGen:
        raise NotImplementedError()
    
class LlamaIndexPaLM():
    def __init__(
        self,
        emb_model: LlamaIndexPaLMEmbeddings = LlamaIndexPaLMEmbeddings(),
        model: LlamaIndexPaLMText = LlamaIndexPaLMText(),
        # prompt_template: str = prompt_template
    ) -> None:
        self.emb_model = emb_model
        self.llm = model
        self.prompt_template = prompt_template

        # Google Generative AI
        genai.configure(api_key=os.environ['PALM_API_KEY'])

        # Pinecone
        pinecone.init(
            api_key=os.environ['PINECONE_API_KEY'],
            environment=os.getenv('PINECONE_ENV')
        )

        # W&B
        wandb.init(project=os.getenv('WANDB_PROJECT'))

        # model metadata
        CONTEXT_WINDOW = os.getenv('CONTEXT_WINDOW', 8196)
        NUM_OUTPUT = os.getenv('NUM_OUTPUT', 1024)
        TEXT_CHUNK_SIZE = os.getenv('TEXT_CHUNK_SIZE', 512)
        TEXT_CHUNK_OVERLAP = os.getenv('TEXT_CHUNK_OVERLAP', 20)
        TEXT_CHUNK_OVERLAP_RATIO = os.getenv('TEXT_CHUNK_OVERLAP_RATIO', 0.1)
        TEXT_CHUNK_SIZE_LIMIT = os.getenv('TEXT_CHUNK_SIZE_LIMIT', None)

        self.node_parser = SimpleNodeParser.from_defaults(
            text_splitter=TokenTextSplitter(
                chunk_size=TEXT_CHUNK_SIZE, chunk_overlap=TEXT_CHUNK_OVERLAP
            )
        )

        self.prompt_helper = PromptHelper(
            context_window=CONTEXT_WINDOW,
            num_output=NUM_OUTPUT,
            chunk_overlap_ratio=TEXT_CHUNK_OVERLAP_RATIO,
            chunk_size_limit=TEXT_CHUNK_SIZE_LIMIT
        )

        self.service_context = ServiceContext.from_defaults(
            llm=self.llm,
            embed_model=self.emb_model,
            node_parser=self.node_parser,
            prompt_helper=self.prompt_helper,
        )
    
        self.emd_evaluator = SemanticSimilarityEvaluator(
            service_context=self.service_context,
            similarity_mode=SimilarityMode.DEFAULT,
            similarity_threshold=os.getenv('SIMILARITY_THRESHOLD', 0.7),
        )

    def get_index_from_pinecone(
        self, 
        index_name: str = os.getenv('PINECONE_INDEX'),
        index_namespace: str = os.getenv('PINECONE_NAMESPACE')
    ) -> None:
        # Pinecone VectorStore
        pinecone_index = pinecone.Index(index_name)
        self.vector_store = PineconeVectorStore(pinecone_index=pinecone_index, add_sparse_vector=True, namespace=index_namespace)
        self.pinecone_index = VectorStoreIndex.from_vector_store(self.vector_store, self.service_context)
        self._index_name = index_name
        self._index_namespace = index_namespace
        return None

    def retrieve_context(
        self,
        query: str
    ) -> Dict[str, Union[str, int]]:
        start_time = round(datetime.datetime.now().timestamp() * 1000)
        response = self.pinecone_index.as_query_engine(similarity_top_k=3).query(query)
        end_time = round(datetime.datetime.now().timestamp() * 1000)
        return {"result": response.response, "start": start_time, "end": end_time}
    
    async def aretrieve_context(
        self,
        query: str
    ) -> Dict[str, Union[str, int]]:
        start_time = round(datetime.datetime.now().timestamp() * 1000)
        response = await self.pinecone_index.as_query_engine(similarity_top_k=3, use_async=True).aquery(query)
        end_time = round(datetime.datetime.now().timestamp() * 1000)
        return {"result": response.response, "start": start_time, "end": end_time}
    
    async def aretrieve_context_multi(
        self,
        query_list: List[str]
    ) -> List[Dict]:
        result = await asyncio.gather(*(self.aretrieve_context(query) for query in query_list))
        return result

    async def aevaluate_context(
        self,
        query: str,
        returned_context: str
    ) -> Dict[str, Any]:
        result = await self.emd_evaluator.aevaluate(
            response=returned_context,
            reference=query,
        )
        return result

    async def aevaluate_context_multi(
        self,
        query_list: List[str],
        returned_context_list: List[str]
    ) -> List[Dict]:
        result = await asyncio.gather(*(self.aevaluate_context(query, returned_context) for query, returned_context in zip(query_list, returned_context_list)))
        return result
    
    def format_history_as_context(
        self,
        history: List[str],
    ) -> str:
        format_chat_history = "\n".join(list(filter(None, history)))
        return format_chat_history

    def generate_text(
        self,
        query: str,
        history: List[str],
    ) -> str:
        # get history
        context_history = self.format_history_as_context(history=history)

        # w&b trace start
        start_time_ms = round(datetime.datetime.now().timestamp() * 1000)
        root_span = Trace(
            name="MetaAgent",
            kind="agent",
            start_time_ms=start_time_ms,
            metadata={"user": "🤗 Space"},
        )

        # get retrieval context(s) from llama-index vectorstore index
        # w&b trace retrieval & select agent
        agent_span = Trace(
            name="LlamaIndexAgent",
            kind="agent",
            start_time_ms=start_time_ms,
        )
        try:
            # No history, single context retrieval without evaluation
            if not history:
                # w&b trace retrieval context
                result_query_only = self.retrieve_context(query)
                # async version
                # result_query_only = asyncio.run(self.retrieve_context(query))
                context_from_index_selected = result_query_only["result"]
                agent_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
                retrieval_span = Trace(
                    name="QueryRetrieval",
                    kind="chain",
                    status_code="success",
                    metadata={
                        "framework": "Llama-Index",
                        "index_type": "VectorStoreIndex",
                        "vector_store": "Pinecone",
                        "vector_store_index": self._index_name,
                        "vector_store_namespace": self._index_namespace,
                        "model_name": self.llm._model_name,
                        "custom_kwargs": self.llm._model_kwargs,
                    },
                    start_time_ms=start_time_ms,
                    end_time_ms=agent_end_time_ms,
                    inputs={"query": query},
                    outputs={"response": context_from_index_selected},
                )
                agent_span.add_child(retrieval_span)
            # Has history, multiple context retrieval with async, then evaluation to determine which context to choose
            else:
                extended_query = f"[History]\n{history[-1]}\n[New Query]\n{query}"

                # thread version
                with ThreadPoolExecutor(2) as executor:
                    results = executor.map(self.retrieve_context, [query, extended_query])
                result_query_only, result_extended_query = [rec for rec in results]

                # async version - not working
                # result_query_only, result_extended_query = asyncio.run(
                #     self.aretrieve_context_multi([query, extended_query])
                # )

                # w&b trace retrieval context query only
                retrieval_query_span = Trace(
                    name="QueryRetrieval",
                    kind="chain",
                    status_code="success",
                    metadata={
                        "framework": "Llama-Index",
                        "index_type": "VectorStoreIndex",
                        "vector_store": "Pinecone",
                        "vector_store_index": self._index_name,
                        "vector_store_namespace": self._index_namespace,
                        "model_name": self.llm._model_name,
                        "custom_kwargs": self.llm._model_kwargs,
                        "start_time": result_query_only["start"],
                        "end_time": result_query_only["end"],
                    },
                    start_time_ms=result_query_only["start"],
                    end_time_ms=result_query_only["end"],
                    inputs={"query": query},
                    outputs={"response": result_query_only["result"]},
                )
                agent_span.add_child(retrieval_query_span)

                # w&b trace retrieval context extended query
                retrieval_extended_query_span = Trace(
                    name="ExtendedQueryRetrieval",
                    kind="chain",
                    status_code="success",
                    metadata={
                        "framework": "Llama-Index",
                        "index_type": "VectorStoreIndex",
                        "vector_store": "Pinecone",
                        "vector_store_index": self._index_name,
                        "vector_store_namespace": self._index_namespace,
                        "model_name": self.llm._model_name,
                        "custom_kwargs": self.llm._model_kwargs,
                        "start_time": result_extended_query["start"],
                        "end_time": result_extended_query["end"],
                    },
                    start_time_ms=result_extended_query["start"],
                    end_time_ms=result_extended_query["end"],
                    inputs={"query": extended_query},
                    outputs={"response": result_extended_query["result"]},
                )
                agent_span.add_child(retrieval_extended_query_span)

                # w&b trace select context
                eval_start_time_ms = round(datetime.datetime.now().timestamp() * 1000)
                eval_context_query_only, eval_context_extended_query = asyncio.run(
                    self.aevaluate_context_multi([query, extended_query], [result_query_only["result"], result_extended_query["result"]])
                )

                if eval_context_query_only.score > eval_context_extended_query.score:
                    query_selected, context_from_index_selected = query, result_query_only["result"] 
                else:
                    query_selected, context_from_index_selected = extended_query, result_extended_query["result"]

                agent_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
                eval_span = Trace(
                    name="EmbeddingsEvaluator",
                    kind="tool",
                    status_code="success",
                    metadata={
                        "framework": "Llama-Index",
                        "evaluator": "SemanticSimilarityEvaluator",
                        "similarity_mode": "DEFAULT",
                        "similarity_threshold": 0.7,
                        "similarity_results": {
                            "eval_context_query_only": eval_context_query_only["result"], 
                            "eval_context_extended_query": eval_context_extended_query["result"],
                        },
                        "model_name": self.emb_model._model_name,
                    },
                    start_time_ms=eval_start_time_ms,
                    end_time_ms=agent_end_time_ms,
                    inputs={"query": query_selected},
                    outputs={"response": context_from_index_selected},
                )
                agent_span.add_child(eval_span)

        except Exception as e:
            logger.error(f"Exception {e} occured when retriving context\n")

            llm_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
            result = "Something went wrong. Please try again later."
            root_span.add_inputs_and_outputs(
                inputs={"query": query}, outputs={"result": result, "exception": e}
            )
            root_span._span.status_code="fail"
            root_span._span.end_time_ms = llm_end_time_ms
            root_span.log(name="llm_app_trace")
            return result
        
        logger.info(f"Context from Llama-Index:\n{context_from_index_selected}\n")

        agent_span.add_inputs_and_outputs(
            inputs={"query": query}, outputs={"result": context_from_index_selected}
        )
        agent_span._span.status_code="success"
        agent_span._span.end_time_ms = agent_end_time_ms
        root_span.add_child(agent_span)

        # generate text with prompt template to roleplay myself
        prompt_with_context = self.prompt_template.format(context_history=context_history, context_from_index=context_from_index_selected, user_query=query)
        try:
            response = genai.generate_text(
                prompt=prompt_with_context,
                safety_settings=[
                    {
                        'category': genai.types.HarmCategory.HARM_CATEGORY_UNSPECIFIED,
                        'threshold': genai.types.HarmBlockThreshold.BLOCK_NONE,
                    },
                ],
                temperature=0.9,
            )
            result = response.result
            success_flag = "success"
            if result is None:
                result = "Seems something went wrong. Please try again later."
                logger.error(f"Result with 'None' received\n")
                success_flag = "fail"

        except Exception as e:
            result = "Seems something went wrong. Please try again later."
            logger.error(f"Exception {e} occured\n")
            success_flag = "fail"

        # w&b trace llm
        llm_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
        llm_span = Trace(
            name="LLM",
            kind="llm",
            status_code=success_flag,
            start_time_ms=agent_end_time_ms,
            end_time_ms=llm_end_time_ms,
            inputs={"input": prompt_with_context},
            outputs={"result": result},
        )
        root_span.add_child(llm_span)

        # w&b finalize trace
        root_span.add_inputs_and_outputs(
            inputs={"query": query}, outputs={"result": result}
        )
        root_span._span.end_time_ms = llm_end_time_ms
        root_span.log(name="llm_app_trace")

        return result