W
Wasp-lang•17mo ago
jtawf

generateGptResponse error

Hi friends, I'm new to wasp so any help would be amazing! @Wasp Team I'm running the generateGptResponse function as it came with the template and i'm receiving this error ([Server!] TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object), any clues on what could be going on here or how to fix it?
9 Replies
Filip
Filip•17mo ago
Hi @jtawf, thanks for trying out Wasp! Could you provide some details about the error (e.g., where does it happen, how did you call the function, etc.)?
miho
miho•17mo ago
Which template were you using? 😄
jtawf
jtawfOP•17mo ago
hey guys sorry for the late reply, I'm using the saas pern template and the issue appears when making the call to the GPT api. I call the action through the front-end as well.
Vinny (@Wasp)
Vinny (@Wasp)•17mo ago
Hey. I created the template and never had any issues with that so far. Could you share the code that causes that error? Thanks!
jtawf
jtawfOP•17mo ago
of course @Vinny (@Wasp)! Just for context here I verified that my open api key is being properly used and I'm receiving the 'fetching' log that details my payload then the errors occurs when fetching the response and I don't receive the log detailing my response
const payload = {
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: "You are a technical recruiter, view the provided document and provide a response according to the command: " + command,
},
{
role: 'user',
content: "hello",
},
],
temperature: 1,
};

try {
if (!context.user.hasPaid && !context.user.credits) {
throw new HttpError(402, 'User has not paid or is out of credits');
} else if (context.user.credits && !context.user.hasPaid) {
console.log('decrementing credits');
await context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
}

console.log('fetching', payload); //logs properly

const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

console.log(response); //does not log
const payload = {
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: "You are a technical recruiter, view the provided document and provide a response according to the command: " + command,
},
{
role: 'user',
content: "hello",
},
],
temperature: 1,
};

try {
if (!context.user.hasPaid && !context.user.credits) {
throw new HttpError(402, 'User has not paid or is out of credits');
} else if (context.user.credits && !context.user.hasPaid) {
console.log('decrementing credits');
await context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
}

console.log('fetching', payload); //logs properly

const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

console.log(response); //does not log
and logs are here as well
[Server] decrementing credits
[Server] fetching {
[Server] model: 'gpt-3.5-turbo',
[Server] messages: [
[Server] {
[Server] role: 'system',
[Server] content: 'You are a technical recruiter, view the provided document and provide a response according to the command: [object Object]'
[Server] },
[Server] { role: 'user', content: 'hello' }
[Server] ],
[Server] temperature: 1
[Server] }
[Server!] TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
[Server!] at checkListener (node:events:266:3)
[Server!] at ClientRequest.once (node:events:647:3)
[Server!] at new ClientRequest (node:_http_client:245:10)
[Server!] at Object.request (node:https:360:10)
[Server] decrementing credits
[Server] fetching {
[Server] model: 'gpt-3.5-turbo',
[Server] messages: [
[Server] {
[Server] role: 'system',
[Server] content: 'You are a technical recruiter, view the provided document and provide a response according to the command: [object Object]'
[Server] },
[Server] { role: 'user', content: 'hello' }
[Server] ],
[Server] temperature: 1
[Server] }
[Server!] TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
[Server!] at checkListener (node:events:266:3)
[Server!] at ClientRequest.once (node:events:647:3)
[Server!] at new ClientRequest (node:_http_client:245:10)
[Server!] at Object.request (node:https:360:10)
Vinny (@Wasp)
Vinny (@Wasp)•17mo ago
if you're using fetch with Node on the backend, then you have to await the JSON. Check that you're doing the following after receiving the response:
const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

// make sure to do this 👇

const json = (await response.json()) as OpenAIResponse;
console.log('response json', json);
const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

// make sure to do this 👇

const json = (await response.json()) as OpenAIResponse;
console.log('response json', json);
I'm not sure if this is the issue, but give this a try
jtawf
jtawfOP•17mo ago
hmm okay, just gave it a try and still not working, does a better look at the try-catch within the function help?
try {
if (!context.user.hasPaid && !context.user.credits) {
throw new HttpError(402, 'User has not paid or is out of credits');
} else if (context.user.credits && !context.user.hasPaid) {
console.log('decrementing credits');
await context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
}

console.log('fetching', payload);

const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

console.log(response);

const json = (await response.json()) as OpenAIResponse;

return context.entities.RelatedObject.create({
data: {
content: json?.choices[0].message.content,
user: { connect: { id: context.user.id } }
}
});

} catch (error: any) {

if (!context.user.hasPaid && error?.statusCode != 402) {
await context.entities.User.update({
where: { id: context.user.id },
data: { credits: { increment: 1 } }
});
}

console.error(error);

}

return new Promise((resolve, reject) => {
reject(new HttpError(500, 'Something went wrong'));
});

};
try {
if (!context.user.hasPaid && !context.user.credits) {
throw new HttpError(402, 'User has not paid or is out of credits');
} else if (context.user.credits && !context.user.hasPaid) {
console.log('decrementing credits');
await context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
}

console.log('fetching', payload);

const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
},
method: 'POST',
body: JSON.stringify(payload),
});

console.log(response);

const json = (await response.json()) as OpenAIResponse;

return context.entities.RelatedObject.create({
data: {
content: json?.choices[0].message.content,
user: { connect: { id: context.user.id } }
}
});

} catch (error: any) {

if (!context.user.hasPaid && error?.statusCode != 402) {
await context.entities.User.update({
where: { id: context.user.id },
data: { credits: { increment: 1 } }
});
}

console.error(error);

}

return new Promise((resolve, reject) => {
reject(new HttpError(500, 'Something went wrong'));
});

};
MEE6
MEE6•17mo ago
Wohooo @jtawf, you just became a Waspeteer level 1!
Vinny (@Wasp)
Vinny (@Wasp)•17mo ago
hmmm... so I pulled up the GPT SaaS Template (https://github.com/wasp-lang/SaaS-Template-GPT) and gave it a try. everything seems to be working fine on my end. I did add a couple changes, but they weren't related to your problem. You can try it out yourself at https://saas-template-gpt-client.fly.dev/ The error you're getting TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object), makes me think there is some kind of event listener. What wasp version are you on (run wasp version )? Did you make any changes to the Saas Template? If so, can you share a github repo? if not, try cloning it and trying again
GitHub
GitHub - wasp-lang/SaaS-Template-GPT
Contribute to wasp-lang/SaaS-Template-GPT development by creating an account on GitHub.
I made a SaaS App. Buy my stuff.
Want results from more Discord servers?
Add your server