mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
626b418622
* .NET: Add a TODO AIContextProvider (#5233) * Add a TODO AIContextProvider * Add unit tests * Address PR comments * Address PR comments * Fix test after removing one tool * .NET: Add a ModeProvider for managing agent modes (#5247) * Add a ModeProvider for managing agent modes * Fix typo * Fix typo * Fix typo * Address PR comments * .NET: Add sample to show how to build a harness (#5268) * Add sample to show how to build a harness * Improve sample * Sample max output tokens and model * Fix encoding * Fix model name in readme * Address PR comments * .NET: Add context window size compaction strategy for harness (#5304) * Add context window size compaction strategy for harness * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * .NET: Add a file memory provider (#5315) * Add a file memory provider * Address PR comments * Fix review comments. * Add additional unit tests * Addressing PR comments. * .NET: Harness: Improve prompts and add FileSystem store (#5365) * Harness: Improve prompts and add FileSystem store * Address PR comments * .NET: Harness: Improve path validation (#5404) * Harness: Improve path validation * Address PR comments * .NET: Add always approve helpers, improve sample and fix bug (#5451) * Add always approve helpers, improve sample and fix bug * Address PR comments * .NET: Make Todo, Mode and FileMemory providers more configurable (#5477) * Make Todo, Mode and FileMemory providers more configurable * Address PR comments. * .NET: Add subagents provider and sample (#5518) * Add subagents provider and sample * Addressing PR comments. * .NET: Harness filememory index plus instructions consistency (#5540) * Add FileMemoryProvider index and improve instruction consistency * Address PR comments. * Address PR comments * Address PR comments. * Apply suggestion from @rogerbarreto Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> * .NET: Refactor harness console to be more extensible and easy to understand with better UX (#5573) * Refactor harness console to be more extensible and easy to understand with better UX. * Fix formatting issues. * Allow multiple clarifications in one response * Address PR comments * .NET: Add FileAccessProvdider and concurrency fix for FileMemoryProvider (#5583) * Add FileAccessProvdider and concurrency fix for FileMemoryProvider * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
111 lines
4.5 KiB
C#
111 lines
4.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample demonstrates how to use a ChatClientAgent with the FileAccessProvider
|
|
// to give an agent access to a folder of CSV data files. The agent can read, analyze,
|
|
// and extract information from the data, then write results back as new files.
|
|
//
|
|
// The sample includes a pre-populated `data/` folder with sales transaction data.
|
|
// Ask the agent to analyze the data, produce summaries, or create new output files.
|
|
//
|
|
// Special commands:
|
|
// exit — End the session.
|
|
|
|
#pragma warning disable OPENAI001 // Suppress experimental API warnings for Responses API usage.
|
|
#pragma warning disable MAAI001 // Suppress experimental API warnings for Agents AI experiments.
|
|
|
|
using System.ClientModel.Primitives;
|
|
using Azure.Identity;
|
|
using Harness.Shared.Console;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Compaction;
|
|
using Microsoft.Extensions.AI;
|
|
using OpenAI;
|
|
using OpenAI.Responses;
|
|
|
|
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_OPENAI_ENDPOINT is not set.");
|
|
var deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME") ?? "gpt-5.4";
|
|
|
|
const int MaxContextWindowTokens = 1_050_000;
|
|
const int MaxOutputTokens = 128_000;
|
|
|
|
// Point the file store at the data/ folder that ships with the sample.
|
|
var dataFolder = Path.Combine(AppContext.BaseDirectory, "data");
|
|
var fileStore = new FileSystemAgentFileStore(dataFolder);
|
|
|
|
var instructions =
|
|
"""
|
|
You are a data analyst assistant. You have access to a folder of data files via the FileAccess_* tools.
|
|
|
|
## Getting started
|
|
- Start by listing available files with FileAccess_ListFiles to see what data is available.
|
|
- Read the files to understand their structure and contents.
|
|
|
|
## Working with data
|
|
- When asked to analyze data, read the relevant files first, then perform the analysis.
|
|
- Show your analysis clearly with tables, summaries, and key insights.
|
|
- When calculations are needed, work through them step by step and show your reasoning.
|
|
|
|
## Writing output
|
|
- When asked to produce output files (e.g., reports, summaries, filtered data), use FileAccess_SaveFile to write them.
|
|
- Use appropriate file formats: CSV for tabular data, Markdown for reports.
|
|
- Confirm what you wrote and where.
|
|
|
|
## Important
|
|
- Never modify or delete the original input data files unless explicitly asked to do so.
|
|
- If asked about data you haven't read yet, read it first before answering.
|
|
- Always explain your reasoning and thought process as you work through tasks.
|
|
- Always explain what you learned and what you are going to do next between tool calls, so the user can follow along with your thought process.
|
|
""";
|
|
|
|
// Create a compaction strategy based on the model's context window.
|
|
var compactionStrategy = new ContextWindowCompactionStrategy(
|
|
maxContextWindowTokens: MaxContextWindowTokens,
|
|
maxOutputTokens: MaxOutputTokens);
|
|
|
|
AIAgent agent =
|
|
new OpenAIClient(
|
|
new BearerTokenPolicy(new DefaultAzureCredential(), "https://ai.azure.com/.default"),
|
|
new OpenAIClientOptions()
|
|
{
|
|
Endpoint = new Uri(endpoint),
|
|
RetryPolicy = new ClientRetryPolicy(3)
|
|
})
|
|
.GetResponsesClient()
|
|
.AsIChatClientWithStoredOutputDisabled(deploymentName)
|
|
|
|
.AsBuilder()
|
|
.UseFunctionInvocation()
|
|
.UsePerServiceCallChatHistoryPersistence()
|
|
.UseAIContextProviders(new CompactionProvider(compactionStrategy))
|
|
|
|
.BuildAIAgent(
|
|
new ChatClientAgentOptions
|
|
{
|
|
Name = "DataAnalyst",
|
|
Description = "A data analyst assistant that reads, analyzes, and processes data files.",
|
|
UseProvidedChatClientAsIs = true,
|
|
RequirePerServiceCallChatHistoryPersistence = true,
|
|
ChatHistoryProvider = new InMemoryChatHistoryProvider(
|
|
new InMemoryChatHistoryProviderOptions
|
|
{
|
|
ChatReducer = compactionStrategy.AsChatReducer(),
|
|
}),
|
|
AIContextProviders =
|
|
[
|
|
new FileAccessProvider(fileStore),
|
|
],
|
|
ChatOptions = new ChatOptions
|
|
{
|
|
Instructions = instructions,
|
|
MaxOutputTokens = MaxOutputTokens,
|
|
},
|
|
})
|
|
.AsBuilder()
|
|
.Build();
|
|
|
|
// Run the interactive console session.
|
|
await HarnessConsole.RunAgentAsync(
|
|
agent,
|
|
title: "Data Processing Assistant",
|
|
userPrompt: "Ask me to analyze the data files, produce summaries, or create output files.");
|