Hi everyone,
We created this assistant bot and prior to the New Year it was working perfectly fine, producing accurate and timely replies. For a few weeks now, we are encountering this bug where all of a sudden the assistant will just not give us any answers, it just goes blank. The bot works just fine on the playground.
I am sharing some of our code here in hopes someone can help us find out what is going wrong. To the extent that I have been able to understand, sometimes openAI is returning a response in a format that our code cannot interpret, or just a straight up empty response. I have checked our limits and API keys and all of those are just fine.
<script>
import { marked } from 'marked';
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: import.meta.env.VITE_OPENAI,
dangerouslyAllowBrowser: true
});
export default {
data() {
return {
messages: JSON.parse(localStorage.getItem('messages')) || [
{ role: 'assistant', content: 'Hello! How can I assist you today?' }
],
thread: {},
assistantid: import.meta.env.VITE_ASSIST,
sampleQuestions: [
(this is a set of questions that I am omitting here)
],
newMessage: '',
isTyping: false // State property to track if the assistant is typing
};
},
async created() {
let x = localStorage.getItem('usermessageid');
x = JSON.parse(x)
if (x) {
this.thread = x
} else {
this.thread = await openai.beta.threads.create();
localStorage.setItem('usermessageid', JSON.stringify(this.thread));
}
},
methods: {
sendMessage() {
if (this.newMessage.trim() !== '') {
this.messages.push({ role: 'user', content: this.newMessage });
localStorage.setItem('messages', JSON.stringify(this.messages));
this.$nextTick(() => {
this.$refs.chatMessages.scrollTop = this.$refs.chatMessages.scrollHeight;
});
this.newMessage = '';
this.replyFromDigiPres();
}
},
setInput(question) {
this.newMessage = question;
},
async replyFromDigiPres() {
try {
// Set typing indicator to true
this.isTyping = true;
// Send the user's message to the OpenAI API
const message = await openai.beta.threads.messages.create(
this.thread.id,
this.messages.at(-1)
);
// Create a run for the assistant
const run = await openai.beta.threads.runs.create(
this.thread.id,
{ assistant_id: this.assistantid }
);
// Polling the status of the run until it is completed
let runStatus = 'in_progress';
while (runStatus === 'in_progress') {
const rrun = await openai.beta.threads.runs.retrieve(
this.thread.id,
run.id
);
runStatus = rrun.status;
// Wait for a short period before checking the status again
if (runStatus === 'in_progress') {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
// Fetch the response once the status is completed
if (runStatus === 'completed') {
const response = await openai.beta.threads.messages.list(
this.thread.id,
);
const messages = response.body.data;
const assistantMessage = messages.find(msg => msg.role === 'assistant');
// Update the messages with the assistant's reply
this.messages.push({ role: 'digiPres', content: assistantMessage.content[0].text.value });
localStorage.setItem('messages', JSON.stringify(this.messages));
this.$nextTick(() => {
if (this.messages.at(-1).role === 'user') {
this.$refs.chatMessages.scrollTop = this.$refs.chatMessages.scrollHeight;
}
});
}
} catch (error) {
console.error('Error communicating with OpenAI API:', error);
} finally {
// Clear typing indicator
this.isTyping = false;
}
},
renderMarkdown(content) {
return marked(content);
},
async clearChat() {
console.log("running")
// Clear messages in local storage and data
this.messages = [{ role: 'assistant', content: 'Hello! How can I assist you today?' }];
localStorage.setItem('messages', JSON.stringify(this.messages));
// Clear the thread
this.thread = await openai.beta.threads.create();
localStorage.setItem('usermessageid', JSON.stringify(this.thread));
// Scroll to top after clearing
this.$nextTick(() => {
this.$refs.chatMessages.scrollTop = 0;
});
},
shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
},
},
computed: {
randomSampleQuestions() {
return this.shuffle([...this.sampleQuestions]).slice(0, 2);
}
}
};
</script>