From fc7b36cde7c13a68b4b0f6d72991093dedd7ee92 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 27 Sep 2024 17:46:36 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20fix:=20dependency=20crash,?= =?UTF-8?q?=20conditional=20imports=20for=20heavier=20packages=20(#79)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 26 +++++++++++++++++--------- requirements.lite.txt | 18 ++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/config.py b/config.py index d8d65ba5..7b94a976 100644 --- a/config.py +++ b/config.py @@ -1,15 +1,11 @@ # config.py import os import json -import logging import boto3 +import logging from enum import Enum from datetime import datetime from dotenv import find_dotenv, load_dotenv -from langchain_ollama import OllamaEmbeddings -from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpointEmbeddings -from langchain_aws import BedrockEmbeddings -from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings from starlette.middleware.base import BaseHTTPMiddleware from store_factory import get_vector_store @@ -179,6 +175,8 @@ async def dispatch(self, request, call_next): def init_embeddings(provider, model): if provider == EmbeddingsProvider.OPENAI: + from langchain_openai import OpenAIEmbeddings + return OpenAIEmbeddings( model=model, api_key=RAG_OPENAI_API_KEY, @@ -186,6 +184,8 @@ def init_embeddings(provider, model): openai_proxy=RAG_OPENAI_PROXY, ) elif provider == EmbeddingsProvider.AZURE: + from langchain_openai import AzureOpenAIEmbeddings + return AzureOpenAIEmbeddings( azure_deployment=model, api_key=RAG_AZURE_OPENAI_API_KEY, @@ -193,14 +193,22 @@ def init_embeddings(provider, model): api_version=RAG_AZURE_OPENAI_API_VERSION, ) elif provider == EmbeddingsProvider.HUGGINGFACE: + from langchain_huggingface import HuggingFaceEmbeddings + return HuggingFaceEmbeddings( model_name=model, encode_kwargs={"normalize_embeddings": True} ) elif provider == EmbeddingsProvider.HUGGINGFACETEI: + from langchain_huggingface import HuggingFaceEndpointEmbeddings + return HuggingFaceEndpointEmbeddings(model=model) elif provider == EmbeddingsProvider.OLLAMA: + from langchain_ollama import OllamaEmbeddings + return OllamaEmbeddings(model=model, base_url=OLLAMA_BASE_URL) elif provider == EmbeddingsProvider.BEDROCK: + from langchain_aws import BedrockEmbeddings + session = boto3.Session( aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, @@ -237,9 +245,7 @@ def init_embeddings(provider, model): EMBEDDINGS_MODEL = get_env_variable( "EMBEDDINGS_MODEL", "amazon.titan-embed-text-v1" ) - AWS_DEFAULT_REGION = get_env_variable( - "AWS_DEFAULT_REGION", "us-east-1" - ) + AWS_DEFAULT_REGION = get_env_variable("AWS_DEFAULT_REGION", "us-east-1") else: raise ValueError(f"Unsupported embeddings provider: {EMBEDDINGS_PROVIDER}") @@ -258,7 +264,9 @@ def init_embeddings(provider, model): elif VECTOR_DB_TYPE == VectorDBType.ATLAS_MONGO: # Backward compatability check if MONGO_VECTOR_COLLECTION: - logger.info(f"DEPRECATED: Please remove env var MONGO_VECTOR_COLLECTION and instead use COLLECTION_NAME and ATLAS_SEARCH_INDEX. You can set both as same, but not neccessary. See README for more information.") + logger.info( + f"DEPRECATED: Please remove env var MONGO_VECTOR_COLLECTION and instead use COLLECTION_NAME and ATLAS_SEARCH_INDEX. You can set both as same, but not neccessary. See README for more information." + ) ATLAS_SEARCH_INDEX = MONGO_VECTOR_COLLECTION COLLECTION_NAME = MONGO_VECTOR_COLLECTION vector_store = get_vector_store( diff --git a/requirements.lite.txt b/requirements.lite.txt index 19d460c5..3f554bbb 100644 --- a/requirements.lite.txt +++ b/requirements.lite.txt @@ -1,7 +1,7 @@ -langchain==0.1.12 -langchain_community==0.0.34 -langchain_openai==0.0.8 -langchain_core==0.1.45 +langchain==0.3 +langchain_community==0.3 +langchain_openai==0.2.0 +langchain_core==0.3.5 sqlalchemy==2.0.28 python-dotenv==1.0.1 fastapi==0.110.0 @@ -9,7 +9,7 @@ psycopg2-binary==2.9.9 pgvector==0.2.5 uvicorn==0.28.0 pypdf==4.1.0 -unstructured==0.12.6 +unstructured==0.15.13 markdown==3.6 networkx==3.2.1 pandas==2.2.1 @@ -20,11 +20,13 @@ PyJWT==2.8.0 asyncpg==0.29.0 python-multipart==0.0.9 aiofiles==23.2.1 -rapidocr-onnxruntime==1.3.17 +rapidocr-onnxruntime==1.3.24 opencv-python-headless==4.9.0.80 pymongo==4.6.3 -langchain-mongodb==0.1.3 +langchain-mongodb==0.2.0 cryptography==42.0.7 python-magic==0.4.27 python-pptx==0.6.23 -xlrd==2.0.1 \ No newline at end of file +xlrd==2.0.1 +langchain-aws==0.2.1 +boto3==1.34.144