OllamaTextEmbedder

OllamaTextEmbedder

Ollama Library와 호환되는 임베딩 모델로 문자열의 임베딩을 계산하는 컴포넌트예요.

출처: 문서

본문

OllamaTextEmbedder는 문자열의 임베딩을 계산하고 그 결과 벡터를 돌려줘요. Ollama Library와 호환되는 임베딩 모델을 사용하죠.

임베딩 검색을 수행할 때 이 컴포넌트를 먼저 사용해서 검색어를 벡터로 바꿔요. 그러면 임베딩 Retriever가 그 벡터로 유사하거나 관련 있는 문서를 검색해요.

Overview

OllamaTextEmbedder는 문자열을 임베딩할 때 써요. 문서 목록을 임베딩할 때는 OllamaDocumentEmbedder를 쓰세요.

이 컴포넌트는 대부분 사용 가능한 환경(Mac, Linux, Docker)이 포트 11434를 기본으로 쓰기 때문에 기본 URL로 http://localhost:11434를 사용해요.

호환 모델

이 컴포넌트를 초기화할 때 따로 지정하지 않으면 기본 임베딩 모델은 "nomic-embed-text"예요. 다른 사전 빌드 모델은 Ollama의 library에서 볼 수 있어요. 자체 커스텀 모델을 쓰려면 Ollama의 지침을 따르세요.

설치

Haystack에서 이 통합을 쓰려면 패키지를 설치해요.

pip install ollama-haystack

실행 중인 Ollama 모델(docker 컨테이너 또는 로컬 호스팅)이 준비돼 있어야 해요. Ollama에는 임베딩 API가 내장되어 있어 추가 설정은 필요 없어요.

메타데이터 임베딩

임베딩된 메타데이터는 대부분 모델 이름과 유형에 대한 정보를 담아요. 선택 인자(temperature, top_p 등)를 Ollama 생성 엔드포인트에 넘길 수 있어요.

사용한 모델 이름은 메타데이터의 일부로 자동으로 덧붙여져요. nomic-embed-text 모델을 쓰면 예시 페이로드는 이렇게 돼요.

{"meta": {"model": "nomic-embed-text"}}

더 알아보기 (Learn more)

단독으로 쓰기

from haystack_integrations.components.embedders.ollama import OllamaTextEmbedder


embedder = OllamaTextEmbedder()

result = embedder.run(
    text="What do llamas say once you have thanked them? No probllama!",
)

print(result["embedding"])

파이프라인에서 쓰기

from haystack import Document
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.ollama import (
    OllamaDocumentEmbedder,
    OllamaTextEmbedder,
)
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever


document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")

documents = [
    Document(content="My name is Wolfgang and I live in Berlin"),
    Document(content="I saw a black horse running"),
    Document(content="Germany has many big cities"),
]

document_embedder = OllamaDocumentEmbedder()
documents_with_embeddings = document_embedder.run(documents)["documents"]
document_store.write_documents(documents_with_embeddings)

query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", OllamaTextEmbedder())
query_pipeline.add_component(
    "retriever",
    InMemoryEmbeddingRetriever(document_store=document_store),
)
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")

query = "Who lives in Berlin?"

result = query_pipeline.run({"text_embedder": {"text": query}})

print(result["retriever"]["documents"][0])