💻Vector search with code

A guide to building vector search with Vector AI

Assumed Knowledge: Vectors, Vector Search, Python (Basic level) Target Audience: General developer, Data scientist, Python developer Reading Time: 5 minutes Requirements: Python 3.6 or Python 3.7

Below, we build a simple example of image search with Vector AI

  1. We get the data on a document-based approach.

collection_name = 'pokemon_images'

documents = []
for i in range(1, 20):
    documents.append({
        'image': 'https://assets.pokemon.com/assets/cms2/img/pokedex/full/{}.png'.format(f'{i:03}'),
        'pokemon_id' : str(i),
        '_id': i
    })

2. We encode the images and instantiate the Vector AI client. If you do not have a username or API key, simply request one Pythonically from this link.

from vectorai.client import ViClient
vi_client = ViClient(username, api_key, url)
from vectorai.models.deployed import ViImage2Vec
image_encoder = ViImage2Vec(username, api_key, url)
for doc in documents:
    doc['image_vector_'] = image_encoder.encode(doc['image'])

3. Add your documents to your index using insert_documents.

vi_client.insert_documents(collection_name, documents)

4. Search your documents using search.

# Search the application
image_url = 'https://assets.pokemon.com/assets/cms2/img/pokedex/full/003.png')
search_results = vi_client.search(
collection_name,
image_encoder.encode(image_url), search_fields=['image_vector_'],
page_size=5)

# Show the results!
vi_client.show_json(search_results, image_fields=['image'], image_width=150)

Last updated