Search And Data Processing

Building AI and ML applications with Xano using Embeddings

Summary

Xano's new vector embeddings field opens up exciting possibilities for enhancing search and data processing capabilities by leveraging AI models like OpenAI. Instead of relying solely on exact matches, you can now determine complex relationships and semantic meanings between your data. This guide will walk you through setting up vector embeddings, indexing them, and building a function stack to query your data based on semantic relationships.

Setting Up Vector Embeddings

  1. Add a Vector Field: In your Xano database table, click the "+" sign and select the "Vector" field type. Give it a name like "embeddings".
  2. Specify the Number of Items: Enter the number of vectors you want to store per piece of data. This should balance your data needs and the model's requirements. For OpenAI, a good starting point is around 1,000 items, but ensure you don't exceed 2,000.
  3. Save Changes: After specifying the number of items, save your changes to create the embeddings column.

Generating Embeddings with OpenAI

  1. Set Up a Database Trigger: Go to the "Triggers" section and create a new trigger to generate embeddings whenever records are added or updated. This trigger should contain a function stack with an API request to OpenAI for generating embeddings based on your data.
  2. Use the OpenAI Curl Command: Copy the curl command from OpenAI's documentation and paste it into a new external API request in your function stack. Set up your OpenAI API key and define the input as the data you want to generate embeddings for.
  3. Publish and Test: Publish your changes, go back to your database, and edit or add records to trigger the embeddings generation.

Indexing Vector Embeddings

Indexing is crucial for efficient querying when working with vector embeddings. Even with smaller datasets, apply an index to your embeddings field:

  1. Create an Index: In your database table, click the "Index" button and choose the "Vector" index type.
  2. Select the Embeddings Field: Choose the embeddings field you created earlier.
  3. Set the Index Type: For OpenAI, select the "Inner Product" index type.
  4. Save Changes: Save your changes to apply the index to your embeddings field.

Building a Function Stack to Query Data

Now that you have indexed embeddings, you can build a function stack to query your data based on semantic relationships:

  1. Create a New Function Stack: Add an input field for the user's question or query.
  2. Generate Embeddings for the Query: Use an external API request to OpenAI to generate embeddings based on the user's input.
  3. Query Your Database: Add a "Query All Records" function and use an "eval" to calculate the similarity between the query embeddings and the database embeddings.
  4. Filter and Sort Results: Apply a vector filter (e.g., "Inner Product" for OpenAI) and sort the results by similarity in ascending order.
  5. Limit and Return Results: Limit the number of results returned based on your needs and send them back to OpenAI for a final response.

By following these steps, you can leverage the power of vector embeddings and AI models like OpenAI to enhance search and data processing capabilities within Xano. This opens up new possibilities for building intelligent applications that can understand and interpret data in more human-like ways.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free