Skip to content

Commit 5b48b8a

Browse files
authored
More refactoring (#886)
* Packaged prism. * More exports, code cleanup to address eslint issues. * Moved API key checking into openai. * More util refactoring. * Updated.
1 parent 526fba1 commit 5b48b8a

File tree

4 files changed

+75
-72
lines changed

4 files changed

+75
-72
lines changed

scalene/scalene-gui/openai.js

+51
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
async function tryApi(apiKey) {
2+
const response = await fetch("https://api.openai.com/v1/completions", {
3+
method: "GET",
4+
headers: {
5+
"Content-Type": "application/json",
6+
Authorization: `Bearer ${apiKey}`,
7+
},
8+
});
9+
return response;
10+
}
11+
12+
export async function isValidApiKey(apiKey) {
13+
const response = await tryApi(apiKey);
14+
const data = await response.json();
15+
if (
16+
data.error &&
17+
data.error.code in
18+
{
19+
invalid_api_key: true,
20+
invalid_request_error: true,
21+
model_not_found: true,
22+
insufficient_quota: true,
23+
}
24+
) {
25+
return false;
26+
} else {
27+
return true;
28+
}
29+
}
30+
31+
export function checkApiKey(apiKey) {
32+
(async () => {
33+
try {
34+
window.localStorage.setItem("scalene-api-key", apiKey);
35+
} catch {
36+
// Do nothing if key not found
37+
}
38+
// If the API key is empty, clear the status indicator.
39+
if (apiKey.length === 0) {
40+
document.getElementById("valid-api-key").innerHTML = "";
41+
return;
42+
}
43+
const isValid = await isValidApiKey(apiKey);
44+
if (!isValid) {
45+
document.getElementById("valid-api-key").innerHTML = "✕";
46+
} else {
47+
document.getElementById("valid-api-key").innerHTML = "✓";
48+
}
49+
})();
50+
}
51+
152
export async function sendPromptToOpenAI(prompt, apiKey) {
253
const endpoint = "https://api.openai.com/v1/chat/completions";
354
const model = document.getElementById("language-model-openai").value;

scalene/scalene-gui/scalene-gui-bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scalene/scalene-gui/scalene-gui.js

+2-71
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import vegaEmbed from 'vega-embed';
55
import { Prism } from "./prism";
66
import Tablesort from "./tablesort";
77
import { optimizeCode } from "./optimizations";
8-
import { memory_consumed_str, time_consumed_str } from "./utils";
8+
import { unescapeUnicode, countSpaces, memory_consumed_str, time_consumed_str } from "./utils";
99
import { makeBar, makeGPUPie, makeMemoryPie, makeMemoryBar, makeSparkline } from "./gui-elements";
10+
import { isValidApiKey, checkApiKey } from "./openai";
1011

1112
export function vsNavigate(filename, lineno) {
1213
// If we are in VS Code, clicking on a line number in Scalene's web UI will navigate to that line in the source code.
@@ -32,76 +33,6 @@ const maxLinesPerRegion = 50; // Only show regions that are no more than this ma
3233

3334
let showedExplosion = {}; // Used so we only show one explosion per region.
3435

35-
function unescapeUnicode(s) {
36-
return s.replace(/\\u([\dA-F]{4})/gi, function (match, p1) {
37-
return String.fromCharCode(parseInt(p1, 16));
38-
});
39-
}
40-
41-
async function tryApi(apiKey) {
42-
const response = await fetch("https://api.openai.com/v1/completions", {
43-
method: "GET",
44-
headers: {
45-
"Content-Type": "application/json",
46-
Authorization: `Bearer ${apiKey}`,
47-
},
48-
});
49-
return response;
50-
}
51-
52-
async function isValidApiKey(apiKey) {
53-
const response = await tryApi(apiKey);
54-
const data = await response.json();
55-
if (
56-
data.error &&
57-
data.error.code in
58-
{
59-
invalid_api_key: true,
60-
invalid_request_error: true,
61-
model_not_found: true,
62-
insufficient_quota: true,
63-
}
64-
) {
65-
return false;
66-
} else {
67-
return true;
68-
}
69-
}
70-
71-
function checkApiKey(apiKey) {
72-
(async () => {
73-
try {
74-
window.localStorage.setItem("scalene-api-key", apiKey);
75-
} catch {
76-
// Do nothing if key not found
77-
}
78-
// If the API key is empty, clear the status indicator.
79-
if (apiKey.length === 0) {
80-
document.getElementById("valid-api-key").innerHTML = "";
81-
return;
82-
}
83-
const isValid = await isValidApiKey(apiKey);
84-
if (!isValid) {
85-
document.getElementById("valid-api-key").innerHTML = "✕";
86-
} else {
87-
document.getElementById("valid-api-key").innerHTML = "✓";
88-
}
89-
})();
90-
}
91-
92-
function countSpaces(str) {
93-
// Use a regular expression to match any whitespace character at the start of the string
94-
const match = str.match(/^\s+/);
95-
96-
// If there was a match, return the length of the match
97-
if (match) {
98-
return match[0].length;
99-
}
100-
101-
// Otherwise, return 0
102-
return 0;
103-
}
104-
10536
export function proposeOptimizationRegion(filename, file_number, line) {
10637
proposeOptimization(
10738
filename,

scalene/scalene-gui/utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
export function unescapeUnicode(s) {
2+
return s.replace(/\\u([\dA-F]{4})/gi, function (match, p1) {
3+
return String.fromCharCode(parseInt(p1, 16));
4+
});
5+
}
6+
7+
8+
export function countSpaces(str) {
9+
// Use a regular expression to match any whitespace character at the start of the string
10+
const match = str.match(/^\s+/);
11+
12+
// If there was a match, return the length of the match
13+
if (match) {
14+
return match[0].length;
15+
}
16+
17+
// Otherwise, return 0
18+
return 0;
19+
}
20+
21+
122
export function memory_consumed_str(size_in_mb) {
223
// Return a string corresponding to amount of memory consumed.
324
let gigabytes = Math.floor(size_in_mb / 1024);

0 commit comments

Comments
 (0)