Skip to content

Commit

Permalink
Update generate.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alfrencho authored Jan 30, 2023
1 parent f884154 commit 3e1b4d7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pages/api/generate.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
// Imports the Configuration and OpenAIApi classes from the openai library
import { Configuration, OpenAIApi } from 'openai';

// Creates a Configuration instance with the OpenAI API key from the environment variable
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

// Creates an OpenAIApi instance using the Configuration instance
const openai = new OpenAIApi(configuration);

// Define the prefix for the API prompt
const basePromptPrefix = "";

// Define the generateAction function
const generateAction = async (req, res) => {
// Run first prompt
// Log the API request
console.log(`API: ${basePromptPrefix}${req.body.userInput}`)

// Call the OpenAI API to generate a response to the prompt
const baseCompletion = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `${basePromptPrefix}${req.body.userInput}`,
temperature: 0.7,
max_tokens: 250,
});

// Get the response from the API call
const basePromptOutput = baseCompletion.data.choices.pop();

// Return the response to the client
res.status(200).json({ output: basePromptOutput });
};

export default generateAction;
// Export the generateAction function
export default generateAction;

1 comment on commit 3e1b4d7

@vercel
Copy link

@vercel vercel bot commented on 3e1b4d7 Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.