Skip to content

Commit f21b6e7

Browse files
authored
🪨 feat: AWS Bedrock embeddings support (#75)
* "WIP: adding bedrock embeddings" * WIP: feat bedrock embeddings support * feat: aws bedrock embeddings support * refactor: update aws region var name * docs: update env variables documentation for bedrock * docs: add bedrock embeddings provider in list
1 parent edd8a0c commit f21b6e7

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ The following environment variables are required to run the application:
6161
- `PDF_EXTRACT_IMAGES`: (Optional) A boolean value indicating whether to extract images from PDF files. Default value is "False".
6262
- `DEBUG_RAG_API`: (Optional) Set to "True" to show more verbose logging output in the server console, and to enable postgresql database routes
6363
- `CONSOLE_JSON`: (Optional) Set to "True" to log as json for Cloud Logging aggregations
64-
- `EMBEDDINGS_PROVIDER`: (Optional) either "openai", "azure", "huggingface", "huggingfacetei" or "ollama", where "huggingface" uses sentence_transformers; defaults to "openai"
64+
- `EMBEDDINGS_PROVIDER`: (Optional) either "openai", "bedrock", "azure", "huggingface", "huggingfacetei" or "ollama", where "huggingface" uses sentence_transformers; defaults to "openai"
6565
- `EMBEDDINGS_MODEL`: (Optional) Set a valid embeddings model to use from the configured provider.
6666
- **Defaults**
6767
- openai: "text-embedding-3-small"
6868
- azure: "text-embedding-3-small" (will be used as your Azure Deployment)
6969
- huggingface: "sentence-transformers/all-MiniLM-L6-v2"
7070
- huggingfacetei: "http://huggingfacetei:3000". Hugging Face TEI uses model defined on TEI service launch.
7171
- ollama: "nomic-embed-text"
72+
- bedrock: "amazon.titan-embed-text-v1"
7273
- `RAG_AZURE_OPENAI_API_VERSION`: (Optional) Default is `2023-05-15`. The version of the Azure OpenAI API.
7374
- `RAG_AZURE_OPENAI_API_KEY`: (Optional) The API key for Azure OpenAI service.
7475
- Note: `AZURE_OPENAI_API_KEY` will work but `RAG_AZURE_OPENAI_API_KEY` will override it in order to not conflict with LibreChat setting.
@@ -79,6 +80,9 @@ The following environment variables are required to run the application:
7980
- `OLLAMA_BASE_URL`: (Optional) defaults to `http://ollama:11434`.
8081
- `ATLAS_SEARCH_INDEX`: (Optional) the name of the vector search index if using Atlas MongoDB, defaults to `vector_index`
8182
- `MONGO_VECTOR_COLLECTION`: Deprecated for MongoDB, please use `ATLAS_SEARCH_INDEX` and `COLLECTION_NAME`
83+
- `AWS_DEFAULT_REGION`: (Optional) defaults to `us-east-1`
84+
- `AWS_ACCESS_KEY_ID`: (Optional) needed for bedrock embeddings
85+
- `AWS_SECRET_ACCESS_KEY`: (Optional) needed for bedrock embeddings
8286

8387
Make sure to set these environment variables before running the application. You can set them in a `.env` file or as system environment variables.
8488

config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import os
33
import json
44
import logging
5+
import boto3
56
from enum import Enum
67
from datetime import datetime
78
from dotenv import find_dotenv, load_dotenv
89
from langchain_ollama import OllamaEmbeddings
910
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpointEmbeddings
11+
from langchain_aws import BedrockEmbeddings
1012
from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings
1113
from starlette.middleware.base import BaseHTTPMiddleware
1214
from store_factory import get_vector_store
@@ -25,6 +27,7 @@ class EmbeddingsProvider(Enum):
2527
HUGGINGFACE = "huggingface"
2628
HUGGINGFACETEI = "huggingfacetei"
2729
OLLAMA = "ollama"
30+
BEDROCK = "bedrock"
2831

2932

3033
def get_env_variable(
@@ -168,6 +171,8 @@ async def dispatch(self, request, call_next):
168171
).rstrip("/")
169172
HF_TOKEN = get_env_variable("HF_TOKEN", "")
170173
OLLAMA_BASE_URL = get_env_variable("OLLAMA_BASE_URL", "http://ollama:11434")
174+
AWS_ACCESS_KEY_ID = get_env_variable("AWS_ACCESS_KEY_ID", "")
175+
AWS_SECRET_ACCESS_KEY = get_env_variable("AWS_SECRET_ACCESS_KEY", "")
171176

172177
## Embeddings
173178

@@ -195,6 +200,17 @@ def init_embeddings(provider, model):
195200
return HuggingFaceEndpointEmbeddings(model=model)
196201
elif provider == EmbeddingsProvider.OLLAMA:
197202
return OllamaEmbeddings(model=model, base_url=OLLAMA_BASE_URL)
203+
elif provider == EmbeddingsProvider.BEDROCK:
204+
session = boto3.Session(
205+
aws_access_key_id=AWS_ACCESS_KEY_ID,
206+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
207+
region_name=AWS_DEFAULT_REGION,
208+
)
209+
return BedrockEmbeddings(
210+
client=session.client("bedrock-runtime"),
211+
model_id=model,
212+
region_name=AWS_DEFAULT_REGION,
213+
)
198214
else:
199215
raise ValueError(f"Unsupported embeddings provider: {provider}")
200216

@@ -217,6 +233,13 @@ def init_embeddings(provider, model):
217233
)
218234
elif EMBEDDINGS_PROVIDER == EmbeddingsProvider.OLLAMA:
219235
EMBEDDINGS_MODEL = get_env_variable("EMBEDDINGS_MODEL", "nomic-embed-text")
236+
elif EMBEDDINGS_PROVIDER == EmbeddingsProvider.BEDROCK:
237+
EMBEDDINGS_MODEL = get_env_variable(
238+
"EMBEDDINGS_MODEL", "amazon.titan-embed-text-v1"
239+
)
240+
AWS_DEFAULT_REGION = get_env_variable(
241+
"AWS_DEFAULT_REGION", "us-east-1"
242+
)
220243
else:
221244
raise ValueError(f"Unsupported embeddings provider: {EMBEDDINGS_PROVIDER}")
222245

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ langchain==0.3
22
langchain_community==0.3
33
langchain_openai==0.2.0
44
langchain_core==0.3.5
5+
langchain-aws==0.2.1
6+
boto3==1.34.144
57
sqlalchemy==2.0.28
68
python-dotenv==1.0.1
79
fastapi==0.110.0

0 commit comments

Comments
 (0)