ClovaXEmbeddings
This notebook covers how to get started with embedding models provided by CLOVA Studio. For detailed documentation on ClovaXEmbeddings
features and configuration options, please refer to the API reference.
Overviewโ
Integration detailsโ
Provider | Package |
---|---|
Naver | ClovaXEmbeddings |
Setupโ
Before using embedding models provided by CLOVA Studio, you must go through the three steps below.
- Creating NAVER Cloud Platform account
- Apply to use CLOVA Studio
- Find API Keys after creating CLOVA Studio Test App or Service App (See here.)
Credentialsโ
CLOVA Studio requires 3 keys (NCP_CLOVASTUDIO_API_KEY
, NCP_APIGW_API_KEY
and NCP_CLOVASTUDIO_APP_ID
) for embeddings.
NCP_CLOVASTUDIO_API_KEY
andNCP_CLOVASTUDIO_APP_ID
is issued per serviceApp or testAppNCP_APIGW_API_KEY
is issued per account
The two API Keys could be found by clicking App Request Status
> Service App, Test App List
> โDetailsโ button for each app
in CLOVA Studio.
import getpass
import os
os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass("NCP CLOVA Studio API Key: ")
os.environ["NCP_APIGW_API_KEY"] = getpass.getpass("NCP API Gateway API Key: ")
os.environ["NCP_CLOVASTUDIO_APP_ID"] = input("NCP CLOVA Studio App ID: ")
Installationโ
ClovaXEmbeddings integration lives in the langchain_community
package:
# install package
!pip install -U langchain-community
Instantiationโ
Now we can instantiate our embeddings object and embed query or document:
- There are several embedding models available in CLOVA Studio. Please refer here for further details.
- Note that you might need to normalize the embeddings depending on your specific use case.
from langchain_community.embeddings import ClovaXEmbeddings
embeddings = ClovaXEmbeddings(
#model="clir-emb-dolphin" #default is `clir-emb-dolphin`. change with the model name of corresponding App ID if needed.
)
Indexing and Retrievalโ
Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the working with external knowledge tutorials.
Below, see how to index and retrieve data using the embeddings
object we initialized above. In this example, we will index and retrieve a sample document in the InMemoryVectorStore
.
# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore
text = "CLOVA Studio is an AI development tool that allows you to customize your own HyperCLOVA X models."
vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)
# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()
# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is CLOVA Studio?")
# show the retrieved document's content
retrieved_documents[0].page_content
Direct Usageโ
Under the hood, the vectorstore and retriever implementations are calling embeddings.embed_documents(...)
and embeddings.embed_query(...)
to create embeddings for the text(s) used in from_texts
and retrieval invoke
operations, respectively.
You can directly call these methods to get embeddings for your own use cases.
Embed single textsโ
You can embed single texts or documents with embed_query
:
embeddings.embed_query("My query to look up")
Embed multiple textsโ
You can embed multiple texts with embed_documents
:
embeddings.embed_documents(
["This is a content of the document", "This is another document"]
)
Embed with asyncโ
There are also async functionalities:
# async embed query
await embeddings.aembed_query("My query to look up")
# async embed documents
await embeddings.aembed_documents(
["This is a content of the document", "This is another document"]
)
Additional functionalitiesโ
Service Appโ
When going live with production-level application using CLOVA Studio, you should apply for and use Service App. (See here.)
For a Service App, corresponding NCP_CLOVASTUDIO_API_KEY
and NCP_CLOVASTUDIO_APP_ID
are issued and can only be called with the API Keys.
#### Update environment variables
os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass("NCP CLOVA Studio API Key for Service App: ")
os.environ["NCP_CLOVASTUDIO_APP_ID"] = input("NCP CLOVA Studio Service App ID: ")
embeddings = ClovaXEmbeddings(service_app=True)
API Referenceโ
For detailed documentation on ClovaXEmbeddings
features and configuration options, please refer to the API reference.
Relatedโ
- Embedding model conceptual guide
- Embedding model how-to guides