Making Basic LLM Calls
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 libraries and set up the OpenAI client. This client will allow us to interact with the OpenAI API.
Here, we import the os module to access environment variables and the OpenAI class from the openai library. We then create an OpenAI client using the API key and base URL stored in environment variables. This client is essential for making requests to the OpenAI API.
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 is the best cooking recipe.
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 parameters such as reasoning_effort.
- Reasoning Effort: For reasoning models like
gpt-6-luna, the amount of reasoning and depth applied to generate an answer is controlled by thereasoning_effortparameter rather than temperature. Typical effort values are"low","medium", and"high". A"low"effort produces faster, more concise responses with less reasoning overhead, while"high"allows the model to think more thoroughly through complex problems. In our example,"medium"strikes a balance between depth and response time. - Model: This parameter controls the model that will be used to generate the response. You can find a list of available models in the OpenAi website. For this course we will use
gpt-6-luna, but you are free to change this parameter to your preferred model.
Executing the LLM Call
Now, let's execute the LLM call using the client.chat.completions.create method.
model: We pass the literal stringgpt-6-lunato specify which model should generate the response.messages: This is a list of messages that includes both thesystem_promptanduser_promptdefined earlier.reasoning_effort: We pass ourreasoning_effortvariable to control the depth of reasoning the model uses.
The create method sends the request to the OpenAI API, and the response is stored in the completion variable. We then print the model's response, which is accessed through completion.choices[0].message.content.strip().
Summary and Preparation for Practice
In this lesson, we explored how to make basic LLM calls using OpenAI's API. We covered the setup of the OpenAI client, the creation of system and user prompts, the configuration of model parameters, and the execution of the LLM call. Understanding these components is essential for leveraging LLMs in your projects.
As you move on to the practice exercises, experiment with different prompts and reasoning effort settings to see how they affect the model's output. This hands-on experience will deepen your understanding and prepare you for more advanced applications in future lessons.
