Skip to content
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

Dalle integration PR #16

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 6 additions & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script>
import { DeepChat } from "deep-chat-dev";
import { onMount } from 'svelte';
import { BIDARA_SYS, PAPER_SEARCH_FUNC } from './bidara';
import { BIDARA_SYS, PAPER_SEARCH_FUNC, GEN_IMAGE_FUNC } from './bidara';
import { funcCalling } from './bidaraFunctions';
import { getKeyAndAsst } from './openaiUtils';
import hljs from "highlight.js";
Expand Down Expand Up @@ -120,7 +120,11 @@
{
type: "function",
function: PAPER_SEARCH_FUNC
}
},
{
type: "function",
function: GEN_IMAGE_FUNC
},
]
},
function_handler: funcCalling
Expand Down
29 changes: 27 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;
}
}
45 changes: 44 additions & 1 deletion src/openaiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,47 @@ export async function getKeyAndAsst() {

let asst = await getAsst(key);
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 {
const key = await getOpenAIKey();
jackitaliano marked this conversation as resolved.
Show resolved Hide resolved
if (!key) {
return null;
}

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

const request = {
method: "POST",
headers: {
'Authorization': 'Bearer ' + key,
'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;
}
}