Use an embedding model

Updated at:

This topic explains how to use the DashScopeEmbedding service to build a vector index in LlamaIndex.

Prerequisites

pip install llama-index-core
pip install llama-index-embeddings-dashscope
pip install llama-index-readers-file
pip install docx2txt

Supported models

MTEB and CMTEB are standard benchmarks for embedding models. Higher scores indicate better model performance.

Update global settings (optional)

from llama_index.core import Settings
from llama_index.embeddings.dashscope import DashScopeEmbedding

# Replace the default LlamaIndex embedding model with a DashScope embedding model
Settings.embed_model = DashScopeEmbedding(
    model_name="text-embedding-v2"
)

Model usage example

from llama_index.embeddings.dashscope import DashScopeEmbedding

# Initialize the embedding model
embedder = DashScopeEmbedding(
    model_name="text-embedding-v2"
)
text_to_embedding = ["The wind blows fiercely. Monkeys wail high in the sky.", "The islet is clear. The sand is white. Birds fly back.", "Endless falling leaves rustle down.", "The Yangtze River rolls on without end."]
# Call the embedding model
result_embeddings = embedder.get_text_embedding_batch(text_to_embedding)
# Print the embedding results
for index, embedding in enumerate(result_embeddings):
    print("Dimension of embeddings: %s" % len(embedding))
    print(
        "Input: %s, embedding is: %s"
        % (text_to_embedding[index], embedding[:5])
    )
Sample output
Dimension of embeddings: 1536
Input: The wind blows fiercely. Monkeys wail high in the sky., embedding is: [-0.0016666285653348784, 0.008690492014557004, 0.02894828715284365, -0.01774133615134858, 0.03627544697161321]
Dimension of embeddings: 1536
Input: The islet is clear. The sand is white. Birds fly back., embedding is: [0.018255604113922633, 0.030631669725945727, 0.0031333343045102462, 0.014323813963475412, 0.009666154862176396]
Dimension of embeddings: 1536
Input: Endless falling leaves rustle down., embedding is: [-0.01270165436681136, 0.011355212676752505, -0.007090375205285297, 0.008317427977013809, 0.0341982923839579]
Dimension of embeddings: 1536
Input: The Yangtze River rolls on without end., embedding is: [0.003449439128962428, 0.02667092110022496, -0.0010223853088419568, -0.00971414215183749, 0.0035561228133633277]

Build a vector index for documents

Sample file used in this example: Alibaba Cloud Model Studio Product Overview (fictional).zip

# Import dependencies
from llama_index.embeddings.dashscope import DashScopeEmbedding
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex

# Load all files from the target directory
documents = SimpleDirectoryReader("<Replace this with the path to your target directory>").load_data()
print("Loaded files from the target directory")

# The from_documents method slices documents and builds an index in one step
index = VectorStoreIndex.from_documents(
    documents,
    # Specify the embedding model
    embed_model=DashScopeEmbedding(
        model_name="text-embedding-v2"
    ))
print("Built a vector index for multiple documents using DashScopeEmbedding")

# Print sample index entries and compressed vectors
print("Sample vector outputs:")
for i, uuid in enumerate(index.vector_store.data.metadata_dict.keys()):
    print("File name: ", end='')
    print(index.vector_store.data.metadata_dict[uuid]['file_name'], end='')
    print(", File size: ", index.vector_store.data.metadata_dict[uuid]['file_size'], end='')
    print(", File type: ", index.vector_store.data.metadata_dict[uuid]['file_type'])
    print("Compressed vector: ", end='')
    print(index.vector_store.data.embedding_dict[uuid][:3], '\n')
    if i > 3:
        break
Sample output

During vector index creation, each source file is split into multiple segments. Each segment is then converted into a vector.

Loaded files from the target directory
Built a vector index for multiple documents using DashScopeEmbedding
Sample vector outputs:
File name: Alibaba Cloud Model Studio Tablet Product Overview.pdf, File size: 144316, File type: application/pdf
Compressed vector: [0.040188307063141346, -0.00039877765589124394, -0.035738459756745854]

File name: Alibaba Cloud Model Studio Tablet Product Overview.pdf, File size: 144316, File type: application/pdf
Compressed vector: [0.04814293357335188, 0.004163492388781393, -0.038165800733263575]

File name: Alibaba Cloud Model Studio Smartphone Product Overview.docx, File size: 14265, File type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Compressed vector: [0.019914771026010504, 0.0009497773001332384, -0.040679692362629784]

File name: Alibaba Cloud Model Studio Smartphone Product Overview.docx, File size: 14265, File type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Compressed vector: [0.02361539453526087, 0.00019768677449582677, -0.03274763169693275]

File name: Alibaba Cloud Model Studio Smart Speaker Product Overview.txt, File size: 2448, File type: text/plain
Compressed vector: [0.03064649761730314, -0.003089192569710745, -0.022280601331799776]

For more details and examples, see the official LlamaIndex Embedding Examples. For the full API reference, see the official LlamaIndex DashScopeEmbedding API Reference.