209 lines
6.5 KiB
Plaintext
209 lines
6.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f4cf9593",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Lesson 01 - Introduction to AI Agents\n",
|
|
"\n",
|
|
"Welcome to the first lesson in the **AI Agents for Beginners** course!\n",
|
|
"\n",
|
|
"An **AI agent** is a program that uses a large language model (LLM) as its reasoning engine and can take *actions* in the real world — calling APIs, querying databases, or running code — to accomplish a goal on behalf of a user.\n",
|
|
"\n",
|
|
"In this notebook you will build your first agent: a **Travel Agent** that recommends vacation destinations. Along the way you will learn how to:\n",
|
|
"\n",
|
|
"1. Connect to Microsoft Foundry Agent Service using the **Microsoft Agent Framework**.\n",
|
|
"2. Give the agent a **tool** — a plain Python function it can call.\n",
|
|
"3. Run the agent and inspect its response.\n",
|
|
"4. Stream the agent's response token-by-token."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b1e2c3d4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Setup\n",
|
|
"\n",
|
|
"Before running this notebook, make sure you have:\n",
|
|
"\n",
|
|
"1. **A Microsoft Foundry project** with a deployed chat model (e.g. `gpt-4o-mini`).\n",
|
|
"2. **Logged in with the Azure CLI** — run `az login` in your terminal.\n",
|
|
"3. **Set the required environment variables:**\n",
|
|
" - `AZURE_AI_PROJECT_ENDPOINT` — your Microsoft Foundry project endpoint.\n",
|
|
" - `AZURE_AI_MODEL_DEPLOYMENT_NAME` — the name of your deployed model.\n",
|
|
"\n",
|
|
"The cell below installs the Python packages you need."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "fda5fa0a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install agent-framework azure-ai-projects azure-identity -q"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c0df8a52",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import logging\n",
|
|
"logging.getLogger(\"agent_framework.foundry\").setLevel(logging.ERROR)\n",
|
|
"\n",
|
|
"import os\n",
|
|
"import dotenv\n",
|
|
"from agent_framework.foundry import FoundryChatClient\n",
|
|
"from azure.identity import AzureCliCredential\n",
|
|
"from agent_framework import tool\n",
|
|
"\n",
|
|
"dotenv.load_dotenv(dotenv.find_dotenv())\n",
|
|
"\n",
|
|
"endpoint = os.getenv(\"AZURE_AI_PROJECT_ENDPOINT\")\n",
|
|
"model = os.getenv(\"AZURE_AI_MODEL_DEPLOYMENT_NAME\")\n",
|
|
"\n",
|
|
"if not endpoint or not model:\n",
|
|
" raise ValueError(\n",
|
|
" \"Missing required environment variables. \"\n",
|
|
" \"Please set AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME in your .env file.\"\n",
|
|
" )\n",
|
|
"\n",
|
|
"provider = FoundryChatClient(\n",
|
|
" project_endpoint=endpoint,\n",
|
|
" model=model,\n",
|
|
" credential=AzureCliCredential()\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e5f6a7b8",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Creating Your First Agent\n",
|
|
"\n",
|
|
"An agent needs two things:\n",
|
|
"\n",
|
|
"- **Instructions** that tell it *who it is* and *how to behave* (a system prompt).\n",
|
|
"- **Tools** — Python functions decorated with `@tool` that the agent can call to retrieve information or perform actions.\n",
|
|
"\n",
|
|
"Below we define a simple tool that returns a list of popular vacation destinations. The agent will use this tool when a user asks for travel recommendations."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a6507f83",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"@tool(approval_mode=\"never_require\")\n",
|
|
"def get_destinations() -> list[str]:\n",
|
|
" \"\"\"Get a list of popular vacation destinations.\"\"\"\n",
|
|
" return [\n",
|
|
" \"Barcelona\",\n",
|
|
" \"Paris\",\n",
|
|
" \"Berlin\",\n",
|
|
" \"Tokyo\",\n",
|
|
" \"Sydney\",\n",
|
|
" \"New York City\",\n",
|
|
" \"Cairo\",\n",
|
|
" \"Cape Town\",\n",
|
|
" \"Rio de Janeiro\",\n",
|
|
" \"Bali\",\n",
|
|
" ]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "cf5a4800",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent = provider.as_agent(\n",
|
|
" name=\"TravelAgent\",\n",
|
|
" instructions=(\n",
|
|
" \"You are a helpful travel agent. Help users find their perfect vacation \"\n",
|
|
" \"destination based on their preferences. Use the get_destinations tool \"\n",
|
|
" \"to see available destinations.\"\n",
|
|
" ),\n",
|
|
" tools=[get_destinations],\n",
|
|
")\n",
|
|
"\n",
|
|
"response = await agent.run(\n",
|
|
" \"I'm looking for a warm beach destination. What do you recommend?\"\n",
|
|
")\n",
|
|
"print(response)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d9e0f1a2",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Streaming Responses\n",
|
|
"\n",
|
|
"For a more interactive experience you can **stream** the agent's response. Instead of waiting for the full reply, the agent yields text chunks as they are generated. This is especially useful in chat interfaces where you want to display output in real time."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "772e9481",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"async for chunk in agent.run(\n",
|
|
" \"Tell me about Tokyo as a travel destination\", stream=True\n",
|
|
"):\n",
|
|
" print(chunk, end=\"\", flush=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a3b4c5d6",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Summary\n",
|
|
"\n",
|
|
"In this lesson you learned how to:\n",
|
|
"\n",
|
|
"- **Create a provider** that connects to Microsoft Foundry Agent Service via `FoundryChatClient`.\n",
|
|
"- **Define a tool** using the `@tool` decorator so the agent can call your Python functions.\n",
|
|
"- **Run the agent** with a user message and print its response.\n",
|
|
"- **Stream responses** for real-time output.\n",
|
|
"\n",
|
|
"In the next lesson we will explore agentic frameworks in more depth and learn how to give agents more powerful tools and multi-step reasoning capabilities."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.12.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|