Skip to content

本页尚未翻译

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

Jina ​

Text Embedding Models ​

Jina embeddings are used to generate embeddings for text and image data. You also need to set the JINA_API_KEY environment variable to use the Jina API.

You can find a list of supported models under https://jina.ai/embeddings/

Supported parameters (to be passed in create method) are:

ParameterTypeDefault ValueDescription
namestr"jina-clip-v1"The model ID of the jina model to use

Usage Example:

Replace your-api-key with your Jina API key. This provider reads it from JINA_API_KEY; the example sets it explicitly. In application code, supply it through your environment or a secrets manager.

python
import os
import tempfile
from pathlib import Path

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

os.environ["JINA_API_KEY"] = "your-api-key"

jina_embed = (
    EmbeddingFunctionRegistry.get_instance()
    .get("jina")
    .create(name="jina-embeddings-v2-base-en")
)

class TextModel(LanceModel):
    text: str = jina_embed.SourceField()
    vector: Vector(jina_embed.ndims()) = jina_embed.VectorField()

data = [{"text": "hello world"}, {"text": "goodbye world"}]

db = lancedb.connect(str(Path(tempfile.mkdtemp()) / "jina-text"))
tbl = db.create_table("test", schema=TextModel, mode="overwrite")

tbl.add(data)

Multimodal Embedding Models ​

Jina embeddings can also be used to embed both text and image data, only some of the models support image data and you can check the list under https://jina.ai/embeddings/

Supported parameters (to be passed in create method) are:

ParameterTypeDefault ValueDescription
namestr"jina-clip-v1"The model ID of the jina model to use

Usage Example:

python
import os
import tempfile
from pathlib import Path

import lancedb
import pandas as pd
import requests
from lancedb.embeddings import get_registry
from lancedb.pydantic import LanceModel, Vector

os.environ["JINA_API_KEY"] = "your-api-key"

db = lancedb.connect(str(Path(tempfile.mkdtemp()) / "jina-images"))
func = get_registry().get("jina").create()

class Images(LanceModel):
    label: str
    image_uri: str = func.SourceField()
    image_bytes: bytes = func.SourceField()
    vector: Vector(func.ndims()) = func.VectorField()
    vec_from_bytes: Vector(func.ndims()) = func.VectorField()

table = db.create_table("images", schema=Images)
labels = ["cat", "cat", "dog", "dog", "horse", "horse"]
uris = [
    "http://farm1.staticflickr.com/53/167798175_7c7845bbbd_z.jpg",
    "http://farm1.staticflickr.com/134/332220238_da527d8140_z.jpg",
    "http://farm9.staticflickr.com/8387/8602747737_2e5c2a45d4_z.jpg",
    "http://farm5.staticflickr.com/4092/5017326486_1f46057f5f_z.jpg",
    "http://farm9.staticflickr.com/8216/8434969557_d37882c42d_z.jpg",
    "http://farm6.staticflickr.com/5142/5835678453_4f3a4edb45_z.jpg",
]
image_bytes = [requests.get(uri).content for uri in uris]
table.add(
    pd.DataFrame({"label": labels, "image_uri": uris, "image_bytes": image_bytes})
)