当前位置:   article > 正文

Ollama利用嵌入模型实现RAG应用_mxbai-embed-large

mxbai-embed-large

Ollama支持embedding models嵌入模型,从而支持RAG(retrieval augmented generation)应用,结合文本提示词,检索到文档或相关数据。嵌入模型是通过训练生成向量嵌入,这是一长串数字数组,代表文本序列的关联关系。
1
Ollama的嵌入模型有三种:mxbai-embed-large、nomic-embed-text 、all-minilm。

使用

ollama pull mxbai-embed-large
  • 1

python接口调用:

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

样例

生成embeddings

pip install ollama chromadb
  • 1

创建文件: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.embeddings(model="mxbai-embed-large", prompt=d)
  embedding = response["embedding"]
  collection.add(
    ids=[str(i)],
    embeddings=[embedding],
    documents=[d]
  )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

检索

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

# generate an embedding for the prompt and retrieve the most relevant doc
response = ollama.embeddings(
  prompt=prompt,
  model="mxbai-embed-large"
)
results = collection.query(
  query_embeddings=[response["embedding"]],
  n_results=1
)
data = results['documents'][0][0]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出数据

# 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: {prompt}"
)

print(output['response'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

参考

https://ollama.com/blog/embedding-models

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/584608
推荐阅读
相关标签
  

闽ICP备14008679号