File size: 10,107 Bytes
c65ba42
b2ba33f
 
bb4f92d
c119679
bb4f92d
d4b9099
c119679
b2ba33f
3c6573c
d4b9099
 
b2ba33f
 
 
f932d05
495b986
 
bb4f92d
 
 
 
c119679
b2ba33f
 
0217d37
f932d05
 
 
 
 
 
 
c119679
 
b2ba33f
75b7c0b
495b986
d4b9099
b2ba33f
 
c119679
 
b2ba33f
 
495b986
b2ba33f
 
 
 
 
 
495b986
 
 
 
 
 
 
c119679
 
d4b9099
c119679
 
 
 
 
 
d4b9099
 
 
 
 
c119679
d4b9099
c119679
b2ba33f
 
 
 
 
 
 
 
 
 
 
 
495b986
b2ba33f
495b986
b2ba33f
 
495b986
 
 
 
 
 
 
 
 
 
 
 
 
b2ba33f
 
 
 
 
495b986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2ba33f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495b986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b9099
c119679
b2ba33f
 
 
 
 
c119679
 
b2ba33f
 
3c6573c
c65ba42
495b986
b2ba33f
c119679
 
495b986
 
 
f932d05
 
c119679
f932d05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import multiprocessing
import concurrent.futures
from langchain_community.document_loaders import TextLoader, DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
from datetime import datetime
import json
import gradio as gr
import re
from threading import Thread
from transformers.agents import Tool, HfEngine, ReactJsonAgent
from huggingface_hub import InferenceClient
import logging
import torch
import numpy as np
import faiss
import warnings

# Suppress specific warnings
warnings.filterwarnings("ignore", category=FutureWarning)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

try:
    from langchain_community.vectorstores import FAISS
except ImportError:
    logger.error("Failed to import FAISS. Make sure it's installed correctly.")
    logger.info("You can try: pip install faiss-cpu --no-cache")
    FAISS = None

