嵌入模型

2024年4月8日

Embedding models

Ollama 支援嵌入模型,使其能夠建構檢索增強生成 (RAG) 應用程式,將文字提示與現有文件或其他資料結合。

什麼是嵌入模型?

嵌入模型是專門訓練用於生成向量嵌入的模型:代表給定文字序列語義的長數字陣列

what-are-embedding-models

產生的向量嵌入陣列接著可以儲存在資料庫中,資料庫將比較這些陣列,以此方式搜尋語義相似的資料。

嵌入模型範例

模型 參數大小
mxbai-embed-large 334M 檢視模型
nomic-embed-text 137M 檢視模型
all-minilm 23M 檢視模型

使用方式

要生成向量嵌入,首先拉取模型

ollama pull mxbai-embed-large

接下來,使用 REST APIPythonJavaScript 程式庫,從模型生成向量嵌入

REST API

curl https://127.0.0.1:11434/api/embed -d '{
  "model": "mxbai-embed-large",
  "input": "Llamas are members of the camelid family"
}'

Python 程式庫

ollama.embed(
  model='mxbai-embed-large',
  input='Llamas are members of the camelid family',
)

Javascript 程式庫

ollama.embed({
    model: 'mxbai-embed-large',
    input: 'Llamas are members of the camelid family',
})

Ollama 也與熱門工具整合,以支援嵌入工作流程,例如 LangChainLlamaIndex

範例

此範例逐步說明如何使用 Ollama 和嵌入模型建構檢索增強生成 (RAG) 應用程式。

步驟 1:生成嵌入

pip install ollama chromadb

建立名為 example.py 的檔案,內容如下

import ollama
import chromadb

documents = [
  "Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels",
  "Llamas were first domesticated and used as pack animals 4,000 to 5,000 years ago in the Peruvian highlands",
  "Llamas can grow as much as 6 feet tall though the average llama between 5 feet 6 inches and 5 feet 9 inches tall",
  "Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight",
  "Llamas are vegetarians and have very efficient digestive systems",
  "Llamas live to be about 20 years old, though some only live for 15 years and others live to be 30 years old",
]

client = chromadb.Client()
collection = client.create_collection(name="docs")

# store each document in a vector embedding database
for i, d in enumerate(documents):
  response = ollama.embed(model="mxbai-embed-large", input=d)
  embeddings = response["embeddings"]
  collection.add(
    ids=[str(i)],
    embeddings=embeddings,
    documents=[d]
  )

步驟 2:檢索

接下來,加入程式碼以檢索給定範例提示的最相關文件

# an example input
input = "What animals are llamas related to?"

# generate an embedding for the input and retrieve the most relevant doc
response = ollama.embed(
  model="mxbai-embed-large",
  input=prompt
)
results = collection.query(
  query_embeddings=[response["embeddings"]],
  n_results=1
)
data = results['documents'][0][0]

步驟 3:生成

最後,使用提示和上一步檢索的文件來生成答案!

# generate a response combining the prompt and data we retrieved in step 2
output = ollama.generate(
  model="llama2",
  prompt=f"Using this data: {data}. Respond to this prompt: {input}"
)

print(output['response'])

然後,執行程式碼

python example.py

Llama 2 將使用資料回答提示 What animals are llamas related to?

Llamas are members of the camelid family, which means they are closely related to two other animals: vicuñas and camels. All three species belong to the same evolutionary lineage and share many similarities in terms of their physical characteristics, behavior, and genetic makeup. Specifically, llamas are most closely related to vicuñas, with which they share a common ancestor that lived around 20-30 million years ago. Both llamas and vicuñas are members of the family Camelidae, while camels belong to a different family (Dromedary).

即將推出

更多功能即將推出,以支援涉及嵌入的工作流程

  • 批次嵌入: 同時處理多個輸入資料提示
  • OpenAI API 相容性:支援與 OpenAI 相容的 /v1/embeddings 端點
  • 更多嵌入模型架構: 支援 ColBERT、RoBERTa 和其他嵌入模型架構