Add natural language search to your app with Dgraph, Modus, and AI embeddings
similar_to
DQL function to search for similar text in
vector space.
Our example app uses ecommerce product data consisting of product details to
enable semantic product search based on natural language terms.
modus.json
to define the connection
to your Dgraph instance and the embedding model used to generate embeddings.
Here weβre using the MiniLM model hosted by Hypermode and connecting to a
locally running Dgraph instance.
hyp
CLI to connect your local environment
with your Hypermode account. See the Using Hypermode-hosted
models docs page for more
information.@json
decorator enables JSON
serialization, while @alias
maps property names to Dgraph convention friendly
formats.
Weβll be using ecommerce data so weβll create simple types defining products and
their categories.
Product.embedding
property, even though Dgraph can function without a
schema.
To define our Dgraph schema with vector indexing support we add the
@index(hnsw)
directive to the property storing the embedding value, in this
case Product.embedding
. We also define the other property types and node
labels.
/alter
endpoint of our Dgraph instance:
Product.embedding
property.
similar_to
query functionsimilar_to
query function
that leverages the vector index to find semantically similar products by
computing an embedding of the search term and searching for nearby product
descriptions in vector space.
modus dev
command which generates a GraphQL
schema from the functions weβve defined and start a local GraphQL endpoint for
testing and development.
Navigate to http://localhost:8686/explorer
in your browser and use the Modus
API Explorer to first insert sample data into Dgraph using the upsert mutation
function we defined previously and then search for similar products using vector
search.
First, to create product and category nodes:
product
variable creating three product
nodes ad their associated category nodes in Dgraph:
Product ID | Title | Description | Category |
---|---|---|---|
P001 | Solar-Powered Umbrella | A stylish umbrella with solar panels that charge your devices while you walk. | Outdoor Gear |
P002 | Self-Warming Coffee Mug | A mug that keeps your coffee at the perfect temperature using smart heating technology. | Kitchen Appliances |
P003 | Smart Pillow 2.0 | A pillow that tracks your sleep patterns and plays soothing sounds to help you fall asleep faster. | Smart Home |
$search
variable.