mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
628bb1af48
* Update Foundry Responses as ChatClientAgent * Migrate obsolete AzureAI integration tests to versioned agent pattern Replace obsolete CreateAIAgentAsync/GetAIAgentAsync calls with Agents.CreateAgentVersionAsync() + AsAIAgent(AgentVersion) in all AzureAI integration tests. - Rename AIProjectClient* test files to FoundryVersionedAgent* - Register AIFunction tools in PromptAgentDefinition.Tools for server-side visibility via AsOpenAIResponseTool() - Skip structured output tests (AzureAIProjectChatClient clears ResponseFormat for versioned agents) - Remove all [Obsolete] attributes and #pragma warning disable CS0618 * Merge FoundryMemory package into AzureAI under Memory/ folder Move all FoundryMemory source, unit tests, and integration tests into the Microsoft.Agents.AI.AzureAI package. Change namespace from Microsoft.Agents.AI.FoundryMemory to Microsoft.Agents.AI.AzureAI. - Add [Experimental] to FoundryMemoryProviderOptions and Scope - Rename internal AIProjectClientExtensions to MemoryStoreExtensions - Update AzureAI .csproj with Compliance.Abstractions, Redaction - Remove FoundryMemory from solution and release filter - Update sample to reference AzureAI instead of FoundryMemory - Delete old Microsoft.Agents.AI.FoundryMemory project and tests * Add EnsureMemoryStoreCreatedAsync and memory existence checks to integration tests - Ensure memory store is created before testing memory operations - Add AZURE_AI_EMBEDDING_DEPLOYMENT_NAME config setting - Assert memories exist in store via SearchMemoriesAsync before cleanup - Verify scope isolation with direct memory store queries * Fix and rename AzureAI unit tests for RAPI vs Versioned clarity - Rename AsAIAgentAsync_* to AsAIAgent_* (drop Async from method group) - Add _Rapi_ prefix to non-versioned (Responses API) tests - Add _Versioned_ prefix to versioned agent tests where needed - Fix RAPI tests: assert GetService<AIProjectClient>() is null - Fix Versioned tests: assert IsType<FoundryAgent> and GetService<AIProjectClient>() returns the client instance - Fix UserAgent header tests: proper HTTP handler routing - Fix ChatClient_UsesDefaultConversationIdAsync test setup - All 153 unit tests pass with 0 failures * Rename Microsoft.Agents.AI.AzureAI to Microsoft.Agents.AI.Foundry Rename the project, namespace, folder, and all references from Microsoft.Agents.AI.AzureAI to Microsoft.Agents.AI.Foundry. Also rename Workflows.Declarative.AzureAI to .Foundry. - Rename src, unit test, integration test, and workflow folders - Update namespaces in all source and test .cs files - Update ProjectReferences in ~47 sample and test .csproj files - Update solution files (.slnx, .slnf) - Update sample using statements - Update READMEs, SKILL.md, ADRs in docs/ - Disable package validation baseline for renamed packages - Fix UTF-8 BOM encoding on all affected .cs files - AzureAI.Persistent left completely unchanged * Fix format: remove ImplicitUsings, add explicit usings, fix BOM encoding - Remove ImplicitUsings=enable from Foundry csproj to resolve IDE0005 on shared ReplacingRedactor.cs - Add explicit System usings to all source files that relied on them - Sort usings alphabetically per editorconfig rules - Fix UTF-8 BOM on 12 sample Program.cs files - Rename Azure AI Foundry Agents to Microsoft Foundry Agents in docs
349 lines
15 KiB
C#
349 lines
15 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using AgentConformance.IntegrationTests.Support;
|
|
using Azure.AI.Projects;
|
|
using Azure.AI.Projects.Agents;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Foundry;
|
|
using Microsoft.Extensions.AI;
|
|
using OpenAI.Files;
|
|
using OpenAI.Responses;
|
|
using Shared.IntegrationTests;
|
|
|
|
namespace Foundry.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Integration tests for versioned <see cref="FoundryAgent"/> creation via
|
|
/// <c>AIProjectClient.Agents.CreateAgentVersionAsync</c> and <c>AIProjectClient.AsAIAgent(AgentVersion)</c>.
|
|
/// </summary>
|
|
public class FoundryVersionedAgentCreateTests
|
|
{
|
|
private readonly AIProjectClient _client = new(new Uri(TestConfiguration.GetRequiredValue(TestSettings.AzureAIProjectEndpoint)), TestAzureCliCredentials.CreateAzureCliCredential());
|
|
|
|
[Fact]
|
|
public async Task CreateAgent_CreatesAgentWithCorrectMetadataAsync()
|
|
{
|
|
// Arrange.
|
|
string AgentName = FoundryVersionedAgentFixture.GenerateUniqueAgentName("IntegrationTestAgent");
|
|
const string AgentDescription = "An agent created during integration tests";
|
|
const string AgentInstructions = "You are an integration test agent";
|
|
|
|
// Act.
|
|
var agentVersion = await this._client.Agents.CreateAgentVersionAsync(
|
|
AgentName,
|
|
new AgentVersionCreationOptions(
|
|
new PromptAgentDefinition(TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName))
|
|
{
|
|
Instructions = AgentInstructions
|
|
})
|
|
{
|
|
Description = AgentDescription
|
|
});
|
|
|
|
var agent = this._client.AsAIAgent(agentVersion);
|
|
|
|
try
|
|
{
|
|
// Assert.
|
|
Assert.NotNull(agent);
|
|
Assert.Equal(AgentName, agent.Name);
|
|
Assert.Equal(AgentDescription, agent.Description);
|
|
Assert.Equal(AgentInstructions, agent.GetService<ChatClientAgent>()!.Instructions);
|
|
|
|
var agentRecord = await this._client.Agents.GetAgentAsync(agent.Name);
|
|
Assert.NotNull(agentRecord);
|
|
Assert.Equal(AgentName, agentRecord.Value.Name);
|
|
var definition = Assert.IsType<PromptAgentDefinition>(agentRecord.Value.GetLatestVersion().Definition);
|
|
Assert.Equal(AgentDescription, agentRecord.Value.GetLatestVersion().Description);
|
|
Assert.Equal(AgentInstructions, definition.Instructions);
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup.
|
|
await this._client.Agents.DeleteAgentAsync(agent.Name);
|
|
}
|
|
}
|
|
|
|
[Theory(Skip = "For manual testing only")]
|
|
[InlineData("FileSearchTool")]
|
|
public async Task CreateAgent_CreatesAgentWithVectorStoresAsync(string _)
|
|
{
|
|
// Arrange.
|
|
string AgentName = FoundryVersionedAgentFixture.GenerateUniqueAgentName("VectorStoreAgent");
|
|
const string AgentInstructions = """
|
|
You are a helpful agent that can help fetch data from files you know about.
|
|
Use the File Search Tool to look up codes for words.
|
|
Do not answer a question unless you can find the answer using the File Search Tool.
|
|
""";
|
|
|
|
// Get the project OpenAI client.
|
|
var projectOpenAIClient = this._client.GetProjectOpenAIClient();
|
|
|
|
// Create a vector store.
|
|
var searchFilePath = Path.GetTempFileName() + "wordcodelookup.txt";
|
|
File.WriteAllText(
|
|
path: searchFilePath,
|
|
contents: "The word 'apple' uses the code 442345, while the word 'banana' uses the code 673457."
|
|
);
|
|
OpenAIFile uploadedAgentFile = projectOpenAIClient.GetProjectFilesClient().UploadFile(
|
|
filePath: searchFilePath,
|
|
purpose: FileUploadPurpose.Assistants
|
|
);
|
|
var vectorStoreMetadata = await projectOpenAIClient.GetProjectVectorStoresClient().CreateVectorStoreAsync(options: new() { FileIds = { uploadedAgentFile.Id }, Name = "WordCodeLookup_VectorStore" });
|
|
|
|
// Act — create agent version with FileSearch tool via native SDK, then wrap with AsAIAgent.
|
|
var definition = new PromptAgentDefinition(TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName))
|
|
{
|
|
Instructions = AgentInstructions,
|
|
Tools = { ResponseTool.CreateFileSearchTool(vectorStoreIds: [vectorStoreMetadata.Value.Id]) }
|
|
};
|
|
|
|
var agentVersion = await this._client.Agents.CreateAgentVersionAsync(
|
|
AgentName,
|
|
new AgentVersionCreationOptions(definition));
|
|
|
|
var agent = this._client.AsAIAgent(agentVersion);
|
|
|
|
try
|
|
{
|
|
// Assert.
|
|
// Verify that the agent can use the vector store to answer a question.
|
|
var result = await agent.RunAsync("Can you give me the documented code for 'banana'?");
|
|
Assert.Contains("673457", result.ToString());
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup.
|
|
await this._client.Agents.DeleteAgentAsync(agent.Name);
|
|
await projectOpenAIClient.GetProjectVectorStoresClient().DeleteVectorStoreAsync(vectorStoreMetadata.Value.Id);
|
|
await projectOpenAIClient.GetProjectFilesClient().DeleteFileAsync(uploadedAgentFile.Id);
|
|
File.Delete(searchFilePath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAgent_CreatesAgentWithCodeInterpreterAsync()
|
|
{
|
|
// Arrange.
|
|
string AgentName = FoundryVersionedAgentFixture.GenerateUniqueAgentName("CodeInterpreterAgent");
|
|
const string AgentInstructions = """
|
|
You are a helpful coding agent. A Python file is provided. Use the Code Interpreter Tool to run the file
|
|
and report the SECRET_NUMBER value it prints. Respond only with the number.
|
|
""";
|
|
|
|
// Get the project OpenAI client.
|
|
var projectOpenAIClient = this._client.GetProjectOpenAIClient();
|
|
|
|
// Create a python file that prints a known value.
|
|
var codeFilePath = Path.GetTempFileName() + "secret_number.py";
|
|
File.WriteAllText(
|
|
path: codeFilePath,
|
|
contents: "print(\"SECRET_NUMBER=24601\")" // Deterministic output we will look for.
|
|
);
|
|
OpenAIFile uploadedCodeFile = projectOpenAIClient.GetProjectFilesClient().UploadFile(
|
|
filePath: codeFilePath,
|
|
purpose: FileUploadPurpose.Assistants
|
|
);
|
|
|
|
// Act — create agent version with CodeInterpreter tool via native SDK, then wrap with AsAIAgent.
|
|
var definition = new PromptAgentDefinition(TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName))
|
|
{
|
|
Instructions = AgentInstructions,
|
|
Tools = { ResponseTool.CreateCodeInterpreterTool(new CodeInterpreterToolContainer(CodeInterpreterToolContainerConfiguration.CreateAutomaticContainerConfiguration([uploadedCodeFile.Id]))) }
|
|
};
|
|
|
|
var agentVersion = await this._client.Agents.CreateAgentVersionAsync(
|
|
AgentName,
|
|
new AgentVersionCreationOptions(definition));
|
|
|
|
var agent = this._client.AsAIAgent(agentVersion);
|
|
|
|
try
|
|
{
|
|
// Assert.
|
|
var result = await agent.RunAsync("What is the SECRET_NUMBER?");
|
|
// We expect the model to run the code and surface the number.
|
|
Assert.Contains("24601", result.ToString());
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup.
|
|
await this._client.Agents.DeleteAgentAsync(agent.Name);
|
|
await projectOpenAIClient.GetProjectFilesClient().DeleteFileAsync(uploadedCodeFile.Id);
|
|
File.Delete(codeFilePath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates that an agent version created with an OpenAPI tool definition via the native
|
|
/// Azure.AI.Projects SDK and then wrapped with <c>AsAIAgent(agentVersion)</c> correctly
|
|
/// invokes the server-side OpenAPI function through <c>RunAsync</c>.
|
|
/// Regression test for https://github.com/microsoft/agent-framework/issues/4883.
|
|
/// </summary>
|
|
[RetryFact(Constants.RetryCount, Constants.RetryDelay, Skip = "For manual testing only")]
|
|
public async Task AsAIAgent_WithOpenAPITool_NativeSDKCreation_InvokesServerSideToolAsync()
|
|
{
|
|
// Arrange — create agent version with OpenAPI tool using native Azure.AI.Projects SDK types.
|
|
string AgentName = FoundryVersionedAgentFixture.GenerateUniqueAgentName("OpenAPITestAgent");
|
|
const string AgentInstructions = "You are a helpful assistant that can use the countries API to retrieve information about countries by their currency code.";
|
|
|
|
const string CountriesOpenApiSpec = """
|
|
{
|
|
"openapi": "3.1.0",
|
|
"info": {
|
|
"title": "REST Countries API",
|
|
"description": "Retrieve information about countries by currency code",
|
|
"version": "v3.1"
|
|
},
|
|
"servers": [
|
|
{
|
|
"url": "https://restcountries.com/v3.1"
|
|
}
|
|
],
|
|
"paths": {
|
|
"/currency/{currency}": {
|
|
"get": {
|
|
"description": "Get countries that use a specific currency code (e.g., USD, EUR, GBP)",
|
|
"operationId": "GetCountriesByCurrency",
|
|
"parameters": [
|
|
{
|
|
"name": "currency",
|
|
"in": "path",
|
|
"description": "Currency code (e.g., USD, EUR, GBP)",
|
|
"required": true,
|
|
"schema": {
|
|
"type": "string"
|
|
}
|
|
}
|
|
],
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful response with list of countries",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"404": {
|
|
"description": "No countries found for the currency"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
// Step 1: Create the OpenAPI function definition and agent version using native SDK types.
|
|
var openApiFunction = new OpenApiFunctionDefinition(
|
|
"get_countries",
|
|
BinaryData.FromString(CountriesOpenApiSpec),
|
|
new OpenAPIAnonymousAuthenticationDetails())
|
|
{
|
|
Description = "Retrieve information about countries by currency code"
|
|
};
|
|
|
|
var definition = new PromptAgentDefinition(model: TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName))
|
|
{
|
|
Instructions = AgentInstructions,
|
|
Tools = { (ResponseTool)AgentTool.CreateOpenApiTool(openApiFunction) }
|
|
};
|
|
|
|
AgentVersionCreationOptions creationOptions = new(definition);
|
|
AgentVersion agentVersion = await this._client.Agents.CreateAgentVersionAsync(AgentName, creationOptions);
|
|
|
|
try
|
|
{
|
|
// Step 2: Wrap the agent version using AsAIAgent extension.
|
|
FoundryAgent agent = this._client.AsAIAgent(agentVersion);
|
|
|
|
// Assert the agent was created correctly and retains version metadata.
|
|
Assert.NotNull(agent);
|
|
Assert.Equal(AgentName, agent.Name);
|
|
var retrievedVersion = agent.GetService<AgentVersion>();
|
|
Assert.NotNull(retrievedVersion);
|
|
|
|
// Step 3: Call RunAsync to trigger the server-side OpenAPI function.
|
|
var result = await agent.RunAsync("What countries use the Euro (EUR) as their currency? Please list them.");
|
|
|
|
// Step 4: Validate the OpenAPI tool was invoked server-side.
|
|
// Note: Server-side OpenAPI tools (executed within the Responses API via AgentReference)
|
|
// do not surface as FunctionCallContent in the MEAI abstraction — the API handles the full
|
|
// tool loop internally. We validate tool invocation by asserting the response contains
|
|
// multiple specific country names that the model would need API data to enumerate accurately.
|
|
var text = result.ToString();
|
|
Assert.NotEmpty(text);
|
|
|
|
// The response must mention multiple well-known Eurozone countries — requiring several
|
|
// correct entries makes it highly unlikely the model answered purely from parametric knowledge.
|
|
int matchCount = 0;
|
|
foreach (var country in new[] { "Germany", "France", "Italy", "Spain", "Portugal", "Netherlands", "Belgium", "Austria", "Ireland", "Finland" })
|
|
{
|
|
if (text.Contains(country, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
matchCount++;
|
|
}
|
|
}
|
|
|
|
Assert.True(
|
|
matchCount >= 3,
|
|
$"Expected response to list at least 3 Eurozone countries from the OpenAPI tool, but found {matchCount}. Response: {text}");
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup.
|
|
await this._client.Agents.DeleteAgentAsync(AgentName);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAgent_CreatesAgentWithAIFunctionToolsAsync()
|
|
{
|
|
// Arrange.
|
|
string AgentName = FoundryVersionedAgentFixture.GenerateUniqueAgentName("WeatherAgent");
|
|
const string AgentInstructions = "You are a helpful weather assistant. Always call the GetWeather function to answer questions about weather.";
|
|
|
|
static string GetWeather(string location) => $"The weather in {location} is sunny with a high of 23C.";
|
|
var weatherFunction = AIFunctionFactory.Create(GetWeather);
|
|
|
|
// Create agent version with the function tool registered in the server-side definition,
|
|
// then wrap with AsAIAgent passing the local AIFunction implementation.
|
|
var definition = new PromptAgentDefinition(TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName))
|
|
{
|
|
Instructions = AgentInstructions,
|
|
};
|
|
definition.Tools.Add(weatherFunction.AsOpenAIResponseTool());
|
|
|
|
var agentVersion = await this._client.Agents.CreateAgentVersionAsync(
|
|
AgentName,
|
|
new AgentVersionCreationOptions(definition));
|
|
|
|
FoundryAgent agent = this._client.AsAIAgent(agentVersion, tools: [weatherFunction]);
|
|
|
|
try
|
|
{
|
|
// Act.
|
|
var response = await agent.RunAsync("What is the weather like in Amsterdam?");
|
|
|
|
// Assert - ensure function was invoked and its output surfaced.
|
|
var text = response.Text;
|
|
Assert.Contains("Amsterdam", text, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Contains("sunny", text, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Contains("23", text, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
finally
|
|
{
|
|
await this._client.Agents.DeleteAgentAsync(agent.Name);
|
|
}
|
|
}
|
|
}
|