「
Google Gemini に相談
」を編集中 (節単位)
ナビゲーションに移動
検索に移動
警告:
ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。
ログイン
または
アカウントを作成
すれば、あなたの編集はその利用者名とともに表示されるほか、さまざまなメリットもあります。
スパム攻撃防止用のチェックです。 決して、ここには、値の入力は
しない
でください!
=== 従来エンジン版 (アーカイブ) === <syntaxhighlight lang="javascript" copy> #title = "Gemini に相談" BeginUndoGroup(); // 会話の履歴を保持する数 (質問と回答で1セット) // 数を増やすと、会話の履歴が増えますが、Meryのメモリ使用量が増えたり、 // 動作が重くなる可能性があるため、適宜調整してください。 // document.Tag に保持されるため、開いているファイルを閉じると履歴は消えます。 var HISTORY_SIZE = 5; // コンテキストメニュー番号 var EXECUTE_AI = 1; var RESET_HISTORY = 2; var ConsoleClass = function () { this.log = function (str) { outputBar.Writeln(str) } } var console = new ConsoleClass(); var doc = document; var sel = document.selection; main(); function main() { var selectionIsEmpty = sel.IsEmpty var beforeScrollY = ScrollY; // プロンプトの取得 var promptText = ""; var promptLength = 0; if (selectionIsEmpty) { sel.StartOfLine(false, mePosLogical); sel.EndOfLine(true, mePosLogical); } promptText = sel.Text.trim(); promptLength = sel.TextLength; // コンテキストメニューの作成 var menu = createContextMenu(selectionIsEmpty); var menuTrack = menu.Track(0); // メニュー選択がキャンセルされた場合は終了 if (menuTrack === 0) return; // 会話履歴のリセット if (menuTrack === 2) { resetTag(); return; } // プロンプトが空の場合はエラー if (promptLength === 0) { alert("プロンプトを入力してください。"); return; } // 文字数が多すぎる場合はエラー if (promptLength > 10000) { alert("プロンプトの文字数が多すぎます。10000 文字以下にしてください。"); return; } // 読み取り専用の場合はエラー if (doc.ReadOnly) { alert("ファイルが読み取り専用のため実行できません。"); return; } // カーソル位置移動 sel.SetActivePoint(mePosLogical, sel.GetBottomPointX(mePosLogical), sel.GetBottomPointY(mePosLogical)); sel.EndOfLine(false, mePosLogical); sel.NewLine(); var activeY = sel.GetActivePointY(mePosLogical); // Gemini API に問い合わせ doc.ReadOnly = true; var ret = callGeminiAPI(promptText); doc.ReadOnly = false; // 結果を表示 sel.Text = "\n---\n\n" + ret + "\n\n---\n"; // 位置調整 sel.SetActivePoint( mePosLogical, 1, activeY, false ) if (beforeScrollY + 2 < ScrollY) { ScrollY = ScrollY - 1; } } function createContextMenu(selectionIsEmpty) { var menu = CreatePopupMenu(); menu.Add("※注意※ プライバシー・機密情報を送信しないこと", 0); menu.Add("", 0, meMenuSeparator); if (selectionIsEmpty) { menu.Add("アクティブ行の内容で相談 (&Q)", EXECUTE_AI); } else { menu.Add("選択範囲の内容で相談 (&Q)", EXECUTE_AI); } if (document.Tag.exists("gemini-text")) { var turn = JSON.parse(document.Tag("gemini-text")).length / 2; menu.Add("会話履歴をリセット (現在の会話数: " + turn + ") (&R)", RESET_HISTORY); } return menu; } function callGeminiAPI(promptText) { try { // APIキーの取得 var gemini_api_key = shell.getEnv("GOOGLE_API_KEY"); var http = new ActiveXObject("Msxml2.ServerXMLHTTP"); var url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=' + gemini_api_key; http.open('POST', url, true); http.setRequestHeader('Content-Type', 'application/json'); http.send('{"contents":' + createGeminiRequest(promptText) + '}'); // 応答待機 var timeout = 60; var retrySec = 0; var startX = sel.GetActivePointX(mePosLogical); var prevPos = sel.GetActivePos(); while (http.readyState != 4) { if (retrySec > timeout) { break; } http.waitForResponse(1); retrySec += 1; inputProgressChar(prevPos); prevPos = sel.GetActivePos(); } // 進捗表示文字を削除 sel.SetActivePoint( mePosLogical, startX, sel.GetActivePointY(mePosLogical), true ); sel.Delete(); var ret = ""; // 応答ステータスチェック if (http.readyState == 4 && http.status == 200) { // JSONをパースして必要なテキストを抽出 var response = JSON.parse(http.responseText); if (response.candidates && response.candidates.length > 0) { response.candidates.forEach(function (candidate) { if (candidate.content && candidate.content.parts) { candidate.content.parts.forEach(function (part) { if (part.text) { ret += part.text; } }); } }); } } else { var ret = "エラーが発生しました: " + http.status + " " + http.statusText http.abort(); return ret; } // 結果を会話履歴に追加 if (HISTORY_SIZE > 0) { appendReturnTextToTag(promptText, ret); } // 整形しつつ返す return ret .trim() .replace(/([。!?]) +/g, "$1") .replace(/\* +/g, "* "); } catch (e) { console.log(e.stack); return "エラーが発生しました: " + e.message; } } // document.Tag の内容を加味してGeminiのリクエストを作成 function createGeminiRequest(promptText) { var tag = document.Tag; // 以前の会話が存在しない場合 if (!tag.exists("gemini-text")) { return JSON.stringify([{ "parts": [{ "text": promptText }] }]); } var geminiText = JSON.parse(tag("gemini-text")); geminiText.push({ "role": "user", "parts": [{ "text": promptText }] }); return JSON.stringify(geminiText); } // document.Tag にGeminiの応答を追加 function appendReturnTextToTag(userText, modelText) { var tag = document.Tag; if (!tag.exists("gemini-text")) { tag("gemini-text") = JSON.stringify([ { "role": "user", "parts": [{ "text": userText }] }, { "role": "model", "parts": [{ "text": modelText }] } ]); } else { var geminiText = JSON.parse(tag("gemini-text")); geminiText.push({ "role": "user", "parts": [{ "text": userText }] }); geminiText.push({ "role": "model", "parts": [{ "text": modelText }] }); // 会話セット (user, model 1往復) が5セットを超えた場合、最初のセットを削除 while (geminiText.length > HISTORY_SIZE * 2) { geminiText.shift(); } tag("gemini-text") = JSON.stringify(geminiText); } } function resetTag() { if (document.Tag.exists("gemini-text")) { document.Tag.remove("gemini-text"); } } function inputProgressChar(pos) { doc.ReadOnly = false; sel.SetActivePos(pos); sel.Text = "."; doc.ReadOnly = true; } </syntaxhighlight>
編集内容の要約:
MeryWikiへの投稿はすべて、他の投稿者によって編集、変更、除去される場合があります。 自分が書いたものが他の人に容赦なく編集されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください(詳細は
MeryWiki:著作権
を参照)。
著作権保護されている作品は、許諾なしに投稿しないでください!
このページを編集するには、下記の数式を計算してその答えを欄に入力してください (
ヘルプ
):
いちたすには =
キャンセル
編集ヘルプ
(新しいウィンドウで開きます)
スポンサーリンク
ナビゲーション メニュー
個人用ツール
ログインしていません
トーク
投稿記録
アカウント作成
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
編集
履歴表示
その他
検索
スポンサーリンク
スポンサーリンク
案内
メインページ
ヘルプ
よくある質問
マクロリファレンス
マクロライブラリ
プラグインライブラリ
構文ファイル
テーマ
寄付・開発支援
練習用ページ
開発室
開発者のブログ
ツール
スポンサーリンク