Welcome to the first lesson of our course on creating a chatbot with OpenAI. In this lesson, we will explore the basics of interacting with OpenAI's API, which is a powerful tool for building chatbots. OpenAI provides advanced language models that can understand and generate human-like text, making it an excellent choice for chatbot development. Our goal in this lesson is to send a simple message to OpenAI's language model and receive a response. This foundational step will set the stage for more complex interactions in future lessons.
Before we can send a message to OpenAI, we need to set up our development environment. This involves installing the necessary tools and libraries. For this course, you will need the official OpenAI Go library, which allows us to interact with OpenAI's API.
To install this library, use the following command in your terminal:
In your Go code, import the package as follows:
You’ll also need to import a few standard packages, as shown in the examples below.
In this course, you'll be using a coding environment where we've already set up everything you need to start working with OpenAI models. This means you don't need to worry about setting up an API key or configuring environment variables — it's all taken care of for you.
However, it's still useful to understand how this process works in case you want to set it up on your own computer in the future. To work with OpenAI models outside of this environment, you need to set up a payment method and obtain an API key from their website. This API key is essential for accessing OpenAI's services and making requests to their API.
To keep your API key secure, you can use an environment variable. An environment variable is like a special note that your computer can read to find out important details, such as your OpenAI API key, without having to write it directly in your code. This helps keep your key safe and secure.
In Go, you can access environment variables using the os.Getenv
function. Here's how you would typically set and access an environment variable:
-
On macOS and Linux, open your terminal and use the
export
command to set the environment variable: -
For Windows, you can set the environment variable using the
set
command in the Command Prompt: -
If you are using PowerShell, use the following command:
In your Go code, you can access this environment variable as follows:
Once the environment variable is set, you can initialize the OpenAI client in your Go program. This is done by importing the openai-go
package and then creating a client instance. The library will automatically use your API key from the environment variable if you do not specify it explicitly:
By initializing the client in this manner, you ensure that your program is ready to authenticate requests to OpenAI's API securely.
Now that your environment is set up and your API client is configured, it's time to send your first message to OpenAI. We'll start by defining a simple user prompt and then use the Chat.Completions.New
method to send this message to the AI model.
Here's the code to accomplish this:
In this code, we define a user prompt asking the AI to tell a joke. The Chat.Completions.New
method of the openai
client is used to send a message to the AI model and receive a response. It takes some basic parameters to function:
-
The
Model
parameter specifies which AI model to use for generating the response. In this example, we useshared.ChatModelGPT4
, which is a constant for OpenAI's GPT-4 language model. Other models exist (shared.ChatModelO1
for reasoning,shared.ChatModelGPT4oMini
for general questions, etc.). We are using GPT-4 consistently in this course. -
The
Messages
parameter is a slice ofChatCompletionMessageParamUnion
where each message represents a part of the conversation. You can use the helper functionopenai.UserMessage(prompt)
to create a user message.
After sending the message to OpenAI, the next step is to extract the AI's reply from the API response and display it. Here's how you can do that:
Once the Chat.Completions.New
method is called, it returns a response object. To extract the AI's reply, you need to access the Choices
slice from the response object, which contains possible responses generated by the AI. You then select the first choice with Choices[0]
and retrieve the message content using Message.Content
.
Finally, we print both the prompt and the AI's reply to see the interaction. This helps verify that the message was successfully sent and received. When you run this code, you should see an output similar to the following:
This output demonstrates a successful interaction with the AI, where it responds to the user's prompt with a joke.
Let's look at the complete code example for sending a message to OpenAI. This example includes all the steps we've discussed so far:
In this lesson, we covered the essential steps to send a simple message to OpenAI's language model using Go. We set up our environment, configured API access, and sent a message to receive a response. This foundational knowledge is crucial as we move forward in building more complex chatbot interactions.
As you proceed to the practice exercises, I encourage you to experiment with different prompts and explore the AI's responses. This hands-on practice will reinforce what you've learned and prepare you for the next unit, where we'll delve deeper into handling API parameters. Keep up the great work, and enjoy the journey of creating your chatbot with OpenAI!
