Skip to content

Commit

Permalink
Refactoring js (#885)
Browse files Browse the repository at this point in the history
* Packaged prism.

* More exports, code cleanup to address eslint issues.

* Refactoring.

* Production with optimizations disabled.

* Refactored some logic.

* Refactoring.

* Added vega.

* More imports.

* Updated.
  • Loading branch information
emeryberger authored Nov 15, 2024
1 parent 5f9ace4 commit 526fba1
Show file tree
Hide file tree
Showing 16 changed files with 2,911 additions and 1,605 deletions.
12 changes: 0 additions & 12 deletions scalene/launchbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,6 @@ def generate_html(profile_fname: Filename, output_fname: Filename) -> None:
"prism_css_text": read_file_content(
scalene_dir, "scalene-gui", "prism.css"
),
"prism_js_text": read_file_content(
scalene_dir, "scalene-gui", "prism.js"
),
"tablesort_js_text": read_file_content(
scalene_dir, "scalene-gui", "tablesort.js"
),
"tablesort_number_js_text": read_file_content(
scalene_dir, "scalene-gui", "tablesort.number.js"
),
}

# Put the profile and everything else into the template.
Expand All @@ -153,9 +144,6 @@ def generate_html(profile_fname: Filename, output_fname: Filename) -> None:
profile=profile,
gui_js=file_contents["scalene_gui_js_text"],
prism_css=file_contents["prism_css_text"],
prism_js=file_contents["prism_js_text"],
tablesort_js=file_contents["tablesort_js_text"],
tablesort_number_js=file_contents["tablesort_number_js_text"],
scalene_version=scalene_config.scalene_version,
scalene_date=scalene_config.scalene_date,
)
Expand Down
64 changes: 64 additions & 0 deletions scalene/scalene-gui/amazon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
BedrockRuntimeClient,
InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";

export async function sendPromptToAmazon(prompt) {
const accessKeyId =
document.getElementById("aws-access-key").value ||
localStorage.getItem("aws-access-key");
const secretAccessKey =
document.getElementById("aws-secret-key").value ||
localStorage.getItem("aws-secret-key");
const region =
document.getElementById("aws-region").value ||
localStorage.getItem("aws-region") ||
"us-east-1";

// Configure AWS Credentials
const credentials = {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
};

// Initialize the Bedrock Runtime Client
const client = new BedrockRuntimeClient({
region: region,
credentials: credentials,
});

const params = {
"modelId": "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
"body": JSON.stringify({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 65536, // arbitrary large number
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
})
}

try {
const command = new InvokeModelCommand(params);
const response = await client.send(command);

// Convert the response body to text
const responseBlob = new Blob([response.body]);
const responseText = await responseBlob.text();
const parsedResponse = JSON.parse(responseText);
const responseContents = parsedResponse.content[0].text;

return responseContents.trim();
} catch (err) {
console.error(err);
return `# Error: ${err.message}`;
}
}
62 changes: 62 additions & 0 deletions scalene/scalene-gui/azure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export async function sendPromptToAzureOpenAI(prompt, apiKey, apiUrl, aiModel) {
const apiVersion = document.getElementById("azure-api-model-version").value;
const endpoint = `${apiUrl}/openai/deployments/${aiModel}/chat/completions?api-version=${apiVersion}`;

const body = JSON.stringify({
messages: [
{
role: "system",
content:
"You are a Python programming assistant who ONLY responds with blocks of commented, optimized code. You never respond with text. Just code, starting with ``` and ending with ```.",
},
{
role: "user",
content: prompt,
},
],
user: "scalene-user",
});

console.log(body);

const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"api-key": apiKey,
},
body: body,
});

const data = await response.json();
if (data.error) {
if (
data.error.code in
{
invalid_request_error: true,
model_not_found: true,
insufficient_quota: true,
}
) {
return "";
}
}
try {
console.log(
`Debugging info: Retrieved ${JSON.stringify(data.choices[0], null, 4)}`,
);
} catch {
console.log(
`Debugging info: Failed to retrieve data.choices from the server. data = ${JSON.stringify(
data,
)}`,
);
}

try {
return data.choices[0].message.content.replace(/^\s*[\r\n]/gm, "");
} catch {
// return "# Query failed. See JavaScript console (in Chrome: View > Developer > JavaScript Console) for more info.\n";
return "# Query failed. See JavaScript console (in Chrome: View > Developer > JavaScript Console) for more info.\n";
}
}
Loading

0 comments on commit 526fba1

Please sign in to comment.