4.1 Environment Preparation and Basic Tool Definition
Before we start building, we need to set up the development environment and define some basic components. This will help us avoid repetitive work and focus more on core logic when implementing different paradigms later.
4.1.1 Installing Dependencies
The practical part of this book will mainly use the Python language, and Python 3.10 or higher is recommended. First, please ensure you have installed the openai library for interacting with large language models, and the python-dotenv library for securely managing our API keys.
Run the following command in your terminal:
pip install openai python-dotenv4.1.2 Configuring API Keys
To make our code more universal, we will uniformly configure model service-related information (model ID, API key, service address) in environment variables.
- In your project root directory, create a file named
.env. - In this file, add the following content. You can point it to OpenAI's official service or any local/third-party service compatible with the OpenAI interface according to your needs.
- If you really don't know how to obtain it, you can refer to Environment Configuration.
# .env file
LLM_API_KEY="YOUR-API-KEY"
LLM_MODEL_ID="YOUR-MODEL"
LLM_BASE_URL="YOUR-URL"Our code will automatically load these configurations from this file.
4.1.3 Encapsulating Basic LLM Call Functions
To make the code structure clearer and more reusable, let's define a dedicated LLM client class. This class will encapsulate all details of interacting with model services, allowing our main logic to focus more on agent construction.
import os
from openai import OpenAI
from dotenv import load_dotenv
from typing import List, Dict
# Load environment variables from .env file
load_dotenv()
class HelloAgentsLLM:
"""
A customized LLM client for the book "Hello Agents".
It is used to call any service compatible with the OpenAI interface and uses streaming responses by default.
"""
def __init__(self, model: str = None, apiKey: str = None, baseUrl: str = None, timeout: int = None):
"""
Initialize the client. Prioritize passed parameters; if not provided, load from environment variables.
"""
self.model = model or os.getenv("LLM_MODEL_ID")
apiKey = apiKey or os.getenv("LLM_API_KEY")
baseUrl = baseUrl or os.getenv("LLM_BASE_URL")
timeout = timeout or int(os.getenv("LLM_TIMEOUT", 60))
if not all([self.model, apiKey, baseUrl]):
raise ValueError("Model ID, API key, and service address must be provided or defined in the .env file.")
self.client = OpenAI(api_key=apiKey, base_url=baseUrl, timeout=timeout)
def think(self, messages: List[Dict[str, str]], temperature: float = 0) -> str:
"""
Call the large language model to think and return its response.
"""
print(f"🧠 Calling {self.model} model...")
try:
response = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=temperature,
stream=True,
)
# Handle streaming response
print("✅ Large language model response successful:")
collected_content = []
for chunk in response:
if not chunk.choices:
continue
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)
collected_content.append(content)
print() # Newline after streaming output ends
return "".join(collected_content)
except Exception as e:
print(f"❌ Error occurred when calling LLM API: {e}")
return None
# --- Client Usage Example ---
if __name__ == '__main__':
try:
llmClient = HelloAgentsLLM()
exampleMessages = [
{"role": "system", "content": "You are a helpful assistant that writes Python code."},
{"role": "user", "content": "Write a quicksort algorithm"}
]
print("--- Calling LLM ---")
responseText = llmClient.think(exampleMessages)
if responseText:
print("\n\n--- Complete Model Response ---")
print(responseText)
except ValueError as e:
print(e)
>>>
--- Calling LLM ---
🧠 Calling xxxxxx model...
✅ Large language model response successful:
Quicksort is a very efficient sorting algorithm...