Create Your First Agent
In this quickstart, we’ll walk through the process of creating your very first AI agent using the Microsoft Foundry Agent Service.
By the end, you’ll have a simple agent running locally that you can interact with in real time.
Login to Azure
Before you can use the Microsoft Foundry Agent Service, you need to sign in to your Azure subscription.
Run the following command and follow the on-screen instructions. Use credentials that have access to your Microsoft Foundry resource:
az login --use-device-codeInstall Required Packages
Next, install the Python packages needed to work with Microsoft Foundry and manage environment variables:
pip install openai
pip install azure-identity
pip install python-dotenv
pip install azure-ai-projects==2.0.0b1Create a Basic Agent
We’ll now create a basic Python script that defines and runs an agent.
- Start by creating a new file called:
agent.pyin the workshop folder
Add Imports to agent.py
These imports bring in the Foundry Projects SDK, environment handling, and helper classes:
import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinitionCreate a .env File
We'll store secrets (such as your project connection string and model deployment name) in an environment file for security and flexibility.
Create a file named .env with the following content:
PROJECT_ENDPOINT="<enter it here>"
MODEL_DEPLOYMENT_NAME="<enter your model deployment name here>"You can find the connection string in the Microsoft Foundry portal, on the homepage of your project. To go to the homepage click on Overview in the Microsoft Foundry portal. The connection string has the following format: https://[name of Foundry resource].services.ai.azure.com/projects/[project name]
Load the .env File
Load environment variables into your script by adding this line to agent.py:
load_dotenv()Create an AIProjectClient Instance
This client connects your script to the Microsoft Foundry service using the connection string and your Azure credentials.
project_client = AIProjectClient(
endpoint=os.environ["PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)Get the OpenAI Client
Get the OpenAI client from the project client to interact with conversations:
openai_client = project_client.get_openai_client()Create the Agent
Now, let's create the agent itself using the create_version method with a PromptAgentDefinition:
agent = project_client.agents.create_version(
agent_name="hello-world-agent",
definition=PromptAgentDefinition(
model=os.environ["MODEL_DEPLOYMENT_NAME"],
instructions="You are a helpful assistant that can answer questions about pizza ordering.",
),
)Create a Conversation
Agents interact within conversations. A conversation is like a container that stores all messages exchanged between the user and the agent.
conversation = openai_client.conversations.create()
print(f"Created conversation (id: {conversation.id})")Chat with the Agent
This loop lets you send messages to the agent and receive responses. Type into the terminal, and the agent will respond.
while True:
# Get the user input
user_input = input("You: ")
if user_input.lower() in ["exit", "quit"]:
print("Exiting the chat.")
break
# Get the agent response
response = openai_client.responses.create(
conversation=conversation.id,
input=user_input,
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
)
# Print the agent response
print(f"Assistant: {response.output_text}")Run the Agent
Finally, run the Python script:
python agent.pyYou can now chat with your agent directly in the terminal. Type exit or quit to stop the conversation.
Recap
In this quickstart, you have:
- Logged in to Azure
- Retrieved a connection string
- Separated secrets from code using
.env - Created a basic agent with the Microsoft Foundry Agent Service
- Started a conversation with the agent
- Interacted with the agent using the OpenAI client