跳至主要內容

Voyage AI

Voyage AI 提供最先進的嵌入/向量化模型。

讓我們載入 Voyage AI Embedding 類別。(使用 pip install langchain-voyageai 安裝 LangChain 合作夥伴套件)

from langchain_voyageai import VoyageAIEmbeddings
API 參考:VoyageAIEmbeddings

Voyage AI 利用 API 金鑰來監控使用情況和管理權限。若要取得您的金鑰,請在我們的 首頁 上建立一個帳戶。然後,使用您的 API 金鑰建立一個 VoyageEmbeddings 模型。您可以使用以下任何模型:(來源

  • voyage-3
  • voyage-3-lite
  • voyage-large-2
  • voyage-code-2
  • voyage-2
  • voyage-law-2
  • voyage-large-2-instruct
  • voyage-finance-2
  • voyage-multilingual-2
embeddings = VoyageAIEmbeddings(
voyage_api_key="[ Your Voyage API key ]", model="voyage-law-2"
)

準備文件並使用 embed_documents 來取得它們的嵌入。

documents = [
"Caching embeddings enables the storage or temporary caching of embeddings, eliminating the necessity to recompute them each time.",
"An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.",
"A Runnable represents a generic unit of work that can be invoked, batched, streamed, and/or transformed.",
]
documents_embds = embeddings.embed_documents(documents)
documents_embds[0][:5]
[0.0562174916267395,
0.018221192061901093,
0.0025736060924828053,
-0.009720131754875183,
0.04108370840549469]

同樣地,使用 embed_query 來嵌入查詢。

query = "What's an LLMChain?"
query_embd = embeddings.embed_query(query)
query_embd[:5]
[-0.0052348352037370205,
-0.040072452276945114,
0.0033957737032324076,
0.01763271726667881,
-0.019235141575336456]

一個極簡的檢索系統

嵌入的主要特點是,兩個嵌入之間的餘弦相似度捕捉了相應原始段落的語義相關性。這允許我們使用嵌入來進行語義檢索/搜尋。

我們可以根據餘弦相似度在文件嵌入中找到幾個最接近的嵌入,並使用 LangChain 的 KNNRetriever 類別檢索相應的文件。

from langchain_community.retrievers import KNNRetriever

retriever = KNNRetriever.from_texts(documents, embeddings)

# retrieve the most relevant documents
result = retriever.invoke(query)
top1_retrieved_doc = result[0].page_content # return the top1 retrieved result

print(top1_retrieved_doc)
API 參考:KNNRetriever
An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.

這個頁面有幫助嗎?