本課將涵蓋如何執行本課程的程式範例。
在開始複製你的 repo 之前,請加入 AI Agents For Beginners Discord channel 以取得任何設定協助、提出課程相關問題,或與其他學習者連繫。
首先,請複製或 Fork GitHub 儲存庫。這會建立你自己的課程教材版本,讓你可以執行、測試並微調程式碼!
This can be done by clicking the link to 分叉此儲存庫
You should now have your own forked version of this course in the following link:

完整的儲存庫在下載完整的歷史紀錄與所有檔案時可能很大(約 3 GB)。如果你只參加工作坊或只需要幾個課程資料夾,淺層複製(或稀疏複製)可透過截短歷史紀錄和/或跳過 blobs 來避免大部分下載。
Replace <your-username> in the below commands with your fork URL (or the upstream URL if you prefer).
To clone only the latest commit history (small download):
git clone --depth 1 https://github.com/<your-username>/ai-agents-for-beginners.git
To clone a specific branch:
git clone --depth 1 --branch <branch-name> https://github.com/<your-username>/ai-agents-for-beginners.git
This uses partial clone and sparse-checkout (requires Git 2.25+ and recommended modern Git with partial clone support):
git clone --depth 1 --filter=blob:none --sparse https://github.com/<your-username>/ai-agents-for-beginners.git
Traverse into the repo folder:
cd ai-agents-for-beginners
Then specify which folders you want (example below shows two folders):
git sparse-checkout set 00-course-setup 01-intro-to-ai-agents
After cloning and verifying the files, if you only need files and want to free space (no git history), please delete the repository metadata (💀irreversible — you will lose all Git functionality: no commits, pulls, pushes, or history access).
# zsh/bash
rm -rf .git
# PowerShell
Remove-Item -Recurse -Force .git
透過 GitHub UI 為此儲存庫建立新的 Codespace。
本課程提供一系列可執行的 Jupyter 筆記本,讓你獲得構建 AI Agents 的實作經驗。
The code samples use Microsoft Agent Framework (MAF) with the AzureAIProjectAgentProvider, which connects to Azure AI Agent Service V2 (the Responses API) through Microsoft Foundry.
All Python notebooks are labelled *-python-agent-framework.ipynb.
注意:如果你還沒安裝 Python 3.12,請確保安裝。然後使用 python3.12 建立你的 venv,以確保從 requirements.txt 安裝正確的版本。
範例
Create Python venv directory:
python -m venv venv
Then activate venv environment for:
# zsh/bash
source venv/bin/activate
# Command Prompt for Windows
venv\Scripts\activate
.NET 10+: For the sample codes using .NET, ensure you install .NET 10 SDK or later. Then, check your installed .NET SDK version:
dotnet --list-sdks
gpt-4o). See Step 1 below.We have included a requirements.txt file in the root of this repository that contains all the required Python packages to run the code samples.
You can install them by running the following command in your terminal at the root of the repository:
pip install -r requirements.txt
We recommend creating a Python virtual environment to avoid any conflicts and issues.
請確認在 VSCode 中使用正確版本的 Python。
You need an Azure AI Foundry hub and project with a deployed model to run the notebooks.
gpt-4o)。From your project in the Microsoft Foundry portal:

