Add ChatGPT sprinkles to your app
PreReqs
To get started with the ChatGPT API you'll need to:
- Create an Open AI account
- Add some payment info
- Generate an API key
ChatGPT API?
Technically ChatGPT is a web app that uses the Open AI Chat Completion API. But for a lightning talk, ChatGPT is faster off the tongue.
Links
Basic Demo
Useful use cases
- Auto-tagging, named entity recognition, classification
- Summarization
- Auto-filling forms
API Request
While OpenAI has a SDK for Node.js you can add, you can also make a raw fetch request to https://api.openai.com/v1/chat/completions
- Make a
POSTrequest to the OpenAIchat/completionsendpoint. - Authorize the request with an API key.
3a. The model allows us to choose between things like gpt-3.5-turbo and gpt-4.
3b. The messages array is how we pass in a chat history. The ChatGPT API itself has no memory/session state.
- Make a
POSTrequest to the OpenAIchat/completionsendpoint. - Authorize the request with an API key.
3a. The model allows us to choose between things like gpt-3.5-turbo and gpt-4.
3b. The messages array is how we pass in a chat history. The ChatGPT API itself has no memory/session state.
chat.completion API Response
The answer will be in choices[0].message.content.
Use it in Remix
Make the OpenAI fetch request inside of a Remix action
- Protects the API key
- Return the
fetchdirectly as the Action response
Submit to the action with a basic HTML <form/>.
- We don't want to navigate after submission, we want to show the response.
- The trick is to use
fetcher.Form
Display the API response stored on fetcher.data
Make the OpenAI fetch request inside of a Remix action
- Protects the API key
- Return the
fetchdirectly as the Action response
Submit to the action with a basic HTML <form/>.
- We don't want to navigate after submission, we want to show the response.
- The trick is to use
fetcher.Form
Display the API response stored on fetcher.data
The final Remix route file would look like this