Embeddings 将文本转换为数值向量,这些向量可以存储在向量数据库中、通过余弦相似度进行搜索,或用于 RAG 流程。向量长度取决于模型(通常为 384–1024 维)。
生成 embeddings
CLI
直接从命令行生成 embeddings:
ollama run embeddinggemma "Hello world"
你也可以通过管道传入文本来生成 embeddings:
echo "Hello world" | ollama run embeddinggemma
输出为 JSON 数组。
cURL
curl -X POST http://localhost:11434/api/embed \
-H "Content-Type: application/json" \
-d '{
"model": "embeddinggemma",
"input": "The quick brown fox jumps over the lazy dog."
}'
Python
import ollama single = ollama.embed( model='embeddinggemma', input='The quick brown fox jumps over the lazy dog.' ) print(len(single['embeddings'][0])) # 向量长度
JavaScript
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) // 向量长度
/api/embed 端点返回 L2 归一化(单位长度)的向量。
生成一批 embeddings
向 input 传递一个字符串数组。
cURL
curl -X POST http://localhost:11434/api/embed \
-H "Content-Type: application/json" \
-d '{
"model": "embeddinggemma",
"input": [
"First sentence",
"Second sentence",
"Third sentence"
]
}'
Python
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'])) # 向量数量
JavaScript
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) // 向量数量
提示
- 对于大多数语义搜索用例,请使用余弦相似度。
- 在索引和查询时请使用相同的 embedding 模型。