Skip to content

OpenAI compatibility API #919

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

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 27 additions & 1 deletion scalene/scalene-gui/index.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
size="22"
placeholder="(OpenAI API key)"
id="api-key"
oninput="checkApiKey(event.target.value)"
oninput="checkApiKey(event.target.value, document.getElementById('openai-compatibility-api-url').value)"
/>
<span id="valid-api-key"></span>
</div>
Expand All @@ -147,6 +147,32 @@
</select>
</div>

<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 5px;">
<label for="language-model-override" style="font-size: 0.8rem; width: 150px;">
Other model:
</label>
<input
type="text"
style="font-size: 0.8rem; flex: 1;"
size="22"
placeholder="(Model name, overrides selection)"
id="language-model-override-openai"
/>
</div>

<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 5px;">
<label for="url-openai-compatibility" style="font-size: 0.8rem; width: 150px;">
URL for compatibility API:
</label>
<input
type="text"
style="font-size: 0.8rem; flex: 1;"
size="22"
placeholder="(URL for OpenAI compatible endpoint, leave blank for https://api.openai.com/v1)"
id="openai-compatibility-api-url"
/>
</div>

</div>

<!-- Azure OpenAI API Key Field -->
Expand Down
22 changes: 13 additions & 9 deletions scalene/scalene-gui/openai.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
async function tryApi(apiKey) {
const response = await fetch("https://api.openai.com/v1/completions", {
async function tryApi(apiKey, customEndpoint) {
const base_url = customEndpoint || "https://api.openai.com/v1";
const response = await fetch(base_url + "/completions", {
method: "GET",
headers: {
"Content-Type": "application/json",
Expand All @@ -9,8 +10,8 @@ async function tryApi(apiKey) {
return response;
}

export async function isValidApiKey(apiKey) {
const response = await tryApi(apiKey);
export async function isValidApiKey(apiKey, customEndpoint = null) {
const response = await tryApi(apiKey, customEndpoint);
const data = await response.json();
if (
data.error &&
Expand All @@ -28,7 +29,8 @@ export async function isValidApiKey(apiKey) {
}
}

export function checkApiKey(apiKey) {
export function checkApiKey(apiKey, customEndpoint = null) {
return true;
(async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be removed

try {
window.localStorage.setItem("scalene-api-key", apiKey);
Expand All @@ -40,7 +42,7 @@ export function checkApiKey(apiKey) {
document.getElementById("valid-api-key").innerHTML = "";
return;
}
const isValid = await isValidApiKey(apiKey);
const isValid = await isValidApiKey(apiKey, customEndpoint);
if (!isValid) {
document.getElementById("valid-api-key").innerHTML = "&#10005;";
} else {
Expand All @@ -49,9 +51,11 @@ export function checkApiKey(apiKey) {
})();
}

export async function sendPromptToOpenAI(prompt, apiKey) {
const endpoint = "https://api.openai.com/v1/chat/completions";
const model = document.getElementById("language-model-openai").value;
export async function sendPromptToOpenAI(prompt, apiKey, customEndpoint = null, customModel = null) {
const base_url = customEndpoint || "https://api.openai.com/v1";
const defaultModel = document.getElementById("language-model-openai").value;
const endpoint = base_url + "/chat/completions";
const model = customModel || defaultModel;

const body = JSON.stringify({
model: model,
Expand Down
6 changes: 5 additions & 1 deletion scalene/scalene-gui/optimizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export async function optimizeCode(imports, code, line, context) {
let aiService = document.getElementById("service-select").value;
if (aiService === "openai") {
apiKey = document.getElementById("api-key").value;
customEndpoint = document.getElementById("openai-compatibility-api-url").value;
} else if (aiService === "azure-openai") {
apiKey = document.getElementById("azure-api-key").value;
}
Expand Down Expand Up @@ -244,10 +245,13 @@ export async function optimizeCode(imports, code, line, context) {

switch (document.getElementById("service-select").value) {
case "openai": {
console.log(prompt);
const customModel = document.getElementById("language-model-override-openai").value;
console.log(prompt, customEndpoint, customModel);
const result = await sendPromptToOpenAI(
prompt,
apiKey,
customEndpoint,
customModel,
);
return extractCode(result);
}
Expand Down
2 changes: 1 addition & 1 deletion scalene/scalene-gui/scalene-gui-bundle.js

Large diffs are not rendered by default.

Loading