gpt-4o)。az login 登入 AzureAll notebooks use AzureCliCredential for authentication — no API keys to manage. This requires you to be signed in via the Azure CLI.
安裝 Azure CLI(如果尚未安裝):aka.ms/installazurecli
登入,執行:
az login
或者如果你在沒有瀏覽器的遠端/Codespace 環境:
az login --use-device-code
選擇你的訂閱(若有提示)— 選擇包含你的 Foundry 專案的那一個。
驗證 你已登入:
az account show
為什麼要
az login? 筆記本使用azure-identity套件中的AzureCliCredential來驗證。這表示你的 Azure CLI 會話會提供認證 — 無需在.env檔案中放置 API 金鑰或祕密。這是一項 安全性最佳實務。
.env 檔案複製範例檔案:
# zsh/bash
cp .env.example .env
# PowerShell
Copy-Item .env.example .env
Open .env and fill in these two values:
AZURE_AI_PROJECT_ENDPOINT=https://<your-project>.services.ai.azure.com/api/projects/<your-project-id>
AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4o
| 變數 | 在哪裡找到 |
|---|---|
AZURE_AI_PROJECT_ENDPOINT |
Foundry 入口網站 → 你的專案 → Overview 頁面 |
AZURE_AI_MODEL_DEPLOYMENT_NAME |
Foundry 入口網站 → Models + Endpoints → 你已部署模型的名稱 |
That’s it for most lessons! The notebooks will authenticate automatically through your az login session.
pip install -r requirements.txt
We recommend running this inside the virtual environment you created earlier.
第 5 課使用 Azure AI Search 進行檢索增強生成。如果你打算執行該課程,請將以下變數新增到你的 .env 檔案:
| 變數 | 在哪裡找到 |
|---|---|
AZURE_SEARCH_SERVICE_ENDPOINT |
Azure 入口網站 → 你的 Azure AI Search 資源 → Overview → URL |
AZURE_SEARCH_API_KEY |
Azure 入口網站 → 你的 Azure AI Search 資源 → Settings → Keys → 主要管理金鑰 |
第 6 與第 8 課的某些筆記本使用 GitHub Models(而不是 Azure AI Foundry)。若你要執行那些範例,請將以下變數新增到你的 .env 檔案:
| 變數 | 在哪裡找到 |
|---|---|
GITHUB_TOKEN |
GitHub → Settings → Developer settings → Personal access tokens |
GITHUB_ENDPOINT |
使用 https://models.inference.ai.azure.com(預設值) |
GITHUB_MODEL_ID |
要使用的模型名稱(例如 gpt-4o-mini) |
The conditional workflow notebook in lesson 8 uses Bing grounding via Azure AI Foundry. If you plan to run that sample, add this variable to your .env file:
| 變數 | 在哪裡找到 |
|---|---|
BING_CONNECTION_ID |
Azure AI Foundry 入口網站 → 你的專案 → Management → Connected resources → 你的 Bing 連線 → 複製連線 ID |
If you are on macOS and encounter an error like:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain
This is a known issue with Python on macOS where the system SSL certificates are not automatically trusted. Try the following solutions in order:
選項 1:執行 Python 的 Install Certificates 腳本(建議)
# 將 3.XX 替換為你已安裝的 Python 版本(例如 3.12 或 3.13):
/Applications/Python\ 3.XX/Install\ Certificates.command
選項 2:在你的筆記本中使用 connection_verify=False(僅針對 GitHub Models 筆記本)
In the Lesson 6 notebook (06-building-trustworthy-agents/code_samples/06-system-message-framework.ipynb), a commented-out workaround is already included. Uncomment connection_verify=False when creating the client:
client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
connection_verify=False, # 如果遇到憑證錯誤,請停用 SSL 驗證
)
⚠️ 警告: 停用 SSL 驗證(
connection_verify=False)會透過跳過憑證驗證而降低安全性。僅在開發環境中作為暫時解法使用,切勿在生產環境中使用。
選項 3:安裝並使用 truststore
pip install truststore
Then add the following at the top of your notebook or script before making any network calls:
import truststore
truststore.inject_into_ssl()
If you have any issues running this setup, hop into our Azure AI 社群 Discord or 建立 issue。
你現在已準備好執行本課程的程式碼。祝你在 AI Agents 的世界中學習愉快!
免責聲明: 本文件已使用 AI 翻譯服務 Co-op Translator 進行翻譯。雖然我們力求準確,但自動翻譯可能包含錯誤或不精確之處。原始語言版本應被視為權威來源。對於重要資訊,建議採用專業人工翻譯。我們對因使用本翻譯而導致的任何誤解或曲解概不負責。