Elffuss BraidHub

kikocis/lancedb public

main · 4.2 KiBRenderedRawPermalink
1Due to the nature of vector embeddings, they can be used to represent any kind of data, from text to images to audio.
2This makes them a very powerful tool for machine learning practitioners.
3However, there's no one-size-fits-all solution for generating embeddings - there are many different libraries and APIs
4(both commercial and open source) that can be used to generate embeddings from structured/unstructured data.
5
6LanceDB supports 3 methods of working with embeddings.
7
81. You can manually generate embeddings for the data and queries. This is done outside of LanceDB.
92. You can use the built-in [embedding functions](./embedding_functions.md) to embed the data and queries in the background.
103. You can define your own [custom embedding function](./custom_embedding_function.md)
11 that extends the default embedding functions.
12
13For python users, there is also a legacy [with_embeddings API](./legacy.md).
14It is retained for compatibility and will be removed in a future version.
15
16## Quickstart
17
18To get started with embeddings, you can use the built-in embedding functions.
19
20### OpenAI Embedding function
21
22LanceDB registers the OpenAI embeddings function in the registry as `openai`. You can pass any supported model name to the `create`. By default it uses `"text-embedding-ada-002"`.
23
24=== "Python"
25
26 ```python
27 import lancedb
28 from lancedb.pydantic import LanceModel, Vector
29 from lancedb.embeddings import get_registry
30
31 db = lancedb.connect("/tmp/db")
32 func = get_registry().get("openai").create(name="text-embedding-ada-002")
33
34 class Words(LanceModel):
35 text: str = func.SourceField()
36 vector: Vector(func.ndims()) = func.VectorField()
37
38 table = db.create_table("words", schema=Words, mode="overwrite")
39 table.add(
40 [
41 {"text": "hello world"},
42 {"text": "goodbye world"}
43 ]
44 )
45
46 query = "greetings"
47 actual = table.search(query).limit(1).to_pydantic(Words)[0]
48 print(actual.text)
49 ```
50
51=== "TypeScript"
52
53 ```typescript
54 --8<--- "nodejs/examples/embedding.ts:imports"
55 --8<--- "nodejs/examples/embedding.ts:openai_embeddings"
56 ```
57
58=== "Rust"
59
60 ```rust
61 --8<--- "rust/lancedb/examples/openai.rs:imports"
62 --8<--- "rust/lancedb/examples/openai.rs:openai_embeddings"
63 ```
64
65### Sentence Transformers Embedding function
66LanceDB registers the Sentence Transformers embeddings function in the registry as `sentence-transformers`. You can pass any supported model name to the `create`. By default it uses `"sentence-transformers/paraphrase-MiniLM-L6-v2"`.
67
68=== "Python"
69 ```python
70 import lancedb
71 from lancedb.pydantic import LanceModel, Vector
72 from lancedb.embeddings import get_registry
73
74 db = lancedb.connect("/tmp/db")
75 model = get_registry().get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cpu")
76
77 class Words(LanceModel):
78 text: str = model.SourceField()
79 vector: Vector(model.ndims()) = model.VectorField()
80
81 table = db.create_table("words", schema=Words)
82 table.add(
83 [
84 {"text": "hello world"},
85 {"text": "goodbye world"}
86 ]
87 )
88
89 query = "greetings"
90 actual = table.search(query).limit(1).to_pydantic(Words)[0]
91 print(actual.text)
92 ```
93
94=== "TypeScript"
95
96 Coming Soon!
97
98=== "Rust"
99
100 Coming Soon!
101
102### Embedding function with LanceDB cloud
103Embedding functions are now supported on LanceDB cloud. The embeddings will be generated on the source device and sent to the cloud. This means that the source device must have the necessary resources to generate the embeddings. Here's an example using the OpenAI embedding function:
104
105```python
106import os
107import lancedb
108from lancedb.pydantic import LanceModel, Vector
109from lancedb.embeddings import get_registry
110os.environ['OPENAI_API_KEY'] = "..."
111
112db = lancedb.connect(
113 uri="db://....",
114 api_key="sk_...",
115 region="us-east-1"
116)
117func = get_registry().get("openai").create()
118
119class Words(LanceModel):
120 text: str = func.SourceField()
121 vector: Vector(func.ndims()) = func.VectorField()
122
123table = db.create_table("words", schema=Words)
124table.add(
125 [
126 {"text": "hello world"},
127 {"text": "goodbye world"}
128 ]
129 )
130
131query = "greetings"
132actual = table.search(query).limit(1).to_pydantic(Words)[0]
133print(actual.text)
134```