homeprojectsresume
 
   
💻 Take your coding skills to the next level with "500+ Ultimate ChatGPT Prompts For Developers" – the game-changer every developer needs, click here to order now.

Integration with ChatGPT API

Published March 19, 2023Last updated May 28, 20231 min read

ChatGPT is a large language model (LLM) that understands and processes human prompts to produce helpful responses. OpenAI provides an API to interact with the ChatGPT model (gpt-3.5-turbo).

Prerequisites

  • OpenAI account
  • Generated API key
  • Enabled billing

Integration

Below is an example of interacting with ChatGPT API based on a given prompt.

const handlePrompt = async (prompt) => {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: prompt,
},
],
},
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
return response?.data?.choices?.[0]?.message?.content;
};

 

© 2023