class DocumentRetrievalAndGeneration:
    def __init__(self, embedding_model_name, lm_model_id, data_folder):
        self.all_splits = self.load_documents(data_folder)
        self.embeddings = HuggingFaceEmbeddings(model_name=embedding_model_name)
        self.gpu_index = self.create_faiss_index()
        self.tokenizer, self.model = self.initialize_llm(lm_model_id)
        self.retriever_tool = self.create_retriever_tool()
        self.agent = self.create_agent()

    def load_documents(self, folder_path):
        loader = DirectoryLoader(folder_path, loader_cls=TextLoader)
        documents = loader.load()
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=250)
        all_splits = text_splitter.split_documents(documents)
        logger.info(f'Loaded {len(documents)} documents')
        logger.info(f"Split into {len(all_splits)} chunks")
        return all_splits

    def create_faiss_index(self):
        all_texts = [split.page_content for split in self.all_splits]
        embeddings = self.embeddings.embed_documents(all_texts)
        index = faiss.IndexFlatL2(len(embeddings[0]))
        index.add(np.array(embeddings))
        gpu_resource = faiss.StandardGpuResources()
        gpu_index = faiss.index_cpu_to_gpu(gpu_resource, 0, index)
        return gpu_index

    def initialize_llm(self, model_id):
        quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_use_double_quant=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch.bfloat16
        )
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        model = AutoModelForCausalLM.from_pretrained(
            model_id,
            torch_dtype=torch.bfloat16,
            device_map="auto",
            quantization_config=quantization_config
        )
        return tokenizer, model

    def create_retriever_tool(self):
        class RetrieverTool(Tool):
            name = "retriever"
            description = "Retrieves documents from the knowledge base that are semantically similar to the input query."
            inputs = {
                "query": {
                    "type": "text",
                    "description": "The query to perform. Use affirmative form rather than a question.",
                }
            }
            output_type = "text"

            def __init__(self, parent, **kwargs):
                super().__init__(**kwargs)
                self.parent = parent

            def forward(self, query: str) -> str:
                similarityThreshold = 1
                query_embedding = self.parent.embeddings.embed_query(query)
                distances, indices = self.parent.gpu_index.search(np.array([query_embedding]), k=3)
                content = ""
                filtered_results = []
                for idx, distance in zip(indices[0], distances[0]):
                    if distance <= similarityThreshold:
                        filtered_results.append(idx)
                    content += "-" * 50 + "\n"
                    content += self.parent.all_splits[idx].page_content + "\n"
                return content

        return RetrieverTool(self)

    def create_agent(self):
        llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-8B-Instruct")
        return ReactJsonAgent(tools=[self.retriever_tool], llm_engine=llm_engine, max_iterations=4, verbose=2)

    def generate_response_with_timeout(self, input_ids, max_new_tokens=1000):
        try:
            streamer = TextIteratorStreamer(self.tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
            generate_kwargs = dict(
                input_ids=input_ids,
                max_new_tokens=max_new_tokens,
                do_sample=True,
                top_p=1.0,
                top_k=20,
                temperature=0.8,
                repetition_penalty=1.2,
                eos_token_id=[128001, 128008, 128009],
                streamer=streamer,
            )
            
            thread = Thread(target=self.model.generate, kwargs=generate_kwargs)
            thread.start()
            
            generated_text = ""
            for new_text in streamer:
                generated_text += new_text
            
            return generated_text
        except Exception as e:
            logger.error(f"Error in generate_response_with_timeout: {str(e)}")
            return "Text generation process encountered an error"

    def run_agentic_rag(self, question: str) -> str:
        enhanced_question = f"""Using the information in your knowledge base, accessible with the 'retriever' tool,
give a comprehensive answer to the question below.
Respond only to the question asked, be concise and relevant.
If you can't find information, try calling your retriever again with different arguments.
Make sure to cover the question completely by calling the retriever tool several times with semantically different queries.
Your queries should be in affirmative form, not questions.

Question:
{question}"""

        return self.agent.run(enhanced_question)

    def run_standard_rag(self, question: str) -> str:
        context = self.retriever_tool(query=question)

        conversation = [
            {"role": "system", "content": "You are a knowledgeable assistant with access to a comprehensive database."},
            {"role": "user", "content": f"""
            I need you to answer my question and provide related information in a specific format.
            I have provided five relatable json files {context}, choose the most suitable chunks for answering the query.
            RETURN ONLY SOLUTION without additional comments, sign-offs, retrived chunks, refrence to any Ticket or extra phrases. Be direct and to the point.
            IF THERE IS NO ANSWER RELATABLE IN RETRIEVED CHUNKS, RETURN "NO SOLUTION AVAILABLE".
            DO NOT GIVE REFRENCE TO ANY CHUNKS OR TICKETS,BE ON POINT.
            
            Here's my question:
            Query: {question}
            Solution==>
            """}
        ]
        input_ids = self.tokenizer.apply_chat_template(conversation, return_tensors="pt").to(self.model.device)
        
        return self.generate_response_with_timeout(input_ids)

    def query_and_generate_response(self, query):
        agentic_answer = self.run_agentic_rag(query)
        standard_answer = self.run_standard_rag(query)
        
        combined_answer = f"Agentic RAG Answer:\n{agentic_answer}\n\nStandard RAG Answer:\n{standard_answer}"
        return combined_answer, ""  # Return empty string for 'content' as it's not used in this implementation

    def qa_infer_gradio(self, query):
        response = self.query_and_generate_response(query)
        return response

if __name__ == "__main__":
    embedding_model_name = 'flax-sentence-embeddings/all_datasets_v3_MiniLM-L12'
    lm_model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
    data_folder = 'sample_embedding_folder2'

    # Set your HuggingFace token here
    os.environ["HUGGINGFACE_TOKEN"] = "your_huggingface_token_here"

    try:
        doc_retrieval_gen = DocumentRetrievalAndGeneration(embedding_model_name, lm_model_id, data_folder)

        def launch_interface():
            css_code = """
                .gradio-container {
                    background-color: #daccdb;
                }
                button {
                    background-color: #927fc7;
                    color: black;
                    border: 1px solid black;
                    padding: 10px;
                    margin-right: 10px;
                    font-size: 16px;
                    font-weight: bold;
                }
            """
            EXAMPLES = [
                "On which devices can the VIP and CSI2 modules operate simultaneously?", 
                "I'm using Code Composer Studio 5.4.0.00091 and enabled FPv4SPD16 floating point support for CortexM4 in TDA2. However, after building the project, the .asm file shows --float_support=vfplib instead of FPv4SPD16. Why is this happening?", 
                "Could you clarify the maximum number of cameras that can be connected simultaneously to the video input ports on the TDA2x SoC, considering it supports up to 10 multiplexed input ports and includes 3 dedicated video input modules?"
            ]

            interface = gr.Interface(
                fn=doc_retrieval_gen.qa_infer_gradio,
                inputs=[gr.Textbox(label="QUERY", placeholder="Enter your query here")],
                allow_flagging='never',
                examples=EXAMPLES,
                cache_examples=False,
                outputs=[gr.Textbox(label="RESPONSE"), gr.Textbox(label="RELATED QUERIES")],
                css=css_code,
                title="TI E2E FORUM Multi-Agent RAG"
            )

            interface.launch(debug=True)

        launch_interface()
    except Exception as e:
        logger.error(f"An error occurred: {str(e)}")
        logger.info("Please check your environment setup and try again.")