DashScopeEmbedding
更新時間:
本文檔介紹使用DashScopeEmbedding服務(wù)來在Llama Index中構(gòu)建向量索引服務(wù)的使用方法。
開始
前提條件
已開通百煉服務(wù):開通阿里云百煉大模型服務(wù)產(chǎn)品。
創(chuàng)建API_KEY: 獲取API-KEY。
安裝DashScopeEmbedding的安裝包 (python>=3.8,<=3.12)
pip install llama-index-core
pip install llama-index-embeddings-dashscope
示例代碼
以下示例展示了在Llama-Index中調(diào)用DashscopeEmbedding服務(wù)
說明
需要使用您的API-KEY替換示例中的 YOUR_DASHSCOPE_API_KEY,代碼才能正常運行。
設(shè)置API-KEY
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_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 = ["風(fēng)急天高猿嘯哀", "渚清沙白鳥飛回", "無邊落木蕭蕭下", "不盡長江滾滾來"]
# 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 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])
)
示例代碼輸出
Dimension of embeddings: 1536
Input: 風(fēng)急天高猿嘯哀, embedding is: [-0.0016666285653348784, 0.008690492014557004, 0.02894828715284365, -0.01774133615134858, 0.03627544697161321]
Dimension of embeddings: 1536
Input: 渚清沙白鳥飛回, embedding is: [0.018255604113922633, 0.030631669725945727, 0.0031333343045102462, 0.014323813963475412, 0.009666154862176396]
Dimension of embeddings: 1536
Input: 無邊落木蕭蕭下, embedding is: [-0.01270165436681136, 0.011355212676752505, -0.007090375205285297, 0.008317427977013809, 0.0341982923839579]
Dimension of embeddings: 1536
Input: 不盡長江滾滾來, embedding is: [0.003449439128962428, 0.02667092110022496, -0.0010223853088419568, -0.00971414215183749, 0.0035561228133633277]
了解更多
更多內(nèi)容,請參考LlamaIndex官方文檔和示例:
文檔內(nèi)容是否對您有幫助?