-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (53 loc) · 1.83 KB
/
main.py
File metadata and controls
76 lines (53 loc) · 1.83 KB
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
from sentence_transformers import SentenceTransformer
from llama_cpp import Llama
import faiss
import os
embedder = SentenceTransformer('all-MiniLM-L6-v2')
def load_text(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
def chunk_text(text, chunk_size=500):
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
def create_faiss_index(chunks):
embeddings = embedder.encode(chunks)
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
return index, chunks
def search_index(query, index, chunks, top_k=3):
q_vec = embedder.encode([query])
distances, indices = index.search(q_vec, top_k)
return [chunks[i] for i in indices[0]]
llm = Llama(
model_path="models/llama-2-7b.Q4_K_M.gguf",
n_ctx=2048,
n_threads=6,
verbose=False
)
def generate_answer(context, question):
prompt = f"""You are a helpful assistant. Use the following context to answer the question.
Context:
{context}
Question: {question}
Answer:"""
output = llm(prompt, max_tokens=256, stop=["\n\n"])
return output["choices"][0]["text"].strip()
def main():
file_path = 'docs/nsx.txt'
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
return
print("Reading file and building index...")
text = load_text(file_path)
chunks = chunk_text(text)
index, stored_chunks = create_faiss_index(chunks)
print(f"Ready! {len(chunks)} chunks indexed.")
while True:
query = input("\nAsk your question (or 'exit'): ")
if query.lower() == 'exit':
break
top_chunks = search_index(query, index, stored_chunks)
context = "\n".join(top_chunks)
answer = generate_answer(context, query)
print(f"\n Answer:\n{answer}\n")
if __name__ == '__main__':
main()