Skip to content

Commit

Permalink
Merge branch 'main' into fix-onnx-memory-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoeller authored Nov 18, 2024
2 parents 0c4cc1b + d63de90 commit a9d5d08
Show file tree
Hide file tree
Showing 148 changed files with 10,117 additions and 1,045 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Run Integration Tests - Completions
id: run_tests_completions
timeout-minutes: 10
timeout-minutes: 15
shell: bash
run: |
uv run pytest -n logical --dist loadfile --dist worksteal ./tests/integration/completions -v --junitxml=pytest-completions.xml
Expand All @@ -185,7 +185,7 @@ jobs:
uv run pytest -n logical --dist loadfile --dist worksteal ./tests/integration/embeddings -v --junitxml=pytest-embeddings.xml
- name: Run Integration Tests - Memory
id: run_tests_memory
timeout-minutes: 5
timeout-minutes: 10
shell: bash
run: |
uv run pytest -n logical --dist loadfile --dist worksteal ./tests/integration/memory -v --junitxml=pytest-memory.xml
Expand Down
5 changes: 3 additions & 2 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@
<PackageVersion Include="Microsoft.Graph" Version="[4.51.0, 5)" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="[2.28.0, )" />
<PackageVersion Include="Microsoft.OpenApi" Version="1.6.22" />
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.21" />
<PackageVersion Include="Microsoft.OpenApi.ApiManifest" Version="0.5.4-preview" />
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
<PackageVersion Include="Microsoft.OpenApi.ApiManifest" Version="0.5.5-preview" />
<PackageVersion Include="Microsoft.Plugins.Manifest" Version="1.0.0-preview3" />
<PackageVersion Include="Google.Apis.CustomSearchAPI.v1" Version="[1.60.0.3001, )" />
<PackageVersion Include="Grpc.Net.Client" Version="2.66.0" />
<PackageVersion Include="protobuf-net" Version="3.2.45" />
Expand Down
5 changes: 3 additions & 2 deletions dotnet/docs/EXPERIMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ You can use the following diagnostic IDs to ignore warnings or errors for a part
| SKEXP0040 | GRPC functions |
| SKEXP0040 | Markdown functions |
| SKEXP0040 | OpenAPI functions |
| SKEXP0040 | OpenAPI function extensions |
| SKEXP0040 | OpenAPI function extensions - API Manifest |
| SKEXP0040 | OpenAPI function extensions - Copilot Agent Plugin |
| SKEXP0040 | Prompty Format support |
| | | | | | | |
| SKEXP0050 | Core plugins |
Expand All @@ -86,4 +87,4 @@ You can use the following diagnostic IDs to ignore warnings or errors for a part
| | | | | | | |
| SKEXP0110 | Agent Framework |
| | | | | | | |
| SKEXP0120 | Native-AOT |
| SKEXP0120 | Native-AOT |
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,7 @@ private Kernel CreateKernelWithFilter()
{
IKernelBuilder builder = Kernel.CreateBuilder();

if (this.UseOpenAIConfig)
{
builder.AddOpenAIChatCompletion(
TestConfiguration.OpenAI.ChatModelId,
TestConfiguration.OpenAI.ApiKey);
}
else
{
builder.AddAzureOpenAIChatCompletion(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
TestConfiguration.AzureOpenAI.Endpoint,
TestConfiguration.AzureOpenAI.ApiKey);
}
base.AddChatCompletionToKernel(builder);

builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(new AutoInvocationFilter());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Sum 426 1622 856 2904
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
chat.AddChatMessage(new(AuthorRole.User, input));
chat.AddChatMessage(message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
Expand Down
216 changes: 216 additions & 0 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_FunctionFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) Microsoft. All rights reserved.
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Agents;

/// <summary>
/// Demonstrate usage of <see cref="IAutoFunctionInvocationFilter"/> for and
/// <see cref="IFunctionInvocationFilter"/> filters with <see cref="OpenAIAssistantAgent"/>
/// via <see cref="AgentChat"/>.
/// </summary>
public class OpenAIAssistant_FunctionFilters(ITestOutputHelper output) : BaseAgentsTest(output)
{
protected override bool ForceOpenAI => true; // %%% REMOVE

[Fact]
public async Task UseFunctionInvocationFilterAsync()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAsync(CreateKernelWithInvokeFilter());

// Invoke assistant agent (non streaming)
await InvokeAssistantAsync(agent);
}

[Fact]
public async Task UseFunctionInvocationFilterStreamingAsync()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAsync(CreateKernelWithInvokeFilter());

// Invoke assistant agent (streaming)
await InvokeAssistantStreamingAsync(agent);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task UseAutoFunctionInvocationFilterAsync(bool terminate)
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAsync(CreateKernelWithAutoFilter(terminate));

// Invoke assistant agent (non streaming)
await InvokeAssistantAsync(agent);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task UseAutoFunctionInvocationFilterWithStreamingAgentInvocationAsync(bool terminate)
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAsync(CreateKernelWithAutoFilter(terminate));

// Invoke assistant agent (streaming)
await InvokeAssistantStreamingAsync(agent);
}

