In the previous chapter, Prompt, you learned how to create Prompt objects to package up your requests to a Large Language Model. But how does that Prompt actually get to the LLM and get a response back? That's where the Model comes in.
Think of a Model as a chef in a restaurant. Each chef (model) has their own recipes (trained parameters) and can prepare different dishes (generate text, answer questions). A Model handles authentication (making sure you have permission to order food!), prompt formatting (preparing the ingredients), and the actual API call to the underlying LLM (sending the order to the kitchen and getting the dish back).
Why do we need a Model object?
Imagine you want to use different LLMs, like gpt-4o-mini or llama-3. Each LLM has its own unique API, its own way of formatting prompts, and its own way of handling authentication. A Model object hides all of that complexity from you. You just tell the Model to execute a Prompt, and it takes care of the rest.
Core Concepts: What's inside a Model?
The Model object is responsible for a few key things:
-
model_id: This is a unique identifier for the model, like"gpt-4o-mini"or"llama-3". Think of it as the chef's name. -
Authentication: Many LLMs require an API key to use them. The
Modelhandles retrieving and using this key. Think of this as paying for the meal! -
Prompt Formatting: LLMs often require prompts to be formatted in a specific way. The
Modelknows how to do this formatting. This is like the chef knowing how to prepare the ingredients correctly. -
API Call: The
Modelmakes the actual API call to the LLM, sends the formatted prompt, and receives the response. This is like the waiter bringing your order to the kitchen and bringing the finished dish back to your table. -
Options: The
Modelstores any specific configuration options available for the particular LLM it represents. These are specific settings that will be sent along the API call.
Solving the Use Case: Asking a Question using a Specific Model
Let's say you want to ask gpt-4o-mini a question. You would use the cli like this:
llm -m gpt-4o-mini "What are the best types of cheese for a cheese board?"Behind the scenes, the cli does the following:
-
It figures out that you want to use the
gpt-4o-minimodel (from the-m gpt-4o-minioption). -
It creates a
Modelobject representing thegpt-4o-miniLLM. ThisModelobject knows how to authenticate with the OpenAI API (if necessary), format the prompt forgpt-4o-mini, and make the API call. -
It creates a Prompt object containing your question.
-
It tells the
Modelobject to execute thePrompt. -
The
Modelobject sends the formatted prompt to the OpenAI API. -
The OpenAI API responds with the answer.
-
The
Modelobject returns the answer to thecli, which then displays it to you.
Example Code (Simplified)
Here's a simplified example of how you might create and use a Model object in Python (this is a simplified example to illustrate the concept):
# Assume we have a Model class defined (see llm/models.py for the actual class)
class Model:
def __init__(self, model_id):
self.model_id = model_id
def prompt(self, prompt_text):
# In a real implementation, this would:
# 1. Authenticate with the LLM API
# 2. Format the prompt correctly
# 3. Make the API call
# 4. Return the response
print(f"Sending prompt '{prompt_text}' to model {self.model_id}...")
response = f"The best cheeses are Brie, Cheddar, and Gouda. - Model: {self.model_id}" # dummy
return response
# Create a Model object for gpt-4o-mini
model = Model("gpt-4o-mini")
# Ask the model a question
question = "What are the best types of cheese for a cheese board?"
answer = model.prompt(question)
# Print the answer
print(answer)Explanation:
- The
Modelclass is a simplified representation of a realModelobject. It has amodel_idand apromptmethod. - We create a
Modelobject forgpt-4o-mini. - We call the
promptmethod to ask the model a question. - The
promptmethod (in a real implementation) would handle all the details of interacting with the LLM API.
Example output:
Sending prompt 'What are the best types of cheese for a cheese board?' to model gpt-4o-mini...
The best cheeses are Brie, Cheddar, and Gouda. - Model: gpt-4o-mini
Internal Implementation Walkthrough
Let's visualize what happens internally when you call the prompt method on a Model object:
sequenceDiagram
participant User
participant CLI
participant LLM Core
participant Model
participant LLM API
User->>CLI: llm -m gpt-4o-mini "Hello, world!"
CLI->>LLM Core: Create Model("gpt-4o-mini") and Prompt("Hello, world!")
LLM Core->>Model: prompt(Prompt)
Model->>Model: Format prompt for LLM API
Model->>LLM API: Send formatted prompt
LLM API-->>Model: Response from LLM
Model-->>LLM Core: Response text
LLM Core-->>CLI: Response text
CLI->>User: Display response in terminal
This diagram shows:
- The user enters a command with a prompt and a model choice.
- The
clicreates aModelobject and a Prompt object. - The
clicalls thepromptmethod on theModelobject, passing in thePromptobject. - The
Modelobject formats the prompt to be compatible with the underlying LLM API. - The
Modelobject sends the formatted prompt to the LLM API. - The LLM API processes the prompt and returns a response.
- The
Modelobject receives the response and returns it to thecli. - The
clidisplays the response to the user.
Diving into the Code
Here's a look at a simplified version of the Model class definition within llm/models.py:
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class Prompt: # Simplified for brevity
prompt: str
class Model(ABC):
model_id: str
@abstractmethod
def execute(self, prompt: Prompt) -> str:
"""Execute a prompt and return the response text."""
pass
def prompt(self, prompt_text: str) -> str:
prompt = Prompt(prompt_text)
return self.execute(prompt)Explanation:
- The
Modelclass is an abstract base class (ABC), which means that concrete model implementations (likegpt-4o-mini) must inherit from it and implement theexecutemethod. - The
executemethod is an abstract method (@abstractmethod), which means that it must be implemented by subclasses. This method is responsible for actually interacting with the LLM API. - The
promptmethod is a convenience method that creates a Prompt object and then calls theexecutemethod.
And here's an example of a concrete Model implementation (simplified):
class MyModel(Model):
def __init__(self, model_id: str):
self.model_id = model_id
def execute(self, prompt: Prompt) -> str:
# Pretend we're calling an API here
return f"Response from {self.model_id}: {prompt.prompt}"Explanation:
- The
MyModelclass inherits from theModelclass and implements theexecutemethod. - The
executemethod simply returns a string indicating that it received the prompt. In a real implementation, this method would make an API call to an LLM.
Other Useful Methods
The Model class may also have other useful methods, such as:
get_key(): Retrieves the API key for the LLM.format_prompt(): Formats the prompt to be compatible with the LLM API.stream(): Returns a generator that yields chunks of text as they are generated by the LLM (for streaming responses).
Conclusion
The Model object is a key abstraction in llm that handles the complexities of interacting with different Large Language Models. It encapsulates authentication, prompt formatting, and API calls, allowing you to easily switch between different LLMs without having to worry about the details.
In the next chapter, we'll explore the Response object, which represents the result of executing a prompt against a Model.
Generated by AI Codebase Knowledge Builder