Amazon Bedrock
- Python
- JavaScript
Chroma provides a convinient wrapper for Amazon Bedrock embedding API. This embedding function runs remotely on Amazon's servers, and requires an Amazon client information.
To use Amazon Bedrock embedding API, you must have boto3
Python package installed.
pip install boto3
To pass AWS credential, create an instance of boto3.Session
with your AWS credentials.
Here is the example:
import boto3
import chromadb.utils.embedding_functions as embedding_functions
session = boto3.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name,
)
bedrock = embedding_functions.AmazonBedrockEmbeddingFunction(session=session)
bedrock(["document1","document2"])
By default, the embedding function uses the amazon.titan-embed-text-v1
for the model, but you can specify a different model name with model_name
parameter. For example:
bedrock = embedding_functions.AmazonBedrockEmbeddingFunction(
session=session, model_name="cohere.embed-multilingual-v3")
bedrock(["こんにちは", "你们好"])
To use Amazon Bedrock embedding API, you must have @aws-sdk/client-bedrock-runtime
installed.
You can pass AWS credentials to the constructor of AmazonBedrockEmbeddingFunction
like this:
import { AmazonBedrockEmbeddingFunction } from 'chromadb';
const ef = new AmazonBedrockEmbeddingFunction({
config: {
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
region: "us-east-1" // Default region. You can change it to your region.
},
})
// use directly
const embeddings = await ef.generate(["foo"])
// pass documents to query for .add and .query
const collection = await client.createCollection({name: "name", embeddingFunction: ef})
const collection = await client.getCollection({name: "name", embeddingFunction: ef})
If you want to use other credentials, e.g., SSO, Temporary Credential, etc., you need to install @aws-sdk/credential-providers
.
See AWS SDK for JavaScript v3 for more information.
Here is the example using SSO credentials:
import { AmazonBedrockEmbeddingFunction } from 'chromadb';
import { fromSSO } = from '@aws-sdk/credential-providers';
const c = await fromSSO({profile: "my-profile"})
const ef = new AmazonBedrockEmbeddingFunction({
config: {
credentials: c,
region: "us-east-1"
},
})