Embeddings convertem texto em vetores numéricos que você pode armazenar em um banco de dados vetorial, buscar com similaridade de cosseno ou usar em pipelines RAG. O comprimento do vetor depende do modelo (tipicamente 384–1024 dimensões).
Modelos recomendados
Gerar embeddings
CLI
Gere embeddings diretamente pela linha de comando:
```shell
ollama run embeddinggemma "Hello world"
```
Você também pode usar pipe para enviar texto e gerar embeddings:
```shell
echo "Hello world" | ollama run embeddinggemma
```
A saída é um array JSON.
cURL
```shell
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
```python
import ollama
single = ollama.embed(
model='embeddinggemma',
input='The quick brown fox jumps over the lazy dog.'
)
print(len(single['embeddings'][0])) # vector length
```
JavaScript
```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) // vector length
```
Nota
O endpoint /api/embed retorna vetores normalizados L2 (de comprimento unitário).
Gerar um lote de embeddings
Passe um array de strings para o parâmetro input.
cURL
```shell
curl -X POST http://localhost:11434/api/embed \
-H "Content-Type: application/json" \
-d '{
"model": "embeddinggemma",
"input": [
"First sentence",
"Second sentence",
"Third sentence"
]
}'
```
Python
```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'])) # number of vectors
```
JavaScript
```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) // number of vectors
```
Dicas
- Use similaridade de cosseno para a maioria dos casos de uso de busca semântica.
- Use o mesmo modelo de embeddings para indexação e consulta.