Setting up the environment and model
After finishing this section, your computer will have a Python environment with dependencies installed, a model that can call tools, and you will know how to use three environment variables to switch between different models.
1. Python environment
Python 3.10 or later is required. Create a virtual environment and install two libraries:
python3 -m venv .venv
source .venv/bin/activate # Windows:.venv\Scripts\activate
pip install openai mcpopenai: for calling models. Although it is named openai, it can connect to any "OpenAI-compatible" API, and most domestic model services are compatible.mcp: used in the third stage for writing MCP servers and clients.
The versions we use: openai 3.57.0, mcp 2.3.0.
2. Choose a model
An Agent requires a model that supports tool calling (tool calling / function calling). Two paths, pick one:
Look it upI want to use...
Click one to see.
All the code in this lesson reads its configuration from three environment variables, with defaults pointing to local Ollama:
| Environment variable | Local Ollama | Cloud |
|---|---|---|
AGENT_BASE_URL | http://localhost:11434/v1 (default) | The API endpoint given by the provider |
AGENT_API_KEY | ollama (default, any value works) | Your API Key |
AGENT_MODEL | qwen3:8b (default) | The provider's model name |
When using a cloud model, set them in the terminal first (macOS / Linux):
export AGENT_BASE_URL="服务商给的地址"
export AGENT_API_KEY="你的 key"
export AGENT_MODEL="模型名"Never write the API Key into your code, and never commit it to Git.
3. First confirm the connection works
Save the following as hello.py and run it:
import os
from openai import OpenAI
client = OpenAI(base_url=os.environ.get("AGENT_BASE_URL", "http://localhost:11434/v1"),
api_key=os.environ.get("AGENT_API_KEY", "ollama"))
resp = client.chat.completions.create(
model=os.environ.get("AGENT_MODEL", "qwen3:8b"),
messages=[{"role": "user", "content": "用一句话介绍你自己"}])
print(resp.choices[0].message.content)You should see the model reply with a self-introduction.
error Using SOCKS proxy, but the 'socksio' package is not installed
Your terminal has a SOCKS proxy set (an environment variable like ALL_PROXY), and the openai library will try to go through the proxy. Two options: if you are connecting to a domestic service or local Ollama and don't need a proxy, unset it before running, for example env -u ALL_PROXY -u all_proxy python hello.py; if you really need the proxy, then pip install "httpx[socks]". We ran into this error ourselves while writing this course.
Quiz timeWhy can the openai library also call DeepSeek, Qwen, or a local Ollama?
Quick check
pip listshows openai and mcp- I have chosen a model and know how to fill in the three environment variables
hello.pyprinted the model's reply
In the next section, hands-on: Have the model call a function.