Skip to content

Commit

Permalink
improved getting and setting openai key and asst_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruffridge committed Feb 1, 2024
1 parent 55cb7b8 commit b8daf74
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm run dev
```

### Known issues

- if key is not saved in url or localStorage, a new assistant will be created even if one already exists. https://github.com/OvidijusParsiunas/deep-chat/issues/110
- deep-chat speechToText: submit command word is sent in message on safari and chrome on iOS.

- deep-chat textToSpeech: doesn't read messages aloud on safari or chrome on iOS.
Expand Down
12 changes: 6 additions & 6 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { onMount } from 'svelte';
import { BIDARA_CONFIG } from './bidara';
import { funcCalling } from './bidaraFunctions';
import { getKeyAndAsst } from './openaiUtils';
import { setOpenAIKey, setAsst, getKeyAndAsst } from './openaiUtils';
import hljs from "highlight.js";
window.hljs = hljs;
Expand All @@ -17,7 +17,7 @@
];
let openAIKeySet = false;
let openAIAsstSet = false;
let openAIAsstIdSet = false;
let deepChatRef;
let welcomeRef;
Expand All @@ -28,17 +28,17 @@
function onNewMessage(message) {
// save asst id to localStorage.
// this function is called once for each message including initialMessages, ai messages, and user messages.
if (!openAIAsstSet && deepChatRef._activeService.rawBody.assistant_id) {
localStorage.setItem('openai-asst-id', deepChatRef._activeService.rawBody.assistant_id);
openAIAsstSet = true;
if (!openAIAsstIdSet && deepChatRef._activeService.rawBody.assistant_id) {
setAsst(deepChatRef._activeService.rawBody.assistant_id)
openAIAsstIdSet = true;
}
}
function onComponentRender() {
// save key to localStorage.
// The event occurs before key is set, and again, after key is set.
if (!openAIKeySet && deepChatRef._activeService.key) {
localStorage.setItem('openai-key', deepChatRef._activeService.key);
setOpenAIKey(deepChatRef._activeService.key);
openAIKeySet = true;
}
if(!openAIKeySet) {
Expand Down
2 changes: 1 addition & 1 deletion src/bidara.js

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

93 changes: 68 additions & 25 deletions src/openaiUtils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as bidara from "./bidara";

export async function validAssistant(id,key) {
let openaiKey = null;
let openaiAsst = null;

export async function validAssistant(id) {
if(!openaiKey) {
throw new Error('openai key not set. cannot validate assistant.');
}
const response = await fetch("https://api.openai.com/v1/assistants/"+id, {
method: "GET",
headers: {
Authorization: 'Bearer ' + key,
Authorization: 'Bearer ' + openaiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
Expand All @@ -22,12 +28,15 @@ export async function validAssistant(id,key) {
return false;
}

export async function updateAssistant(id,key) {
// returns true on successful update, false otherwise.
export async function updateAssistant(id) {
// returns id on successful update, null otherwise.
if(!openaiKey) {
throw new Error('openai key not set. cannot update assistant.');
}
const response = await fetch("https://api.openai.com/v1/assistants/"+id, {
method: "POST",
headers: {
Authorization: 'Bearer ' + key,
Authorization: 'Bearer ' + openaiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
Expand All @@ -36,18 +45,20 @@ export async function updateAssistant(id,key) {

const r = await response.json();
if (r.hasOwnProperty('id')) {
return true;
return r.id;
}
return false;
return null;
}

export async function getBidaraAssistant(key) {

export async function getBidaraAssistant() {
if(!openaiKey) {
throw new Error('openai key not set. cannot search for bidara assistant.');
}
// get assistants
const response = await fetch("https://api.openai.com/v1/assistants?limit=50", {
method: "GET",
headers: {
Authorization: 'Bearer ' + key,
Authorization: 'Bearer ' + openaiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
Expand All @@ -69,7 +80,8 @@ export async function getBidaraAssistant(key) {
}
else {
// otherwise update it.
updateAssistant(bidaraAsst.id,key);
bidaraAsst.id = await updateAssistant(bidaraAsst.id);
return bidaraAsst.id;
}
}
}
Expand All @@ -91,37 +103,68 @@ export async function validApiKey(key) {
}

export async function getOpenAIKey() {

// if openAIKey has already been validated, return it.
if (openaiKey) {
return openaiKey;
}

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);

let openai_key = urlParams.get('key');
let localOpenAiKey = urlParams.get('key');

if (openai_key === null) {
openai_key = localStorage.getItem('openai-key');
if (localOpenAiKey === null) {
localOpenAiKey = localStorage.getItem('openai-key');
}
if (openai_key !== null) {
if (localOpenAiKey !== null) {
// validate key. if invalid set openai_key to null
let isValidApiKey = await validApiKey(openai_key);
let isValidApiKey = await validApiKey(localOpenAiKey);
if(!isValidApiKey) {
openai_key = null;
openaiKey = null;
}
else {
openaiKey = localOpenAiKey;
}
}
return openai_key;
else {
openaiKey = null;
}
return openaiKey;
}

export function setOpenAIKey(key) {
// key must have already been validated
openaiKey = key;
localStorage.setItem('openai-key', openaiKey);
}

export async function getAsst(key) {
let openai_asst_id = localStorage.getItem('openai-asst-id');
export async function getAsst() {
if(!openaiKey) {
throw new Error('openai key not set. cannot get assistant.');
}
if (openaiAsst) {
return openaiAsst;
}

openaiAsst = localStorage.getItem('openai-asst-id');

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

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

return openai_asst_id;
return openaiAsst;
}

export function setAsst(id) {
// assistant id must have already been validated
openaiAsst = id;
localStorage.setItem('openai-asst-id', openaiAsst);
}

export async function getKeyAndAsst() {
Expand All @@ -130,6 +173,6 @@ export async function getKeyAndAsst() {
return [null, null]
}

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

0 comments on commit b8daf74

Please sign in to comment.