ollama 嵌入模型

嵌入模型

Ollama 支持嵌入模型,使得构建增强检索生成(RAG)应用成为可能,这些应用将文本提示与现有文档或其他数据结合起来。

什么是嵌入模型?

嵌入模型是专门训练生成向量嵌入的模型:这些向量是表示给定文本序列语义含义的长数组。

生成的向量嵌入数组可以存储在数据库中,这些数组可以用来比较,从而搜索语义上相似的数据。

示例嵌入模型

模型 参数大小
mxbai-embed-large 334M
nomic-embed-text 137M
all-minilm 23M

使用方法

要生成向量嵌入,首先获取一个模型:

ollama pull mxbai-embed-large

接下来,使用 REST API、Python 或 JavaScript 库从模型生成向量嵌入:

REST API

curl http://localhost:11434/api/embeddings -d '{
  "model": "mxbai-embed-large",
  "prompt": "Llamas are members of the camelid family"
}'

Python 库

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

JavaScript 库

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

Ollama 还与流行的工具集成,以支持嵌入工作流,如 LangChain 和 LlamaIndex。

示例

以下示例演示如何使用 Ollama 和嵌入模型构建增强检索生成(RAG)应用。

第一步:生成嵌入

首先安装必要的库:

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")

# 将每个文档存储在向量嵌入数据库中
for i, d in enumerate(documents):
  response = ollama.embeddings(model="mxbai-embed-large", prompt=d)
  embedding = response["embedding"]
  collection.add(
    ids=[str(i)],
    embeddings=[embedding],
    documents=[d]
  )

第二步:检索

接下来,添加代码以根据示例提示检索最相关的文档:

# 示例提示
prompt = "What animals are llamas related to?"

# 为提示生成嵌入并检索最相关的文档
response = ollama.embeddings(
  prompt=prompt,
  model="mxbai-embed-large"
)
results = collection.query(
  query_embeddings=[response["embedding"]],
  n_results=1
)
data = results['documents'][0][0]

第三步:生成

最后,使用在上一步检索到的文档和提示生成答案!

# 生成响应,结合提示和我们在第2步中检索到的数据
output = ollama.generate(
  model="llama2",
  prompt=f"Using this data: {data}. Respond to this prompt: {prompt}"
)

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).”

即将推出

更多功能即将推出,以支持涉及嵌入的工作流:

  • 批量嵌入:同时处理多个输入数据提示
  • OpenAI API 兼容性:支持 /v1/embeddings OpenAI 兼容端点
  • 更多嵌入模型架构:支持 ColBERT、RoBERTa 和其他嵌入模型架构

原文地址 https://ollama.com/blog/embedding-models

最后更新时间 2024年4月8日