Skip to content

Commit bc120bd

Browse files
committed
formatting
1 parent d450fea commit bc120bd

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

MIstral7B/Rag-test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
2-
from llama_index.llms.llama_cpp import LlamaCPP
1+
from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex
32
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
3+
from llama_index.llms.llama_cpp import LlamaCPP
4+
45
# --- Configuration ---
56
# Point to your downloaded model file
6-
MODEL_PATH = "D:/Mistral7B/mistral-7b-instruct-v0.2.Q4_K_M.gguf" # <-- IMPORTANT: update this path
7+
MODEL_PATH = "D:/Mistral7B/mistral-7b-instruct-v0.2.Q4_K_M.gguf" # <-- IMPORTANT: update this path
78

89
# --- 1. Load the LLM (our quantized Mistral model) ---
910
# This uses llama-cpp-python to run the GGUF model on your CPU
@@ -12,9 +13,11 @@
1213
# Model parameters - you can adjust these
1314
temperature=0.1,
1415
max_new_tokens=512,
15-
context_window=3900, # The model's context window size
16+
context_window=3900, # The model's context window size
1617
generate_kwargs={},
17-
model_kwargs={"n_gpu_layers": -1}, # Set to > 0 if you have a GPU and want to offload layers
18+
model_kwargs={
19+
"n_gpu_layers": -1
20+
}, # Set to > 0 if you have a GPU and want to offload layers
1821
verbose=True,
1922
)
2023

@@ -45,12 +48,12 @@
4548
print("\n--- Query Engine Ready ---")
4649
while True:
4750
query = input("Ask a question about your documents: ")
48-
if query.lower() == 'exit':
51+
if query.lower() == "exit":
4952
break
50-
53+
5154
response_stream = query_engine.query(query)
52-
55+
5356
print("\nAssistant: ", end="")
5457
# Stream the response to the console
5558
response_stream.print_response_stream()
56-
print("\n" + "-"*50)
59+
print("\n" + "-" * 50)

MIstral7B/With-Eval.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import os
21
import 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+
63
from codecarbon import OfflineEmissionsTracker
7-
from ragas import evaluate
8-
from ragas.metrics import faithfulness, answer_relevancy
94
from datasets import Dataset
105
from 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
1110
from 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
2727
TEST_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 ---
@@ -43,7 +43,7 @@
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

@@ -84,38 +84,38 @@
8484
try:
8585
for query in TEST_QUESTIONS:
8686
print(f"\nQuerying: {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

121121
finally:
@@ -124,7 +124,9 @@
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

@@ -148,10 +150,10 @@
148150
# This points to the same model file.
149151
eval_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
@@ -164,8 +166,8 @@
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

171173
print("\n--- Ragas Accuracy Results ---")
@@ -174,4 +176,4 @@
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 ---")

MIstral7B/testing-gpu.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
llm = Llama(
1111
model_path=MODEL_PATH,
1212
n_gpu_layers=-1, # Try to offload all layers to GPU
13-
verbose=True # This is the most important part!
13+
verbose=True, # This is the most important part!
1414
)
1515
print("\n--- TEST SUCCESSFUL ---")
1616
# Check the output above for lines mentioning CUDA or cuBLAS and layer offloading
1717

1818
except Exception as e:
19-
print(f"\n--- TEST FAILED ---")
20-
print(f"An error occurred: {e}")
19+
print("\n--- TEST FAILED ---")
20+
print(f"An error occurred: {e}")

0 commit comments

Comments
 (0)