diff --git a/api_webchat/controller.js b/api_webchat/controller.js index bcdb851b..cb315ea8 100644 --- a/api_webchat/controller.js +++ b/api_webchat/controller.js @@ -66,13 +66,13 @@ messageInput.setMessagesArea(messagesArea); switch (llm) { case "chatgpt_api": { - let prefs_api = await browser.storage.sync.get({chatgpt_api_key: '', chatgpt_model: '', do_debug: false}); + let prefs_api = await browser.storage.sync.get({chatgpt_api_key: '', chatgpt_model: '', chatgpt_developer_messages:'', do_debug: false}); let i18nStrings = {}; i18nStrings["chatgpt_api_request_failed"] = browser.i18n.getMessage('chatgpt_api_request_failed'); i18nStrings["error_connection_interrupted"] = browser.i18n.getMessage('error_connection_interrupted'); messageInput.setModel(prefs_api.chatgpt_model); messagesArea.setLLMName("ChatGPT"); - worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: prefs_api.chatgpt_model, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); + worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: prefs_api.chatgpt_model, chatgpt_developer_messages: prefs_api.chatgpt_developer_messages, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); messagesArea.appendUserMessage(browser.i18n.getMessage("chagpt_api_connecting") + " " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.chatgpt_model + "\"...", "info"); browser.runtime.sendMessage({command: "openai_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); break; diff --git a/js/api/openai.js b/js/api/openai.js index 11d4398d..2bcf86a8 100644 --- a/js/api/openai.js +++ b/js/api/openai.js @@ -23,11 +23,13 @@ export class OpenAI { apiKey = ''; model = ''; + developer_messages = ''; stream = false; - constructor(apiKey, model, stream) { + constructor(apiKey, model, developer_messages, stream) { this.apiKey = apiKey; this.model = model; + this.developer_messages = developer_messages; this.stream = stream; } @@ -69,6 +71,13 @@ export class OpenAI { } fetchResponse = async (messages, maxTokens = 0) => { + + if(this.developer_messages !== ''){ + messages.push({role: "developer", content: [{"type": "text", "text": this.developer_messages}]}); + } + + // console.log(">>>>>>>>>>> OpenAI API request: " + JSON.stringify(messages)); + try { const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", diff --git a/js/special_commands/mzta-add-tags.js b/js/special_commands/mzta-add-tags.js index 6280ef84..3902b00e 100644 --- a/js/special_commands/mzta-add-tags.js +++ b/js/special_commands/mzta-add-tags.js @@ -53,8 +53,8 @@ async initWorker() { switch (this.llm) { case "chatgpt_api": { - let prefs_api = await browser.storage.sync.get({chatgpt_api_key: '', chatgpt_model: ''}); - this.worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: prefs_api.chatgpt_model, do_debug: this.do_debug, i18nStrings: ''}); + let prefs_api = await browser.storage.sync.get({chatgpt_api_key: '', chatgpt_model: '', chatgpt_developer_messages: ''}); + this.worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: prefs_api.chatgpt_model, chatgpt_developer_messages: prefs_api.chatgpt_developer_messages, do_debug: this.do_debug, i18nStrings: ''}); break; } case "google_gemini_api": { diff --git a/js/workers/model-worker-openai.js b/js/workers/model-worker-openai.js index 3f364682..536dc96d 100644 --- a/js/workers/model-worker-openai.js +++ b/js/workers/model-worker-openai.js @@ -38,7 +38,7 @@ self.onmessage = async function(event) { if (event.data.type === 'init') { chatgpt_api_key = event.data.chatgpt_api_key; chatgpt_model = event.data.chatgpt_model; - openai = new OpenAI(chatgpt_api_key, chatgpt_model, true); + openai = new OpenAI(chatgpt_api_key, chatgpt_model, event.data.chatgpt_developer_messages, true); do_debug = event.data.do_debug; i18nStrings = event.data.i18nStrings; taLog = new taLogger('model-worker-openai', do_debug);