Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users/markwallace/azureai tool support #3

Open
wants to merge 8 commits into
base: users/markwallace/adr_declarative_agents
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@ public Step08_AzureAIAgent_Declarative(ITestOutputHelper output) : base(output)
this._kernel = builder.Build();
}

[Fact]
public async Task AzureAIAgentWithConfigurationAsync()
{
var text =
$"""
type: azureai_agent
name: StoryAgent
description: Store Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: gpt-4o-mini
configuration:
connection_string: {TestConfiguration.AzureAI.ConnectionString}
""";
AzureAIAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text) as AzureAIAgent;

await InvokeAgentAsync(agent!, "Cats and Dogs");
}

[Fact]
public async Task AzureAIAgentWithKernelAsync()
{
var text =
"""
type: azureai_agent
name: StoryAgent
description: Store Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: gpt-4o-mini
""";
AzureAIAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as AzureAIAgent;

await InvokeAgentAsync(agent!, "Cats and Dogs");
}

[Fact]
public async Task AzureAIAgentWithCodeInterpreterAsync()
{
Expand All @@ -38,14 +78,71 @@ public async Task AzureAIAgentWithCodeInterpreterAsync()
""";
AzureAIAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(this._kernel, text) as AzureAIAgent;
var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as AzureAIAgent;
Assert.NotNull(agent);

await InvokeAgentAsync(agent, "Use code to determine the values in the Fibonacci sequence that that are less then the value of 101?");
await InvokeAgentAsync(agent!, "Use code to determine the values in the Fibonacci sequence that that are less then the value of 101?");
}

[Fact]
public async Task AzureAIAgentWithOpenApiAsync()
{
var text =
"""
type: azureai_agent
name: AzureAIAgent
description: AzureAIAgent Description
instructions: AzureAIAgent Instructions
model:
id: gpt-4o-mini
tools:
- type: openapi
name: RestCountriesAPI
description: Web API version 3.1 for managing country items, based on previous implementations from restcountries.eu and restcountries.com.
specification: '{"openapi":"3.1.0","info":{"title":"Get Weather Data","description":"Retrieves current weather data for a location based on wttr.in.","version":"v1.0.0"},"servers":[{"url":"https://wttr.in"}],"auth":[],"paths":{"/{location}":{"get":{"description":"Get weather information for a specific location","operationId":"GetCurrentWeather","parameters":[{"name":"location","in":"path","description":"City or location to retrieve the weather for","required":true,"schema":{"type":"string"}},{"name":"format","in":"query","description":"Always use j1 value for this parameter","required":true,"schema":{"type":"string","default":"j1"}}],"responses":{"200":{"description":"Successful response","content":{"text/plain":{"schema":{"type":"string"}}}},"404":{"description":"Location not found"}},"deprecated":false}}},"components":{"schemes":{}}}'
""";
AzureAIAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as AzureAIAgent;
Assert.NotNull(agent);

await InvokeAgentAsync(agent!, "What country has Dublin as it's capital city?");
}

[Fact]
public async Task AzureAIAgentWithFunctionsAsync()
{
var text =
"""
type: azureai_agent
name: RestaurantHost
instructions: Answer questions about the menu.
description: This agent answers questions about the menu.
model:
id: gpt-4o-mini
options:
temperature: 0.4
tools:
- type: function
name: MenuPlugin.GetSpecials
description: Retrieves the specials for the day.
- type: function
name: MenuPlugin.GetItemPrice
description: Retrieves the price of an item on the menu.
parameters:
""";
AzureAIAgentFactory factory = new();

KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
this._kernel.Plugins.Add(plugin);

var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as AzureAIAgent;

await InvokeAgentAsync(agent!, "What is the special soup and how much does it cost?");
}

[Fact]
public async Task AzureAIAgentWithKernelFunctionsAsync()
public async Task AzureAIAgentWithFunctionsViaOptionsAsync()
{
var text =
"""
Expand All @@ -68,10 +165,10 @@ public async Task AzureAIAgentWithKernelFunctionsAsync()
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
this._kernel.Plugins.Add(plugin);

var agent = await factory.CreateAgentFromYamlAsync(this._kernel, text) as AzureAIAgent;
var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as AzureAIAgent;
Assert.NotNull(agent);

await InvokeAgentAsync(agent, "What is the special soup and how much does it cost?");
await InvokeAgentAsync(agent!, "What is the special soup and how much does it cost?");
}

#region private
Expand All @@ -83,7 +180,7 @@ public async Task AzureAIAgentWithKernelFunctionsAsync()
private async Task InvokeAgentAsync(AzureAIAgent agent, string input)
{
// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = await agent.Client.CreateThreadAsync(metadata: SampleMetadata);

try
{
Expand All @@ -92,8 +189,8 @@ private async Task InvokeAgentAsync(AzureAIAgent agent, string input)
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await this.AgentsClient.DeleteAgentAsync(agent.Id);
await agent.Client.DeleteThreadAsync(thread.Id);
await agent.Client.DeleteAgentAsync(agent!.Id);
}

// Local function to invoke agent and display the conversation messages.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI;

namespace GettingStarted.OpenAIAssistants;

/// <summary>
/// This example demonstrates how to declaratively create instances of <see cref="OpenAIAssistantAgent"/>.
/// </summary>
public class Step07_Assistant_Declarative : BaseAssistantTest
{
public Step07_Assistant_Declarative(ITestOutputHelper output) : base(output)
{
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<OpenAIClient>(this.Client);
this._kernel = builder.Build();
}

[Fact]
public async Task OpenAIAssistantAgentWithConfigurationForOpenAIAsync()
{
var text =
$"""
type: openai_assistant
name: StoryAgent
description: Store Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: gpt-4o-mini
configuration:
type: openai
api_key: {TestConfiguration.OpenAI.ApiKey}
""";
OpenAIAssistantAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text) as OpenAIAssistantAgent;

await InvokeAgentAsync(agent!, "Cats and Dogs");
}

[Fact]
public async Task OpenAIAssistantAgentWithConfigurationForAzureOpenAIAsync()
{
var text =
$"""
type: openai_assistant
name: StoryAgent
description: Store Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: gpt-4o-mini
configuration:
type: azure_openai
endpoint: {TestConfiguration.AzureOpenAI.Endpoint}
""";
OpenAIAssistantAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text) as OpenAIAssistantAgent;

await InvokeAgentAsync(agent!, "Cats and Dogs");
}

[Fact]
public async Task OpenAIAssistantAgentWithKernelAsync()
{
var text =
"""
type: openai_assistant
name: StoryAgent
description: Store Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: gpt-4o-mini
""";
OpenAIAssistantAgentFactory factory = new();

var agent = await factory.CreateAgentFromYamlAsync(text, this._kernel) as OpenAIAssistantAgent;

await InvokeAgentAsync(agent!, "Cats and Dogs");
}

#region private
private readonly Kernel _kernel;

/// <summary>
/// Invoke the agent with the user input.
/// </summary>
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string input)
{
// Create a thread for the agent conversation.
string threadId = await agent.Client.CreateThreadAsync(metadata: SampleMetadata);

try
{
await InvokeAgentAsync(input);
}
finally
{
await agent.Client.DeleteThreadAsync(threadId);
await agent.Client.DeleteAssistantAsync(agent.Id);
}

// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(threadId, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
{
WriteAgentChatMessage(response);
}
}
}
#endregion
}
Loading
Loading