Log to phospho in a Supabase app with a webhook
Add AI analytics to your Supabase chatbot with phospho
phospho is a platform that helps you build better chatbots by providing AI analytics about the user experience of your chatbot.
Supabase is an open-source database, authentication system, and hosting platform that allows you to quickly and easily build powerful web-based applications.
If you’re using Supabase to build a chatbot, here’s how you can log your chatbot messages to phospho using a Supabase Database webhook, a Supabase Edge Function, and the phospho API.
Prerequisites
We assume in this guide that you have already set up a Supabase project.
We also assume that you have already created the chatbot UI using Supabase (here’s a template).
Add the phospho API key and project id to your Supabase project
Create an account on phospho and get your API key and project id from the Settings.
Then, add the PHOSPHO_API_KEY
and PHOSPHO_PROJECT_ID
secrets to your Supabase project.
Option 1: In the CLI
Add the phospho API key and project id to your ./supabase/.env
file:
Push those secrets to your Supabase project:
Option 2: In the console UI
Add directly the phospho API key and project id as Edge Functions Secrets in the Supabase console. Go to Settings/Edge Functions, and create the PHOSPHO_API_KEY
and PHOSPHO_PROJECT_ID
secrets.
Setup your chat_history table
If you’re using Supabase to build a chatbot, you probably already have a table that stores the chat history of your users. This table lets your users access their chat history on your app event after they close the website.
If you don’t, you need to create a chat_history
table.
Here’s what your chat_history
table should look like:
message_id | chat_id | user_message | assistant_response | metadata |
---|---|---|---|---|
c8902bda28 | 9bc8eda | Hi | Hello! How can I help you? | {“model_name”: “gpt-3.5”} |
Here are the columns of the table:
message_id
(UUID), the unique id of the message.chat_id
(UUID), the unique id of the chat. All the messages from the same conversation should have the samechat_id
.user_message
(TEXT), the message sent by the user.assistant_response
(TEXT), is the response displayed to the user. It can be the direct generation of an LLM, or the result of a multistep generation.- (Optional)
metadata
(JSON), a dictionary containing metadata about the message
Create the table
In Supabase, create a new table called chat_history
with the columns described above. Customize the table to match your app behaviour.
Here’s for example the SQL code to create the table with the columns described above:
Update the table
The table chat_history
should be updated every time a new message is sent to your chatbot.
Example of how to insert a new row in the chat_history table with Supabase:
Setup the Supabase Edge Function
Let’s create a Supabase Edge Function that will log the chat message to phospho using the phospho API. Later, we will trigger this function with a Supabase Database webhook.
Create the Edge Function
Create a new Edge Function called phospho-logging inside your project:
This creates a function stub in your supabase
folder:
Write the code to call the phospho API
In the newly created index.ts
file, we add a basic code that:
- Gets the phospho API key and project id from the environment variables.
- Converts the payload sent by Supabase to the format expected by the phospho API.
- Sends the payload to the phospho API.
Here’s an example of what the code could look like:
Feel free to change the code to adapt it to your chat_history
table and to how you chat messages are stored.
Deploy the Edge Function
Deploy the function to your Supabase project:
https://supabase.com/dashboard/project/project-ref
Setup the Supabase Webhook
Now that you have created the Supabase Edge Function, create a Supabase Database webhook to trigger it.
Create the webhook
In the Supabase console, go to Database/Webhook.
Click on Create new in the top right. Make the webhook trigger on the chat_history
table, and on the INSERT
and UPDATE
events.
Call the Edge Function with authentication
In the webhook configuration, select the type of webhook “Supabase Edge Function” and select the phospho-logging
you just deployed.
In the HTTP Headers section, add an Authorization
header with the value Bearer ${SUPABSE_PROJECT_ANON_PUBLIC_KEY}
. Find your anon public key in the console, in the tabs Settings/API/Project API keys.
Test the webhook
To test the webhook, insert a row in the chat_history
table, and the webhook should be triggered. You’ll see the logs in the phospho dashboard.
You can also send a message to your chatbot. This will now trigger the webhook and log the message to phospho.
Next steps
You’re done! Your are now logging the chatbot messages to phospho and can learn how the users interact with your chatbot using the phospho dashboard and AI analytics.
Learn more about phospho features by reading the guides:
Was this page helpful?