diff --git a/db-tabulator/autosql/landing.hbs b/db-tabulator/autosql/landing.hbs new file mode 100644 index 0000000..0587627 --- /dev/null +++ b/db-tabulator/autosql/landing.hbs @@ -0,0 +1,16 @@ +

AutoSQL

+
+
+ +
+
+
+ +
+
+
+ +
+
\ No newline at end of file diff --git a/db-tabulator/autosql/result.hbs b/db-tabulator/autosql/result.hbs new file mode 100644 index 0000000..247a6dd --- /dev/null +++ b/db-tabulator/autosql/result.hbs @@ -0,0 +1,17 @@ +
+

The generated SQL query is given below. Mistakes are possible. Use with caution.

+
+ +
{{sql}}
+ + + + + + {{#if warnOnField}} +
+

NOTE: The above SQL may need adjustments for schema normalization, as the AI does not know that {{warnOnField}} field is no longer available. The field needs to be replaced with use of link_target table. See phab:T300222.

+ {{/if}} +
diff --git a/db-tabulator/autosql/web-endpoint.ts b/db-tabulator/autosql/web-endpoint.ts new file mode 100644 index 0000000..15907b1 --- /dev/null +++ b/db-tabulator/autosql/web-endpoint.ts @@ -0,0 +1,46 @@ +import * as express from "express"; +import 'express-async-errors'; +import OpenAI from "openai"; +import {AuthManager, log} from "../../botbase"; + +const router = express.Router(); + +const client = new OpenAI({ + apiKey: AuthManager.get('openai').key +}); + +router.get('/', async function (req, res) { + return res.render('db-tabulator/autosql/landing') +}); + +router.post('/generate', async function (req, res, next) { + if (!req.body.prompt) { + return res.status(400).render('webservice/views/oneline', { + text: 'Bad request: required parameter "prompt" missing' + }) + } + const response = await client.chat.completions.create({ + messages: [{ + role: 'user', + content: + 'Using MediaWiki\'s db schema outlined at https://www.mediawiki.org/wiki/Manual:Database_layout, write an SQL query to perform the below-mentioned. Respond only with the SQL query.\n\n' + + req.body.prompt + }], + model: 'gpt-3.5-turbo', + }) + const sql = response.choices[0].message.content + const logEntry = { + prompt: req.query.prompt, + response: sql + + } + return res.render('db-tabulator/autosql/result', { + sql: sql, + warnOnField: + sql.includes('pl_title') ? 'pl_title' : + sql.includes('tl_title') ? 'tl_title' : + null + }) +}); + +export default router; diff --git a/webservice/route-registry.ts b/webservice/route-registry.ts index 479bfd7..7672530 100644 --- a/webservice/route-registry.ts +++ b/webservice/route-registry.ts @@ -10,12 +10,14 @@ import dykRouter from '../dyk-counts/web-endpoint'; import gitsync from "./routes/gitsync"; import botMonitorRouter from '../bot-monitor/web-endpoint' import gitlabRouter from './routes/gitlab'; +import autoSqlRouter from "../db-tabulator/autosql/web-endpoint"; export function registerRoutes(app: express.Router) { app.use('/', indexRouter); app.use('/logs', logsRouter); app.use('/logs.php', logsRouter); // support old URLs from the time webservice was in php app.use('/database-report', dbReportRouter); + app.use('/autosql', autoSqlRouter) app.use('/gans', gansRouter); app.use('/summary', summaryRouter); app.use('/articlesearch', articleSearchRouter);