docs/examples/embeddings/dashscope_embeddings.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/dashscope_embeddings.ipynb" target="_parent"></a>
# DashScope Embeddings
If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install llama-index-core
%pip install llama-index-embeddings-dashscope
# Set API key
%env DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
# you can set API key parameter DashScopeTextEmbedding(model=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2, api_key=api_key)
# imports
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeTextEmbeddingModels,
DashScopeTextEmbeddingType,
)
# Create embeddings
# text_type=`document` to build index
embedder = DashScopeEmbedding(
model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,
text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
)
text_to_embedding = ["风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来"]
# Call text Embedding
result_embeddings = embedder.get_text_embedding_batch(text_to_embedding)
# requests and embedding result index is correspond to.
for index, embedding in enumerate(result_embeddings):
if embedding is None: # if the correspondence request is embedding failed.
print("The %s embedding failed." % text_to_embedding[index])
else:
print("Dimension of embeddings: %s" % len(embedding))
print(
"Input: %s, embedding is: %s"
% (text_to_embedding[index], embedding[:5])
)
# imports
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeTextEmbeddingModels,
DashScopeTextEmbeddingType,
)
# Create embeddings
# text_type=`query` to retrive relevant context.
embedder = DashScopeEmbedding(
model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,
text_type=DashScopeTextEmbeddingType.TEXT_TYPE_QUERY,
)
# Call text Embedding
embedding = embedder.get_text_embedding("衣服的质量杠杠的,很漂亮,不枉我等了这么久啊,喜欢,以后还来这里买")
print(f"Dimension of embeddings: {len(embedding)}")
print(embedding[:5])
# call batch text embedding
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeBatchTextEmbeddingModels,
DashScopeTextEmbeddingType,
)
embedder = DashScopeEmbedding(
model_name=DashScopeBatchTextEmbeddingModels.TEXT_EMBEDDING_ASYNC_V2,
text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
)
embedding_result_file_url = embedder.get_batch_text_embedding(
embedding_file_url="https://dashscope.oss-cn-beijing.aliyuncs.com/samples/text/text-embedding-test.txt"
)
print(embedding_result_file_url)
# call multimodal embedding service
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeMultiModalEmbeddingModels,
)
embedder = DashScopeEmbedding(
model_name=DashScopeMultiModalEmbeddingModels.MULTIMODAL_EMBEDDING_ONE_PEACE_V1,
)
embedding = embedder.get_image_embedding(
img_file_path="https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png"
)
print(f"Dimension of embeddings: {len(embedding)}")
print(embedding[:5])
# call multimodal embedding service
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeMultiModalEmbeddingModels,
)
embedder = DashScopeEmbedding(
model_name=DashScopeMultiModalEmbeddingModels.MULTIMODAL_EMBEDDING_ONE_PEACE_V1,
)
input = [
{"factor": 1, "text": "你好"},
{
"factor": 2,
"audio": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac",
},
{
"factor": 3,
"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png",
},
]
embedding = embedder.get_multimodal_embedding(input=input)
print(f"Dimension of embeddings: {len(embedding)}")
print(embedding[:5])