File size: 10,206 Bytes
b22c324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6663c6a
 
 
 
b22c324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6663c6a
b22c324
6663c6a
b22c324
 
 
 
 
 
 
 
6663c6a
 
b22c324
 
 
 
 
 
 
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
from pathlib import Path
import pandas as pd
# import math, random
from sentence_transformers import SentenceTransformer
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_ollama import ChatOllama
from langchain_core.messages import HumanMessage, SystemMessage
import chromadb
from langchain_chroma import Chroma
from sklearn.metrics.pairwise import cosine_similarity
from chroma.chatbot_retriever import ask
from tqdm import tqdm
import random
import json
import numpy as np
import re

model      = HuggingFaceEmbeddings(model_name="multi-qa-mpnet-base-dot-v1")
client     = chromadb.PersistentClient(path="chroma/chroma_db")
data_dir = Path("datasets")
llm = ChatOllama(model="llama3.2", temperature=0)

vectorstore = Chroma(
    client = client,
    collection_name = "bank_faq",
    embedding_function = model
)

retriever = vectorstore.as_retriever(
    search_type = "similarity",
    search_kwargs = {"k": 3}
)
   

# test_docs = vectorstore.similarity_search("what are the fees", k=3)
# print("Search results:", test_docs)

# test_docs_scores = vectorstore.similarity_search_with_score("what are the fees", k=3)
# print("With scores:", test_docs_scores)


def llm_judge(query, expected, predicted, llm):
    prompt = f"""
    You are evaluating a question answering system.
    
    Question: {query}
    Expected Answer: {expected}
    Predicted Answer: {predicted}
    
    IMPORTANT RULES:
    - Focus on whether the MEANING and KEY INFORMATION is the same
    - As long as most of the words matches with the expected answer, do not score as 0.0
    - Ignore differences in formatting (bullet points vs numbered list vs plain text)
    - Ignore minor wording differences as long as the information is the same
    - Mark as correct if the predicted answer covers all the key points in the expected answer
    - Mark as incorrect ONLY if key information is missing or wrong
    
    Respond ONLY in this JSON format, no other text:
    - "correct": True if the meaning of the response matches with the expected answer, otherwise False
    - "score": 0.0 - 1.0
    - "reason": one-sentence explanation
    """
    
    response = llm.invoke(prompt)

    # If response has a 'content' attribute (e.g. an AIMessage object), use response.content. Otherwise, use response itself.
    # text = str(getattr(response, "content", response))
    # print(text)
    # Remove Markdown code block markers and trim any leading/trailing whitespace.
    # text = re.sub(r"```json|```", "", text).strip()
    # Search for the first JSON object in the text.
    # \{.*\} matches everything between the first '{' and the last '}'
    raw = response.content.strip()
    raw = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw).strip()
   
 

    try:
        parsed = json.loads(raw)
    except json.JSONDecodeError as e:
        print(f"JSON parse failed: {e}\nRaw text: {raw!r}")
        parsed = {}

    return {
        "correct": False,
        "score": 0.0,
        "reason": "Predicted does not match with expected",
        **parsed
    }

# from chroma.chatbot_retriever import ask
train_dataset = pd.read_csv(data_dir/"processed"/"bank_faq" /"train_faq.csv")
test_dataset = pd.read_csv(data_dir/"processed"/"bank_faq" /"test_faq.csv")

train_dataset = train_dataset.rename(columns={
    "Question": "query",
    "Answer":   "expected_answer"
}).to_dict(orient="records")

test_dataset = test_dataset.rename(columns={
    "Question": "query",
    "Answer":   "expected_answer"
}).to_dict(orient="records")

def retrieval_success_check(expected, retrieved_text, threshold=0.6):
    # embed both the expected answer and the retrieved docs into vector space
    expected_vec = model.embed_query(expected)
    retrieved_vec = model.embed_query(retrieved_text)

    # measure how semantically similar they are (0 = no match, 1 = identical)
    score = cosine_similarity([expected_vec], [retrieved_vec])[0][0]

    # consider retrieval successful if similarity meets the threshold
    return score >= threshold


def measure_hallucination(query: str, response: str, chunks: list[dict], llm) -> dict:
    """
    Uses the LLM to check whether the response contains claims
    not supported by the retrieved chunks.
 
    Returns:
      - hallucinated: bool
      - confidence:   "high" | "medium" | "low"
      - reason:       short explanation
    """
    context = "\n".join(c["text"] for c in chunks) if chunks else "No context retrieved."
 
    system = """
        You are a hallucination detector. Given a context, a query, and a response, 
        determine if the response contains information NOT supported by the context.
        Return ONLY valid JSON:
        - "hallucinated": true if the response contains unsupported claims, false otherwise
        - "confidence": "high", "medium", or "low"
        - "reason": one-sentence explanation
        
        Return ONLY the JSON object. No explanation.
        """
 
    result = llm.invoke([
        SystemMessage(content=system),
        HumanMessage(content=(
            f"Context:\n{context}\n\n"
            f"Query: {query}\n\n"
            f"Response: {response}"
        ))
    ])
 
    raw = result.content.strip()
    raw = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw).strip()
 
    try:
        parsed = json.loads(raw)
    except json.JSONDecodeError:
        parsed = {}
 
    return {
        "hallucinated": True,
        "confidence":   "low",
        "reason":       "Hallucination check failed",
        **parsed
    }


