Skip to content

本页尚未翻译

本页内容仍为英文原文,源自官方文档 integrations/embedding/ollama。中文翻译正在分批进行中。

Ollama ​

Generate embeddings via the ollama python library. More details:

For direct calls, compute_source_embeddings accepts a list of strings or a one-dimensional NumPy string array. Both inputs are sent to Ollama in one batch.

ParameterTypeDefault ValueDescription
namestrnomic-embed-textThe name of the model.
hoststrhttp://localhost:11434The Ollama host to connect to.
optionsollama.Options or dictNoneAdditional model parameters listed in the documentation for the Modelfile such as temperature.
keep_alivefloat or str"5m"Controls how long the model will stay loaded into memory following the request.
ollama_client_kwargsdict{}kwargs that can be past to the ollama.Client.
python
import tempfile
from pathlib import Path

import lancedb
from lancedb.embeddings import get_registry
from lancedb.pydantic import LanceModel, Vector

db = lancedb.connect(str(Path(tempfile.mkdtemp()) / "ollama-demo"))
func = get_registry().get("ollama").create(name="nomic-embed-text")

class Words(LanceModel):
    text: str = func.SourceField()
    vector: Vector(func.ndims()) = func.VectorField()

table = db.create_table("words", schema=Words, mode="overwrite")
table.add(
    [
        {"text": "hello world"},
        {"text": "goodbye world"},
    ]
)

query = "greetings"
actual = table.search(query).limit(1).to_pydantic(Words)[0]
print(actual.text)