Skip to content

Commit

Permalink
.Net - Fix Assistant type conversion for function calling (#5707)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

- Fix to specify function types propertly
- Add support for ability to list agents.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

I experienced both issues and also worked with community who has also
experienced / identified both.


https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-4.2.1

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
  • Loading branch information
crickman and markwallace-microsoft committed Apr 2, 2024
1 parent 1b43bcc commit 0735e84
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
21 changes: 20 additions & 1 deletion dotnet/samples/KernelSyntaxExamples/Plugins/MenuPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Plugins;

public sealed class MenuPlugin
{
/// <summary>
/// Returns a mock item menu.
/// </summary>
[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()
Expand All @@ -18,11 +21,27 @@ public string GetSpecials()
";
}

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

/// <summary>
/// An item is 86'd when the kitchen cannot serve due to running out of ingredients.
/// </summary>
[KernelFunction, Description("Returns true if the kitchen has ran out of the item.")]
public bool IsItem86d(
[Description("The name of the menu item.")]
string menuItem,
[Description("The number of items requested.")]
int count)
{
return count < 3;
}
}
46 changes: 46 additions & 0 deletions dotnet/src/Experimental/Agents/AgentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,50 @@ public AgentBuilder WithFiles(params string[] fileIds)

return this;
}

/// <summary>
/// Retrieve defined agents from an Azure OpenAI endpoint.
/// </summary>
/// <remarks>
/// The <see cref="AgentReference.Id"/> can be used to retrieve a hydrated agent via <see cref="GetAsync(string, CancellationToken)"/>/
/// </remarks>
public static async Task<IList<AgentReference>> GetAzureOpenAIAgentsAsync(string endpoint, string apiKey, string? version = null)
{
endpoint = $"{endpoint}/openai";
version ??= "2024-02-15-preview";

var context = new OpenAIRestContext(endpoint!, apiKey, version);
var result = await context.ListAssistantModelsAsync().ConfigureAwait(false);

return
result.Select(
m =>
new AgentReference()
{
Id = m.Id,
Name = m.Name
}).ToArray();
}

/// <summary>
/// Retrieve defined agents from OpenAI services.
/// </summary>
/// <remarks>
/// The <see cref="AgentReference.Id"/> can be used to retrieve a hydrated agent via <see cref="GetAsync(string, CancellationToken)"/>/
/// </remarks>
public static async Task<IList<AgentReference>> GetOpenAIAgentsAsync(string apiKey)
{
var context = new OpenAIRestContext(OpenAIBaseUrl, apiKey);

var result = await context.ListAssistantModelsAsync().ConfigureAwait(false);

return
result.Select(
m =>
new AgentReference()
{
Id = m.Id,
Name = m.Name
}).ToArray();
}
}
19 changes: 19 additions & 0 deletions dotnet/src/Experimental/Agents/AgentReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.SemanticKernel.Experimental.Agents;

/// <summary>
/// Response from agent when called as a <see cref="KernelFunction"/>.
/// </summary>
public class AgentReference
{
/// <summary>
/// The agent identifier (which can be referenced in API endpoints).
/// </summary>
public string Id { get; internal set; } = string.Empty;

/// <summary>
/// Name of the agent
/// </summary>
public string? Name { get; internal set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,21 @@ private static string ConvertType(Type? type)
return "number";
}

if (type == typeof(bool))
{
return "boolean";
}

if (type.IsEnum)
{
return "enum";
}

return type.Name;
if (type.IsArray)
{
return "array";
}

return "object";
}
}

0 comments on commit 0735e84

Please sign in to comment.