Document Embeddings
The IONOS AI Model Hub API allows you to access vector databases to persist your document collections and find semantically similar documents.
The vector database is used to persist documents in document collections. Each document is any form of pure text. In the document collection not only the input text is persisted, but also a transformation of the input text into an embedding. Each embedding is a vector of numbers. Input texts which are semantically similar have similar embeddings. A similarity search on a document collection finds the most similar embeddings for a given input text. These embeddings and the corresponding input text are returned to the user.
Overview
This tutorial is intended for developers. It assumes you have basic knowledge of:
REST APIs and how to call them
A programming language to handle REST API endpoints (for illustration purposes, the tutorials uses Python and Bash scripting)
By the end of this tutorial, you'll be able to:
Create, delete and query a document collection in the IONOS vector database
Save, delete and modify documents in the document collection and
Answer customer queries using the document collection.
Background
The IONOS AI Model Hub API offers a vector database that you can use to persist text in document collections without having to manage corresponding hardware yourself.
Our AI Model Hub API provides all required functionality without your data being transfered out of Germany.
Before you begin
To get started, you should open your IDE to enter Python code.
Install required libraries
You need to install the modules requests and pandas to your python environment:
Import required libraries
You need to import the following modules:
Generate header for API requests
Next generate a header document to authenticate yourself against the REST API:
After this step, you have installed all python modules and have one variable header you can use to access our vector database.
Manage document collections
In this section you learn how to create a document collection. We will use this document collection to fill it with the data from your knowledge base in the next step.
To track, if something went wrong this section also shows how to:
List existing document collections
Remove document collections
Get meta data of a document collection
Create document collections
Create a document collections
To create a document collection, you have to specify the name of the collection and a description and invoke the endpoint to generate document collections:
If the creation of the document collection was successful, the status code of the request is 201 and it returns a JSON document with all relevant information concerning the document collection.
Extract collection id from request result
To modify the document collection you need its identifier. You can extract it using:
List existing document collections
To ensure that the previous step went as expected, you can list the existing document collections.
List all existing document collections
To retrieve a list of all document collections saved by you:
This query returns a JSON document consisting of your document collections and corresponding meta information
Convert list of endpoints to a pandas dataframe
You can convert this JSON document to a human readable form using:
The result consists of 8 attributes of which 3 are relevant for you:
id: The identifier of the document collection
properties.description: The textual description of the document collection
properties.documentsCount: The number of documents persisted in the document collection
If you have not created a collection yet, the field items is an empty list.
Remove a document collection
If the list of document collections consists of document collections you do not need anymore, you can remove a document collection by invoking:
This query returns a status code which indicates whether the deletion was successful:
204: Status code for successfull deletion
404: Status code given the collection did not exist
Get meta data for a document collection
Access meta data from a document collection
If you are interested in the meta data of a collection, you can extract it by invoking:
This query returns a status code which indicates whether the collection exists:
200: Status code if the collection exists
404: Status code given the collection does not exist
Extract collection meta data from request result
The body of the request consists of all meta data of the document collection.
Manage documents in document collection
In this section, you learn how to add documents to the newly created document collection. To validate your insertion, this section also shows how to
List the documents in the document collection,
Get meta data for a document,
Update an existing document and
Prune a document collection.
Add documents to document collection
To add an entry to the document collection, you need to at least specify the content, the name of the content and the contentType:
Note:
You need to encode your content using base64 prior to adding it to the document collection. This is done here in line 4 of the source code.
This request returns a status code 200 if adding the document to the document collection was successful.
List existing documents in document collection
To ensure that the previous step went as expected, you can list the existing documents of your document collection.
List all existing documents in a document collections
To retrieve a list of all documents in the document collection saved by you:
This query returns a JSON document consisting of your documents in the document collection and corresponding meta information
Convert list of documents to a pandas dataframe
You can convert this JSON document to a pandas dataframe using:
The result consists of 10 attributes of which 5 are relevant for you:
id: The identifier of the document
properties.content: The base64 encoded content of the document
properties.name: The name of the document
properties.description: The description of the document
properties.labels.number_of_tokens: The number of tokens in the document
If you have not created the collection yet, the request will return a status code 404. It will return a JSON document with the field items set to an empty list if no documents were added yet.
Get meta data for a document
Access meta data from a document
If you are interested in the metadata of a document, you can extract it by invoking:
This query returns a status code which indicates whether the document exists:
200: Status code if the document exists
404: Status code given the document does not exist
Extract collection meta data from request result
The body of the request consists of all meta data of the document.
Update a document
If you want to update a document, invoke:
This will replace the existing entry in the document collection with the given id by the payload of this request.
Prune a document collection
If you want to remove all documents from a document collection invoke:
This query returns the status code 204 if pruning the document collection was successful.
Query documents in the document collection
Finally, this section shows how to use the document collection and the contained documents to answer a user query.
Retrieve document relevant for querying
To retrieve the documents relevant for answering the user query, invoke the query endpoint as follows:
This will return a list of the NUM_OF_DOCUMENTS most relevant documents in your document collection for answering the user query.
Decode Base64 encoded documents
Now, decode the retrieved documents back to string using:
Summary
In this tutorial you learned how to use the IONOS AI Model Hub API to conduct semantic similarity searches using our vector database.
Namely, you learned how to:
Create a necessary document collection in the vector database and modify it
Insert your documents into the document collection and modify the documents
Conduct semantic similarity searches using your document collection.
Last updated