getting the model from the options. added a tolltip to the send button to show the used model

This commit is contained in:
mic 2024-07-29 23:12:47 +02:00
parent 5b4cd31152
commit ecf4a7a167
5 changed files with 19 additions and 9 deletions

View file

@ -430,5 +430,9 @@
"chatgpt_empty_model": {
"message": "You've not choosen a model for the ChatGPT API. Please choose one in the options page.",
"description": ""
},
"chagtp_api_send_button": {
"message": "Using model",
"description": ""
}
}

View file

@ -55,10 +55,10 @@ let promptData = null;
// ============================== TESTING - END
const params = new URLSearchParams(window.location.search);
let prefs_api_key = await browser.storage.sync.get({api_key_chatgpt: ''});
let prefs_api = await browser.storage.sync.get({api_key_chatgpt: '', model_chatgpt: ''});
// const openaiApiKey = params.get('openapi-key');
//console.log(">>>>>>>>>>> api_key_chatgpt: " + prefs_api_key.api_key_chatgpt);
worker.postMessage({ type: 'init', api_key_chatgpt: prefs_api_key.api_key_chatgpt });
worker.postMessage({ type: 'init', api_key_chatgpt: prefs_api.api_key_chatgpt, model_chatgpt: prefs_api.model_chatgpt});
messagesArea.appendUserMessage("Will attempt to connect to OpenAI using the API key provided.", "info");
// Event listeners for worker messages

View file

@ -105,8 +105,10 @@ class MessageInput extends HTMLElement {
this._messageInputField.focus();
}
init(worker) {
async init(worker) {
this.worker = worker;
let prefs_api = await browser.storage.sync.get({model_chatgpt: ''});
this._sendButton.title = await browser.i18n.getMessage("chagtp_api_send_button") + ": " + prefs_api.model_chatgpt;
}
setMessagesArea(messagesAreaComponent) {

View file

@ -24,7 +24,7 @@ import { OpenAI } from '../js/api/openai.js';
//========================== for testing
// const MOCK_TOKENS = ['Good', ' morning', ' Mr', ' Plop', 'py', ',', 'and', ' I', ' said', '\n', '"', 'Good', ' morn', 'ing', ' Mrs',' Plop', 'py', ,'"', '\n', 'Oh', ' how', ' the', ' win', 'ter', ' even', 'ings', ' must', ' just', ' fly'];
//
// function mockDelay(ms) {
// return new Promise(resolve => setTimeout(resolve, ms));
// }
@ -40,6 +40,7 @@ import { OpenAI } from '../js/api/openai.js';
let api_key_chatgpt = null;
let model_chatgpt = '';
let openai = null;
let conversationHistory = [];
@ -48,8 +49,9 @@ let assistantResponseAccumulator = '';
self.onmessage = async function(event) {
if (event.data.type === 'init') {
api_key_chatgpt = event.data.api_key_chatgpt;
model_chatgpt = event.data.model_chatgpt;
//console.log(">>>>>>>>>>> api_key_chatgpt: " + api_key_chatgpt);
openai = new OpenAI(api_key_chatgpt, true);
openai = new OpenAI(api_key_chatgpt, model_chatgpt, true);
} else if (event.data.type === 'chatMessage') {
conversationHistory.push({ role: 'user', content: event.data.message });
@ -83,7 +85,7 @@ self.onmessage = async function(event) {
// stream: true,
// }),
// });
const response = await openai.fetchResponse("gpt-4o-mini", conversationHistory); //4096);
const response = await openai.fetchResponse(conversationHistory); //4096);
postMessage({ type: 'messageSent' });
if (!response.ok) {

View file

@ -22,10 +22,12 @@
export class OpenAI {
apiKey = '';
model = '';
stream = false;
constructor(apiKey, stream) {
constructor(apiKey, model, stream) {
this.apiKey = apiKey;
this.model = model;
this.stream = stream;
}
@ -51,7 +53,7 @@ export class OpenAI {
return output.data.filter(item => item.id.startsWith('gpt-')).sort((a, b) => b.id.localeCompare(a.id));
}
fetchResponse = async (model, messages, maxTokens = 0) => {
fetchResponse = async (messages, maxTokens = 0) => {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
@ -59,7 +61,7 @@ export class OpenAI {
Authorization: "Bearer "+ this.apiKey
},
body: JSON.stringify({
model: model,
model: this.model,
messages: messages,
stream: this.stream,
...(maxTokens > 0 ? { 'max_tokens': parseInt(maxTokens) } : {})