1. Introduction on GenAI API

So what is GenAI API anyways? The GenAI API is an interface that allows you to interact with OpenAI’s powerful language models, including ChatGPT and their other models. These models are designed to understand and generate human-like text, making them perfect for building conversational AI, chatbots, and more.

Basic setup for the chatbot

  1. Create a react project by typing: npm create vite@latest app -- -- teamplate react

  2. Create a github repository and upload your code base onto github (optional if wanted to keep local)

How to set up your API Key

  1. Create an OpenAI Account: Visit OpenAI and create an account if you don’t have one.

  2. Generate API Key: Once logged in, navigate to the API section and generate your API key. Store this key securely, as it’s needed to authenticate requests.

  3. Environment Setup: In your project, create a .env file and add your API key like this (make sure NOT to publish this onto github when pushing into your repository):

REACT_APP_OPENAI_API_KEY=your_api_key_here

Making Your First API Call

Using curl or any HTTP client in your terminal, you can make a simple API request:

/curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
     "model": "gpt-3.5-turbo",
     "messages": [{"role": "user", "content": "Hello, Chat GPT!"}],
     "temperature": 0.7
   }'

If you did this correctly, you should be able to see a response from GenAI just like when you type something into ChatGPT!

Last updated