Skip to content

Commit

Permalink
Merge pull request #104 from nasa-petal/store-asst
Browse files Browse the repository at this point in the history
Assistant ID Storage
  • Loading branch information
bruffridge authored Apr 23, 2024
2 parents 1f7df4f + 0517166 commit 9aaa635
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
17 changes: 9 additions & 8 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import { BIDARA_CONFIG, BIDARA_INITIAL_MESSAGES } from './assistant/bidara';
import { funcCalling } from './assistant/bidaraFunctions';
import * as threadUtils from './utils/threadUtils';
import { getKeyAsstAndThread } from './utils/openaiUtils';
import { getKeyAndThread, getAsst, setAsst } from './utils/openaiUtils';
import { createBidaraDB, closeBidaraDB } from "./utils/bidaraDB";
import hljs from "highlight.js";
window.hljs = hljs;
let activeKey = null;
let activeThread = null;
let activeAsstId = null;
let activeAsstConfig = null;
let activeFuncCalling = null;
let activeInitialMessages = null;
Expand All @@ -24,15 +23,14 @@
let loadedMessages = false;
async function initKeyAsstAndThreads() {
const keyAsstAndThread = await getKeyAsstAndThread();
const keyAsstAndThread = await getKeyAndThread();
if (!keyAsstAndThread[0]) {
return;
}
activeKey = keyAsstAndThread[0];
activeAsstId = keyAsstAndThread[1];
activeThread = keyAsstAndThread[2];
activeThread = keyAsstAndThread[1];
activeInitialMessages = BIDARA_INITIAL_MESSAGES;
activeAsstConfig = BIDARA_CONFIG;
Expand Down Expand Up @@ -67,9 +65,9 @@
return;
}
const thread = await threadUtils.getNewThread();
await switchActiveThread(thread);
const thread = await threadUtils.getNewThread(activeThread.asst_id);
threads = await threadUtils.getThreads();
await switchActiveThread(thread);
}
async function deleteThreadAndSwitch(thread) {
Expand All @@ -95,6 +93,10 @@
async function switchActiveThread(thread) {
if (!thread?.asst_id) {
const asstId = await getAsst();
await setAsst(thread, asstId);
}
if (activeThread && thread.id === activeThread.id) {
return;
}
Expand Down Expand Up @@ -133,7 +135,6 @@
<div id="chat-container" class="w-full" class:loading>
<AssistantDeepChat
key={activeKey}
asstId={activeAsstId}
asstConfig={activeAsstConfig}
thread={activeThread}
funcCalling={activeFuncCalling}
Expand Down
3 changes: 1 addition & 2 deletions src/components/AssistantDeepChat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import * as threadUtils from '../utils/threadUtils';
export let key = null;
export let asstId = null;
export let asstConfig = null;
export let thread = null;
export let initialMessages = null;
Expand All @@ -19,6 +18,7 @@
let lastMessageId;
let threadId = thread?.id;
let asstId = thread?.asst_id;
let currRunId = null;
let newFileUploads = [];
let newFileIds = [];
Expand Down Expand Up @@ -93,7 +93,6 @@
await loadMessages(thread)
}
setTimeout(()=> loading = false, 400);
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/bidaraDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export async function setLengthById(id, length) {
await dbUtils.updateProperty(BIDARA_DB, CHAT_STORE_NAME, id, "length", length);
}

export async function setAsstIdById(id, asstId) {
await dbUtils.updateProperty(BIDARA_DB, CHAT_STORE_NAME, id, "asst_id", asstId);
}

export async function setActiveStatusById(id, status) {
if (status) {
await dbUtils.updateProperty(BIDARA_DB, CHAT_STORE_NAME, id, "active", ACTIVE_STATUS);
Expand Down
38 changes: 15 additions & 23 deletions src/utils/openaiUtils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as bidara from "../assistant/bidara";

import { getStoredAPIKey, getStoredAsstId, setStoredAPIKey, setStoredAsstId } from "./storageUtils";
import { getActiveThread, getFileByFileId, getThreadImages } from "./threadUtils";
import { getActiveThread, setThreadAsstId, getFileByFileId, getThreadImages } from "./threadUtils";

let openaiKey = null;
let openaiAsst = null;

export async function validAssistant(id) {
if (!openaiKey) {
Expand Down Expand Up @@ -147,29 +146,17 @@ export async function getAsst() {
if (!openaiKey) {
throw new Error('openai key not set. cannot get assistant.');
}
if (openaiAsst) {
return openaiAsst;
}

openaiAsst = getStoredAsstId();

let isValidAsstId = false;
if (openaiAsst !== null) {
isValidAsstId = await validAssistant(openaiAsst);
}

if (!isValidAsstId) {
openaiAsst = getBidaraAssistant(); // returns asst_id or null.
}
const openaiAsst = getBidaraAssistant(); // returns asst_id or null.

return openaiAsst;
}

export function setAsst(id) {
export async function setAsst(thread, asstId) {
// assistant id must have already been validated
if (id) {
openaiAsst = id;
setStoredAsstId(openaiAsst);

if (thread && asstId) {
await setThreadAsstId(thread, asstId);
}
}

Expand Down Expand Up @@ -241,17 +228,22 @@ export async function getNewThreadId() {
}


export async function getKeyAsstAndThread() {
export async function getKeyAndThread() {
let key = await getOpenAIKey();
if (key === null) {
return [null, null, null]
}

let asst = await getAsst();

let thread = await getActiveThread();

return [key, asst, thread]
if (!thread.asst_id) {
const asstId = await getBidaraAssistant();
await setAsst(thread, asstId);

thread.asst_id = asstId;
}

return [key, thread]
}

export async function getDalleImageGeneration(prompt, image_size = null, image_quality = null, num_images = null, response_format = null) {
Expand Down
19 changes: 15 additions & 4 deletions src/utils/threadUtils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { validThread, getNewThreadId, getThreadMessages, getFileSrc, getFileInfo } from "./openaiUtils";
import * as bidaraDB from "./bidaraDB";

async function createNewThread() {
async function createNewThread(asstId) {
const new_id = await getNewThreadId();

if (new_id) {
const new_name = "New Chat";
return {name: new_name, id: new_id, length: 0, active: true};

return {name: new_name, id: new_id, asst_id: asstId, length: 0, active: true};
}

return null;
Expand All @@ -28,8 +29,8 @@ export async function getActiveThread() {
return thread;
}

export async function getNewThread() {
const thread = await createNewThread();
export async function getNewThread(asstId) {
const thread = await createNewThread(asstId);
await bidaraDB.setThread(thread);

return thread;
Expand Down Expand Up @@ -90,6 +91,16 @@ export async function setThreadLength(id, length) {
await bidaraDB.setLengthById(id, length);
}

export async function setThreadAsstId(thread, asstId) {
if (!thread.hasOwnProperty("asst_id")) {
thread.asst_id = asstId;
await bidaraDB.setThread(thread);

} else {
await bidaraDB.setAsstIdById(thread.id, asstId);
}
}

export async function deleteThread(threadId) {
await bidaraDB.deleteThreadById(threadId);
}
Expand Down

0 comments on commit 9aaa635

Please sign in to comment.