Embedding 模型
2024年4月8日
Ollama 支援 embedding 模型,讓建構檢索增強生成 (RAG) 應用程式成為可能,這些應用程式將文字提示與現有文件或其他資料結合。
什麼是 embedding 模型?
Embedding 模型是專門訓練用來產生向量嵌入 (vector embedding)的模型:代表給定文字序列語義含義的長數字陣列。
然後,產生的向量嵌入陣列可以儲存在資料庫中,資料庫會比較這些陣列,以此作為搜尋語義上相似資料的方式。
Embedding 模型範例
模型 | 參數大小 | |
---|---|---|
mxbai-embed-large |
334M | 查看模型 |
nomic-embed-text |
137M | 查看模型 |
all-minilm |
23M | 查看模型 |
使用方式
若要產生向量嵌入,首先拉取模型
ollama pull mxbai-embed-large
接下來,使用 REST API、Python 或 JavaScript 函式庫,從模型產生向量嵌入
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 也與熱門工具整合,以支援 embedding 工作流程,例如 LangChain 和 LlamaIndex。
範例
此範例逐步說明如何使用 Ollama 和 embedding 模型建構檢索增強生成 (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["embedding"]],
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 將使用資料回答提示 駱馬與哪些動物有親緣關係?
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).
即將推出
更多功能即將推出,以支援涉及 embedding 的工作流程
- 批次嵌入: 同時處理多個輸入資料提示
- OpenAI API 相容性:支援與 OpenAI 相容的
/v1/embeddings
端點 - 更多 embedding 模型架構: 支援 ColBERT、RoBERTa 和其他 embedding 模型架構