File: sql-gemini.php

package info (click to toggle)
adminer 5.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,828 kB
  • sloc: php: 28,768; javascript: 1,188; xml: 107; makefile: 48; sh: 3
file content (120 lines) | stat: -rw-r--r-- 4,294 bytes parent folder | download
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
<?php

/** AI prompt in SQL command generating the queries with Google Gemini
* Beware that this sends your whole database structure (not data) to Google Gemini.
* @link https://gemini.google.com/
* @link https://www.adminer.org/plugins/#use
* @author Jakub Vrana, https://www.vrana.cz/
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerSqlGemini extends Adminer\Plugin {
	private $apiKey;
	private $model;

	/**
	* @param string $apiKey The default key is shared with all users and may run out of quota; get your own API key at: https://aistudio.google.com/apikey
	* @param string $model Available models: https://ai.google.dev/gemini-api/docs/models#available-models
	*/
	function __construct($apiKey = 'AIzaSyDWDbPjmvH9_hphsnY_yJGdue42qRMG3do', $model = "gemini-2.0-flash") {
		$this->apiKey = $apiKey;
		$this->model = $model;
	}

	function headers() {
		if (isset($_POST["gemini"]) && !isset($_POST["query"])) {
			$prompt = "I have a " . Adminer\get_driver(Adminer\DRIVER) . " database with this structure:\n\n";
			foreach (Adminer\tables_list() as $table => $type) {
				$prompt .= Adminer\create_sql($table, false, "CREATE") . ";\n\n";
			}
			$prompt .= "Prefer returning relevant columns including primary key.\n\n";
			$prompt .= "Give me this SQL query and nothing else:\n\n$_POST[gemini]\n\n";
			//~ echo $prompt; exit;
			$context = stream_context_create(array("http" => array(
				"method" => "POST",
				"header" => array("User-Agent: AdminerSqlGemini/" . Adminer\VERSION, "Content-Type: application/json"),
				"content" => '{"contents": [{"parts":[{"text": ' . json_encode($prompt) . '}]}]}',
				"ignore_errors" => true,
			)));
			$response = json_decode(file_get_contents("https://generativelanguage.googleapis.com/v1beta/models/$this->model:generateContent?key=$this->apiKey", false, $context));
			if (isset($response->error)) {
				echo "-- " . $response->error->message;
			} else {
				$text = $response->candidates[0]->content->parts[0]->text;
				$text2 = preg_replace('~(\n|^)```sql\n(.+)\n```(\n|$)~sU', "*/\n\n\\2\n\n/*", "/*\n$text\n*/", -1, $count);
				echo ($count ? preg_replace('~/\*\s*\*/\n*~', '', $text2) : $text);
			}
			exit;
		}
	}

	function sqlPrintAfter() {
		echo "<p><textarea name='gemini' rows='5' cols='50' placeholder='" . $this->lang('Ask Gemini') . "'>" . Adminer\h($_POST["gemini"]) . "</textarea>\n";
		?>
<p><input type='button' value='Gemini'>
<script <?php echo Adminer\nonce(); ?>>
const geminiText = qsl('textarea');
const geminiButton = qsl('input');

function setSqlareaValue(value) {
	const sqlarea = qs('textarea.sqlarea');
	sqlarea.value = value;
	sqlarea.onchange && sqlarea.onchange();
}

geminiButton.onclick = () => {
	setSqlareaValue('-- <?php echo $this->lang('Just a sec...'); ?>');
	ajax(
		'',
		req => setSqlareaValue(req.responseText),
		'gemini=' + encodeURIComponent(geminiText.value)
	);
};

geminiText.onfocus = event => {
	alterClass(findDefaultSubmit(geminiText), 'default');
	alterClass(geminiButton, 'default', true);
	event.stopImmediatePropagation();
};

geminiText.onblur = () => {
	alterClass(geminiButton, 'default');
};

geminiText.onkeydown = event => {
	if (isCtrl(event) && (event.keyCode == 13 || event.keyCode == 10)) {
		geminiButton.onclick();
		event.stopPropagation();
	}
};
</script>
<?php
	}

	function screenshot() {
		return "https://www.adminer.org/static/plugins/sql-gemini.gif";
	}

	// use the phrases from https://gemini.google.com/
	protected $translations = array(
		'cs' => array(
			'' => 'Generování SQL příkazů pomocí umělé inteligence Google Gemini',
			'Ask Gemini' => 'Zeptat se Gemini',
			'Just a sec...' => 'Chviličku...',
		),
		'pl' => array(
			'Ask Gemini' => 'Zapytaj Gemini',
			'Just a sec...' => 'Chwileczkę...',
		),
		'de' => array(
			'' => 'KI-Eingabeaufforderung im SQL-Befehl zur Erstellung der Abfragen mit Google Gemini',
			'Ask Gemini' => 'Gemini fragen',
			'Just a sec...' => 'Einen Moment...',
		),
		'ja' => array(
			'' => 'Google Gemini AI を用いて SQL 文を生成',
			'Ask Gemini' => 'Gemini に聞く',
			'Just a sec...' => 'しばらくお待ち下さい...',
		),
	);
}