Skip to content

Commit

Permalink
Add bug reproduction
Browse files Browse the repository at this point in the history
  • Loading branch information
rstropek committed Sep 17, 2024
1 parent ab826da commit 3e16d90
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
16 changes: 16 additions & 0 deletions labs/090-bug-repro/090-bug-repro.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>_090_bug_repro</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" />
<PackageReference Include="OpenAI" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions labs/090-bug-repro/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.ClientModel;
using System.Text.Json;
using dotenv.net;
using OpenAI.Assistants;
#pragma warning disable OPENAI001

var env = DotEnv.Read(options: new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 7));

var client = new AssistantClient(env["OPENAI_KEY"]);

var assistant = await client.CreateAssistantAsync(env["OPENAI_MODEL"], new AssistantCreationOptions
{
Name = "Test Assistant",
Description = "Test Assistant",
Instructions = "You are a helpful assistant that can answer questions about the secret number.",
Tools = {
new CodeInterpreterToolDefinition(),
new FunctionToolDefinition()
{
FunctionName = "getSecretNumber",
Description = "Gets the secret number",
Parameters = BinaryData.FromObjectAsJson(
new
{
Type = "object",
Properties = new
{
Seed = new { Type = "integer", Description = "Optional seed for the secret number." },
},
Required = Array.Empty<string>()
}, new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
}
}
});

var thread = await client.CreateThreadAsync();
Console.WriteLine($"Thread ID: {thread.Value.Id}");

// The following line works (no function call)
//await client.CreateMessageAsync(thread.Value.Id, MessageRole.User, ["Are dolphins fish?"]);

// The following line crashes (function call)
await client.CreateMessageAsync(thread.Value.Id, MessageRole.User, ["Tell me the scret number with seed 1"]);

AsyncCollectionResult<StreamingUpdate> asyncUpdate = client.CreateRunStreamingAsync(thread.Value.Id, assistant.Value.Id);
Console.WriteLine();
ThreadRun? currentRun;
do
{
List<ToolOutput> outputsToSumit = [];
currentRun = null;
await foreach (StreamingUpdate update in asyncUpdate)
{
if (update is RunUpdate runUpdate) { currentRun = runUpdate; }
else if (update is RequiredActionUpdate requiredActionUpdate)
{
Console.WriteLine($"Calling function {requiredActionUpdate.FunctionName} {requiredActionUpdate.FunctionArguments}");
outputsToSumit.Add(new ToolOutput(requiredActionUpdate.ToolCallId, "{ \"SecretNumber\": 42 }"));
}
else if (update is MessageContentUpdate contentUpdate)
{
Console.Write(contentUpdate.Text);
}

if (outputsToSumit.Count != 0)
{
asyncUpdate = client.SubmitToolOutputsToRunStreamingAsync(currentRun, outputsToSumit);
}
}

if (outputsToSumit.Count != 0)
{
asyncUpdate = client.SubmitToolOutputsToRunStreamingAsync(currentRun, outputsToSumit);
}
} while (currentRun?.Status.IsTerminal is false);

Console.WriteLine("\nDone.");
6 changes: 6 additions & 0 deletions labs/HandsOnLabs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionCallingDotNet", "02
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "035-assistants-dotnet", "035-assistants-dotnet\035-assistants-dotnet.csproj", "{E3AACF07-5434-4B5C-820D-B5E1A661C6D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "090-bug-repro", "090-bug-repro\090-bug-repro.csproj", "{6FBC2C6A-A3D4-4459-A995-C94B9213C1CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,6 +36,10 @@ Global
{E3AACF07-5434-4B5C-820D-B5E1A661C6D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3AACF07-5434-4B5C-820D-B5E1A661C6D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3AACF07-5434-4B5C-820D-B5E1A661C6D5}.Release|Any CPU.Build.0 = Release|Any CPU
{6FBC2C6A-A3D4-4459-A995-C94B9213C1CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6FBC2C6A-A3D4-4459-A995-C94B9213C1CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FBC2C6A-A3D4-4459-A995-C94B9213C1CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FBC2C6A-A3D4-4459-A995-C94B9213C1CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6569C5F1-0230-43E5-9041-1CD4C214F748} = {ECF40993-4AD0-441E-93E7-F80E62465259}
Expand Down

0 comments on commit 3e16d90

Please sign in to comment.