Skip to content

Instantly share code, notes, and snippets.

@mfrancois3k
Forked from eniodev/openai_api_config.js
Created April 19, 2023 05:09
Show Gist options
  • Select an option

  • Save mfrancois3k/34711d03bae9a52d1e4bf3d9a791c991 to your computer and use it in GitHub Desktop.

Select an option

Save mfrancois3k/34711d03bae9a52d1e4bf3d9a791c991 to your computer and use it in GitHub Desktop.
This is a starter snippet for building applications powered by openAI models
// npm i openai dotenv
import { config } from 'dotenv';
config();
// OpenAI API Configuration
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY, // Replace this with your key
});
const openai = new OpenAIApi(configuration);
// Fetch the data passing your prompt
export const getGPTResponse = async (prompt) => {
const response = await openai.createCompletion({
model: "text-davinci-003", // This is where you put your openAI model
prompt: `${prompt}`,
temperature: 0,
max_tokens: 150, // This is the max tokens you want for your response
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
})
if(response) {
return `${response.data.choices[0].text}`; // This is your final response
}
}
//Ok, you are all set up! 😁
//For more information about each parameter and the variety of available models, please check: https://platform.openai.com/docs/models
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment