O'Reilly's Hands-On Large Language Models: A Practical Look for Engineers
HandsOnLLM/Hands-On-Large-Language-Models
This repository helps software engineers in several key ways
Practical Implementation
It provides concrete examples and working code for various LLM applications, allowing you to move from theory to practice quickly. You can see how concepts like fine-tuning, retrieval-augmented generation (RAG), and agent-based systems are implemented in real code.
Best Practices
The code examples demonstrate best practices for interacting with LLM APIs, handling data, and structuring projects, which can prevent common pitfalls when starting out.
Learning and Prototyping
It's an excellent resource for learning and experimenting. You can use the provided code to quickly prototype new ideas or understand how different LLM techniques work by running and modifying the examples yourself.
Debugging and Troubleshooting
By providing a clean, working baseline, it can help you debug your own code. If your implementation isn't working, you can compare it to the examples in the repository to identify issues.
Getting this repository set up is straightforward. You'll need to have Python and pip installed on your system.
Clone the repository
First, you'll want to clone the repository to your local machine using git.
git clone https://github.com/HandsOnLLM/Hands-On-Large-Language-Models.git
cd Hands-On-Large-Language-Models
Install dependencies
The repository is organized into different chapters. Each chapter's code has its own set of dependencies, which are listed in requirements.txt files. A convenient way to manage these is by creating a virtual environment.
# Create and activate a virtual environment
python3 -m venv venv
source venv/bin/activate
# Or on Windows: venv\Scripts\activate
# Navigate to a specific chapter's directory and install its dependencies
cd ch01_hello_llm
pip install -r requirements.txt
Set up API Keys
Many of the examples require API keys for services like OpenAI. You should set these up as environment variables to keep them secure and avoid hardcoding them in your files.
export OPENAI_API_KEY="your-api-key-here"
Let's look at a simplified example based on the code from Chapter 1, which introduces how to make a basic API call to an LLM.
Objective
Write a simple script that asks an LLM a question and prints the response.
Sample Python Code (simple_llm_call.py)
import os
import openai
# Make sure you have the OPENAI_API_KEY environment variable set!
openai.api_key = os.environ.get("OPENAI_API_KEY")
def ask_llm(question):
"""Sends a question to the LLM and returns the response."""
try:
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": question}
]
)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {e}"
if __name__ == "__main__":
user_question = "What is the capital of Japan?"
print(f"Asking the LLM: '{user_question}'")
answer = ask_llm(user_question)
print(f"LLM's answer: {answer}")
How to run it
Navigate to the directory containing this file.
Ensure your OPENAI_API_KEY is set as an environment variable.
Run the script from your terminal
python simple_llm_call.py