Skip to content

feat: allow users to give custom base urls #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion public/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
</style>
</head>
<body>
Please store your Open AI API Key here. Fetch one at https://platform.openai.com/account/api-keys.
<h2>Open AI API Key</h2>
<p>Please store your Open AI API Key. Fetch one at https://platform.openai.com/account/api-keys.</p>
<input type="password" name="openai_apikey" id="openai_apikey" />
<br />
<h2>OpenAPI Base URL</h2>
<p>Please store your custom OpenAPI Base URL here.</p>
<input type="text" name="openai_base_url" id="openai_base_url" value="https://api.openai.com/v1" />
<div id="status"></div>
<br />
<button id="save">Save</button>
<script src="options.js"></script>
</body>
Expand Down
58 changes: 30 additions & 28 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
// Saves options to chrome.storage
const saveOptions = () => {
const openai_apikey = document.getElementById('openai_apikey').value;

chrome.storage.sync.set(
{ openai_apikey: openai_apikey },
() => {
// Update status to let user know options were saved.
const status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(() => {
status.textContent = '';
}, 750);
}
);
};

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
const restoreOptions = () => {
chrome.storage.sync.get(
{ openai_apikey: '' },
(items) => {
document.getElementById('openai_apikey').value = items.openai_apikey;
}
);
};

document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions);
const openai_apikey = document.getElementById('openai_apikey').value;
const openai_base_url = document.getElementById('openai_base_url').value;

chrome.storage.sync.set(
{ openai_apikey: openai_apikey, openai_base_url: openai_base_url },
() => {
// Update status to let user know options were saved.
const status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(() => {
status.textContent = '';
}, 750);
}
);
};

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
const restoreOptions = () => {
chrome.storage.sync.get(
{ openai_apikey: '', openai_base_url: 'https://api.openai.com/v1' },
(items) => {
document.getElementById('openai_apikey').value = items.openai_apikey;
document.getElementById('openai_base_url').value = items.openai_base_url;
}
);
};

document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions);
11 changes: 11 additions & 0 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ async function getApiKey() {
return options['openai_apikey'];
}

async function getBaseURL() {
let options = await new Promise((resolve) => {
chrome.storage.sync.get('openai_base_url', resolve);
});
console.log(options);
return options['openai_base_url'];
}

async function callChatGPT(messages, callback, onDone) {
let apiKey;
let baseURL;
try {
apiKey = await getApiKey();
baseURL = await getBaseURL();
} catch (e) {
callback('Please add your Open AI API key to the settings of this Chrome Extension.');
onDone();
Expand All @@ -64,6 +74,7 @@ async function callChatGPT(messages, callback, onDone) {

const api = new ChatGPTAPI({
apiKey: apiKey,
apiBaseUrl: baseURL,
systemMessage: `You are a programming code change reviewer, provide feedback on the code changes given. Do not introduce yourselves.`
})

Expand Down