Skip to content

Commit 025f684

Browse files
authored
Merge pull request #590 from betalgo/dev
8.4.0
2 parents 7772cf3 + 85bfe12 commit 025f684

File tree

11 files changed

+542
-223
lines changed

11 files changed

+542
-223
lines changed

OpenAI.Playground/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using OpenAI.Extensions;
55
using OpenAI.Interfaces;
66
using OpenAI.Playground.TestHelpers;
7+
using OpenAI.Playground.TestHelpers.AssistantHelpers;
78

89
var builder = new ConfigurationBuilder().AddJsonFile("ApiSettings.json")
910
.AddUserSecrets<Program>();

OpenAI.Playground/TestHelpers/AssistantHelpers/RunTestHelper.cs

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static async Task RunTests(IOpenAIService openAI)
2222
await RunCancelTests(openAI);
2323
await RunToolTests(openAI);
2424
await RunThreadAndRunTests(openAI);
25+
await RunStreamTests(openAI);
2526
await Cleanup(openAI);
2627
}
2728

@@ -56,11 +57,28 @@ public static async Task RunToolTests(IOpenAIService openAI)
5657
await Cleanup(openAI);
5758
}
5859

60+
public static async Task RunStreamTests(IOpenAIService openAI)
61+
{
62+
ConsoleExtensions.WriteLine("Run Stream Testing is starting:", ConsoleColor.Blue);
63+
await CreateRunAsStreamTest(openAI);
64+
await Cleanup(openAI);
65+
await CreateThreadAndRunAsStream(openAI);
66+
await Cleanup(openAI);
67+
await CreateToolRunTest(openAI);
68+
await ListRunsTest(openAI);
69+
await RetrieveRunTest(openAI);
70+
await ModifyRunTest(openAI);
71+
await WaitUntil(openAI, "requires_action");
72+
await SubmitToolOutputsAsStreamToRunTest(openAI);
73+
await Cleanup(openAI);
74+
}
75+
5976
public static async Task RunThreadAndRunTests(IOpenAIService openAI)
6077
{
6178
ConsoleExtensions.WriteLine("Run Thread and Run Testing is starting:", ConsoleColor.Blue);
6279
await CreateThreadAndRun(openAI);
6380
}
81+
6482

6583
public static async Task CreateRunTest(IOpenAIService openAI)
6684
{
@@ -111,6 +129,65 @@ public static async Task CreateRunTest(IOpenAIService openAI)
111129
}
112130
}
113131

132+
public static async Task CreateRunAsStreamTest(IOpenAIService openAI)
133+
{
134+
ConsoleExtensions.WriteLine("Run Create As Stream Testing is starting:", ConsoleColor.Cyan);
135+
var assistantResult = await openAI.Beta.Assistants.AssistantCreate(new()
136+
{
137+
Instructions = "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
138+
Name = "Math Tutor",
139+
Tools = [ToolDefinition.DefineCodeInterpreter()],
140+
Model = Models.Gpt_4_turbo
141+
});
142+
if (assistantResult.Successful)
143+
{
144+
CreatedAssistantId = assistantResult.Id;
145+
ConsoleExtensions.WriteLine($"Assistant Created Successfully with ID: {assistantResult.Id}", ConsoleColor.Green);
146+
}
147+
else
148+
{
149+
ConsoleExtensions.WriteError(assistantResult.Error);
150+
return;
151+
}
152+
153+
var threadResult = await openAI.Beta.Threads.ThreadCreate();
154+
if (threadResult.Successful)
155+
{
156+
CreatedThreadId = threadResult.Id;
157+
ConsoleExtensions.WriteLine($"Thread Created Successfully with ID: {threadResult.Id}", ConsoleColor.Green);
158+
}
159+
else
160+
{
161+
ConsoleExtensions.WriteError(threadResult.Error);
162+
return;
163+
}
164+
165+
var result = openAI.Beta.Runs.RunCreateAsStream(CreatedThreadId, new()
166+
{
167+
AssistantId = assistantResult.Id
168+
});
169+
170+
await foreach (var run in result)
171+
{
172+
if (run.Successful)
173+
{
174+
if (string.IsNullOrEmpty(run.Status))
175+
{
176+
Console.Write(".");
177+
}
178+
else
179+
{
180+
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
181+
}
182+
}
183+
else
184+
{
185+
ConsoleExtensions.WriteError(run.Error);
186+
}
187+
}
188+
189+
}
190+
114191
public static async Task CreateToolRunTest(IOpenAIService openAI)
115192
{
116193
ConsoleExtensions.WriteLine("Run Create Tool Testing is starting:", ConsoleColor.Cyan);
@@ -343,6 +420,55 @@ public static async Task SubmitToolOutputsToRunTest(IOpenAIService openAI)
343420
}
344421
}
345422

