Skip to content

Commit acf228b

Browse files
committed
db-tabulator: rename js preprocess to postprocess for clarity
1 parent 387156f commit acf228b

File tree

6 files changed

+650
-56
lines changed

6 files changed

+650
-56
lines changed

db-tabulator/autosql/landing.hbs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<h2>AutoSQL</h2>
2+
<form action="/autosql/generate" method="POST">
3+
<div>
4+
<label for="query-english">
5+
Enter your query in English to get the corresponding SQL:
6+
</label>
7+
<br>
8+
<br>
9+
</div>
10+
<textarea id="query-english" name="prompt" rows="4" cols="100"></textarea>
11+
<br>
12+
<br>
13+
<div>
14+
<button type="submit" class="sd-button">Generate SQL</button>
15+
</div>
16+
</form>

db-tabulator/autosql/result.hbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div style="max-width: 800px">
2+
<p>The generated SQL query is given below. Mistakes are possible. Use with caution.</p>
3+
<br>
4+
5+
<pre>{{sql}}</pre>
6+
7+
<a href="https://quarry.wmcloud.org/" target="_blank">
8+
<button>
9+
Test it in Quarry
10+
</button>
11+
</a>
12+
13+
{{#if warnOnField}}
14+
<br>
15+
<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>
16+
{{/if}}
17+
</div>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as express from "express";
2+
import 'express-async-errors';
3+
import OpenAI from "openai";
4+
import {AuthManager, log} from "../../botbase";
5+
6+
const router = express.Router();
7+
8+
const client = new OpenAI({
9+
apiKey: AuthManager.get('openai').key
10+
});
11+
12+
router.get('/', async function (req, res) {
13+
return res.render('db-tabulator/autosql/landing')
14+
});
15+
16+
router.post('/generate', async function (req, res, next) {
17+
if (!req.body.prompt) {
18+
return res.status(400).render('webservice/views/oneline', {
19+
text: 'Bad request: required parameter "prompt" missing'
20+
})
21+
}
22+
const response = await client.chat.completions.create({
23+
messages: [{
24+
role: 'user',
25+
content:
26+
'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' +
27+
req.body.prompt
28+
}],
29+
model: 'gpt-3.5-turbo',
30+
})
31+
const sql = response.choices[0].message.content
32+
const logEntry = {
33+
prompt: req.query.prompt,
34+
response: sql
35+
36+
}
37+
return res.render('db-tabulator/autosql/result', {
38+
sql: sql,
39+
warnOnField:
40+
sql.includes('pl_title') ? 'pl_title' :
41+
sql.includes('tl_title') ? 'tl_title' :
42+
null
43+
})
44+
});
45+
46+
export default router;

0 commit comments

Comments
 (0)