The Bagel Python client offers a comprehensive interface for interacting with the BagelDB service, enabling users to perform a wide range of operations such as creating clusters, adding documents, querying clusters, managing images, and more.
Installation
To install the Bagel Python client, you can use pip:
pip install betabageldb
Getting Started
Begin by initializing a Bagel client with the appropriate server settings. You can specify the server host, API implementation, and other settings using the Settings object.
Verify connectivity to the Bagel server by pinging it:
print("Ping:", client.ping())
Get Bagel Server Version
Retrieve the version of the Bagel server:
print("Version:", client.get_version())
Cluster Operations
Create and Delete a Cluster
Create a new cluster and then delete it:
defcreate_and_delete(api): name =str(uuid.uuid4())# Generate a unique cluster name using UUID cluster = api.create_cluster(name)print("Cluster created:", cluster) api.delete_cluster(name)print("Cluster deleted:", name)create_and_delete(client)
Create, Add, and Get Documents
Create a cluster, add documents to it, and then retrieve documents:
Add images to a cluster and then find similar images based on embeddings:
pythonCopy codedef add_image_find(api): name ="image_add_test" cluster = api.get_or_create_cluster(name=name, embedding_model="bagel-multi-modal") img_file_list = ["image1.jpg","image2.jpg"] # Add image paths to the listfor filename in img_file_list: resp = cluster.add_image(filename)print("Response:", resp.json())print("Count of images:", cluster.count()) first_item = cluster.peek(1) embeddings = first_item.get("embeddings")[0] results = cluster.find(query_embeddings=embeddings, n_results=5)print("Search results:", results)add_image_find(client)
Add Images by URLs and Find Similar Images
Add images to a cluster by providing their URLs and then find similar images based on embeddings:
pythonCopy codedef add_image_urls_find(api): name ="image_add_urls_test" cluster = api.get_or_create_cluster(name=name, embedding_model="bagel-multi-modal") urls = ["https://example.com/image1.jpg","https://example.com/image2.jpg", ] # Add image URLs to the list ids = [str(uuid.uuid4())for _ inrange(len(urls))] resp = cluster.add_image_urls(ids=ids, urls=urls)print("Response:", resp.json())print("Count of images:", cluster.count()) first_item = cluster.peek(1) embeddings = first_item.get("embeddings")[0] results = cluster.find(query_embeddings=embeddings, n_results=5)print("Search results:", results)add_image_urls_find(client)
With these functionalities documented, users can effectively utilize the Bagel Python client to interact with the BagelDB service for various tasks, including document and image management, similarity search, and more.