Skip to content

Commit

Permalink
Merge pull request #16 from nasa-petal/js-dalle-integration
Browse files Browse the repository at this point in the history
Dalle integration PR
  • Loading branch information
bruffridge authored Feb 5, 2024
2 parents f5ccafd + 05028e5 commit 95bf5da
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/bidara.js

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

36 changes: 35 additions & 1 deletion src/bidaraFunctions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDalleImageGeneration } from "./openaiUtils";

export function getCurrentWeather(location) {
location = location.toLowerCase();
if (location.includes('tokyo')) {
Expand Down Expand Up @@ -39,11 +41,43 @@ export async function ssSearch(params) {
return JSON.stringify(papers);
}

async function genImage(params) {

var imageParams = JSON.parse(params);

if ("parameters" in imageParams) {
imageParams = imageParams.parameters;
}

var imageDescription = JSON.stringify(imageParams.description) + " Realistic depiction of the object and its environment. Stay true to science, engineering, and biology. DO NOT INCLUDE ANY WORDS OR BRANDING."

const res = await getDalleImageGeneration(imageDescription);

if (!res) {
return "We are having trouble generating images at this time.";
}

const imageURL = res.data[0].url;

const imageWidth = 256;
const imageHeight = 256;
const imageTag = `<img src=${imageURL} alt='Image of: ${imageDescription}. Generated by AI.' width='${imageWidth}' height='${imageHeight}'/>`;
const message = {html: imageTag, role: "ai"};

const deepChatRef = document.getElementById('chat-element');
deepChatRef._addMessage(message);

return "The image has been inserted into the chat, respond by tieing this image back into the Biomimicry Design Process with a short question or response. DO NOT RESPOND WITH AN IMAGE, URL, OR MARKDOWN.";
}

export async function callFunc(functionDetails) {
let tmp = '';
if(functionDetails.name == "get_graph_paper_relevance_search") {
tmp = await ssSearch(functionDetails.arguments);
}
else if(functionDetails.name == "generate_image_from_description") {
tmp = await genImage(functionDetails.arguments);
}
else if(functionDetails.name == "get_weather") {
tmp = getCurrentWeather(functionDetails.arguments);
}
Expand All @@ -56,4 +90,4 @@ export async function callFunc(functionDetails) {
export async function funcCalling(functionsDetails) {
let tmp = await Promise.all(functionsDetails.map(callFunc));
return tmp;
}
}
44 changes: 43 additions & 1 deletion src/openaiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,46 @@ export async function getKeyAndAsst() {

let asst = await getAsst();
return [key, asst]
}
}

export async function getDalleImageGeneration(prompt, image_size = null, image_quality = null, num_images = null) {
if (!image_size) image_size = "1024x1024";
if (!image_quality) image_quality = "standard";
if (!num_images) num_images = 1;

try {
if (!openaiKey) {
return null;
}

const requestURL = "https://api.openai.com/v1/images/generations";

const request = {
method: "POST",
headers: {
'Authorization': 'Bearer ' + openaiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: "dall-e-3",
prompt: prompt,
n: num_images,
size: image_size,
quality: image_quality
})
}

const response = await fetch(requestURL, request);

const r = await response.json();
if (r.error && r.error.type === 'invalid_request_error') {
return null;
}

return r;

} catch (e) {

return null;
}
}

0 comments on commit 95bf5da

Please sign in to comment.