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 naturally respond to user inputs. 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 will 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.

import os
from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_BASE_URL"))

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 the 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 = "You are a coding assistant that talks like a pirate."
user_prompt = "What is the best cooking recipe in the world?"
  • System Prompt: This sets the context for the model. In our example, the system_prompt instructs the model to act as a coding assistant that speaks 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 parameters such as the temperature.

temperature = 0.7
  • 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 Selection: Instead of using a variable, we will pass the model name as a literal string directly into our request. We will use gpt-4o-mini, which is a powerful and efficient model for these tasks.
Executing the LLM Call

Now, let's execute the LLM call using the client.chat.completions.create method.

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
       {"role": "system", "content": system_prompt},
       {"role": "user", "content": user_prompt},
    ],
    temperature=temperature
)

print(completion.choices[0].message.content.strip())
  • model: We pass the literal string gpt-4o-mini to specify which model should generate the response.
  • messages: This is a list of messages that includes both the system_prompt and user_prompt defined earlier.
  • temperature: We pass our temperature variable to control the output's randomness.

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 the openai library. We covered the setup of the OpenAI client, the creation of the specific system_prompt and user_prompt, the configuration of the temperature parameter, and the execution of the call using a literal model string. Understanding these components is essential for leveraging LLMs in your projects.

As you move on to the practice exercises, experiment with different prompts and temperature 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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal