Update src/rag_engine.py
Browse files- src/rag_engine.py +11 -7
src/rag_engine.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
| 1 |
import os
|
| 2 |
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class ProjectRAGEngine:
|
| 12 |
def __init__(self):
|
| 13 |
-
# ✅
|
| 14 |
-
self.embeddings =
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# ✅ OpenRouter LLM
|
| 19 |
self.llm = ChatOpenAI(
|
| 20 |
model="openai/gpt-oss-120b:free",
|
| 21 |
temperature=0,
|
|
@@ -43,6 +46,7 @@ class ProjectRAGEngine:
|
|
| 43 |
|
| 44 |
splits = splitter.split_documents(all_docs)
|
| 45 |
|
|
|
|
| 46 |
self.vector_store = FAISS.from_documents(
|
| 47 |
splits, self.embeddings
|
| 48 |
)
|
|
@@ -59,10 +63,10 @@ class ProjectRAGEngine:
|
|
| 59 |
Answer strictly using the context.
|
| 60 |
If unknown, say you don't know.
|
| 61 |
Cite document names and page numbers.
|
| 62 |
-
|
| 63 |
Context:
|
| 64 |
{context}
|
| 65 |
-
|
| 66 |
Question:
|
| 67 |
{question}
|
| 68 |
"""
|
|
|
|
| 1 |
import os
|
| 2 |
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
|
| 11 |
|
| 12 |
class ProjectRAGEngine:
|
| 13 |
def __init__(self):
|
| 14 |
+
# ✅ Hugging Face Embeddings (LOCAL / FREE)
|
| 15 |
+
self.embeddings = HuggingFaceEmbeddings(
|
| 16 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 17 |
+
model_kwargs={"device": "cpu"}, # change to "cuda" if GPU available
|
| 18 |
+
encode_kwargs={"normalize_embeddings": True}
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ✅ OpenRouter LLM (Chat only)
|
| 22 |
self.llm = ChatOpenAI(
|
| 23 |
model="openai/gpt-oss-120b:free",
|
| 24 |
temperature=0,
|
|
|
|
| 46 |
|
| 47 |
splits = splitter.split_documents(all_docs)
|
| 48 |
|
| 49 |
+
# ✅ FAISS with HuggingFace embeddings
|
| 50 |
self.vector_store = FAISS.from_documents(
|
| 51 |
splits, self.embeddings
|
| 52 |
)
|
|
|
|
| 63 |
Answer strictly using the context.
|
| 64 |
If unknown, say you don't know.
|
| 65 |
Cite document names and page numbers.
|
| 66 |
+
|
| 67 |
Context:
|
| 68 |
{context}
|
| 69 |
+
|
| 70 |
Question:
|
| 71 |
{question}
|
| 72 |
"""
|