Making Your First LLM Call
Introduction to LLM Calls
Welcome to the first lesson of our course on building your own AI Cooking Helper. In this lesson, we will explore the concept of making basic LLM (Large Language Model) calls. LLMs, such as OpenAI's models, are powerful tools that can generate human-like text responses. They are integral to AI applications, enabling them to understand and respond to user inputs naturally. By the end of this lesson, you will understand how to make a basic LLM call and interpret its output.
Understanding the Code Structure
Let's start by understanding the structure of the code used to make an LLM call. We'll build this step by step.
First, we need to import the necessary library and set up the OpenAI client. This client will allow us to interact with the OpenAI API.
Here, we import the OpenAI class from the openai library. We then create an OpenAI client using the API key stored in an environment variable. This client is essential for making requests to the OpenAI API.
In the Codesignal environment, this variables are already configured!
Creating System and User Prompts
Next, we need to define the prompts that will guide the model's behavior. There are two types of prompts: system prompts and user prompts.
- System Prompt: This sets the context for the model. In our example, the system prompt instructs the model to respond like a pirate.
- User Prompt: This is the input from the user. Here, the user is asking what the best cooking recipe is.
These prompts are crucial, as they shape the model's responses, ensuring they are relevant and contextually appropriate.
Configuring the Model Parameters
To control the model's output, we configure certain parameters, such as temperature and the model used.
- Temperature: This parameter controls the randomness of the model's output. A lower temperature (e.g., 0.2) makes the output more deterministic, while a higher temperature (e.g., 0.8) introduces more randomness and creativity. In our example, a temperature of 0.7 strikes a balance between creativity and coherence.
- Model: This parameter controls the model that will be used to generate the response. You can find a list of available models on the OpenAI website. For this course, we will use
gpt-4o-mini, but you are free to change this parameter to your preferred model.
