เอ็มเบดดิ้งจะเปลี่ยนข้อความเป็นเวกเตอร์ตัวเลขที่คุณสามารถเก็บในฐานข้อมูลเวกเตอร์ ค้นหาด้วยความคล้ายคลึงโคไซน์ หรือใช้ใน pipeline RAG ความยาวของเวกเตอร์ขึ้นอยู่กับโมเดล (โดยปกติคือ 384–1024 มิติ)
โมเดลที่แนะนำ
สร้างเอ็มเบดดิ้ง
CLI
สร้างเอ็มเบดดิ้งได้โดยตรงจากบรรทัดคำสั่ง:
```shell
ollama run embeddinggemma "Hello world"
```
คุณยังสามารถส่งข้อความผ่าน pipe เพื่อสร้างเอ็มเบดดิ้งได้:
```shell
echo "Hello world" | ollama run embeddinggemma
```
เอาต์พุตจะเป็นอาร์เรย์ 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])) # ความยาวของเวกเตอร์
```
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) // ความยาวของเวกเตอร์
```
หมายเหตุ
เอนด์พอยต์ /api/embed จะส่งคืนเวกเตอร์ที่ผ่านการปรับเป็น L2-normalized (ความยาวเท่ากับ 1)
สร้างเอ็มเบดดิ้งเป็นชุด
ส่งอาร์เรย์ของสตริงไปยังพารามิเตอร์ 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'])) # จำนวนเวกเตอร์
```
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) // จำนวนเวกเตอร์
```
เคล็ดลับ
- ใช้ความคล้ายคลึงโคไซน์สำหรับกรณีการค้นหาสัมพันธ์ส่วนใหญ่
- ใช้โมเดลเอ็มเบดดิ้งเดียวกันสำหรับทั้งการสร้างดัชนีและการค้นหา