Skip to content

Commit

Permalink
Add OpenAI tools integration test for streaming / multiple function c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
stephentoub committed Dec 17, 2023
1 parent d3dd5c8 commit a8409a1
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAIToolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ void MyInvokingHandler(object? sender, FunctionInvokingEventArgs e)
Assert.Contains("GetCurrentUtcTime", invokedFunctions);
}

[Fact]
public async Task CanAutoInvokeKernelFunctionsStreamingAsync()
{
// Arrange
Kernel kernel = this.InitializeKernel();
kernel.ImportPluginFromType<TimeInformation>();

var invokedFunctions = new List<string>();
void MyInvokingHandler(object? sender, FunctionInvokingEventArgs e)
{
invokedFunctions.Add($"{e.Function.Name}({string.Join(", ", e.Arguments)})");
}
kernel.FunctionInvoking += MyInvokingHandler;

// Act
OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
string result = "";
await foreach (string c in kernel.InvokePromptStreamingAsync<string>(
$"How much older is John than Jim? Compute that value and pass it to the {nameof(TimeInformation)}.{nameof(TimeInformation.InterpretValue)} function, then respond only with its result.",
new(settings)))
{
result += c;
}

// Assert
Assert.Equal("6", result);
Assert.Contains("GetAge([personName, John])", invokedFunctions);
Assert.Contains("GetAge([personName, Jim])", invokedFunctions);
Assert.Contains("InterpretValue([value, 3])", invokedFunctions);
}

private Kernel InitializeKernel()
{
OpenAIConfiguration? openAIConfiguration = this._configuration.GetSection("Planners:OpenAI").Get<OpenAIConfiguration>();
Expand All @@ -70,24 +101,7 @@ private Kernel InitializeKernel()
private readonly RedirectOutput _testOutputHelper;
private readonly IConfigurationRoot _configuration;

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

~OpenAIToolsTests()
{
this.Dispose(false);
}

private void Dispose(bool disposing)
{
if (disposing)
{
this._testOutputHelper.Dispose();
}
}
public void Dispose() => this._testOutputHelper.Dispose();

/// <summary>
/// A plugin that returns the current time.
Expand All @@ -97,5 +111,25 @@ public class TimeInformation
[KernelFunction]
[Description("Retrieves the current time in UTC.")]
public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");

[KernelFunction]
[Description("Gets the age of the specified person.")]
public int GetAge(string personName)
{
if ("John".Equals(personName, StringComparison.OrdinalIgnoreCase))
{
return 33;
}

if ("Jim".Equals(personName, StringComparison.OrdinalIgnoreCase))
{
return 30;
}

return -1;
}

[KernelFunction]
public int InterpretValue(int value) => value * 2;
}
}

0 comments on commit a8409a1

Please sign in to comment.