Skip to main content

Command Palette

Search for a command to run...

Define, Update, Repeat: AI Agents Made Easy with YAML

A practical guide to defining AI agent behavior using prompt templates.

Updated
3 min read
Define, Update, Repeat: AI Agents Made Easy with YAML
D

Expert insights on Azure AI architecture and implementation. Real-world solutions for building intelligent enterprise systems.

Introduction

An agent’s behavior and actions are largely determined by the instructions it is given. Developers can set up an agent directly through a Prompt Template Configuration, giving them a structured and reusable method for specifying its behavior. This method provides a robust way to standardize and tailor agent instructions, ensuring consistent performance across different scenarios while preserving flexibility and adaptability.


Agent Definition from a YAML Template

Separating agent definitions from the codebase streamlines the management of multiple agents, allowing updates and maintenance without altering the underlying logic. This separation also increases flexibility, letting developers change agent behavior or add new agents simply by updating the configuration, rather than modifying the code.

In my example, I define a Finance Reporter agent using the following YAML template:

name: FinanceReporter
template: |
  You are a financial analyst. Given the company {{$company}} and quarter {{$quarter}}, 
  provide a financial report in JSON format only.

  Return your response as valid JSON matching this exact structure:
  {
    "revenue": <number in millions USD>,
    "profit": <number in millions USD>,
    "growthRate": "<percentage string>",
    "summary": "<brief summary string>"
  }

  Return only the JSON object, no additional text.
template_format: semantic-kernel
description: Generates a financial report for a company in JSON format.
input_variables:
  - name: company
    description: The company name
    is_required: true
  - name: quarter
    description: The quarter (e.g., Q1 2025)
    is_required: true
output_variable:
  description: JSON object with revenue, profit, growthRate, and summary
  json_schema: |
    {
      "type": "object",
      "properties": {
        "revenue": { "type": "number", "description": "Revenue in millions USD" },
        "profit": { "type": "number", "description": "Profit in millions USD" },
        "growthRate": { "type": "string", "description": "Growth percentage compared to last quarter" },
        "summary": { "type": "string", "description": "Short summary of financial performance" }
      },
      "required": ["revenue", "profit", "growthRate", "summary"]
    }

Keep in mind that we define the template and input variables, along with many other agent attributes. To create an agent from the template, we use the following code:

string financeAgent = File.ReadAllText("./Agents/FinanceReporter.yaml");

PromptTemplateConfig templateConfig = KernelFunctionYaml.ToPromptTemplateConfig(financeAgent);

ChatCompletionAgent agent =
    new(templateConfig, new KernelPromptTemplateFactory())
    {
        Kernel = kernel,
        // Provide default values for template parameters
        Arguments = new KernelArguments()
        {
            { "company", "Microsoft" },
            { "quarter", "Q1 2024" },
        }
    };

await foreach (ChatMessageContent response in agent.InvokeAsync([]))
{
    Console.WriteLine(response.Content);
}

As demonstrated, this method is quite straightforward. Furthermore, when calling an agent directly, its parameters can be adjusted as required.

KernelArguments overrideArguments =
    new()
    {
           { "company", "Google" },
           { "quarter", "Q1 2023" },
    };

await foreach (ChatMessageContent response in agent.InvokeAsync([], options: new() { KernelArguments = overrideArguments }))
{
    Console.WriteLine(response.Content);
}

In summary, using prompt template configurations to define agents provides a structured, flexible, and maintainable approach. It simplifies management, ensures consistency, and allows developers to easily customize agent behavior without modifying underlying code, making it a powerful strategy for building scalable and adaptable AI systems.

The Azure Behind the Madness

Part 16 of 16

Explore the world of Microsoft Azure - from AI and cloud architecture to data and DevOps. The Azure Behind the Madness brings insights, stories, and hands-on guidance for building intelligent, scalable solutions in the cloud.

Start from the beginning

The Secret Behind Better RAG Results: Hybrid Search in Azure AI Search

Inside Azure AI Search: Hybrid Retrieval with Vector and Semantic Ranking