AI ChatGPT
Let's take a look at how to use ai ChatGPT.
ChatGPT is a powerful AI model that you can use inside your NextStarter app. It's a great way to add AI-powered chatbots to your app. 🤖
First things first, add your OPENAI_API_KEY to your .env.local file.
Then, you can use the ChatGPT API In two ways:
ChatGPT Assistant
Inside your NextStarter app, you can use the ChatGPT API directly by making a POST request to the API endpoint. Go into the /libs/gpt.ts and check out the sendChatGPTCompletion function. Pass this function the messages you want to send to the ChatGPT and it will return the response. It has a custom retry logic built-in, so you don't have to worry about handling retries☀️
....
const body = JSON.stringify({
model: "gpt-4", // gpt-4, gpt-3.5-turbo, etc
messages, // array of message objects with role and content. e.g. [{role: "user", content: "What's the weather in San Francisco?"}]
max_tokens: max, // max tokens to generate in the response
temperature: temp, // temperature of the response, higher is more random
user: userId, // user id
});
const options = {
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, // place in your API key here or use environment variables
"Content-Type": "application/json",
},
};
try {
// Custom retry delay
axiosRetry(axios, {
retryDelay: (retryCount) => {
return retryCount * 1;
},
retries: 15,
});
const res = await axios.post(url, body, options);
const answer = res.data.choices[0].message.content;
const usage = res?.data?.usage;
console.log(">>> " + answer);
console.log(
"TOKENS USED: " +
usage?.total_tokens +
" (prompt: " +
usage?.prompt_tokens +
" / response: " +
usage?.completion_tokens +
")"
);
console.log("
");
return answer;
} catch (e) {
console.error("GPT Error: " + e?.response?.status, e?.response?.data);
return null;
}
};
ChatGPT for Function Calling
You can also use ChatGPT completion API to decide which function to call based on the user's input. This is useful when you want to create a chatbot that can call different functions based on the user's input.
Inside /libs/gpt.ts , simply use sendChatGPTFunctions by passing in the user messages and available functions you want to choose from. ChatGPT will automatically decide which function to call based on the user's input, along with the parameters.
Here's an example of a tools array to use sendChatGPTFunctions function to call a function based on the user's input.
const tools = [ // List of available functions, called tools.
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
];
const body = JSON.stringify({
model: "gpt-3.5-turbo", // gpt-4, gpt-3.5-turbo, etc
messages, // array of message objects with role and content. e.g. [{role: "user", content: "What's the weather in San Francisco?"}]
max_tokens: max, // max tokens to generate in the response
temperature: temp, // temperature of the response, higher is more random
tools: tools, // array of available function objects
tool_choice: "auto", // let ChatGPT decide when and which function to call
});
const options = {
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, // place in your API key here or use environment variables
"Content-Type": "application/json",
},
};
try {
// Custom retry delay
axiosRetry(axios, {
retryDelay: (retryCount) => {
return retryCount * 1;
},
retries: 15,
});
const res = await axios.post(url, body, options);
const toolCalls = res.data.choices[0].message.tool_calls;
const usage = res?.data?.usage;
console.log(
"TOKENS USED: " +
usage?.total_tokens +
" (prompt: " +
usage?.prompt_tokens +
" / response: " +
usage?.completion_tokens +
")"
);
return toolCalls;
} catch (e) {
console.error("GPT Error: " + e?.response?.status, e?.response?.data);
return null;
}
};