|
| 1 | +--- |
| 2 | +title: AWS Bedrock Embedder |
| 3 | +sidebarTitle: AWS Bedrock |
| 4 | +--- |
| 5 | + |
| 6 | +The `AwsBedrockEmbedder` class is used to embed text data into vectors using the AWS Bedrock API. By default, it uses the Cohere Embed Multilingual V3 model for generating embeddings. |
| 7 | + |
| 8 | +# Setup |
| 9 | + |
| 10 | +## Set your AWS credentials |
| 11 | + |
| 12 | +```bash |
| 13 | +export AWS_ACCESS_KEY_ID = xxx |
| 14 | +export AWS_SECRET_ACCESS_KEY = xxx |
| 15 | +export AWS_REGION = xxx |
| 16 | +``` |
| 17 | + |
| 18 | +<Note> |
| 19 | +By default, this embedder uses the `cohere.embed-multilingual-v3` model. You must enable access to this model from the AWS Bedrock model catalog before using this embedder. |
| 20 | +</Note> |
| 21 | + |
| 22 | +## Run PgVector |
| 23 | +```bash |
| 24 | +docker run - d \ |
| 25 | + - e POSTGRES_DB = ai \ |
| 26 | + - e POSTGRES_USER = ai \ |
| 27 | + - e POSTGRES_PASSWORD = ai \ |
| 28 | + - e PGDATA = /var/lib/postgresql/data/pgdata \ |
| 29 | + - v pgvolume: / var/lib/postgresql/data \ |
| 30 | + - p 5532: 5432 \ |
| 31 | + - -name pgvector \ |
| 32 | + agnohq/pgvector: 16 |
| 33 | +``` |
| 34 | + |
| 35 | +# Usage |
| 36 | + |
| 37 | +```python cookbook/embedders/aws_bedrock_embedder.py |
| 38 | + |
| 39 | +# Embed sentence in database |
| 40 | +embeddings = AwsBedrockEmbedder().get_embedding( |
| 41 | + "The quick brown fox jumps over the lazy dog." |
| 42 | +) |
| 43 | +# Print the embeddings and their dimensions |
| 44 | +print(f"Embeddings: {embeddings[:5]}") |
| 45 | +print(f"Dimensions: {len(embeddings)}") |
| 46 | + |
| 47 | +# Example usage with a PDF knowledge base |
| 48 | +knowledge_base = PDFUrlKnowledgeBase( |
| 49 | + urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"], |
| 50 | + reader=PDFUrlReader( |
| 51 | + chunk_size=2048 |
| 52 | + ), # Required because Cohere model has a fixed size of 2048 |
| 53 | + vector_db=PgVector( |
| 54 | + table_name="recipes", |
| 55 | + db_url="postgresql+psycopg://ai:ai@localhost:5532/ai", |
| 56 | + embedder=AwsBedrockEmbedder(), |
| 57 | + ), |
| 58 | +) |
| 59 | +knowledge_base.load(recreate=False) |
| 60 | +``` |
| 61 | + |
| 62 | +# Params |
| 63 | + |
| 64 | +| Parameter | Type | Default | Description | |
| 65 | +| ----------------------- | ----------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------ | |
| 66 | +| `id` | `str` | `"cohere.embed-multilingual-v3"` | The model ID to use. You need to enable this model in your AWS Bedrock model catalog. | |
| 67 | +| `dimensions` | `int` | `1024` | The dimensionality of the embeddings generated by the model(1024 for Cohere models). | |
| 68 | +| `input_type` | `str` | `"search_query"` | Prepends special tokens to differentiate types. Options: 'search_document', 'search_query', 'classification', 'clustering'. | |
| 69 | +| `truncate` | `Optional[str]` | `None` | How to handle inputs longer than the maximum token length. Options: 'NONE', 'START', 'END'. | |
| 70 | +| `embedding_types` | `Optional[List[str]]` | `None` | Types of embeddings to return . Options: 'float', 'int8', 'uint8', 'binary', 'ubinary'. | |
| 71 | +| `aws_region` | `Optional[str]` | `None` | The AWS region to use. If not provided, falls back to AWS_REGION env variable. | |
| 72 | +| `aws_access_key_id` | `Optional[str]` | `None` | The AWS access key ID. If not provided, falls back to AWS_ACCESS_KEY_ID env variable. | |
| 73 | +| `aws_secret_access_key` | `Optional[str]` | `None` | The AWS secret access key. If not provided, falls back to AWS_SECRET_ACCESS_KEY env variable. | |
| 74 | +| `session` | `Optional[Session]` | `None` | A boto3 Session object to use for authentication. | |
| 75 | +| `request_params` | `Optional[Dict[str, Any]]` | `None` | Additional parameters to pass to the API requests. | |
| 76 | +| `client_params` | `Optional[Dict[str, Any]]` | `None` | Additional parameters to pass to the boto3 client. | |
| 77 | +| `client` | `Optional[AwsClient]` | `None` | An instance of the AWS Bedrock client to use for making API requests. | |
| 78 | + |
| 79 | +# Developer Resources |
| 80 | +- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/embedders/aws_bedrock_embedder.py) |
0 commit comments