嵌入将文本转换为数值向量,您可以将其存储在向量数据库中,使用余弦相似度进行搜索,或在 RAG 流水线中使用。向量长度取决于模型(通常为 384–1024 维)。
推荐模型
生成嵌入
CLI
cURL
Python
JavaScript
直接通过命令行生成嵌入ollama run embeddinggemma "Hello world"
您也可以通过管道传输文本来生成嵌入echo "Hello world" | ollama run embeddinggemma
输出是一个 JSON 数组。 curl -X POST https://:11434/api/embed \
-H "Content-Type: application/json" \
-d '{
"model": "embeddinggemma",
"input": "The quick brown fox jumps over the lazy dog."
}'
import ollama
single = ollama.embed(
model='embeddinggemma',
input='The quick brown fox jumps over the lazy dog.'
)
print(len(single['embeddings'][0])) # vector length
import ollama from 'ollama'
const single = await ollama.embed({
model: 'embeddinggemma',
input: 'The quick brown fox jumps over the lazy dog.',
})
console.log(single.embeddings[0].length) // vector length
/api/embed 端点返回 L2 归一化(单位长度)向量。
批量生成嵌入
向 input 传递一个字符串数组。
curl -X POST https://:11434/api/embed \
-H "Content-Type: application/json" \
-d '{
"model": "embeddinggemma",
"input": [
"First sentence",
"Second sentence",
"Third sentence"
]
}'
import ollama
batch = ollama.embed(
model='embeddinggemma',
input=[
'The quick brown fox jumps over the lazy dog.',
'The five boxing wizards jump quickly.',
'Jackdaws love my big sphinx of quartz.',
]
)
print(len(batch['embeddings'])) # number of vectors
import ollama from 'ollama'
const batch = await ollama.embed({
model: 'embeddinggemma',
input: [
'The quick brown fox jumps over the lazy dog.',
'The five boxing wizards jump quickly.',
'Jackdaws love my big sphinx of quartz.',
],
})
console.log(batch.embeddings.length) // number of vectors
- 对于大多数语义搜索用例,请使用余弦相似度。
- 在索引和查询时请使用相同的嵌入模型。