{ "cells": [ { "cell_type": "markdown", "id": "374051ac", "metadata": {}, "source": [ "# πŸ” Exploring Agentic Frameworks - Basic Agent (Python)\n", "\n", "## πŸ“‹ Learning Objectives\n", "\n", "This notebook explores the fundamental concepts of the Microsoft Agent Framework through a basic agent implementation. You'll learn core agentic patterns and understand how intelligent agents work under the hood.\n", "\n", "**What You'll Discover:**\n", "- πŸ—οΈ **Agent Architecture**: Understanding the basic structure of AI agents\n", "- πŸ› οΈ **Tool Integration**: How agents use external functions to extend capabilities \n", "- πŸ’¬ **Conversation Flow**: Managing multi-turn conversations and context\n", "- πŸ”§ **Configuration Patterns**: Best practices for agent setup and management\n", "\n", "## 🎯 Key Concepts Covered\n", "\n", "### Agentic Framework Principles\n", "- **Autonomy**: How agents make independent decisions\n", "- **Reactivity**: Responding to environmental changes and user inputs\n", "- **Proactivity**: Taking initiative based on goals and context\n", "- **Social Ability**: Interacting through natural language\n", "\n", "### Technical Components\n", "- **ChatAgent**: Core agent orchestration and conversation management\n", "- **Tool Functions**: Extending agent capabilities with custom functions\n", "- **OpenAI Integration**: Leveraging language models through standardized APIs\n", "- **Environment Management**: Secure configuration and credential handling\n", "\n", "## βš™οΈ Prerequisites & Setup\n", "\n", "**Required Dependencies:**\n", "```bash\n", "\n", "pip install agent-framework-core -U\n", "```\n", "\n", "**Environment Configuration (.env file):**\n", "```env\n", "GITHUB_TOKEN=your_github_personal_access_token\n", "GITHUB_ENDPOINT=https://models.inference.ai.azure.com\n", "GITHUB_MODEL_ID=gpt-4o-mini\n", "```\n", "\n", "## πŸ”§ Technical Stack\n", "\n", "**Core Technologies:**\n", "- Microsoft Agent Framework (Python)\n", "- GitHub Models API integration\n", "- OpenAI-compatible client patterns\n", "- Environment-based configuration\n", "\n", "**Agent Capabilities:**\n", "- Natural language understanding and generation\n", "- Function calling and tool usage\n", "- Context-aware responses\n", "- Extensible architecture\n", "\n", "## πŸ“š Framework Comparison\n", "\n", "This example demonstrates the Microsoft Agent Framework approach compared to other agentic frameworks:\n", "\n", "| Feature | Microsoft Agent Framework | Other Frameworks |\n", "|---------|-------------------------|------------------|\n", "| **Integration** | Native Microsoft ecosystem | Varied compatibility |\n", "| **Simplicity** | Clean, intuitive API | Often complex setup |\n", "| **Extensibility** | Easy tool integration | Framework-dependent |\n", "| **Enterprise Ready** | Built for production | Varies by framework |\n", "\n", "## πŸš€ Getting Started\n", "\n", "Follow the cells below to build your first basic agent and understand foundational agentic concepts!" ] }, { "cell_type": "code", "execution_count": 1, "id": "b97bee6f", "metadata": {}, "outputs": [], "source": [ "# ! pip install -r ../../../Installation/requirements.txt -U" ] }, { "cell_type": "code", "execution_count": 2, "id": "c0df8a52", "metadata": {}, "outputs": [], "source": [ "# πŸ“š Exploring Agentic Frameworks - Basic Agent Example\n", "# This example demonstrates core concepts of the Microsoft Agent Framework\n", "\n", "# πŸ“¦ Import Required Libraries\n", "import os # For environment variable access\n", "from random import randint # For generating random selections\n", "\n", "from dotenv import load_dotenv # For loading .env configuration files" ] }, { "cell_type": "code", "execution_count": 3, "id": "151e0314", "metadata": {}, "outputs": [], "source": [ "# πŸ€– Import Core Agent Framework Components\n", "# ChatAgent: The main conversational agent class\n", "# OpenAIChatClient: Client for connecting to OpenAI-compatible APIs\n", "from agent_framework import Agent\n", "from agent_framework.openai import OpenAIChatCompletionClient" ] }, { "cell_type": "code", "execution_count": 4, "id": "a6141584", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# πŸ”§ Initialize Environment Configuration\n", "# Load environment variables from .env file\n", "# This enables secure storage of API keys and endpoints\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 5, "id": "a6507f83", "metadata": {}, "outputs": [], "source": [ "# πŸ› οΈ Tool Function: Random Destination Generator\n", "# This demonstrates how to create tools that agents can use\n", "# Tools extend the agent's capabilities beyond just conversation\n", "def get_random_destination() -> str:\n", " \"\"\"Get a random vacation destination.\n", " \n", " This function serves as a tool that the agent can call when it needs\n", " to suggest a random destination for travel planning.\n", " \n", " Returns:\n", " str: A randomly selected destination from the predefined list\n", " \"\"\"\n", " # Curated list of popular vacation destinations worldwide\n", " destinations = [\n", " \"Barcelona, Spain\",\n", " \"Paris, France\", \n", " \"Berlin, Germany\",\n", " \"Tokyo, Japan\",\n", " \"Sydney, Australia\",\n", " \"New York, USA\",\n", " \"Cairo, Egypt\",\n", " \"Cape Town, South Africa\",\n", " \"Rio de Janeiro, Brazil\",\n", " \"Bali, Indonesia\"\n", " ]\n", " # Return a randomly selected destination\n", " return destinations[randint(0, len(destinations) - 1)]" ] }, { "cell_type": "code", "execution_count": null, "id": "5d4f0568", "metadata": {}, "outputs": [], "source": [ "# πŸ”— Create OpenAI Chat Client\n", "# This demonstrates how to connect to GitHub Models (OpenAI-compatible API)\n", "# The client handles all communication with the AI model\n", "openai_chat_client = OpenAIChatCompletionClient(\n", " base_url=os.environ.get(\"GITHUB_ENDPOINT\"), # API endpoint URL\n", " api_key=os.environ.get(\"GITHUB_TOKEN\"), # Authentication token\n", " modelO=os.environ.get(\"GITHUB_MODEL_ID\") # Model identifier (e.g., gpt-4o-mini)\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "id": "cf5a4800", "metadata": {}, "outputs": [], "source": [ "# πŸ€– Create the Basic Agent\n", "# This demonstrates the core agent creation pattern in the framework\n", "# Key components: chat client, instructions, and tools\n", "agent = Agent(\n", " client=openai_chat_client, # The AI model client for generating responses\n", " instructions=\"You are a helpful AI Agent that can help plan vacations for customers at random destinations.\", # System prompt defining agent behavior\n", " tools=[get_random_destination] # List of available tools the agent can use\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "be18ac4f", "metadata": {}, "outputs": [], "source": [ "session = agent.create_session()" ] }, { "cell_type": "code", "execution_count": 9, "id": "772e9481", "metadata": {}, "outputs": [], "source": [ "response1 = await agent.run(\"Plan me a day trip\",session= session)" ] }, { "cell_type": "code", "execution_count": 10, "id": "a731b547", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Travel plan:\n", "How about a day trip to Berlin, Germany? \n", "\n", "Here's a suggested itinerary for your day:\n", "\n", "- Morning: Start your day with a visit to the Brandenburg Gate, an iconic symbol of Berlin. From there, walk to the Reichstag Building, where you can take a tour and enjoy views of the city from its glass dome.\n", "\n", "- Late Morning: Head to the Berlin Wall Memorial to learn about the history of the wall and see some preserved sections.\n", "\n", "- Lunch: Enjoy a meal at a local restaurant serving traditional German cuisineβ€”try some currywurst or schnitzel.\n", "\n", "- Afternoon: Visit Museum Island, home to several world-class museums including the Pergamon Museum and the Altes Museum.\n", "\n", "- Late Afternoon: Stroll through Alexanderplatz, and if time permits, take a ride up the Fernsehturm (TV Tower) for panoramic views.\n", "\n", "- Evening: End your day with dinner in the vibrant district of Kreuzberg, known for its diverse food scene and lively atmosphere.\n", "\n", "Would you like me to help with specific recommendations on places to eat or how to get around?\n" ] } ], "source": [ "\n", "last_message = response1.messages[-1]\n", "text_content = last_message.contents[0].text\n", "print(\"Travel plan:\")\n", "print(text_content)" ] }, { "cell_type": "code", "execution_count": 11, "id": "7d3fe00a", "metadata": {}, "outputs": [], "source": [ "response2 = await agent.run(\"I don't like that destination. Plan me another vacation.\",session= session)" ] }, { "cell_type": "code", "execution_count": 12, "id": "ba50c92d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Change plan:\n", "Let's plan a day trip to Cairo, Egypt!\n", "\n", "Here's a suggested itinerary for your day:\n", "\n", "- Morning: Start your day early by visiting the Great Pyramids of Giza and the Sphinx. These ancient wonders are a must-see and offer amazing photo opportunities.\n", "\n", "- Late Morning: Head to the Egyptian Museum in Tahrir Square to explore its vast collection of artifacts including treasures from King Tutankhamun's tomb.\n", "\n", "- Lunch: Try some traditional Egyptian dishes such as koshari or falafel at a nearby local restaurant.\n", "\n", "- Afternoon: Visit the historic Islamic Cairo district. Explore the Khan El Khalili bazaar, where you can shop for souvenirs, spices, and local crafts.\n", "\n", "- Late Afternoon: Stop by the Al-Azhar Mosque or the Citadel of Salah El-Din for a dose of history and stunning city views.\n", "\n", "- Evening: Enjoy a relaxing dinner cruise on the Nile River with traditional music and local cuisine.\n", "\n", "Would you like suggestions on places to eat or how to get around in Cairo?\n" ] } ], "source": [ "last_message = response2.messages[-1]\n", "text_content = last_message.contents[0].text\n", "print(\"Change plan:\")\n", "print(text_content)" ] } ], "metadata": { "kernelspec": { "display_name": "agentdev", "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.10" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 5 }