File size: 5,968 Bytes
7e1dfd1
 
80ac8cc
35a96c0
 
 
 
 
7e1dfd1
 
80ac8cc
35a96c0
 
 
 
 
 
 
 
7e1dfd1
35a96c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e1dfd1
35a96c0
80ac8cc
 
 
6b255cb
80ac8cc
 
35a96c0
80ac8cc
 
 
 
 
 
 
35a96c0
83ac167
80ac8cc
 
 
 
 
 
 
 
83ac167
80ac8cc
 
 
35a96c0
 
 
 
 
83ac167
35a96c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e1dfd1
80ac8cc
 
 
 
35a96c0
 
 
 
 
 
 
 
 
 
 
 
7e1dfd1
 
 
80ac8cc
 
35a96c0
 
80ac8cc
35a96c0
 
7e1dfd1
 
80ac8cc
7e1dfd1
 
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
import gradio as gr
import openai
import os
import nltk
import shutil
import numpy as np
import torch
from datasets import load_dataset
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.schema import Document
from sentence_transformers import SentenceTransformer
from sklearn.metrics import mean_squared_error, roc_auc_score
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# βœ… Load Pretrained Model
model_name = "bert-base-uncased"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
embedding_model.client.to(device)

# βœ… Set OpenAI API Key (Replace with your own)
openai.api_key = os.getenv("OPENAI_API_KEY")  

# βœ… Download NLTK Dependencies
nltk.download('punkt')

# βœ… Load RunGalileo Datasets
ragbench = {}
for dataset in ['covidqa', 'cuad', 'delucionqa', 'emanual', 'expertqa', 'finqa', 'hagrid', 'hotpotqa', 'msmarco', 'pubmedqa', 'tatqa', 'techqa']:
    ragbench[dataset] = load_dataset("rungalileo/ragbench", dataset)
print("Datasets Loaded βœ…")

# βœ… Function to Chunk Documents
def chunk_documents_semantic(documents, max_chunk_size=500):
    chunks = []
    for doc in documents:
        sentences = nltk.sent_tokenize(doc)
        current_chunk = ""
        for sentence in sentences:
            if len(current_chunk) + len(sentence) <= max_chunk_size:
                current_chunk += sentence + " "
            else:
                chunks.append(current_chunk.strip())
                current_chunk = sentence + " "
        if current_chunk:
            chunks.append(current_chunk.strip())
    return chunks

# βœ… Chunk the Entire Dataset
chunked_ragbench = {}
for dataset_name in ragbench.keys():
    for split in ragbench[dataset_name].keys():
        original_documents_full = ragbench[dataset_name][split]['documents']
        chunked_documents_full = chunk_documents_semantic(original_documents_full)
        chunked_ragbench[split] = chunked_documents_full
print("Chunking Completed βœ…")

# βœ… Setup ChromaDB
persist_directory = "chroma_db_directory"
if os.path.exists(persist_directory):
    shutil.rmtree(persist_directory)

documents = [Document(page_content=chunk) for chunk in chunked_documents_full]
vectordb = Chroma.from_documents(
    documents=documents,
    embedding=embedding_model,
    persist_directory=persist_directory
)
vectordb.persist()

# βœ… Retrieve Documents
def retrieve_documents(question, k=5):
    docs = vectordb.similarity_search(question, k=k)
    if not docs:
        return ["⚠️ No relevant documents found. Try a different query."]
    return [doc.page_content for doc in docs]

# βœ… Generate AI Response
def generate_response(question, context):
    if not context or "No relevant documents found." in context:
        return "No relevant context available. Try a different query."

    full_prompt = f"Context: {context}\n\nQuestion: {question}"

    try:
        client = openai.OpenAI()
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": "You are an AI assistant that answers user queries based on the given context."},
                {"role": "user", "content": full_prompt}
            ],
            max_tokens=300,
            temperature=0.7
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"Error generating response: {str(e)}"

# βœ… Compute Context Relevance, Utilization, Completeness, Adherence
def compute_cosine_similarity(text1, text2):
    vectorizer = TfidfVectorizer()
    vectors = vectorizer.fit_transform([text1, text2])
    return cosine_similarity(vectors[0], vectors[1])[0][0]

def context_relevance(question, relevant_documents):
    combined_docs = " ".join(relevant_documents)
    return compute_cosine_similarity(question, combined_docs)

def context_utilization(response, relevant_documents):
    combined_docs = " ".join(relevant_documents)
    return compute_cosine_similarity(response, combined_docs)

def completeness(response, ground_truth_answer):
    return compute_cosine_similarity(response, ground_truth_answer)

def adherence(response, relevant_documents):
    combined_docs = " ".join(relevant_documents)
    response_tokens = set(response.split())
    relevant_tokens = set(combined_docs.split())
    supported_tokens = response_tokens.intersection(relevant_tokens)
    return len(supported_tokens) / len(response_tokens)

def compute_rmse(predicted_values, ground_truth_values):
    return np.sqrt(mean_squared_error(ground_truth_values, predicted_values))

# βœ… Full RAG Pipeline
def rag_pipeline(question):
    retrieved_docs = retrieve_documents(question, k=5)
    context = " ".join(retrieved_docs)
    response = generate_response(question, context)

    # Compute Evaluation Metrics
    ground_truth_answer = "Sample ground truth answer from dataset"
    predicted_metrics = {
        "context_relevance": context_relevance(question, retrieved_docs),
        "context_utilization": context_utilization(response, retrieved_docs),
        "completeness": completeness(response, ground_truth_answer),
        "adherence": adherence(response, retrieved_docs)
    }
    
    return response, "\n\n".join(retrieved_docs), predicted_metrics

# βœ… Gradio UI Interface
iface = gr.Interface(
    fn=rag_pipeline,
    inputs=gr.Textbox(label="Enter your question"),
    outputs=[
        gr.Textbox(label="Generated Response"),
        gr.Textbox(label="Retrieved Documents"),
        gr.JSON(label="Evaluation Metrics")
    ],
    title="RAG-Based QA System for RunGalileo",
    description="Enter a question and retrieve relevant documents with AI-generated response & evaluation metrics."
)

# βœ… Launch the Gradio App
if __name__ == "__main__":
    iface.launch()