added OpenAI support and markdown in the chat window
This commit is contained in:
+31
-14
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
Local RAG setup with LangChain, Ollama, and FAISS
|
||||
Local RAG setup with LangChain, Ollama/OpenAI, and FAISS
|
||||
Minimal dependencies, simple code
|
||||
"""
|
||||
import os
|
||||
@@ -9,34 +9,51 @@ from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
||||
from langchain_community.vectorstores import FAISS
|
||||
from langchain_huggingface import HuggingFaceEmbeddings
|
||||
from langchain_ollama import ChatOllama
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
|
||||
|
||||
class LocalRAG:
|
||||
def __init__(self, vectorstore_path="./vectorstore", ollama_model="mistral:7b"):
|
||||
"""Initialize local RAG system"""
|
||||
def __init__(
|
||||
self,
|
||||
vectorstore_path="./vectorstore",
|
||||
llm_provider="ollama",
|
||||
ollama_model="gpt-oss:20b",
|
||||
openai_model="gpt-5.2",
|
||||
ollama_base_url="http://localhost:11434",
|
||||
):
|
||||
"""Initialize local RAG system. llm_provider: 'ollama' or 'openai'."""
|
||||
self.vectorstore_path = vectorstore_path
|
||||
self.ollama_model = ollama_model
|
||||
|
||||
self.llm_provider = llm_provider
|
||||
|
||||
# Embeddings
|
||||
print("Loading embeddings model...")
|
||||
self.embeddings = HuggingFaceEmbeddings(
|
||||
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
||||
)
|
||||
|
||||
|
||||
# Text splitter
|
||||
self.text_splitter = RecursiveCharacterTextSplitter(
|
||||
chunk_size=2000,
|
||||
chunk_overlap=400
|
||||
)
|
||||
|
||||
# Ollama LLM
|
||||
print(f"Connecting to Ollama (model: {ollama_model})...")
|
||||
self.llm = ChatOllama(
|
||||
model=ollama_model,
|
||||
base_url="http://localhost:11434"
|
||||
)
|
||||
|
||||
|
||||
# LLM (Ollama or OpenAI)
|
||||
if llm_provider == "openai":
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
if not api_key:
|
||||
raise ValueError(
|
||||
"OPENAI_API_KEY environment variable is required when llm_provider='openai'"
|
||||
)
|
||||
print(f"Using OpenAI (model: {openai_model})...")
|
||||
self.llm = ChatOpenAI(model=openai_model, api_key=api_key)
|
||||
else:
|
||||
print(f"Using Ollama (model: {ollama_model})...")
|
||||
self.llm = ChatOllama(
|
||||
model=ollama_model,
|
||||
base_url=ollama_base_url
|
||||
)
|
||||
|
||||
# Vector store (load if exists, otherwise None)
|
||||
self.vectorstore = None
|
||||
self._load_vectorstore()
|
||||
|
||||
Reference in New Issue
Block a user