Share via


Microsoft Agent Framework Quick-Start Guide

This guide will help you get up and running quickly with a basic agent using Agent Framework and Azure OpenAI.

Prerequisites

Before you begin, ensure you have the following:

Note

Microsoft Agent Framework is supported with all actively supported versions of .NET. For the purposes of this sample, we recommend the .NET 8 SDK or a later version.

Note

This demo uses Azure CLI credentials for authentication. Make sure you're logged in with az login and have access to the Azure OpenAI resource. For more information, see the Azure CLI documentation. It is also possible to replace the AzureCliCredential with an ApiKeyCredential if you have an api key and do not wish to use role based authentication, in which case az login is not required.

Install Packages

Packages will be published to NuGet Gallery | MicrosoftAgentFramework.

First, add the following Microsoft Agent Framework NuGet packages into your application, using the following commands:

dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease

Running a Basic Agent Sample

This sample demonstrates how to create and use a simple AI agent with Azure OpenAI Chat Completion as the backend. It will create a basic agent using AzureOpenAIClient with gpt-4o-mini and custom instructions.

Sample Code

Make sure to replace https://your-resource.openai.azure.com/ with the endpoint of your Azure OpenAI resource.

using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;

AIAgent agent = new AzureOpenAIClient(
  new Uri("https://your-resource.openai.azure.com/"),
  new AzureCliCredential())
    .GetChatClient("gpt-4o-mini")
    .CreateAIAgent(instructions: "You are good at telling jokes.");

Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));

(Optional) Install Nightly Packages

If you need to get a package containing the latest enhancements or fixes, nightly builds of Agent Framework are available at https://github.com/orgs/microsoft/packages?repo_name=agent-framework.

To download nightly builds, follow these steps:

  1. You will need a GitHub account to complete these steps.

  2. Create a GitHub Personal Access Token with the read:packages scope using these instructions.

  3. If your account is part of the Microsoft organization, then you must authorize the Microsoft organization as a single sign-on organization.

    1. Click the "Configure SSO" next to the Personal Access Token you just created and then authorize Microsoft.
  4. Use the following command to add the Microsoft GitHub Packages source to your NuGet configuration:

    dotnet nuget add source --username GITHUBUSERNAME --password GITHUBPERSONALACCESSTOKEN --store-password-in-clear-text --name GitHubMicrosoft "https://nuget.pkg.github.com/microsoft/index.json"
    
  5. Or you can manually create a NuGet.Config file.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        <add key="github" value="https://nuget.pkg.github.com/microsoft/index.json" />
      </packageSources>
    
      <packageSourceMapping>
        <packageSource key="nuget.org">
          <package pattern="*" />
        </packageSource>
        <packageSource key="github">
          <package pattern="*nightly"/>
          <package pattern="Microsoft.Agents.AI" />
        </packageSource>
      </packageSourceMapping>
    
      <packageSourceCredentials>
        <github>
            <add key="Username" value="<Your GitHub Id>" />
            <add key="ClearTextPassword" value="<Your Personal Access Token>" />
          </github>
      </packageSourceCredentials>
    </configuration>
    
    • If you place this file in your project folder, make sure to have Git (or whatever source control you use) ignore it.
    • For more information on where to store this file, see nuget.config reference.
  6. You can now add packages from the nightly build to your project.

    For example, use this command dotnet add package Microsoft.Agents.AI --prerelease

  7. And the latest package release can be referenced in the project like this:

    <PackageReference Include="Microsoft.Agents.AI" Version="*-*" />

For more information, see https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry.

Prerequisites

Before you begin, ensure you have the following:

Note

This demo uses Azure CLI credentials for authentication. Make sure you're logged in with az login and have access to the Azure AI project. For more information, see the Azure CLI documentation.

Running a Basic Agent Sample

This sample demonstrates how to create and use a simple AI agent with Azure AI as the backend. It will create a basic agent using ChatAgent with AzureAIAgentClient and custom instructions.

Make sure to set the following environment variables:

  • AZURE_AI_PROJECT_ENDPOINT: Your Azure AI project endpoint
  • AZURE_AI_MODEL_DEPLOYMENT_NAME: The name of your model deployment

Sample Code

import asyncio
from agent_framework import ChatAgent
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential

async def main():
    async with (
        AzureCliCredential() as credential,
        ChatAgent(
            chat_client=AzureAIAgentClient(async_credential=credential),
            instructions="You are good at telling jokes."
        ) as agent,
    ):
        result = await agent.run("Tell me a joke about a pirate.")
        print(result.text)

if __name__ == "__main__":
    asyncio.run(main())

More Examples

For more detailed examples and advanced scenarios, see the Azure AI Agent Examples.

Next steps