423+
public static async Task SubmitToolOutputsAsStreamToRunTest(IOpenAIService openAI)
424+
{
425+
ConsoleExtensions.WriteLine("Submit Tool Outputs To Run Testing is starting:", ConsoleColor.Cyan);
426+
if (string.IsNullOrWhiteSpace(CreatedRunId))
427+
{
428+
ConsoleExtensions.WriteLine("Run Id is not found. Please create a run first.", ConsoleColor.Red);
429+
return;
430+
}
431+
432+
if (string.IsNullOrWhiteSpace(CreatedThreadId))
433+
{
434+
ConsoleExtensions.WriteLine("Thread Id is not found. Please create a thread first.", ConsoleColor.Red);
435+
return;
436+
}
437+
438+
var retrieveResult = await openAI.Beta.Runs.RunRetrieve(CreatedThreadId, CreatedRunId);
439+
var result = openAI.Beta.Runs.RunSubmitToolOutputsAsStream(CreatedThreadId, CreatedRunId, new()
440+
{
441+
ToolOutputs =
442+
[
443+
new()
444+
{
445+
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First()
446+
.Id,
447+
Output = "70 degrees and sunny."
448+
}
449+
]
450+
});
451+
452+
await foreach (var run in result)
453+
{
454+
if (run.Successful)
455+
{
456+
if (string.IsNullOrEmpty(run.Status))
457+
{
458+
Console.Write(".");
459+
}
460+
else
461+
{
462+
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
463+
}
464+
}
465+
else
466+
{
467+
ConsoleExtensions.WriteError(run.Error);
468+
}
469+
}
470+
}
471+
346472
public static async Task CancelRunTest(IOpenAIService openAI)
347473
{
348474
ConsoleExtensions.WriteLine("Run Cancel Testing is starting:", ConsoleColor.Cyan);
@@ -488,6 +614,56 @@ public static async Task CreateThreadAndRun(IOpenAIService sdk)
488614
}
489615
}
490616

617+
public static async Task CreateThreadAndRunAsStream(IOpenAIService sdk)
618+
{
619+
ConsoleExtensions.WriteLine("Create Thread and Run As Stream Testing is starting:", ConsoleColor.Cyan);
620+
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new()
621+
{
622+
Instructions = "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
623+
Name = "Math Tutor",
624+
Tools = [ToolDefinition.DefineCodeInterpreter()],
625+
Model = Models.Gpt_4_turbo
626+
});
627+
CreatedAssistantId = assistantResult.Id;
628+
var runResult = sdk.Beta.Runs.CreateThreadAndRunAsStream(new()
629+
{
630+
AssistantId = assistantResult.Id,
631+
Thread = new()
632+
{
633+
Messages =
634+
[
635+
new()
636+
{
637+
Role = StaticValues.AssistantsStatics.MessageStatics.Roles.User,
638+
Content = new("Explain deep learning to a 5 year old.")
639+
}
640+
]
641+
}
642+
});
643+
644+
await foreach (var run in runResult)
645+
{
646+
if (run.Successful)
647+
{
648+
if (string.IsNullOrEmpty(run.Status))
649+
{
650+
Console.Write(".");
651+
}
652+
else
653+
{
654+
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
655+
}
656+
}
657+
else
658+
{
659+
ConsoleExtensions.WriteError(run.Error);
660+
}
661+
}
662+
ConsoleExtensions.WriteLine("Create Thread and Run As Stream Test is successful.", ConsoleColor.Green);
663+
664+
665+
}
666+
491667
public static async Task Cleanup(IOpenAIService sdk)
492668
{
493669
ConsoleExtensions.WriteLine("Cleanup Testing is starting:", ConsoleColor.Cyan);
@@ -496,6 +672,7 @@ public static async Task Cleanup(IOpenAIService sdk)
496672
var threadResult = await sdk.Beta.Threads.ThreadDelete(CreatedThreadId);
497673
if (threadResult.Successful)
498674
{
675+
CreatedThreadId = null;
499676
ConsoleExtensions.WriteLine("Thread Deleted Successfully.", ConsoleColor.Green);
500677
}
501678
else
@@ -509,6 +686,7 @@ public static async Task Cleanup(IOpenAIService sdk)
509686
var assistantResult = await sdk.Beta.Assistants.AssistantDelete(CreatedAssistantId);
510687
if (assistantResult.Successful)
511688
{
689+
CreatedAssistantId = null;
512690
ConsoleExtensions.WriteLine("Assistant Deleted Successfully.", ConsoleColor.Green);
513691
}
514692
else

0 commit comments

Comments
 (0)