Spaces:
Running
Running
swaroop-uddandarao
commited on
Commit
·
0dd3f9b
1
Parent(s):
8048d7d
added try and except
Browse files- app.py +40 -34
- generationhelper.py +34 -27
app.py
CHANGED
@@ -38,56 +38,62 @@ EmbedAllDocumentsAndInsert(QUERY_EMBEDDING_MODEL, rag_extracted_data, db_collect
|
|
38 |
"""
|
39 |
|
40 |
def EvaluateRAGModel(question, evaluation_model):
|
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 |
-
return answer, rmsecontextrel, rmsecontextutil, aucscore, execution_time
|
90 |
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Create Gradio UI
|
93 |
with gr.Blocks() as iface:
|
|
|
38 |
"""
|
39 |
|
40 |
def EvaluateRAGModel(question, evaluation_model):
|
41 |
+
try:
|
42 |
+
start_time = time.time()
|
43 |
|
44 |
+
query = question.strip()
|
45 |
|
46 |
+
if evaluation_model == "LLaMA 3.3":
|
47 |
+
EVAL_MODEL = "llama-3.3-70b-specdec"
|
48 |
+
PROMPT_MODEL = "llama-3.3-70b-specdec"
|
49 |
+
elif evaluation_model == "Mistral 7B":
|
50 |
+
EVAL_MODEL = "mixtral-8x7b-32768"
|
51 |
+
PROMPT_MODEL = "mixtral-8x7b-32768"
|
52 |
+
elif evaluation_model == "Deepseek 70b":
|
53 |
+
EVAL_MODEL = "deepseek-r1-distill-llama-70b"
|
54 |
+
PROMPT_MODEL = "deepseek-r1-distill-llama-70b"
|
55 |
|
56 |
+
#invoke create milvus db function
|
57 |
+
try:
|
58 |
+
db_collection = CreateMilvusDbSchema()
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error creating Milvus DB schema: {e}")
|
61 |
|
62 |
+
#insert embdeding to milvus db
|
63 |
|
64 |
+
#query = "what would the net revenue have been in 2015 if there wasn't a stipulated settlement from the business combination in october 2015?"
|
65 |
|
66 |
+
results_for_top10_chunks = SearchTopKDocuments(db_collection, query, QUERY_EMBEDDING_MODEL, top_k=RETRIVE_TOP_K_SIZE)
|
67 |
|
68 |
+
reranked_results = FineTuneAndRerankSearchResults(results_for_top10_chunks, rag_extracted_data, query, RERANKING_MODEL)
|
69 |
|
70 |
+
answer = GenerateAnswer(query, reranked_results.head(3), PROMPT_MODEL)
|
71 |
|
72 |
+
completion_result,relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level = FormatAndScores(query, reranked_results.head(1), answer, EVAL_MODEL)
|
73 |
|
74 |
|
75 |
+
print(relevant_sentence_keys)
|
76 |
+
print(all_utilized_sentence_keys)
|
77 |
+
print(support_keys)
|
78 |
+
print(support_level)
|
79 |
+
print(completion_result)
|
80 |
|
81 |
+
document_id = reranked_results.head(1)['doc_id'].values[0]
|
82 |
+
extarcted_row_for_given_id = rag_extracted_data[rag_extracted_data["id"]==document_id]
|
83 |
|
84 |
+
rmsecontextrel, rmsecontextutil, aucscore = CalculateScores(relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level,extarcted_row_for_given_id)
|
85 |
|
86 |
+
print(rmsecontextrel)
|
87 |
+
print(rmsecontextutil)
|
88 |
+
print(aucscore)
|
89 |
+
end_time = time.time()
|
90 |
|
91 |
+
execution_time = end_time - start_time
|
|
|
|
|
92 |
|
93 |
+
return answer, rmsecontextrel, rmsecontextutil, aucscore, execution_time
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Error in evaluation: {e}")
|
96 |
+
return "I apologize, but I encountered an error processing your question. Please try again.", 0, 0, 0, time.time() - start_time
|
97 |
|
98 |
# Create Gradio UI
|
99 |
with gr.Blocks() as iface:
|
generationhelper.py
CHANGED
@@ -8,35 +8,42 @@ groq_client = Groq(
|
|
8 |
)
|
9 |
|
10 |
|
11 |
-
|
|
|
12 |
"""
|
13 |
Generates an answer using an AI model based on the top retrieved documents.
|
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 |
return response_text
|
|
|
8 |
)
|
9 |
|
10 |
|
11 |
+
|
12 |
+
def GenerateAnswer(query, top_documents, prompt_model, timeout_seconds: int = 30):
|
13 |
"""
|
14 |
Generates an answer using an AI model based on the top retrieved documents.
|
15 |
"""
|
16 |
+
try:
|
17 |
+
# Convert each document (if it's a list) into a string before joining
|
18 |
+
documents = "\n\n".join([" ".join(doc) if isinstance(doc, list) else str(doc) for doc in top_documents["document"]])
|
19 |
+
|
20 |
+
# Construct the prompt
|
21 |
+
prompt = f"""
|
22 |
+
You are an AI assistant tasked with answering a question based on the information provided in the given documents. Your response should be accurate, concise, and directly address the question using only the information from the documents. If the documents do not contain sufficient information to answer the question, state that clearly.
|
23 |
+
|
24 |
+
Documents:
|
25 |
+
{documents}
|
26 |
+
|
27 |
+
Question: {query}
|
28 |
+
Answer:
|
29 |
+
"""
|
30 |
+
|
31 |
+
# Call Groq API (Llama 3.3-70B)
|
32 |
+
completion = groq_client.chat.completions.create(
|
33 |
+
model=prompt_model,
|
34 |
+
messages=[{"role": "user", "content": prompt}],
|
35 |
+
temperature=0.7,
|
36 |
+
max_tokens=2048,
|
37 |
+
top_p=1,
|
38 |
+
timeout=timeout_seconds
|
39 |
+
)
|
40 |
+
|
41 |
+
# Extract and print the response
|
42 |
+
response_text = completion.choices[0].message.content
|
43 |
+
print("\nGenerated Response:\n", response_text)
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Error generating answer: {e}")
|
47 |
+
return None
|
48 |
|
49 |
return response_text
|