LangChain with Oracle Cloud Infrastructure (OCI) Embeddings
Posted: Nov 7, 2024.
Oracle Cloud Infrastructure (OCI) Generative AI provides state-of-the-art language models through a managed service. In this guide, we'll look at how to use OCI's embedding models with LangChain through the OCIGenAIEmbeddings class.
What is OCIGenAIEmbeddings?
OCIGenAIEmbeddings is a LangChain integration that allows you to generate embeddings (vector representations) of text using Oracle Cloud's Generative AI models. These embeddings can be used for semantic search, text similarity, and other NLP tasks.
Reference
Method | Description |
---|---|
embed_documents(texts: List[str]) | Converts a list of texts into their vector embeddings |
embed_query(text: str) | Converts a single text into its vector embedding |
aembed_documents(texts: List[str]) | Async version of embed_documents |
aembed_query(text: str) | Async version of embed_query |
Key parameters:
model_id
: ID of the OCI embedding model to useservice_endpoint
: OCI GenAI service endpoint URLcompartment_id
: OCID of your OCI compartmentauth_type
: Authentication method ('API_KEY', 'SECURITY_TOKEN', 'INSTANCE_PRINCIPLE', 'RESOURCE_PRINCIPLE')auth_profile
: Profile name from OCI config file (default: 'DEFAULT')batch_size
: Number of texts to process per request (max 96)truncate
: How to handle long texts ('NONE', 'START', 'END')
How to Use OCIGenAIEmbeddings
Installation
First, install the required packages:
Authentication Setup
The OCIGenAIEmbeddings class supports multiple authentication methods. The most common is using an API key:
You can also use session token authentication:
Generating Embeddings
Single Text Embedding
To generate an embedding for a single piece of text:
Batch Text Embeddings
To generate embeddings for multiple texts at once:
Async Usage
For better performance in async applications, you can use the async methods:
Advanced Configuration
You can customize the embedding generation with additional parameters:
Remember to handle credentials securely and follow Oracle's best practices for authentication when deploying to production environments.
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.