Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

178 lines
4.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/bedrock.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bedrock Embeddings\n",
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-embeddings-bedrock"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from llama_index.embeddings.bedrock import BedrockEmbedding"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embed_model = BedrockEmbedding(\n",
" aws_access_key_id=os.getenv(\"AWS_ACCESS_KEY_ID\"),\n",
" aws_secret_access_key=os.getenv(\"AWS_SECRET_ACCESS_KEY\"),\n",
" aws_session_token=os.getenv(\"AWS_SESSION_TOKEN\"),\n",
" region_name=\"<aws-region>\",\n",
" profile_name=\"<aws-profile>\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embedding = embed_model.get_text_embedding(\"hello world\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List supported models\n",
"\n",
"To check list of supported models of Amazon Bedrock on LlamaIndex, call `BedrockEmbedding.list_supported_models()` as follows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.bedrock import BedrockEmbedding\n",
"import json\n",
"\n",
"supported_models = BedrockEmbedding.list_supported_models()\n",
"print(json.dumps(supported_models, indent=2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Provider: Amazon\n",
"Amazon Bedrock Titan embeddings."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.bedrock import BedrockEmbedding\n",
"\n",
"model = BedrockEmbedding(model_name=\"amazon.titan-embed-g1-text-02\")\n",
"embeddings = model.get_text_embedding(\"hello world\")\n",
"print(embeddings)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Provider: Cohere\n",
"\n",
"### cohere.embed-english-v3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = BedrockEmbedding(model_name=\"cohere.embed-english-v3\")\n",
"coherePayload = [\"This is a test document\", \"This is another test document\"]\n",
"\n",
"embed1 = model.get_text_embedding(\"This is a test document\")\n",
"print(embed1)\n",
"\n",
"embeddings = model.get_text_embedding_batch(coherePayload)\n",
"print(embeddings)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MultiLingual Embeddings from Cohere "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = BedrockEmbedding(model_name=\"cohere.embed-multilingual-v3\")\n",
"coherePayload = [\n",
" \"This is a test document\",\n",
" \"తెలుగు అనేది ద్రావిడ భాషల కుటుంబానికి చెందిన భాష.\",\n",
" \"Esto es una prueba de documento multilingüe.\",\n",
" \"攻殻機動隊\",\n",
" \"Combien de temps ça va prendre ?\",\n",
" \"Документ проверен\",\n",
"]\n",
"embeddings = model.get_text_embedding_batch(coherePayload)\n",
"print(embeddings)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llama",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}