#I want to create an action that calls openAI API (perplexity in this case).
5 messages · Page 1 of 1 (latest)
Hi @charred abyss. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
The OpenAI model doesn't have access to real-time data or the internet. It's trained on a large corpus of text from the internet, but it doesn't know anything about specific documents or sources within that set. It also doesn't know anything about events in the world that occurred after its training data was collected in September 2021. Therefore, it won't be able to provide real-time stock prices or any other real-time information.
When you're making a request to the OpenAI API from your Convex action, you're interacting with the model, not the internet. The model generates responses based on the input it's given and the data it was trained on. It doesn't fetch data from the internet or any external databases.
Here's an example of how you can create an action that calls the OpenAI API:
"use node";
import { Configuration, OpenAIApi } from "openai";
import { action } from "../_generated/server";
export const chat = action(async (ctx, { body }) => {
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error("Add your OPENAI_API_KEY as an env variable");
}
const configuration = new Configuration({ apiKey });
const openai = new OpenAIApi(configuration);
const openaiResponse = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: instructions,
},
...messages.map(({ body, author }) => ({
role: author,
content: body,
})),
],
});
if (openaiResponse.status !== 200) {
throw new Error("OpenAI error: " + openaiResponse.statusText);
}
const body = openaiResponse.data.choices[0].message.content;
console.log("Response: " + body);
});
This action sends a series of messages to the gpt-3.5-turbo model and logs the response. You can replace the messages array with your own input.
Remember to add your OpenAI API key to your Convex backend so your action has access to it. You can set this on the command line with:
npx convex env set OPENAI_API_KEY <your key>
npx convex env --prod set OPENAI_API_KEY <your key>
Or in the dashboard.
Sources:
- Building a Full-Stack ChatGPT app (https://stack.convex.dev/full-stack-chatgpt-app#add-your-api-key-to-the-backend)
- Using Dall-E from Convex (https://stack.convex.dev/using-dall-e-from-convex#configuring-openai)
- The platform in action (https://docs.convex.dev/tutorial/actions#fire-up-fetch)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@charred abyss kapa.ai is still learning and improving, please let us know how it did by reacting below