Uunan commited on
Commit
dc03ec3
·
verified ·
1 Parent(s): aa48680

Create rag_pipeline.py

Browse files
Files changed (1) hide show
  1. rag_pipeline.py +75 -0
rag_pipeline.py CHANGED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import faiss
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain_community.document_loaders import PyPDFLoader, TextLoader
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from llama_cpp import Llama
9
+
10
+ # Embedder
11
+ embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
12
+
13
+ # LLM
14
+ llm = Llama(
15
+ model_path="model/qwen2_5-7b-instruct-q4_K_M.gguf",
16
+ n_ctx=4096,
17
+ n_threads=4,
18
+ chat_format="chatml"
19
+ )
20
+
21
+ def load_documents():
22
+ docs = []
23
+ for file in os.listdir("docs"):
24
+ path = f"docs/{file}"
25
+ if file.endswith(".pdf"):
26
+ loader = PyPDFLoader(path)
27
+ else:
28
+ loader = TextLoader(path)
29
+ docs.extend(loader.load())
30
+ return docs
31
+
32
+ def prepare_vector_store():
33
+ if os.path.exists("vectorstore.faiss"):
34
+ return FAISS.load_local("vectorstore", embedder)
35
+
36
+ docs = load_documents()
37
+ splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
38
+ chunks = splitter.split_documents(docs)
39
+
40
+ embeddings = embedder.encode([c.page_content for c in chunks])
41
+
42
+ index = faiss.IndexFlatL2(embeddings.shape[1])
43
+ index.add(np.array(embeddings).astype("float32"))
44
+
45
+ vectorstore = FAISS(embedding_function=embedder, index=index, docs=chunks)
46
+ vectorstore.save_local("vectorstore")
47
+
48
+ return vectorstore
49
+
50
+ vectorstore = prepare_vector_store()
51
+
52
+ def ask_rag(question):
53
+ results = vectorstore.similarity_search(question, k=5)
54
+ context = "\n".join([r.page_content for r in results])
55
+
56
+ template = f"""
57
+ Aşağıdaki bağlama göre soruyu cevapla:
58
+
59
+ BAĞLAM:
60
+ {context}
61
+
62
+ SORU:
63
+ {question}
64
+
65
+ Cevap:
66
+ """
67
+
68
+ out = llm(
69
+ template,
70
+ max_tokens=500,
71
+ temperature=0.4,
72
+ stop=["</s>"]
73
+ )
74
+
75
+ return out["choices"][0]["text"].strip()