Hey,
I have read all the threads already related to this but nothing has helped me so far… In short, the api works meaby 60% of the time, and then it just doesn’t. The try catch is not triggered meaning the api returns 200 but with undefined as a response.
I know for sure if the values are not stringified, especifically the function responses, it will give me this kind of error. But in this case, I just don’t know what is causing it, full prompt doesn’t get over 800 tokens, sometimes it works, sometimes it doesn’t. I thought maybe there is a big usage at the time, but it happens at different times of the day too.
Last thing I tried is to stringify all the values passed in the AiPrompt, but still nothing.
ANY HELP is much appreciated!
Below is my code, I’m using nodeJS/NextJS and api v4
API:
export async function gptQuery(
query: string,
aiPrompt: string,
functionRes: Array<{
role: ChatCompletionRequestMessageRoleEnum;
content: string;
name: string;
}> = [],
model: string = "gpt-3.5-turbo-0613"
) {
const functions = [
{
"name": "get_json_categories",
"description":
"Get a json structure based on one or multiple categories",
"parameters": {
"type": "object",
"properties": {
"industry_type":{
"type": "string",
"description":"The type of industry to get the categories from, e.g. 'Medicine'",
},
"categories": {
"type": "array",
"description":"An array of the categories to fetch, e.g. ['personalDemographics', 'medicalHistory']",
"items": {
"type": "string"
}
},
},
"required": ["industry_type","categories"],
},
},
{
"name": "populate_json_categories",
"description":"populate a json structure with a given text",
"parameters": {
"type": "object",
"properties": {
"text":{
"type": "string",
"description":"The full unfiltered text to populate the json with",
},
"json": {
"type": "string",
"description":
"The json to populate, e.g. '{\"name\":\"\"}'",
},
},
"required": ["text","json"],
},
},
{
"name": "get_calendar_events",
"description":
"Get the calendar events for a given day, user, or time period.",
"parameters": {
"type": "object",
"properties": {
"event_date": {
"type": "string",
"format": "date",
"description":
"date to filter the calendar events in yyyy-mm-dd HH:MM:SS format (e.g. 2023-02-18 23:38:44)",
},
},
"required": ["event_date"],
},
},
{
"name": "get_answer_from_docs",
"description":
"Large Collection of documents, answers any general question user asks. It can be filtered based on title, or time period",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The semantic query to search for, e.g. 'joe smith'",
},
"title": {
"type": "string",
"description": "The title to search for",
},
"date": {
"format": "date",
"description":
"date to filter by in yyyy-mm-dd format (e.g. 2023-02-18)",
},
},
"required": ["query"],
},
},
{
name: "get_company_info",
description:
"Get all the company information for a given company, including address, phone number, and detailed information of the staff",
parameters: {
type: "object",
properties: {},
},
},
];
const messages = [{
role: "system",
name: "system",
content: aiPrompt,
},
{
role: "user",
name: "user",
content: query,
}]
if (functionRes.length > 0) {
messages.push(...functionRes)
}
try {
const res = await openaiClient.chat.completions.create({
model,
messages: messages,
temperature: 0,
functions,
});
return res;
} catch (error: any) {
`ChatGPT was unable to find an answer for that! (Error: ${error.message})`;
}
}
PROMPT:
export const assistantRole = `
You are a specialized AI chatbot called Andreas.
When faced with ambiguous questions, make informed assumptions derived strictly from the data within this interaction.
Discard any content or question not tied directly to the central topic of the inquiry.
Always follow the format of this example to generate your response: "Your next patient is Joana Nunez, she has been experiencing sleep problems and nausea
for the last two weeks. She is under prescribed medication."
Always follow the listed Gender when using pronouns.
Only use the functions you have been provided with.
`
const aiPrompt = `
${assistantRole}\n
Current Session: ${JSON.stringify(userSession)}\n
Chat Summary: ${JSON.stringify(chatSummary)}\n
Previous Chat:${JSON.stringify(previousChat)}\n
Current date: ${JSON.stringify(currentDate)}.
`;