「OpenAI GPT に相談」の版間の差分
見出しの追加と修正 |
|||
| 74行目: | 74行目: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
#language = " | #language = "v8" | ||
#title = "GPT に相談 (V8版)" | #title = "GPT に相談 (V8版)" | ||
BeginUndoGroup(); | BeginUndoGroup(); | ||
| 80行目: | 80行目: | ||
// カスタムインストラクション | // カスタムインストラクション | ||
// これを設定することで、生成AIの回答全体への生成方針を指示できます。 | // これを設定することで、生成AIの回答全体への生成方針を指示できます。 | ||
// 例えば " | // 例えば "あなたは侍で名前は権兵衛です。回答をござる口調にしてください" と設定すると、AIの回答がござる口調になります。 | ||
// 何も指示しない場合は、空文字 "" を設定してください。 | // 何も指示しない場合は、空文字 "" を設定してください。 | ||
const DEFAULT_CUSTOM_INSTRUCTION = ""; | const DEFAULT_CUSTOM_INSTRUCTION = ""; | ||
| 89行目: | 89行目: | ||
// テンプレートは以下の { ~ }, で囲まれたブロックを好きに増減させて利用できます。 | // テンプレートは以下の { ~ }, で囲まれたブロックを好きに増減させて利用できます。 | ||
const PROMPT_TEMPLATE = [ | const PROMPT_TEMPLATE = [ | ||
// 以下はサンプルです。1階層メニューと2階層メニューが利用できます。 | |||
// { | |||
// "title": "要約", | |||
// "prompt": "以下の文章を要約してください。" | |||
// }, | |||
// { | |||
// "submenu_title": "翻訳系", | |||
// "submenu": [ | |||
// { | |||
// "title": "日本語翻訳", | |||
// "prompt": "以下の文章を日本語に翻訳してください。" | |||
// }, | |||
// { | |||
// "title": "英語翻訳", | |||
// "prompt": "以下の文章を英語に翻訳してください。" | |||
// }, | |||
// ] | |||
// }, | |||
{ | { | ||
"title": "要約する", | "submenu_title": "理解を深める", | ||
"submenu": [ | |||
{ | |||
"title": "要約する", | |||
"prompt": "要約して。専門的な用語と思われる単語は簡単に解説も入れて。" | |||
}, | |||
{ | |||
"title": "論点を整理する", | |||
"prompt": "以下の論点を整理して。" | |||
}, | |||
{ | |||
"title": "物語対話形式で教えて", | |||
"prompt": "以下の内容について物語対話形式で教えてください。" | |||
}, | |||
] | |||
}, | }, | ||
{ | { | ||
"submenu_title": " | "submenu_title": "翻訳する", | ||
"submenu": [ | "submenu": [ | ||
{ | { | ||
| 173行目: | 199行目: | ||
}, | }, | ||
{ | { | ||
"submenu_title": " | "submenu_title": "ビジネス文書作成支援", | ||
"submenu": [ | "submenu": [ | ||
{ | |||
"title": "誤字脱字、不正確な表現をチェック", | |||
"prompt": "以下の2つのカテゴリーで問題点を指摘してください\n" | |||
+ "- 【誤字脱字】:明確な誤字脱字および誤字の疑いがある箇所\n" | |||
+ "- 【不正確・曖昧な表現】:解釈に個人差が生じる可能性がある表現、より適切な表現に改善できる箇所" | |||
}, | |||
{ | { | ||
"title": "提案書を作成する", | "title": "提案書を作成する", | ||
| 253行目: | 285行目: | ||
// 自動設定にする場合は空文字 "" を設定してください。 | // 自動設定にする場合は空文字 "" を設定してください。 | ||
const LANGUAGE = "Japanese"; | const LANGUAGE = "Japanese"; | ||
// 入力プロンプトの最大トークン数 | |||
// 超適当なトークナイザー関数により計測し、英単語は4文字1トークン、それ以外は1文字1トークンとして数えます。 | |||
const MAX_PROMPT_TOKEN = 60000; | |||
// 会話の履歴を保持する数 (質問と回答で1セット) | // 会話の履歴を保持する数 (質問と回答で1セット) | ||
| 259行目: | 295行目: | ||
// document.Tag に保持されるため、開いているファイルを閉じると履歴は消えます。 | // document.Tag に保持されるため、開いているファイルを閉じると履歴は消えます。 | ||
const HISTORY_SIZE = 4; | const HISTORY_SIZE = 4; | ||
// 会話履歴に保持する最大トークン数 | |||
// 会話履歴のトークン数がこの数を超える場合、古い履歴から削除されます。 | |||
const MAX_HISTORY_TOKEN = 300000; | |||
// 生成AI応答の入力毎の遅延時間 (ミリ秒) | // 生成AI応答の入力毎の遅延時間 (ミリ秒) | ||
| 266行目: | 306行目: | ||
// 1回の入力での文字数 | // 1回の入力での文字数 | ||
const INPUT_LENGTH = 3; | const INPUT_LENGTH = 3; | ||
// コンテキストメニュー番号の定義 | // コンテキストメニュー番号の定義 | ||
| 339行目: | 376行目: | ||
} | } | ||
// 文字数が多すぎる場合はエラー | // 文字数が多すぎる場合はエラー | ||
if ( | if (roughTokenize(promptText) > MAX_PROMPT_TOKEN) { | ||
alert(` | alert(`プロンプトの文字数が多すぎます。文字数を少なくしてください。`); | ||
return; | return; | ||
} | } | ||
| 512行目: | 549行目: | ||
// fetch で OpenAI API にリクエスト | // fetch で OpenAI API にリクエスト | ||
// stream: true, stream_options: { "include_usage": true } で利用トークン数を取得 | // stream: true, stream_options: { "include_usage": true } で利用トークン数を取得 | ||
const requestBody = { | |||
model: 'gpt-4o-mini', | |||
messages: createGPTRequest(promptText, isTemplate), | |||
stream: true, | |||
stream_options: { "include_usage": true }, | |||
max_tokens: 5000, | |||
}; | |||
if (isTemplate) { | |||
// テンプレートの場合は応答を決定論的にする | |||
// テンプレート以外の場合はデフォルト値とするために指定しない | |||
requestBody.temperature = 0.42; | |||
} | |||
const response = await fetch('https://api.openai.com/v1/chat/completions', { | const response = await fetch('https://api.openai.com/v1/chat/completions', { | ||
signal: signal, | signal: signal, | ||
| 519行目: | 568行目: | ||
'Authorization': 'Bearer ' + apiKey, | 'Authorization': 'Bearer ' + apiKey, | ||
}, | }, | ||
body: JSON.stringify( | body: JSON.stringify(requestBody), | ||
}); | }); | ||
let | let responseAll = ""; | ||
const reader = response.body.getReader(); | const reader = response.body.getReader(); | ||
const decoder = new TextDecoder('utf-8'); | const decoder = new TextDecoder('utf-8'); | ||
| 597行目: | 638行目: | ||
// トークン数文字列がある場合は入力 | // トークン数文字列がある場合は入力 | ||
if (usageText) { | if (usageText) { | ||
sel.Text = | sel.Text = (responseAll.slice(-1) === "\n" ? "\n" : "\n\n") + `(${usageText})`; | ||
} | } | ||
// 会話履歴用に応答内容を追加 | // 会話履歴用に応答内容を追加 | ||
responseAll += inputText; | |||
// 入力を少しゆっくりにする | // 入力を少しゆっくりにする | ||
await new Promise(resolve => setTimeout(resolve, INPUT_DELAY)); | await new Promise(resolve => setTimeout(resolve, INPUT_DELAY)); | ||
| 607行目: | 648行目: | ||
// 結果を会話履歴に追加 | // 結果を会話履歴に追加 | ||
if (HISTORY_SIZE > 0 && !isTemplate) { | if (HISTORY_SIZE > 0 && !isTemplate) { | ||
appendReturnTextToTag(promptText, | appendReturnTextToTag(promptText, responseAll); | ||
} | } | ||
} catch (e) { | } catch (e) { | ||
| 615行目: | 656行目: | ||
requestCompletedFlg = true; | requestCompletedFlg = true; | ||
} | } | ||
} | |||
function roughTokenize(text) { | |||
// 超適当なトークナイザ。トークン数をざっくりと多めに数える | |||
// 空白文字・ハイフン・アンダースコア区切りの単語に分割して、 | |||
// 単語がアルファベットのみ、数字のみの場合には 4文字=1トークン として数える。 | |||
// 単語にアルファベット以外の文字が含まれる場合は 1文字=1トークン として数える。 | |||
return text | |||
.split(/[\s\-_]+/) | |||
.map(w => /^[a-zA-Z]+$|^[0-9]+$|/.test(w) ? Math.ceil(w.length / 4) : w.length) | |||
.reduce((a, b) => a + b, 0); | |||
} | } | ||
| 687行目: | 739行目: | ||
}); | }); | ||
// 会話セット (user, model 1往復) が特定セット数を超えた場合、最初のセットを削除 | // 会話セット (user, model 1往復) が特定セット数を超えた場合、最初のセットを削除 | ||
if (gptText.length > HISTORY_SIZE * 2) { | |||
gptText. | gptText.splice(0, 2); | ||
} | |||
// 会話履歴全体が MAX_HISTORY_TOKEN を超えた場合、最初のセットを削除 | |||
let joinedText = gptText.map(e => e.content).join("\n"); | |||
while (roughTokenize(joinedText) > MAX_HISTORY_TOKEN) { | |||
gptText.splice(0, 2); | |||
joinedText = gptText.map(e => e.content).join("\n"); | |||
} | } | ||
tag[HISTORY_TAG] = JSON.stringify(gptText); | tag[HISTORY_TAG] = JSON.stringify(gptText); | ||
| 695行目: | 753行目: | ||
function resetTag() { | function resetTag() { | ||
const tag = | const tag = doc.Tag; | ||
if (tag.exists(HISTORY_TAG)) { | if (tag.exists(HISTORY_TAG)) { | ||
tag.remove(HISTORY_TAG); | tag.remove(HISTORY_TAG); | ||
| 704行目: | 762行目: | ||
let customInstruction = getTemporaryCustomInstruction(); | let customInstruction = getTemporaryCustomInstruction(); | ||
if (!customInstruction) { | if (!customInstruction) { | ||
// | // 一時的なカスタムインストラクションがない場合はデフォルトを使用 | ||
customInstruction = DEFAULT_CUSTOM_INSTRUCTION | customInstruction = DEFAULT_CUSTOM_INSTRUCTION; | ||
} | } | ||
const language = LANGUAGE.trim(); | const language = LANGUAGE.trim(); | ||
| 711行目: | 769行目: | ||
return ""; | return ""; | ||
} | } | ||
customInstruction = customInstruction.replace(/^[\s ]+|[\s ]+$/g, ""); | |||
+ (language ? `Please make sure to answer in ${language}.\n` : "") | return (customInstruction ? `${customInstruction}\n` : "") | ||
+ "If a response might contain hallucinations, please refrain from answering or suggest that the information could be incorrect.\n" | |||
+ (language ? `Please make sure to answer in ${language}.\n` : ""); | |||
} | } | ||
| 731行目: | 790行目: | ||
} else { | } else { | ||
// 空文字でOK、もしくはキャンセルされた場合はデフォルトに戻すか確認 | // 空文字でOK、もしくはキャンセルされた場合はデフォルトに戻すか確認 | ||
if (Confirm("カスタムインストラクションをデフォルトに戻しますか?")) { | |||
if ( | |||
setTemporaryCustomInstruction(""); | setTemporaryCustomInstruction(""); | ||
} else { | |||
alert("カスタムインストラクションの変更をキャンセルしました。\n※カスタムインストラクションを空にしたい場合はスペースのみを入力してください。"); | |||
} | } | ||
} | } | ||
// 変更が加わっている場合は会話履歴をリセット | // 変更が加わっている場合は会話履歴をリセット | ||
if (tempCustomInstruction !== getTemporaryCustomInstruction()) { | if (tempCustomInstruction !== getTemporaryCustomInstruction()) { | ||
スポンサーリンク