From 42631634cfe608f0f338df2e94b10e83bba43337 Mon Sep 17 00:00:00 2001 From: Rainer Date: Tue, 11 Jun 2024 13:15:35 +0200 Subject: [PATCH] Add code interpreter logging --- labs/035-assistants-dotnet/OpenAIExtensions.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/labs/035-assistants-dotnet/OpenAIExtensions.cs b/labs/035-assistants-dotnet/OpenAIExtensions.cs index 10b0554..f312cf1 100644 --- a/labs/035-assistants-dotnet/OpenAIExtensions.cs +++ b/labs/035-assistants-dotnet/OpenAIExtensions.cs @@ -1,3 +1,5 @@ +using System.Text; +using System.Text.Json; using OpenAI; using OpenAI.Assistants; #pragma warning disable OPENAI001 @@ -43,6 +45,7 @@ public static async IAsyncEnumerable AddMessageAndRunToCompletion(this A var asyncUpdate = client.CreateRunStreamingAsync(threadId, assistantId); ThreadRun? currentRun; + var codeInterpreterCode = new StringBuilder(); do { currentRun = null; @@ -68,6 +71,11 @@ public static async IAsyncEnumerable AddMessageAndRunToCompletion(this A outputsToSumit.Add(new ToolOutput(requiredActionUpdate.ToolCallId, functionResponse)); } + else if (update is RunStepDetailsUpdate runStepDetailsUpdate + && !string.IsNullOrEmpty(runStepDetailsUpdate.CodeInterpreterInput)) + { + codeInterpreterCode.Append(runStepDetailsUpdate.CodeInterpreterInput); + } else if (update is MessageContentUpdate contentUpdate) { yield return contentUpdate.Text; @@ -80,6 +88,12 @@ public static async IAsyncEnumerable AddMessageAndRunToCompletion(this A } } while (currentRun?.Status.IsTerminal is false); + + if (codeInterpreterCode.Length > 0) + { + Console.WriteLine("\n\nCODE INTERPRETER:"); + Console.WriteLine(codeInterpreterCode.ToString()); + } } public static async Task GetLatestMessage(this AssistantClient client, string threadId)