FALLBACK_PHRASES = [
    "I don't have information on that.",
]

def evaluate(dataset: list[dict], split: str = "", sample: int = None):
    data = random.sample(dataset, sample) if sample else dataset

    results = []

    correct = 0
    hallucinated = 0
    retrieval_failures = 0

    for item in tqdm(data, desc=f"Evaluating {split}", unit="q"):

        query = item["query"]
        expected = item["expected_answer"]

        # run chatbot
        predicted = ask(query=query, persist=False)

        # Score-aware retrieval
        docs_with_scores = vectorstore.similarity_search_with_score(query, k=3)
        # print("Docs: ", docs_with_scores)
        # --- retrieval check (simple proxy) ---
        # retrieved_docs = retriever.invoke(query) 

        retrieved_docs = [doc for doc, _ in docs_with_scores]
        confidence_scores = [round(1 / (1 + score), 4) for _, score in docs_with_scores]
        avg_confidence = round(sum(confidence_scores) / len(confidence_scores), 4) if confidence_scores else 0.0

        # retrieved_text = " ".join(retrieved_docs).lower()
        retrieved_text = " ".join([doc.page_content for doc in retrieved_docs]).lower()
        expected_in_context = expected.lower() in retrieved_text

        retrieval_success = retrieval_success_check(expected, retrieved_text)

        # --- correctness ---
        # correct_flag = is_correct(predicted, expected)
        
        is_fallback = any(phrase.lower() in predicted.lower() for phrase in FALLBACK_PHRASES)

        if is_fallback:
            judgement = {
                "correct": False,
                "score": 0.0,
                "reason": "Model returned a fallback response"
            }
            hallucinated_flag = False
            hallucination_detail = {
                "hallucinated": False,
                "confidence": "high",
                "reason": "Model does not know the answer"
            }
        else:
            judgement = llm_judge(query, expected, predicted, llm)

            if judgement["correct"]:
                hallucinated_flag = False
                hallucination_detail = {
                    "hallucinated": False,
                    "confidence": "high",
                    "reason": "Response is correct"
                }
                correct += 1
            else:
                chunks = [
                    {
                        "text":             doc.page_content,
                        "similarity_score": score
                    }
                    for doc, score in docs_with_scores
                ]
                hallucination_detail  = measure_hallucination(query, predicted, chunks, llm)
                hallucinated_flag     = hallucination_detail["hallucinated"]
            
        # hallucinated_flag = not expected_in_context and not correct_flag and not is_fallback

        # if correct_flag:
        #     correct += 1

        if hallucinated_flag:
            hallucinated += 1

        if not retrieval_success:
            retrieval_failures += 1

        results.append({
            "query": query,
            "expected": expected,
            "predicted": predicted,
            "retrieval_success": bool(retrieval_success),
            "correct": judgement["correct"],
            "score": judgement["score"],
            "reason": judgement["reason"],
            "is_fallback": is_fallback, 
            "hallucination_detail": hallucination_detail,
            "confidence_scores": confidence_scores, 
            "avg_confidence":    avg_confidence
        })

    metrics = {
        "split": split,  
        "accuracy": correct / len(data),
        "hallucination_rate": hallucinated / len(data),
        "retrieval_failure_rate": retrieval_failures / len(data),
        "samples": len(data)
    }

    return metrics, results

def save_results(metrics, results, filename="eval_results.json"):
    output = {
        "metrics": metrics,
        "detailed_results": results
    }

    with open(filename, "w") as f:
        json.dump(output, f, indent=2)

train_metrics, train_results = evaluate(train_dataset, "train", sample=200)
test_metrics, test_results   = evaluate(test_dataset, "test", sample=20)

# Bucket failures:
# retrieval_failed = len([x for x in test_results if not x["retrieval_success"]])
# retrieval_ok_but_wrong = len([x for x in test_results if x["retrieval_success"] and not x["correct"]])
# fallbacks = len([x for x in test_results if x["is_fallback"]])

# print(retrieval_failed)
# print(retrieval_ok_but_wrong)
# print(fallbacks)

# save_results(train_metrics, train_results, "train_eval.json")
save_results(test_metrics, test_results, "test_eval.json")