-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatWithYourDb.ts
307 lines (283 loc) · 9.14 KB
/
chatWithYourDb.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { ActionDefinition, ActionContext, OutputObject } from 'connery';
import pkg from 'pg';
const { Client } = pkg;
import { Anthropic } from '@anthropic-ai/sdk';
const actionDefinition: ActionDefinition = {
key: 'chatWithYourDb',
name: 'Chat with your DB',
description: 'Users can send DB requests in natural language and receive data and/or helpful feedback.',
type: 'read',
inputParameters: [
{
key: 'anthropicApiKey',
name: 'Anthropic API Key',
description: 'Your Anthropic API key',
type: 'string',
validation: {
required: true,
},
},
{
key: 'connectionString',
name: 'Database Connection String',
description: 'PostgreSQL connection string (should use read-only credentials)',
type: 'string',
validation: {
required: true,
},
},
{
key: 'instructions',
name: 'Instructions',
description: 'Optional instructions for processing the response',
type: 'string',
validation: {
required: false,
},
},
{
key: 'maxRows',
name: 'Maximum Rows',
description: 'Maximum number of rows to return (default: 100)',
type: 'string',
validation: {
required: false,
},
},
{
key: 'question',
name: 'Question',
description: 'Your database question in natural language',
type: 'string',
validation: {
required: true,
},
},
],
operation: {
handler: handler,
},
outputParameters: [
{
key: 'data',
name: 'Data',
description: 'The data returned by your database query',
type: 'string',
validation: {
required: true,
},
},
{
key: 'query',
name: 'Query',
description: 'The generated SQL query',
type: 'string',
validation: {
required: true,
},
},
],
};
export default actionDefinition;
export async function handler({ input }: ActionContext): Promise<OutputObject> {
let client: pkg.Client | null = null;
try {
// Always generate new schema
client = new Client(input.connectionString);
await client.connect();
await client.query('SELECT 1'); // Test connection
const schemaInfo = await getSchemaInfo(client);
const sqlQuery = await generateSqlQuery(input.anthropicApiKey, schemaInfo, input.question, parseInt(input.maxRows || '100'));
const result = await client.query(sqlQuery);
// Format each part separately
const dataResponse = formatDataResponse(result.rows, input.instructions);
const queryResponse = formatQueryResponse(sqlQuery);
// Return all responses
return {
data: dataResponse,
query: queryResponse,
};
} catch (error: unknown) {
throw error;
} finally {
if (client) {
try {
await client.end();
} catch (closeError) {
// Silently handle connection closing errors
}
}
}
}
async function getSchemaInfo(client: pkg.Client): Promise<string> {
const schemaQuery = `
WITH columns_info AS (
SELECT
c.table_schema,
c.table_name,
c.column_name,
c.data_type,
c.is_nullable,
c.column_default,
c.ordinal_position
FROM
information_schema.columns c
WHERE
c.table_schema NOT IN ('pg_catalog', 'information_schema')
),
primary_keys AS (
SELECT
kcu.table_schema,
kcu.table_name,
kcu.column_name
FROM
information_schema.table_constraints tc
JOIN
information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
WHERE
tc.constraint_type = 'PRIMARY KEY'
AND tc.table_schema NOT IN ('pg_catalog', 'information_schema')
),
foreign_keys AS (
SELECT
kcu.table_schema AS table_schema,
kcu.table_name AS table_name,
kcu.column_name AS column_name,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints tc
JOIN
information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN
information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE
tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema NOT IN ('pg_catalog', 'information_schema')
)
SELECT
jsonb_pretty(
jsonb_agg(
jsonb_build_object(
'table_schema', tbl.table_schema,
'table_name', tbl.table_name,
'columns', tbl.columns
)
)
) AS schema_json
FROM (
SELECT
c.table_schema,
c.table_name,
jsonb_agg(
jsonb_build_object(
'column_name', c.column_name,
'data_type', c.data_type,
'is_nullable', c.is_nullable,
'column_default', c.column_default,
'is_primary_key', CASE WHEN pk.column_name IS NOT NULL THEN true ELSE false END,
'is_foreign_key', CASE WHEN fk.column_name IS NOT NULL THEN true ELSE false END,
'foreign_table_schema', fk.foreign_table_schema,
'foreign_table_name', fk.foreign_table_name,
'foreign_column_name', fk.foreign_column_name
) ORDER BY c.ordinal_position
) AS columns
FROM
columns_info c
LEFT JOIN
primary_keys pk
ON c.table_schema = pk.table_schema
AND c.table_name = pk.table_name
AND c.column_name = pk.column_name
LEFT JOIN
foreign_keys fk
ON c.table_schema = fk.table_schema
AND c.table_name = fk.table_name
AND c.column_name = fk.column_name
GROUP BY
c.table_schema,
c.table_name
ORDER BY
c.table_schema,
c.table_name
) tbl;
`;
const schemaResult = await client.query(schemaQuery);
const schemaJson = schemaResult.rows[0].schema_json;
return schemaJson;
}
async function generateSqlQuery(apiKey: string, schemaInfo: string, question: string, maxRows: number): Promise<string> {
const systemPrompt = `You are a PostgreSQL expert. Generate secure, read-only SQL queries based on natural language questions.
Schema information: ${schemaInfo}
Important: Return ONLY the raw SQL query without any formatting, markdown, or code blocks.
Rules:
- Use ONLY tables and columns that exist in the provided schema information
- Do not make assumptions about columns that aren't explicitly listed in the schema
- Generate only SELECT queries (no INSERT, UPDATE, DELETE, etc.)
- Ensure queries are optimized for performance
- Include relevant JOINs when needed
- Add inline comments with -- to explain the query
- Limit results to ${maxRows} rows using LIMIT clause
- Use explicit column names instead of SELECT *
- Add ORDER BY clauses when relevant
- Do not include markdown code blocks or SQL syntax highlighting in your response
- Do not include any other text in your response
- If you cannot construct a query using only the available columns, respond with an error message starting with "ERROR:"`;
const ai = new Anthropic({ apiKey });
const completion = await ai.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 8192,
messages: [
{
role: "user",
content: systemPrompt + "\n\n" + question
}
],
temperature: 0
});
const sqlQuery = completion.content[0]?.type === 'text' ? completion.content[0].text : null;
if (!sqlQuery) {
throw new Error('Failed to generate SQL query: No response from Anthropic');
}
if (sqlQuery.startsWith('ERROR:')) {
throw new Error(sqlQuery);
}
return sqlQuery;
}
function formatDataResponse(rows: any[], instructions?: string): string {
let response = '';
// Handle empty results
if (!rows || rows.length === 0) {
response = "No data found for your query.";
} else {
try {
const sanitizedRows = rows.map(row => {
const sanitizedRow: any = {};
for (const [key, value] of Object.entries(row)) {
sanitizedRow[key] = typeof value === 'bigint' || typeof value === 'number'
? value.toString()
: value;
}
return sanitizedRow;
});
response = JSON.stringify(sanitizedRows, null, 2);
} catch (error) {
throw new Error(`Error formatting database response: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Add instructions if provided
if (instructions) {
response = `Instructions for the following content: ${instructions}\n\n${response}`;
}
return response;
}
function formatQueryResponse(sqlQuery: string): string {
return sqlQuery;
}