Using SparkLLM Text Embeddings in LangChain

Posted: Feb 9, 2025.

SparkLLM Text Embeddings provides a powerful way to generate vector representations of text using iFlytek's embedding service. In this guide, we'll explore how to use this integration in LangChain to create embeddings for both individual queries and documents.

What is SparkLLM Text Embeddings?

SparkLLM Text Embeddings is an embedding service provided by iFlytek that converts text into high-dimensional vectors (2560 dimensions). These embeddings can be used for various natural language processing tasks like semantic search, text clustering, and similarity comparisons. The service supports a 2K token window, making it suitable for embedding longer text passages.

Reference

MethodDescription
embed_query(text)Generates embeddings for a single text string
embed_documents(texts)Generates embeddings for a list of text strings
aembed_query(text)Async version of embed_query
aembed_documents(texts)Async version of embed_documents

Configuration parameters:

  • base_url: Base URL for API requests (defaults to iFlytek's endpoint)
  • domain: Can be 'para' for document embedding or 'query' for query embedding
  • spark_api_key: Your API key (can be set via environment variable)
  • spark_api_secret: Your API secret (can be set via environment variable)
  • spark_app_id: Your app ID (can be set via environment variable)

How to Use SparkLLM Text Embeddings

Setup and Authentication

First, you'll need to set up authentication credentials. You can do this either through environment variables or by passing them directly to the constructor:

from langchain_community.embeddings import SparkLLMTextEmbeddings

# Using constructor parameters
embeddings = SparkLLMTextEmbeddings(
    spark_app_id="your_app_id",
    spark_api_key="your_api_key", 
    spark_api_secret="your_api_secret"
)

# Or using environment variables
# export SPARK_APP_ID="your_app_id"
# export SPARK_API_KEY="your_api_key"
# export SPARK_API_SECRET="your_api_secret"
embeddings = SparkLLMTextEmbeddings()

Generating Single Text Embeddings

To generate embeddings for a single piece of text:

text = "The quick brown fox jumps over the lazy dog"
embedding = embeddings.embed_query(text)

# Returns a list of floats representing the embedding vector
print(embedding[:5])  # Show first 5 dimensions

Generating Document Embeddings

For multiple texts:

documents = [
    "Science and technology are advancing rapidly",
    "Artificial intelligence is transforming industries",
    "Machine learning models require quality data"
]

doc_embeddings = embeddings.embed_documents(documents)

# Returns a list of embedding vectors, one for each document
print(len(doc_embeddings))  # Number of documents
print(len(doc_embeddings[0]))  # Dimensions per embedding (2560)

Async Operations

If you're working in an async context, you can use the async versions of the methods:

import asyncio

async def generate_embeddings():
    # Single query
    query_embedding = await embeddings.aembed_query("What is artificial intelligence?")
    
    # Multiple documents
    texts = ["Document 1", "Document 2", "Document 3"]
    doc_embeddings = await embeddings.aembed_documents(texts)
    
    return query_embedding, doc_embeddings

# Run the async function
query_emb, doc_embs = asyncio.run(generate_embeddings())

Custom Configuration

You can customize the embedding behavior using different domains:

# For query-specific embeddings
query_embeddings = SparkLLMTextEmbeddings(
    domain="query",  # Optimized for query embedding
    base_url="https://custom-endpoint.example.com/"  # Optional custom endpoint
)

# For document/paragraph embeddings
doc_embeddings = SparkLLMTextEmbeddings(
    domain="para"  # Default, optimized for document embedding
)

Remember that the embeddings service has a 2K token window limit, so make sure your input texts don't exceed this limit for optimal results.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs