|
| 1 | +# Build a Simple Chat UI for Your AI Agent |
| 2 | + |
| 3 | +In this lab, you will learn how to integrate an AI Agent into your own application. |
| 4 | + |
| 5 | +The example app is built using a .NET backend API and a React-based chat UI. It walks through a basic scenario of a **Technical Support Agent** that can answer user questions to demonstrate the core concepts. |
| 6 | + |
| 7 | +The goal is to provide a view of the end-to-end data flow between the frontend, backend, and Azure AI services. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## How It Works |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- The AI agent is set up in Azure AI Foundry. It can answer IT questions, help with support tickets, and more. |
| 16 | +- The .NET backend API connects the chat website to the agent and handles the following requests: |
| 17 | + - Calls the AI agent to answer user prompts |
| 18 | + - Create Agent and chat threads |
| 19 | + - Get chat history for the thread |
| 20 | +- The agent has tools (stubs) to do things like set up Office 365 accounts or fix network issues. |
| 21 | +- The React frontend is a simple chat page. It sends your messages to the backend and shows the full chat history of the thread. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Build the Application |
| 26 | + |
| 27 | +1. The code for the backend and frontend is in the `src/` folder of the repository. |
| 28 | + |
| 29 | + Refer to the file `/src/backend/ai-agent-api/Controllers/ChatController.cs` for the details of the API endpoints that the frontend uses. |
| 30 | + |
| 31 | + It has the following endpoints: |
| 32 | + |
| 33 | + - `GET /api/agent` |
| 34 | + Gets information about the chat agent (name, display name, description). |
| 35 | + |
| 36 | + - `POST /api/chat/send` |
| 37 | + Sends a chat message to the agent and returns the agent thread id. |
| 38 | + |
| 39 | + - `GET /api/chat/history?agentId={agentId}&threadId={threadId}` |
| 40 | + Retrieves the chat message history for a given thread. |
| 41 | + |
| 42 | +2. Update the `TechSupportAgentConfig` class. |
| 43 | + |
| 44 | + The file `src/backend/ai-agent-api/Agents/TechSupportAgentConfig.cs` already exists. |
| 45 | + |
| 46 | + Add or update the following method overrides to define the agent's display name, description, and system message: |
| 47 | + |
| 48 | + !!! info |
| 49 | + The setup uses a simple Factory pattern to create the agent configuration. In real applicaitons you would store the agent configuration in a database or a configuration file and load it dynamically. |
| 50 | + |
| 51 | + ```csharp |
| 52 | + public override string GetAgentDisplayName() |
| 53 | + { |
| 54 | + return "Technical Support Agent"; |
| 55 | + } |
| 56 | + |
| 57 | + public override string GetDescription() |
| 58 | + { |
| 59 | + return "This agent provides IT and technical support for the company. " + |
| 60 | + "It assists users with troubleshooting, technical queries, and problem resolution. " + |
| 61 | + "The agent utilizes the `TechSupportTools` to effectively address and resolve technical issues."; |
| 62 | + } |
| 63 | + |
| 64 | + public override string GetSystemMessage() |
| 65 | + { |
| 66 | + return "You are a technical support agent. " + |
| 67 | + "Your role is to assist users with IT and technical issues, providing solutions and troubleshooting steps. " + |
| 68 | + "Use the tools available to you to resolve problems efficiently."; |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +3. Review the source code in the file `src/backend/ai-agent-api/Services/AzureAIAgentService.cs` to understand how the backend API interacts with the Azure AI Agent. |
| 73 | + |
| 74 | + !!! warning |
| 75 | + Some API methods from the Semantic Kernel SDK are in preview and may change in future releases. |
| 76 | + |
| 77 | + Check the official docs for updates. |
| 78 | + |
| 79 | +4. Build the backend API to ensure it compiles correctly. |
| 80 | + If the build completes without errors, you are ready to proceed to deployment. |
| 81 | + |
| 82 | + ```bash |
| 83 | + dotnet build src/backend/ai-agent-api/AIAgent.API.sln |
| 84 | + ``` |
| 85 | + |
| 86 | +## Deploy the Application |
| 87 | + |
| 88 | +5. The Azure resources were already created in a previous lab (with `azd up`). |
| 89 | +6. Run the following command. This will build the backend and frontend applications and deploy them to Azure. |
| 90 | + |
| 91 | + ```bash |
| 92 | + azd deploy |
| 93 | + ``` |
| 94 | + |
| 95 | + !!! note |
| 96 | + If you want to run the app locally, you can use `dotnet run` for the backend and `npm start` for the frontend. Make sure you have the required environment variables set up. |
| 97 | + |
| 98 | +7. The backend and the frontend applications are hosted in Azure App Service. |
| 99 | + |
| 100 | + You can find the URLs in the Azure portal or in the `azd` output. |
| 101 | + |
| 102 | +  |
| 103 | + |
| 104 | +8. You can find the url of the chat UI in the `.env` file with the variable `FRONTEND_APP_URL`. |
| 105 | + |
| 106 | + Copy the URL and open it in your browser to access the chat UI. |
| 107 | + |
| 108 | +  |
| 109 | + |
| 110 | + The Agent has access to the tools defined in the `src/backend/ai-agent-api/KernelTools/TechSupportTools.cs` file. These methods are stubs and simulate the actions that the agent can perform for an IT support scenario. |
| 111 | + |
| 112 | +9. Now start chatting with the agent! |
| 113 | + |
| 114 | + You can ask the agent questions like: |
| 115 | + |
| 116 | + - Reset the password for John Doe |
| 117 | + - Create a new Office 365 account for John Doe |
| 118 | + - Send a Welcome email to John Doe |
| 119 | + |
| 120 | +  |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Code Challenges |
| 125 | + |
| 126 | +Test your understanding and extend the solution with these challenges: |
| 127 | + |
| 128 | +1. **Add a Knowledge Tool** |
| 129 | + Enhance the agent by integrating a file search tool. This tool should allow the agent to read a file and answer user questions based on its content. |
| 130 | + |
| 131 | +2. **Execute Tasks via OpenAI Spec** |
| 132 | + Use the OpenAI Spec tool to execute tasks by calling external APIs. For example, expose an API that allows the agent to create a new support ticket in an external system. |
| 133 | + |
| 134 | +3. **Create a New Agent Scenario** |
| 135 | + Design and implement a new agent for a different use case. |
| 136 | + |
| 137 | + For example, integrate the `ContosoInventoryAgent` we built in a previous lab. |
0 commit comments