임베딩(Embeddings)

임베딩(Embeddings)

텍스트를 의미 검색·검색(retrieval)·RAG 파이프라인에 쓸 수 있는 숫자 벡터로 바꾸고 싶을 때, Ollama의 임베딩 기능이 그 역할을 맡아요. 임베딩은 텍스트를 숫자 벡터로 변환해 주는데, 이 벡터를 벡터 데이터베이스에 저장하고 코사인 유사도로 검색하거나 RAG 파이프라인에 활용할 수 있어요. 벡터의 길이는 모델에 따라 달라지며 보통 384~1024 차원입니다.

출처: 공식문서

추천 모델

임베딩 생성하기

명령줄에서 바로 임베딩을 생성할 수도 있어요.

ollama run embeddinggemma "Hello world"

텍스트를 파이프로 넘겨도 됩니다.

echo "Hello world" | ollama run embeddinggemma

출력은 JSON 배열로 나옵니다.

cURL로는 /api/embed 엔드포인트에 POST 요청을 보내면 돼요.

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 SDK를 쓴다면 ollama.embed를 호출합니다.

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도 동일한 형태로 ollama.embed를 쓸 수 있어요.

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:

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']))  # number of vectors

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

  • 대부분의 의미 검색 시나리오에서는 코사인 유사도를 사용하세요.
  • 색인할 때와 검색(쿼리)할 때는 같은 임베딩 모델을 사용하세요.

더 알아보기 (Learn more)