Assistants
Deploy your customized AI assistants
AI Assistants leverages ChatGPT and allows you to define different AI assistants with pre-defined tools and accesses to your data. This pre-defined access is expressed as GraphQL queries or mutations and/or webhooks and are unique to each AI assistant, meaning you can create different AI assistants with access to different type of data. In addition, AI assistants are fully integrated with Nhost permissions which means only data the user has access to will be made available to each AI assistant session.
Demo
To demonstrate how to configure and use AI Assistants we are going to be using a sample project with a database full of movies.
Creating AI Assistants
The first step is to create an AI assistant. You can create an AI assistant by utilizing the insertAssistant
mutation or by using the dashboard. For instance:
Tools
In order to enhance our AI assistants we are leveraging tools. Tools can be either queries or mutations performed against your project’s GraphQL engine or they can also be any arbitrary endpoint you choose. Using the example above, we added 4 different tools to our AI Assistant:
- The GraphQL query
GetMoviesWithScoreHigherThan
, which will allow the AI Assistant to query movies based on their score. - The GraphQL query
SearchMovies
, which leverages auto-embeddings and will allow the AI Assistant to query movies using natural language. - The GraphQL mutation
InsertMovie
, which will let the AI Assistant to insert new movies directly into the database. - The webhook
answer_deep_questions
, which is a serverless function that can respond to deep questions about life and the universe.
With a good name and description, the AI Assistant will know that certain information can be made available or that certain actions are at its disposal via these tools. We will see more in detail how this works a bit later but for now suffices to say that:
- ChatGPT doesn’t actually query the data or perform any action, it tells graphite to execute the tools on its behalf and provide the result.
- graphite will respect the user session when calling tools so permissions are respected.
- GraphQL queries and mutations are performed against the project’s GraphQL endpoint
- For webhooks you can specify any URL you want and graphite will perform a POST with a JSON object as body
Starting a session
To start a session you can use the mutation startSession
and pass the id of the assistant you want to use. For instance, to start a session using the assistant we created in the previous section:
You should receive a response similar to:
Interacting with the Assistant
Now that you have started a session you can send your first message:
The field prevMessageID
is used to only return messages after this one. As this is the beginning of the conversation we send an empty string to get all messages back. After executing that mutation we should receive a response like:
Something to note is that ChatGPT detected that this was a “deep question” that could be answered with the webhook answer_deep_questions
so this is roughly what happened:
We can continue the conversation by performing from mutations. Notice this time we can ask graphite to only return new message by specifying prevMessageID
:
Which should return something like:
In this case ChatGPT detected some of the information needed to formulate the answer could be retrieved with the GraphQL query GetMoviesWithScoreHigherThan
so it requested graphite to run the query and return the data. This is what happened:
Implementing workflows
Interactions are not limited to just one tool or to simply query data. Tools can be combined and they can perform any action you wish. For instance, let’s take a look to the following session:
In this session the following happened:
- We asked about comedies in space, which prompted the AI Assistant to leverage tool
SearchMovies
, which leverages auto-embeddings to perform searches using natural language. - Then we asked the Assistant to imagine a sequel and insert it in the database, which prompted the AI assistant to leverage the tool
InsertMovie
to add it to the database.
True, to its word, the movie was added to the database successfully:
And remember, this is just a tiny example of what tools can do. Tools can do anything you want; from working with data in your project to booking flights or selling your products to your users.
User session
Interacting with graphite requires a user session (either an Authorization header or the admin secret). When interacting with the GraphQL endpoint or the webhook, graphite will re-use this session, which means that the webhook can use these headers to authorize the request. It also means that the GraphQL query will be performed as the user, so the user will have to have permissions to perform the query and will only get data the user has access to.
Other GraphQL queries/mutation
In addition to the queries/mutations shown in this document there are other mutations for listing, deleting and updating assistants, sessions, etc. We recommend you to check the GraphQL schema to see what’s available.
Metadata and Permissions
For each assistant and session managed by graphite, a row in the tables graphite.assistants
and graphite.sessions
will be made available. The purpose of this metadata is to allow you to set permissions as with any other object in your project. For details on which permissions are needed for each specific query and mutation refer to the GraphQL documentation in the reference section.