-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_llm.sql
More file actions
98 lines (84 loc) · 2.83 KB
/
call_llm.sql
File metadata and controls
98 lines (84 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
CREATE EXTENSION IF NOT EXISTS http;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE OR REPLACE FUNCTION call_llm()
RETURNS TRIGGER AS $$
DECLARE
-- Customizable variables or can be referenced from environment variables/Vault
API_KEY CONSTANT TEXT := '<your-api-key>'; -- Get your api key from https://interfaze.ai/dashboard
BASE_URL CONSTANT TEXT := 'https://api.interfaze.ai/v1/chat/completions';
MODEL_NAME CONSTANT TEXT := 'interfaze-alpha';
MAX_TOKENS CONSTANT INTEGER := 1000;
prompt_text TEXT;
target_column TEXT;
context_column TEXT;
context_value TEXT;
request_body TEXT;
response RECORD;
llm_result TEXT;
BEGIN
IF TG_ARGV[1] IS NULL THEN
RAISE EXCEPTION 'Second argument (target column) is required';
END IF;
target_column := TG_ARGV[1];
IF TG_ARGV[0] IS NULL THEN
RAISE EXCEPTION 'First argument (prompt) is required';
END IF;
prompt_text := TG_ARGV[0];
IF TG_ARGV[2] IS NOT NULL THEN
context_column := TG_ARGV[2];
BEGIN
context_value := (hstore(NEW) -> context_column);
IF context_value IS NOT NULL AND context_value != '' THEN
prompt_text := prompt_text || ' Context: "' || context_value || '"';
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE WARNING 'Failed to read context column %: %', context_column, SQLERRM;
END;
END IF;
IF prompt_text IS NULL OR prompt_text = '' THEN
RAISE EXCEPTION 'Prompt text cannot be null or empty';
END IF;
request_body := json_build_object(
'model', MODEL_NAME,
'messages', json_build_array(
json_build_object(
'role', 'user',
'content', prompt_text
)
),
'max_tokens', MAX_TOKENS
)::TEXT;
SET http.timeout_msec = 30000;
SELECT * INTO response
FROM http((
'POST',
BASE_URL,
ARRAY[
http_header('Content-Type', 'application/json'),
http_header('Authorization', 'Bearer ' || API_KEY)
],
'application/json',
request_body
)::http_request);
IF response.status != 200 THEN
RAISE WARNING 'API request failed with status %: %', response.status, response.content;
RETURN NEW;
END IF;
BEGIN
llm_result := (response.content::jsonb)->'choices'->0->'message'->>'content';
EXCEPTION
WHEN OTHERS THEN
RAISE WARNING 'Failed to parse response: %', response.content;
RETURN NEW;
END;
BEGIN
NEW := NEW #= hstore(target_column, llm_result);
EXCEPTION
WHEN OTHERS THEN
RAISE WARNING 'Failed to update column %: %', target_column, SQLERRM;
END;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
set statement_timeout TO '1min';