console.log("OpenAI script started on this page");
chrome.runtime.sendMessage({ type: "injectScript" });
chrome.storage.local.get("openaiApiKey", function (result) {
var apiKey = result.openaiApiKey;
if (apiKey == null) {
apiKey = prompt("Please enter your OpenAI API key:");
chrome.storage.local.set({ "openaiApiKey": apiKey });
}
document.addEventListener("keydown", function (event) {
if (event.ctrlKey && event.shiftKey && event.keyCode === 75) {
const activeElement = document.activeElement;
const isContentEditable = activeElement.isContentEditable;
const isInputField = activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement;
const textValue = isContentEditable ? activeElement.textContent : activeElement.value;
if (textValue.includes("::")) {
const promptText = textValue.substring(textValue.indexOf("::") + 2).trim();
const apiURL = "https://api.openai.com/v1/engines/gpt-3.5-turbo";
const headers = {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
};
const data = {
prompt: promptText,
max_tokens: 128,
temperature: 0.7,
};
console.log("Detected keyboard combo");
console.log("Sending API request with prompt:", promptText);
fetch(apiURL, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
const generatedText = data.choices[0].text.trim();
const newTextValue = textValue.replace("::" + promptText, generatedText);
if (isContentEditable) {
activeElement.textContent = newTextValue;
} else {
activeElement.value = newTextValue;
activeElement.dispatchEvent(new Event("input", { bubbles: true }));
}
console.log("API response received:", generatedText);
})
.catch((error) => console.log(error));
}
}
});
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === "injectScript") {
chrome.tabs.executeScript({
file: "content.js",
});
}
});
I am getting the following error
POST https://api.openai.com/v1/engines/gpt-3.5-turbo 405
(anonymous) @ content.js:31
I have no idea what I am doing wrong, checked it multiple times, also ran the code through ChatGPT to find an issue with it, it could not find an issue other than
- checking API key is valid
- endpoint is valid.
which both should be correct ( API key works on other models)
Endpoint is correct as well as far as I am aware