tRPC router refreshes and gives 429 HTTP error

I am implementing a new "endpoint" as from what I have understood. The tRPC looks like the following:
export const openai = new OpenAIApi(configuration);

const getStory = async (storySummary: string, title: string) => {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `Write me a story based on the story summary:
${storySummary}

with the title:
${title}
`
})

return response;
}
export const promptRouter = createTRPCRouter({
createStory: publicProcedure
.input(z.object({
storySummary: z.string(),
title: z.string()
}))
.query(async ({ input }) => {
const response = await getStory(input.storySummary, input.title);

return response.data;
})
});
export const openai = new OpenAIApi(configuration);

const getStory = async (storySummary: string, title: string) => {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `Write me a story based on the story summary:
${storySummary}

with the title:
${title}
`
})

return response;
}
export const promptRouter = createTRPCRouter({
createStory: publicProcedure
.input(z.object({
storySummary: z.string(),
title: z.string()
}))
.query(async ({ input }) => {
const response = await getStory(input.storySummary, input.title);

return response.data;
})
});
I query this in the frontend like so:
const { data, isLoading } = api.prompt.createStory.useQuery({
storySummary: 'A little piggy lost her family in a giant thunderstorm and had to live by herself',
title: 'The piggy tale'
});
const { data, isLoading } = api.prompt.createStory.useQuery({
storySummary: 'A little piggy lost her family in a giant thunderstorm and had to live by herself',
title: 'The piggy tale'
});
My problem is that I think something with the async function is messing up, it gives me a 429 error meaning I am sending too many requests, and I really don't understand why the data is not being fetched.
4 Replies
Mads
Mads2y ago
Actually It might have something to do with the OpenAI api endpoint
Diogo
Diogo2y ago
i dont think you can use trpc with openai
Neto
Neto2y ago
you can
Neto
Neto2y ago
GitHub
GitHub - jlalmes/trpc-openapi: OpenAPI support for tRPC 🧩
OpenAPI support for tRPC 🧩. Contribute to jlalmes/trpc-openapi development by creating an account on GitHub.
Want results from more Discord servers?
Add your server