From 3e16d90d67edb14d7b44bd9647ddd68e4c88e8d8 Mon Sep 17 00:00:00 2001 From: Rainer Date: Tue, 17 Sep 2024 07:49:45 +0200 Subject: [PATCH] Add bug reproduction --- labs/090-bug-repro/090-bug-repro.csproj | 16 +++++ labs/090-bug-repro/Program.cs | 77 +++++++++++++++++++++++++ labs/HandsOnLabs.sln | 6 ++ 3 files changed, 99 insertions(+) create mode 100644 labs/090-bug-repro/090-bug-repro.csproj create mode 100644 labs/090-bug-repro/Program.cs diff --git a/labs/090-bug-repro/090-bug-repro.csproj b/labs/090-bug-repro/090-bug-repro.csproj new file mode 100644 index 0000000..a67b4a1 --- /dev/null +++ b/labs/090-bug-repro/090-bug-repro.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + _090_bug_repro + enable + enable + + + + + + + + diff --git a/labs/090-bug-repro/Program.cs b/labs/090-bug-repro/Program.cs new file mode 100644 index 0000000..ba1bef9 --- /dev/null +++ b/labs/090-bug-repro/Program.cs @@ -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() + }, 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 asyncUpdate = client.CreateRunStreamingAsync(thread.Value.Id, assistant.Value.Id); +Console.WriteLine(); +ThreadRun? currentRun; +do +{ + List 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."); diff --git a/labs/HandsOnLabs.sln b/labs/HandsOnLabs.sln index c4614bd..93d8f8a 100644 --- a/labs/HandsOnLabs.sln +++ b/labs/HandsOnLabs.sln @@ -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 @@ -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}