private async Task InvokeAssistantAsync(OpenAIAssistantAgent agent)
{
// Create a thread for the agent conversation.
AgentGroupChat chat = new();

try
{
// Respond to user input, invoking functions where appropriate.
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
chat.AddChatMessage(message);
await chat.InvokeAsync(agent).ToArrayAsync();

// Display the entire chat history.
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
this.WriteChatHistory(history);
}
finally
{
await chat.ResetAsync();
await agent.DeleteAsync();
}
}

private async Task InvokeAssistantStreamingAsync(OpenAIAssistantAgent agent)
{
// Create a thread for the agent conversation.
AgentGroupChat chat = new();

try
{
// Respond to user input, invoking functions where appropriate.
ChatMessageContent message = new(AuthorRole.User, "What is the special soup?");
chat.AddChatMessage(message);
await chat.InvokeStreamingAsync(agent).ToArrayAsync();

// Display the entire chat history.
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
this.WriteChatHistory(history);
}
finally
{
await chat.ResetAsync();
await agent.DeleteAsync();
}
}

private void WriteChatHistory(IEnumerable<ChatMessageContent> history)
{
Console.WriteLine("\n================================");
Console.WriteLine("CHAT HISTORY");
Console.WriteLine("================================");
foreach (ChatMessageContent message in history)
{
this.WriteAgentChatMessage(message);
}
}

private async Task<OpenAIAssistantAgent> CreateAssistantAsync(Kernel kernel)
{
OpenAIAssistantAgent agent =
await OpenAIAssistantAgent.CreateAsync(
this.GetClientProvider(),
new OpenAIAssistantDefinition(base.Model)
{
Instructions = "Answer questions about the menu.",
Metadata = AssistantSampleMetadata,
},
kernel: kernel
);

KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
agent.Kernel.Plugins.Add(plugin);

return agent;
}

private Kernel CreateKernelWithAutoFilter(bool terminate)
{
IKernelBuilder builder = Kernel.CreateBuilder();

base.AddChatCompletionToKernel(builder);

builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(new AutoInvocationFilter(terminate));

return builder.Build();
}

private Kernel CreateKernelWithInvokeFilter()
{
IKernelBuilder builder = Kernel.CreateBuilder();

base.AddChatCompletionToKernel(builder);

builder.Services.AddSingleton<IFunctionInvocationFilter>(new InvocationFilter());

return builder.Build();
}

private sealed class MenuPlugin
{
[KernelFunction, Description("Provides a list of specials from the menu.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1024:Use properties where appropriate", Justification = "Too smart")]
public string GetSpecials()
{
return
"""
Special Soup: Clam Chowder
Special Salad: Cobb Salad
Special Drink: Chai Tea
""";
}

[KernelFunction, Description("Provides the price of the requested menu item.")]
public string GetItemPrice([Description("The name of the menu item.")] string menuItem)
{
return "$9.99";
}
}

private sealed class InvocationFilter() : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
System.Console.WriteLine($"FILTER INVOKED {nameof(InvocationFilter)} - {context.Function.Name}");

// Execution the function
await next(context);

// Signal termination if the function is from the MenuPlugin
if (context.Function.PluginName == nameof(MenuPlugin))
{
context.Result = new FunctionResult(context.Function, "BLOCKED");
}
}
}

private sealed class AutoInvocationFilter(bool terminate = true) : IAutoFunctionInvocationFilter
{
public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next)
{
System.Console.WriteLine($"FILTER INVOKED {nameof(AutoInvocationFilter)} - {context.Function.Name}");

// Execution the function
await next(context);

// Signal termination if the function is from the MenuPlugin
if (context.Function.PluginName == nameof(MenuPlugin))
{
context.Terminate = terminate;
}
}
}
}
12 changes: 6 additions & 6 deletions dotnet/samples/Concepts/Concepts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@
</None>
</ItemGroup>
<ItemGroup>
<Content Include="Resources\Plugins\EventPlugin\openapiV1.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Resources\Plugins\EventPlugin\openapiV2.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="Resources\Plugins\CopilotAgentPlugins\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Resources\Plugins\CopilotAgentPlugins\**\*.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Resources\Plugins\RepairServicePlugin\repair-service.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
Loading

0 comments on commit a9d5d08

Please sign in to comment.