1- import os
21import time
3- from llama_index .core import VectorStoreIndex , SimpleDirectoryReader , Settings
4- from llama_index .llms .llama_cpp import LlamaCPP
5- from llama_index .embeddings .huggingface import HuggingFaceEmbedding
2+
63from codecarbon import OfflineEmissionsTracker
7- from ragas import evaluate
8- from ragas .metrics import faithfulness , answer_relevancy
94from datasets import Dataset
105from langchain_community .llms import LlamaCpp
6+ from llama_index .core import Settings , SimpleDirectoryReader , VectorStoreIndex
7+ from llama_index .embeddings .huggingface import HuggingFaceEmbedding
8+ from llama_index .llms .llama_cpp import LlamaCPP
9+ from ragas import evaluate
1110from ragas .llms import LangchainLLMWrapper
11+ from ragas .metrics import answer_relevancy , faithfulness
1212
1313# --- 1. Configuration ---
1414
1515# Set the path to your downloaded GGUF model
16- MODEL_PATH = "D:/Mistral7B/mistral-7b-instruct-v0.2.Q4_K_M.gguf" # <-- IMPORTANT: Update this path if needed
16+ MODEL_PATH = "D:/Mistral7B/mistral-7b-instruct-v0.2.Q4_K_M.gguf" # <-- IMPORTANT: Update this path if needed
1717
1818# Set the path to your data (PDFs, .txt, etc.)
19- DATA_PATH = "D:/Mistral7B/data" # <-- IMPORTANT: Update this path if needed
19+ DATA_PATH = "D:/Mistral7B/data" # <-- IMPORTANT: Update this path if needed
2020
2121# Set your country's ISO code for CodeCarbon
2222# Find your code: https://en.wikipedia.org/wiki/List_of_ISO_3166-1_alpha-3_codes
2323# Using "EGY" for Egypt as an example
24- YOUR_COUNTRY_ISO_CODE = "EGY"
24+ YOUR_COUNTRY_ISO_CODE = "EGY"
2525
2626# Define your "Golden Set" of test questions
2727TEST_QUESTIONS = [
2828 "What is the main topic of the document?" ,
29- #"Summarize the key findings in three bullet points.",
29+ # "Summarize the key findings in three bullet points.",
3030 # ... add 10-15 more of your own questions ...
31- #"What is [a specific term] according to the text?",
32- #"What conclusion does the author reach?",
31+ # "What is [a specific term] according to the text?",
32+ # "What conclusion does the author reach?",
3333]
3434
3535# --- 2. Initialize Models ---
4343 max_new_tokens = 512 ,
4444 context_window = 3900 ,
4545 generate_kwargs = {},
46- model_kwargs = {"n_gpu_layers" : 1 }, # Set > 0 if you have GPU offloading
46+ model_kwargs = {"n_gpu_layers" : 1 }, # Set > 0 if you have GPU offloading
4747 verbose = True ,
4848)
4949
8484try :
8585 for query in TEST_QUESTIONS :
8686 print (f"\n Querying: { query } " )
87-
87+
8888 # --- Start tracking for this specific query ---
89- tracker .start_task ("RAG Query" )
89+ tracker .start_task ("RAG Query" )
9090 start_time = time .time ()
91-
91+
9292 # Run the query
9393 response = query_engine .query (query )
94-
94+
9595 # --- Stop tracking for this query ---
9696 end_time = time .time ()
9797 # stop_task() returns an EmissionsData OBJECT
98- emissions_data = tracker .stop_task ()
99-
98+ emissions_data = tracker .stop_task ()
99+
100100 # Collect results for ragas
101101 answer = str (response )
102102 contexts = [node .get_content () for node in response .source_nodes ]
103-
103+
104104 eval_data ["question" ].append (query )
105105 eval_data ["answer" ].append (answer )
106106 eval_data ["contexts" ].append (contexts )
107-
107+
108108 # --- Print Results for this Query ---
109109 print (f"Answer: { answer } " )
110110 print ("-" * 30 )
111111 print (f"Latency: { end_time - start_time :.2f} seconds" )
112-
112+
113113 # --- CORRECTED LINES ---
114114 # Access attributes using dot notation
115115 print (f"Emissions: { emissions_data .emissions * 1000 :.6f} gCO2eq" )
116116 print (f"Energy: { emissions_data .energy_consumed * 1000 :.6f} Wh" )
117117 # --- END OF CORRECTION ---
118-
118+
119119 print ("=" * 50 )
120120
121121finally :
124124 total_emissions_kg = tracker .stop ()
125125 print ("\n --- Total Emissions Summary (Saved to emissions.csv) ---" )
126126 # Access total energy from the tracker object itself
127- print (f"Total Energy Consumed: { tracker .final_emissions_data .energy_consumed * 1000 :.4f} Wh" )
127+ print (
128+ f"Total Energy Consumed: { tracker .final_emissions_data .energy_consumed * 1000 :.4f} Wh"
129+ )
128130 print (f"Total CO2 Emitted: { total_emissions_kg * 1000 :.4f} gCO2eq" )
129131 # --- END OF CORRECTION ---
130132
148150# This points to the same model file.
149151eval_llm = LlamaCpp (
150152 model_path = MODEL_PATH ,
151- n_gpu_layers = 1 , # Match your settings from Section 2
152- n_batch = 512 , # Match your settings
153- n_ctx = 3900 , # Match your settings
154- temperature = 0 , # Evaluators should be deterministic
153+ n_gpu_layers = 1 , # Match your settings from Section 2
154+ n_batch = 512 , # Match your settings
155+ n_ctx = 3900 , # Match your settings
156+ temperature = 0 , # Evaluators should be deterministic
155157 verbose = False ,
156158)
157159# 3. Wrap the LangChain object for Ragas
164166 faithfulness ,
165167 answer_relevancy ,
166168 ],
167- llm = ragas_llm , # <-- Pass the evaluator LLM here
168- embeddings = embed_model , # <-- Pass the embeddings here
169+ llm = ragas_llm , # <-- Pass the evaluator LLM here
170+ embeddings = embed_model , # <-- Pass the embeddings here
169171)
170172
171173print ("\n --- Ragas Accuracy Results ---" )
174176# The result will be a dictionary like:
175177# {'faithfulness': 0.85, 'answer_relevancy': 0.92}
176178
177- print ("\n --- Project Evaluation Complete ---" )
179+ print ("\n --- Project Evaluation Complete ---" )
0 commit comments