Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The following environment variables are required to run the application:
- bedrock: "amazon.titan-embed-text-v1"
- google_genai: "gemini-embedding-001"
- `EMBEDDINGS_CHUNK_SIZE`: (Optional) The chunk size used by the OpenAI and Azure embeddings clients to limit the number of inputs per request. Default value is `200`.
- `EMBEDDINGS_DIMENSIONS`: (Optional) Output vector size to request from the embedding model. Only honored by the `openai` and `azure` providers, and only supported by `text-embedding-3-*` models. Leave unset to use the model's native dimensionality (1536 for `text-embedding-3-small`, 3072 for `text-embedding-3-large`). Setting a smaller value (e.g. `512`, `1024`) trades some retrieval quality for lower storage cost and faster similarity search. Note: do not change this on an existing collection — all vectors in a `pgvector` column must share the same dimensionality.
- `RAG_AZURE_OPENAI_API_VERSION`: (Optional) Default is `2023-05-15`. The version of the Azure OpenAI API.
- `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.
Expand Down
28 changes: 24 additions & 4 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,29 +218,35 @@ async def dispatch(self, request, call_next):
## Embeddings


def init_embeddings(provider, model):
def init_embeddings(provider, model, dimensions=None):
if provider == EmbeddingsProvider.OPENAI:
from langchain_openai import OpenAIEmbeddings

return OpenAIEmbeddings(
kwargs = dict(
model=model,
api_key=RAG_OPENAI_API_KEY,
openai_api_base=RAG_OPENAI_BASEURL,
openai_proxy=RAG_OPENAI_PROXY,
chunk_size=EMBEDDINGS_CHUNK_SIZE,
check_embedding_ctx_length=RAG_CHECK_EMBEDDING_CTX_LENGTH,
)
if dimensions is not None:
kwargs["dimensions"] = dimensions
return OpenAIEmbeddings(**kwargs)
elif provider == EmbeddingsProvider.AZURE:
from langchain_openai import AzureOpenAIEmbeddings

return AzureOpenAIEmbeddings(
kwargs = dict(
azure_deployment=model,
api_key=RAG_AZURE_OPENAI_API_KEY,
azure_endpoint=RAG_AZURE_OPENAI_ENDPOINT,
api_version=RAG_AZURE_OPENAI_API_VERSION,
chunk_size=EMBEDDINGS_CHUNK_SIZE,
check_embedding_ctx_length=RAG_CHECK_EMBEDDING_CTX_LENGTH,
)
if dimensions is not None:
kwargs["dimensions"] = dimensions
return AzureOpenAIEmbeddings(**kwargs)
elif provider == EmbeddingsProvider.HUGGINGFACE:
from langchain_huggingface import HuggingFaceEmbeddings

Expand Down Expand Up @@ -298,6 +304,18 @@ def init_embeddings(provider, model):
get_env_variable("EMBEDDINGS_PROVIDER", EmbeddingsProvider.OPENAI.value).lower()
)

# Only parse EMBEDDINGS_DIMENSIONS for providers that honor it (OpenAI / Azure).
# Parsing unconditionally at import would turn an unrelated stale env var
# (e.g. EMBEDDINGS_DIMENSIONS=foo left over from an OpenAI deployment) into a
# hard boot failure under bedrock / hf / ollama / etc., even though those
# providers silently ignore the value.
EMBEDDINGS_DIMENSIONS = None

if EMBEDDINGS_PROVIDER in (EmbeddingsProvider.OPENAI, EmbeddingsProvider.AZURE):
_embeddings_dimensions_raw = get_env_variable("EMBEDDINGS_DIMENSIONS", None)
if _embeddings_dimensions_raw not in (None, ""):
EMBEDDINGS_DIMENSIONS = int(_embeddings_dimensions_raw)

if EMBEDDINGS_PROVIDER == EmbeddingsProvider.OPENAI:
EMBEDDINGS_MODEL = get_env_variable("EMBEDDINGS_MODEL", "text-embedding-3-small")
# 1000 is the default chunk size for OpenAI, but this causes API rate limits to be hit
Expand Down Expand Up @@ -328,7 +346,9 @@ def init_embeddings(provider, model):
else:
raise ValueError(f"Unsupported embeddings provider: {EMBEDDINGS_PROVIDER}")

embeddings = init_embeddings(EMBEDDINGS_PROVIDER, EMBEDDINGS_MODEL)
embeddings = init_embeddings(
EMBEDDINGS_PROVIDER, EMBEDDINGS_MODEL, dimensions=EMBEDDINGS_DIMENSIONS
)

logger.info(f"Initialized embeddings of type: {type(embeddings)}")

Expand Down
Loading