{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "4b2cf5f5", "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "import json\n", "import os\n", "from typing import Annotated, Any, Never\n", "\n", "from agent_framework import (\n", " AgentExecutor,\n", " AgentExecutorRequest,\n", " AgentExecutorResponse,\n", " Message,\n", " WorkflowBuilder,\n", " WorkflowContext,\n", " executor,\n", " tool,\n", ")\n", "from agent_framework.azure import AzureAIProjectAgentProvider\n", "from azure.identity import AzureCliCredential\n", "from dotenv import load_dotenv\n", "from IPython.display import HTML, display\n", "from pydantic import BaseModel\n", "\n", "print(\"✅ All imports successful!\")" ] }, { "cell_type": "markdown", "id": "001c224e", "metadata": {}, "source": [ "## گام ۱: تعریف مدلهای Pydantic برای خروجیهای ساختار یافته\n", "\n", "این مدلها **طرح**ی را که عاملها بازمیگردانند تعریف میکنند. استفاده از `response_format` همراه با Pydantic تضمین میکند:\n", "- ✅ استخراج دادهها با نوع ایمن\n", "- ✅ اعتبارسنجی خودکار\n", "- ✅ بدون خطاهای تجزیه از پاسخهای متنی آزاد\n", "- ✅ مسیریابی شرطی آسان بر اساس فیلدها\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6c2ef582", "metadata": {}, "outputs": [], "source": [ "class BookingCheckResult(BaseModel):\n", " \"\"\"Result from checking hotel availability at a destination.\"\"\"\n", "\n", " destination: str\n", " has_availability: bool\n", " message: str\n", "\n", "\n", "class AlternativeResult(BaseModel):\n", " \"\"\"Suggested alternative destination when no rooms available.\"\"\"\n", "\n", " alternative_destination: str\n", " reason: str\n", "\n", "\n", "class BookingConfirmation(BaseModel):\n", " \"\"\"Booking suggestion when rooms are available.\"\"\"\n", "\n", " destination: str\n", " action: str\n", " message: str\n", "\n", "\n", "print(\"✅ Pydantic models defined:\")\n", "print(\" - BookingCheckResult (availability check)\")\n", "print(\" - AlternativeResult (alternative suggestion)\")\n", "print(\" - BookingConfirmation (booking confirmation)\")" ] }, { "cell_type": "markdown", "id": "48423ecc", "metadata": {}, "source": [ "## مرحله ۲: ایجاد ابزار رزرو هتل\n", "\n", "این ابزار همان چیزی است که **availability_agent** برای بررسی در دسترس بودن اتاقها فراخوانی خواهد کرد. ما از دکوراتور `@ai_function` استفاده میکنیم تا:\n", "- یک تابع پایتون را به ابزاری قابل فراخوانی توسط هوش مصنوعی تبدیل کنیم\n", "- بهطور خودکار طرحواره JSON برای LLM ایجاد کنیم\n", "- اعتبارسنجی پارامترها را مدیریت کنیم\n", "- اجازه فراخوانی خودکار توسط عوامل را فعال کنیم\n", "\n", "برای این دموی آزمایشی:\n", "- **استکهلم، سیاتل، توکیو، لندن، آمستردام** → دارای اتاق ✅\n", "- **تمام شهرهای دیگر** → بدون اتاق ❌\n" ] }, { "cell_type": "code", "execution_count": null, "id": "aad7e7ec", "metadata": {}, "outputs": [], "source": [ "@tool(description=\"Check hotel room availability for a destination city\")\n", "def hotel_booking(destination: Annotated[str, \"The destination city to check for hotel rooms\"]) -> str:\n", " \"\"\"\n", " Simulates checking hotel room availability.\n", "\n", " Returns JSON string with availability status.\n", " \"\"\"\n", " display(\n", " HTML(f\"\"\"\n", "
\n",
" Conditional Routing:
\n",
" • If NO availability → alternative_agent → display_result
\n",
" • If availability → booking_agent → display_result\n",
"
Expected workflow path: availability_agent → alternative_agent → display_result
\n", "Status: ❌ No rooms in Paris
\n", "Alternative Suggestion: 🏨 {result_paris.alternative_destination}
\n", "Reason: {result_paris.reason}
\n", "Expected workflow path: availability_agent → booking_agent → display_result
\n", "Status: ✅ Rooms Available!
\n", "Destination: 🏨 {result_stockholm.destination}
\n", "Action: {result_stockholm.action}
\n", "Message: {result_stockholm.message}
\n", "