quick fix to use openai instead of ollama, in vetor_storage.py

This commit is contained in:
2026-02-05 00:04:10 +03:00
parent f87f3c0cdd
commit 833aad317a
2 changed files with 40 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
"""Vector storage module using Qdrant and Ollama embeddings for the RAG solution."""
"""Vector storage module using Qdrant and configurable embeddings for the RAG solution."""
import os
from typing import Optional
@@ -7,6 +7,7 @@ from dotenv import load_dotenv
from langchain_qdrant import QdrantVectorStore
from langchain_core.documents import Document
from langchain_ollama import OllamaEmbeddings
from langchain_openai import OpenAIEmbeddings
from qdrant_client import QdrantClient
# Load environment variables
@@ -17,15 +18,19 @@ QDRANT_HOST = os.getenv("QDRANT_HOST", "localhost")
QDRANT_REST_PORT = int(os.getenv("QDRANT_REST_PORT", 6333))
QDRANT_GRPC_PORT = int(os.getenv("QDRANT_GRPC_PORT", 6334))
# Ollama embedding model configuration
# Embedding model configuration
EMBEDDING_STRATEGY = os.getenv("EMBEDDING_STRATEGY", "ollama").lower()
OLLAMA_EMBEDDING_MODEL = os.getenv("OLLAMA_EMBEDDING_MODEL", "nomic-embed-text")
OPENAI_EMBEDDING_MODEL = os.getenv("OPENAI_EMBEDDING_MODEL", "text-embedding-ada-002")
OPENAI_EMBEDDING_BASE_URL = os.getenv("OPENAI_EMBEDDING_BASE_URL")
OPENAI_EMBEDDING_API_KEY = os.getenv("OPENAI_EMBEDDING_API_KEY")
def initialize_vector_store(
collection_name: str = "documents_langchain", recreate_collection: bool = False
) -> QdrantVectorStore:
"""
Initialize and return a Qdrant vector store with Ollama embeddings.
Initialize and return a Qdrant vector store with configurable embeddings.
Args:
collection_name: Name of the Qdrant collection to use
@@ -34,11 +39,24 @@ def initialize_vector_store(
Returns:
Initialized Qdrant vector store
"""
# Initialize Ollama embeddings
embeddings = OllamaEmbeddings(
model=OLLAMA_EMBEDDING_MODEL,
base_url="http://localhost:11434", # Default Ollama URL
)
# Determine which embedding strategy to use
if EMBEDDING_STRATEGY == "openai":
# Validate required OpenAI embedding variables
if not OPENAI_EMBEDDING_API_KEY or not OPENAI_EMBEDDING_BASE_URL:
raise ValueError("OPENAI_EMBEDDING_API_KEY and OPENAI_EMBEDDING_BASE_URL must be set when using OpenAI embedding strategy")
# Initialize OpenAI embeddings
embeddings = OpenAIEmbeddings(
model=OPENAI_EMBEDDING_MODEL,
openai_api_base=OPENAI_EMBEDDING_BASE_URL,
openai_api_key=OPENAI_EMBEDDING_API_KEY,
)
else: # Default to ollama
# Initialize Ollama embeddings
embeddings = OllamaEmbeddings(
model=OLLAMA_EMBEDDING_MODEL,
base_url="http://localhost:11434", # Default Ollama URL
)
# Check if collection exists and create if needed
client = QdrantClient(