UpstageEmbeddings
本筆記本涵蓋如何開始使用 Upstage 嵌入模型。
安裝
安裝 langchain-upstage
套件。
pip install -U langchain-upstage
環境設定
請務必設定以下環境變數
UPSTAGE_API_KEY
: 您的 Upstage API 金鑰,來自 Upstage 控制台。
import os
os.environ["UPSTAGE_API_KEY"] = "YOUR_API_KEY"
使用方式
初始化 UpstageEmbeddings
類別。
from langchain_upstage import UpstageEmbeddings
embeddings = UpstageEmbeddings(model="solar-embedding-1-large")
API 參考:UpstageEmbeddings
使用 embed_documents
嵌入文字或文件的列表。
doc_result = embeddings.embed_documents(
["Sung is a professor.", "This is another document"]
)
print(doc_result)
使用 embed_query
嵌入查詢字串。
query_result = embeddings.embed_query("What does Sung do?")
print(query_result)
使用 aembed_documents
和 aembed_query
進行非同步操作。
# 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"]
)
搭配向量儲存使用
您可以將 UpstageEmbeddings
與向量儲存元件一起使用。以下展示一個簡單的範例。
from langchain_community.vectorstores import DocArrayInMemorySearch
vectorstore = DocArrayInMemorySearch.from_texts(
["harrison worked at kensho", "bears like to eat honey"],
embedding=UpstageEmbeddings(model="solar-embedding-1-large"),
)
retriever = vectorstore.as_retriever()
docs = retriever.invoke("Where did Harrison work?")
print(docs)
API 參考:DocArrayInMemorySearch