Skip to content

Commit b580fdb

Browse files
committed
Docs: Add example
1 parent c83e507 commit b580fdb

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Compact, yet Powerful<br/>
4141
- [ ] Multi-index lookups in Python.
4242
- [ ] Thread-safe `reserve`.
4343
- [ ] Distributed construction.
44+
- [x] AI + Vector Search = Semantic Search.
4445

4546
[usearch-header]: https://github.com/unum-cloud/usearch/blob/main/include/usearch/usearch.hpp
4647

@@ -52,7 +53,7 @@ Most vector-search packages focus on just 2 metrics - "Inner Product distance" a
5253
The lack of dedicated "Cosine distance" can be justified with the simplicity of normalizing such vectors on the fly.
5354
But that hardly exhausts the list of possible metrics.
5455

55-
![USearch: Vector Search Approaches](assets/usearch-approaches-transparent.png)
56+
![USearch: Vector Search Approaches](assets/usearch-approaches-white.png)
5657

5758
Older approaches indexing high-dimensional spaces, like KD-Trees and Locality Sensitive Hashing are hardly extendible to vectors/objects of variable length.
5859
Modern NSW-like approaches, however, only require two objects to be comparable.
@@ -378,3 +379,39 @@ OPTIONS
378379

379380
- JavaScript: Allow calling from "worker threads".
380381
- Rust: Allow passing a custom thread ID.
382+
383+
## AI + Vector Search = Semantic Search
384+
385+
AI has a growing number of applications, but one of the coolest classic ideas is to use it for Semantic Search.
386+
One can take an encoder model, like the multi-modal UForm, and a web-programming framework, like UCall, and build an image search platform in just 20 lines of Python.
387+
388+
```python
389+
import ucall.rich_posix as ucall
390+
import uform
391+
import usearch
392+
393+
import numpy as np
394+
from PIL import Image
395+
396+
server = ucall.Server()
397+
model = uform.get_model('unum-cloud/uform-vl-multilingual')
398+
index = usearch.Index(dim=256)
399+
400+
@server
401+
def add(label: int, photo: Image.Image):
402+
image = model.preprocess_image(photo)
403+
vector = model.encode_image(image).detach().numpy()
404+
labels = np.array([label], dtype=np.longlong)
405+
index.add(labels, vector, copy=True)
406+
407+
@server
408+
def search(query: str) -> np.ndarray:
409+
tokens = model.preprocess_text(query)
410+
vector = model.encode_text(tokens).detach().numpy()
411+
neighbors = index.search(vector, 3)
412+
return neighbors[0][:neighbors[2][0]]
413+
414+
server.run()
415+
```
416+
417+
Check [that](https://github.com/ashvardanian/image-search) and [other](https://github.com/unum-cloud/examples) examples on our corporate GitHub 🤗

0 commit comments

Comments
 (0)