Skip to content

Commit

Permalink
db-tabulator: rename js preprocess to postprocess for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Sep 1, 2024
1 parent 387156f commit acf228b
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 56 deletions.
16 changes: 16 additions & 0 deletions db-tabulator/autosql/landing.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>AutoSQL</h2>
<form action="/autosql/generate" method="POST">
<div>
<label for="query-english">
Enter your query in English to get the corresponding SQL:
</label>
<br>
<br>
</div>
<textarea id="query-english" name="prompt" rows="4" cols="100"></textarea>
<br>
<br>
<div>
<button type="submit" class="sd-button">Generate SQL</button>
</div>
</form>
17 changes: 17 additions & 0 deletions db-tabulator/autosql/result.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div style="max-width: 800px">
<p>The generated SQL query is given below. Mistakes are possible. Use with caution.</p>
<br>

<pre>{{sql}}</pre>

<a href="https://quarry.wmcloud.org/" target="_blank">
<button>
Test it in Quarry
</button>
</a>

{{#if warnOnField}}
<br>
<p>NOTE: The above SQL may need adjustments for schema normalization, as the AI does not know that <code>{{warnOnField}}</code> field is no longer available. The field needs to be replaced with use of <code>link_target</code> table. See <a href="https://phabricator.wikimedia.org/T300222">phab:T300222</a>.</p>
{{/if}}
</div>
46 changes: 46 additions & 0 deletions db-tabulator/autosql/web-endpoint.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit acf228b

Please sign in to comment.