主题
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-embeddings-bedrock1
import os
from llama_index.embeddings.bedrock import BedrockEmbedding1
2
3
2
3
embed_model = BedrockEmbedding(
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
aws_session_token=os.getenv("AWS_SESSION_TOKEN"),
region_name="<aws-region>",
profile_name="<aws-profile>",
)1
2
3
4
5
6
7
2
3
4
5
6
7
embedding = embed_model.get_text_embedding("hello world")1
列出支持的模型
要查看 LlamaIndex 中 Amazon Bedrock 支持的模型列表,可按如下方式调用 BedrockEmbedding.list_supported_models()。
from llama_index.embeddings.bedrock import BedrockEmbedding
import json
supported_models = BedrockEmbedding.list_supported_models()
print(json.dumps(supported_models, indent=2))1
2
3
4
5
2
3
4
5
提供商:Amazon
Amazon Bedrock Titan 嵌入模型。
from llama_index.embeddings.bedrock import BedrockEmbedding
model = BedrockEmbedding(model_name="amazon.titan-embed-g1-text-02")
embeddings = model.get_text_embedding("hello world")
print(embeddings)1
2
3
4
5
2
3
4
5
提供商:Cohere
cohere.embed-english-v3
model = BedrockEmbedding(model_name="cohere.embed-english-v3")
coherePayload = ["This is a test document", "This is another test document"]
embed1 = model.get_text_embedding("This is a test document")
print(embed1)
embeddings = model.get_text_embedding_batch(coherePayload)
print(embeddings)1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Cohere 的多语言嵌入模型
model = BedrockEmbedding(model_name="cohere.embed-multilingual-v3")
coherePayload = [
"This is a test document",
"తెలుగు అనేది ద్రావిడ భాషల కుటుంబానికి చెందిన భాష.",
"Esto es una prueba de documento multilingüe.",
"攻殻機動隊",
"Combien de temps ça va prendre ?",
"Документ проверен",
]
embeddings = model.get_text_embedding_batch(coherePayload)
print(embeddings)1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11