LoopAI-103

Exercise - Integrate an AI agent with Foundry IQ

Unit 6 of 7 · Module 10: Build knowledge-enhanced AI agents with Foundry IQ

Before you start

Short explainers for ideas this lesson builds on. Watch these first if they're new to you.

0 of 1 watched · about 2 min
Read this lesson

The plan

In this lab, you build a product expert for Wrenfield, an outdoor gear company, that answers from three product guides: tents, backpacks, and camping accessories. You'll need an Azure subscription, VS Code, Python, and Git, and about an hour. It comes in two parts: first the portal, where you build the agent and its knowledge base, then the code, where your own app talks to that agent. Picture a ranger's hut at a campsite at dusk, with the run sheet pinned to the noticeboard. Let's read it together.

Part one: the portal

In the Foundry portal, create a project, then create an agent, which deploys a default model for you. Give it instructions: always search the knowledge base for product questions, give accurate details, cite the sources, and say so clearly when the knowledge base doesn't have the answer. Then connect Foundry IQ: create an Azure AI Search resource, on the free tier if it's available. In the Azure portal, create a storage account, make a container called wrenfield products, and upload the three product guides. Back in Foundry, create a knowledge base with a Blob Storage knowledge source pointed at that container, with an embedding model and a chat model, and wait until the source shows active. Test the agent in the playground with a few product questions, and check that the answers come from the guides. Finally, in VS Code, open the agent in Foundry Toolkit's Agent Builder, and set its knowledge base tool to ask for approval, so your app can review every lookup.

Part two: your own app

Now the code, in agent client dot py. You connect to the project with a credential and the project client, get an OpenAI client from it, find your agent by name, and start a conversation. Each time the user types, you add their message to the conversation, and ask for a response with an agent reference. Because the agent needs approval, the response may come back with approval requests instead of an answer. So the code loops: for each request, it shows which tool wants to run and with what arguments, asks you yes or no, and sends your decisions back as approval responses, then asks for a new response, until nothing is waiting. Run it, approve the lookups, and ask about tents, backpacks, and accessories, then a follow-up that depends on the last answer, to see the conversation keep its context. When you're finished, delete the resource group, so nothing keeps costing money. You can practice every step here first, in the guided lab and the coding exercise.

Facts from Microsoft's documentation: What is Foundry IQ?; Connect Agents to Foundry IQ Knowledge Bases; What is a Knowledge Source? (CC BY 4.0), rewritten in Loop's own words and examples. Narration, animation, and quiz by Loop. The coding lab is based on Microsoft's lab files for the agent exercises (MicrosoftLearning/mslearn-ai-agents, MIT License; notice). Microsoft, Azure, and Microsoft Foundry are trademarks of Microsoft. Loop is independent and not affiliated with or endorsed by Microsoft.