From 1a8f65368bd194718496e046334d98bd317ce57f Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 3 Apr 2024 12:46:53 -0400 Subject: [PATCH 1/4] fix: correctly handle embedding errors --- main.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index a72444f..37160e1 100644 --- a/main.py +++ b/main.py @@ -310,6 +310,8 @@ async def embed_local_file(document: StoreDocument, request: Request): @app.post("/embed") async def embed_file(request: Request, file_id: str = Form(...), file: UploadFile = File(...)): + response_status = True + response_message = "File processed successfully." known_type = None if not hasattr(request.state, 'user'): user_id = "public" @@ -335,11 +337,25 @@ async def embed_file(request: Request, file_id: str = Form(...), file: UploadFil result = await store_data_in_vector_db(data, file_id, user_id) if not result: + response_status = False + response_message = "Failed to process/store the file data." raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to process/store the file data.", ) + elif 'error' in result: + response_status = False + response_message = "Failed to process/store the file data." + if isinstance(result['error'], str): + response_message = result['error'] + else: + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="An unspecified error occurred.", + ) except Exception as e: + response_status = False + response_message = f"Error during file processing: {str(e)}" raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Error during file processing: {str(e)}") finally: @@ -349,8 +365,8 @@ async def embed_file(request: Request, file_id: str = Form(...), file: UploadFil logger.info(f"Failed to remove temporary file: {str(e)}") return { - "status": True, - "message": "File processed successfully.", + "status": response_status, + "message": response_message, "file_id": file_id, "filename": file.filename, "known_type": known_type, From 60a588c1ca5b6407a6bd083c225947e35d12119f Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 3 Apr 2024 13:01:13 -0400 Subject: [PATCH 2/4] chore: log query error --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 37160e1..2695ac7 100644 --- a/main.py +++ b/main.py @@ -182,6 +182,7 @@ async def query_embeddings_by_file_id(body: QueryRequestBody, request: Request): return authorized_documents except Exception as e: + logger.error(e) raise HTTPException(status_code=500, detail=str(e)) From 1fc7f75a9e082484a520cb75ff2d4b9fd75bb294 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 3 Apr 2024 13:41:56 -0400 Subject: [PATCH 3/4] refactor: add override environment variables to be used as default --- README.md | 12 +++++++++--- config.py | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b5c8f33..cbc97ea 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ uvicorn main:app The following environment variables are required to run the application: -- `OPENAI_API_KEY`: The API key for OpenAI API Embeddings (if using default settings). +- `RAG_OPENAI_API_KEY`: The API key for OpenAI API Embeddings (if using default settings). + - Note: `OPENAI_API_KEY` will work but `RAG_OPENAI_API_KEY` will override it in order to not conflict with LibreChat setting. +- `OPENAI_BASEURL`: (Optional) The base URL for your OpenAI API Embeddings +- `RAG_OPENAI_PROXY`: (Optional) Proxy for OpenAI API Embeddings - `POSTGRES_DB`: (Optional) The name of the PostgreSQL database. - `POSTGRES_USER`: (Optional) The username for connecting to the PostgreSQL database. - `POSTGRES_PASSWORD`: (Optional) The password for connecting to the PostgreSQL database. @@ -65,8 +68,11 @@ The following environment variables are required to run the application: - huggingface: "sentence-transformers/all-MiniLM-L6-v2" - huggingfacetei: "http://huggingfacetei:3000". Hugging Face TEI uses model defined on TEI service launch. - ollama: "nomic-embed-text" -- `AZURE_OPENAI_API_KEY`: (Optional) The API key for Azure OpenAI service. -- `AZURE_OPENAI_ENDPOINT`: (Optional) The endpoint URL for Azure OpenAI service, including the resource. Example: `https://example-resource.azure.openai.com/`. +- `RAG_AZURE_OPENAI_API_KEY`: (Optional) The API key for Azure OpenAI service. + - Note: `AZURE_OPENAI_API_KEY` will work but `RAG_AZURE_OPENAI_API_KEY` will override it in order to not conflict with LibreChat setting. +- `RAG_AZURE_OPENAI_ENDPOINT`: (Optional) The endpoint URL for Azure OpenAI service, including the resource. + - Example: `https://example-resource.azure.openai.com`. + - Note: `AZURE_OPENAI_ENDPOINT` will work but `RAG_AZURE_OPENAI_ENDPOINT` will override it in order to not conflict with LibreChat setting. - `HF_TOKEN`: (Optional) if needed for `huggingface` option. - `OLLAMA_BASE_URL`: (Optional) defaults to `http://ollama:11434`. diff --git a/config.py b/config.py index ebd57b9..fc74f3c 100644 --- a/config.py +++ b/config.py @@ -15,10 +15,10 @@ load_dotenv(find_dotenv()) -def get_env_variable(var_name: str, default_value: str = None) -> str: +def get_env_variable(var_name: str, default_value: str = None, required: bool = False) -> str: value = os.getenv(var_name) if value is None: - if default_value is None: + if default_value is None and required: raise ValueError(f"Environment variable '{var_name}' not found.") return default_value return value @@ -130,8 +130,13 @@ async def dispatch(self, request, call_next): ## Credentials OPENAI_API_KEY = get_env_variable("OPENAI_API_KEY", "") +RAG_OPENAI_API_KEY = get_env_variable("RAG_OPENAI_API_KEY", OPENAI_API_KEY) +OPENAI_BASEURL = get_env_variable("OPENAI_BASEURL", None) +RAG_OPENAI_PROXY = get_env_variable("RAG_OPENAI_PROXY", None) AZURE_OPENAI_API_KEY = get_env_variable("AZURE_OPENAI_API_KEY", "") +RAG_AZURE_OPENAI_API_KEY = get_env_variable("RAG_AZURE_OPENAI_API_KEY", AZURE_OPENAI_API_KEY) AZURE_OPENAI_ENDPOINT = get_env_variable("AZURE_OPENAI_ENDPOINT", "") +RAG_AZURE_OPENAI_ENDPOINT = get_env_variable("RAG_AZURE_OPENAI_ENDPOINT", AZURE_OPENAI_ENDPOINT).rstrip("/") HF_TOKEN = get_env_variable("HF_TOKEN", "") OLLAMA_BASE_URL = get_env_variable("OLLAMA_BASE_URL", "http://ollama:11434") @@ -140,10 +145,18 @@ async def dispatch(self, request, call_next): def init_embeddings(provider, model): if provider == "openai": - return OpenAIEmbeddings(model=model, api_key=OPENAI_API_KEY) + return OpenAIEmbeddings( + model=model, + api_key=RAG_OPENAI_API_KEY, + openai_api_base=OPENAI_BASEURL, + openai_proxy=RAG_OPENAI_PROXY + ) elif provider == "azure": - return AzureOpenAIEmbeddings(model=model, - api_key=AZURE_OPENAI_API_KEY) # AZURE_OPENAI_ENDPOINT is being grabbed from the environment + return AzureOpenAIEmbeddings( + model=model, + api_key=RAG_AZURE_OPENAI_API_KEY, + azure_endpoint=RAG_AZURE_OPENAI_ENDPOINT + ) elif provider == "huggingface": return HuggingFaceEmbeddings(model_name=model, encode_kwargs={ 'normalize_embeddings': True}) From 866c65b27a7b2f826c55c100a9700bfff8e4be16 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 3 Apr 2024 13:52:44 -0400 Subject: [PATCH 4/4] chore: rename OPENAI_BASEURL to RAG_OPENAI_BASEURL as standard --- README.md | 2 +- config.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cbc97ea..8b6084b 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ The following environment variables are required to run the application: - `RAG_OPENAI_API_KEY`: The API key for OpenAI API Embeddings (if using default settings). - Note: `OPENAI_API_KEY` will work but `RAG_OPENAI_API_KEY` will override it in order to not conflict with LibreChat setting. -- `OPENAI_BASEURL`: (Optional) The base URL for your OpenAI API Embeddings +- `RAG_OPENAI_BASEURL`: (Optional) The base URL for your OpenAI API Embeddings - `RAG_OPENAI_PROXY`: (Optional) Proxy for OpenAI API Embeddings - `POSTGRES_DB`: (Optional) The name of the PostgreSQL database. - `POSTGRES_USER`: (Optional) The username for connecting to the PostgreSQL database. diff --git a/config.py b/config.py index fc74f3c..43c2437 100644 --- a/config.py +++ b/config.py @@ -131,7 +131,7 @@ async def dispatch(self, request, call_next): OPENAI_API_KEY = get_env_variable("OPENAI_API_KEY", "") RAG_OPENAI_API_KEY = get_env_variable("RAG_OPENAI_API_KEY", OPENAI_API_KEY) -OPENAI_BASEURL = get_env_variable("OPENAI_BASEURL", None) +RAG_OPENAI_BASEURL = get_env_variable("RAG_OPENAI_BASEURL", None) RAG_OPENAI_PROXY = get_env_variable("RAG_OPENAI_PROXY", None) AZURE_OPENAI_API_KEY = get_env_variable("AZURE_OPENAI_API_KEY", "") RAG_AZURE_OPENAI_API_KEY = get_env_variable("RAG_AZURE_OPENAI_API_KEY", AZURE_OPENAI_API_KEY) @@ -148,7 +148,7 @@ def init_embeddings(provider, model): return OpenAIEmbeddings( model=model, api_key=RAG_OPENAI_API_KEY, - openai_api_base=OPENAI_BASEURL, + openai_api_base=RAG_OPENAI_BASEURL, openai_proxy=RAG_OPENAI_PROXY ) elif provider == "azure":