I have the open source “Apache answer” project deployed. Now, whenever a new question is asked I want to make an api call to OpenAI. What are some approaches? I like ease of development right now since this is an mvp.
Answer doesn't seem to have any webhooks out of the box. Here's the API for my instance.
It does have some limited email notifications, but those seem slow. Querying every few minutes for new questions also seems slow. I want the user to have the feeling of their question getting answered in seconds, not minutes.
I have a working Function defined using DigitalOcean Functions that, when called with a question_id will POST an answer from ChatGPT-4o back to that question.
I tried migrating Ducky.foo over to using Postgres on Supabase thinking that I'd kill two birds with one stone-- a more scalable database and the ability to call my DO Function from a database trigger. I had a successful test environment with Answer running on Supabase Postgres but got stuck trying to setup the trigger.
From the Supabase docs, I got this trigger set up:
-- Create the trigger for table update
CREATE TRIGGER sample_trigger
AFTER UPDATE ON test_table
FOR EACH ROW
EXECUTE FUNCTION sample_trigger_function();
and it's calling this function:
-- Create the function that will be called by the trigger
CREATE OR REPLACE FUNCTION sample_trigger_function()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
-- Calls pg_net function net.http_post to send a request to the Postman API
PERFORM "net"."http_post"(
'https://postman-echo.com/post'::text,
jsonb_build_object(
'old_row', to_jsonb(OLD.*),
'new_row', to_jsonb(NEW.*)
),
headers:='{"Content-Type": "application/json"}'::jsonb
) AS request_id;
RETURN NEW;
END $$;
It seems to work when I UPDATE a row in the table:
UPDATE test_table SET value = 706 WHERE name = 'Test';
returns UPDATE test_table SET value = 706 WHERE name = 'Test';
But I couldn't find Trigger/Function logs in Supabase to see the result of the run.
So then I tried updating the sample_trigger_function
to call my DO Function:
CREATE OR REPLACE FUNCTION public.sample_trigger_function()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
-- Calls pg_net function net.http_post to send a request to the specified API
PERFORM public.net.http_post(
'https://faas-nyc1-2ef2e6cc.doserverless.co/api/v1/namespaces/fn-4debf4e9-6121-41dc-a674-9ba721490ff8/actions/answer/test-pg-chatgpt-4o?blocking=true&result=true'::text,
jsonb_build_object(
'question_id', '10010000000000009'
),
headers:='{
"Content-Type": "application/json",
"Authorization": "Basic <TOKEN>"
}'::jsonb
) AS request_id;
RETURN NEW;
END $$;
But now when I UPDATE a row in the database, I get this error:
ERROR: 0A000: cross-database references are not implemented: public.net.http_post
QUERY: SELECT public.net.http_post(
'https://faas-nyc1-2ef2e6cc.doserverless.co/api/v1/namespaces/fn-4debf4e9-6121-41dc-a674-9ba721490ff8/actions/answer/test-pg-chatgpt-4o?blocking=true&result=true'::text,
jsonb_build_object(
'question_id', '10010000000000009'
),
headers:='{
"Content-Type": "application/json",
"Authorization": "Basic ZDU3NGM0ZTYtMzMzOS00NDJmLWE1NjUtOGI3ZDJlMmU1ZjdhOlB6MWs3Q0ZKdjREM2Y0WWZmd1JDYjBVR3p0R3F5bjJCbFpSMHpDcjhRdmlyZFB2eHlsUXdDZHloV3RRS2syS2s="
}'::jsonb
) AS request_id
CONTEXT: PL/pgSQL function sample_trigger_function() line 4 at PERFORM
I'm not sure whether to keep plowing ahead or to try a different path.