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
Method | Description |
---|---|
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 embeddingspark_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:
Generating Single Text Embeddings
To generate embeddings for a single piece of text:
Generating Document Embeddings
For multiple texts:
Async Operations
If you're working in an async context, you can use the async versions of the methods:
Custom Configuration
You can customize the embedding behavior using different domains:
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 DocsJoin 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.