Introduction
In this tutorial, we'll explore how to work with large language models (LLMs) like those developed by Anthropic and OpenAI. We'll learn how to make API requests to these models using Python, which is essential for anyone interested in AI development or integration. This tutorial is perfect for beginners who want to understand how to interact with cutting-edge AI models, even though the article discusses the slow adoption of models like Fable 5, which highlights the importance of understanding how to properly use these tools.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- Python installed (version 3.7 or higher recommended)
- A free account with either OpenAI or Anthropic (to get an API key)
- Basic knowledge of Python syntax and how to use a code editor
Step-by-Step Instructions
Step 1: Set Up Your Python Environment
First, we need to install the necessary Python packages. Open your terminal or command prompt and run the following command:
pip install openai
This installs the official OpenAI Python library, which makes it easy to interact with OpenAI's models. If you're working with Anthropic models, you'll also need to install their library:
pip install anthropic
Why: These libraries provide pre-built functions to make API calls, saving us from having to manually construct HTTP requests.
Step 2: Get Your API Key
Before you can use any AI model, you need an API key:
- Visit the OpenAI website or Anthropic website
- Create an account if you don't have one
- Navigate to your account settings and generate a new API key
- Copy the key - you'll need it in the next step
Why: The API key authenticates your requests to the AI services and allows you to use their computing resources.
Step 3: Create a Python Script
Now, create a new Python file called ai_demo.py in your preferred code editor:
import os
from openai import OpenAI
# Initialize the client with your API key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
Why: This sets up the connection to OpenAI's API using your key. We use os.getenv() to securely store your key in environment variables instead of hardcoding it in your script.
Step 4: Set Your API Key as an Environment Variable
Before running your script, set your API key as an environment variable:
On Windows (Command Prompt):
set OPENAI_API_KEY=your_api_key_here
On macOS/Linux (Terminal):
export OPENAI_API_KEY=your_api_key_here
Why: Storing API keys in environment variables prevents them from being accidentally shared or committed to version control systems.
Step 5: Make Your First API Call
Add the following code to your Python script:
def get_ai_response(prompt):
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": prompt}
],
max_tokens=150
)
return response.choices[0].message.content
# Test the function
prompt = "Explain what a large language model is in simple terms"
result = get_ai_response(prompt)
print(result)
Why: This function sends a message to the AI model and returns its response. We're using the gpt-4 model, which is one of the more advanced models available.
Step 6: Run Your Script
Save your Python file and run it in your terminal:
python ai_demo.py
You should see a response from the AI model explaining large language models in simple terms.
Why: Running the script demonstrates how easy it is to integrate AI into your applications and shows that the technology is accessible to developers.
Step 7: Experiment with Different Models
Try changing the model name in your function to see how different models respond:
response = client.chat.completions.create(
model="gpt-3.5-turbo", # Try changing this to different models
messages=[
{"role": "user", "content": prompt}
],
max_tokens=150
)
Some models include gpt-4, gpt-3.5-turbo, and claude-3-opus (for Anthropic models).
Why: Different models have different capabilities and prices. Understanding how to switch between them helps you optimize for both performance and cost.
Step 8: Handle Errors Gracefully
Update your function to handle potential errors:
import openai
def get_ai_response(prompt):
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": prompt}
],
max_tokens=150
)
return response.choices[0].message.content
except openai.APIError as e:
return f"API Error: {e}"
Why: Error handling is crucial when working with external APIs. It prevents your program from crashing and helps you understand what went wrong.
Summary
In this tutorial, we've learned how to set up a Python environment, get an API key, and make calls to large language models. We've seen how easy it is to integrate AI into your projects, even though the article discusses how corporate adoption of top-tier models like Fable 5 is slow. This hands-on experience gives you the foundation to explore more advanced AI applications, understand the cost considerations, and appreciate why companies might be cautious about investing in the most expensive models without clear returns on investment.



