@@ -22,6 +22,7 @@ public static async Task RunTests(IOpenAIService openAI)
22
22
await RunCancelTests ( openAI ) ;
23
23
await RunToolTests ( openAI ) ;
24
24
await RunThreadAndRunTests ( openAI ) ;
25
+ await RunStreamTests ( openAI ) ;
25
26
await Cleanup ( openAI ) ;
26
27
}
27
28
@@ -56,11 +57,28 @@ public static async Task RunToolTests(IOpenAIService openAI)
56
57
await Cleanup ( openAI ) ;
57
58
}
58
59
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
+
59
76
public static async Task RunThreadAndRunTests ( IOpenAIService openAI )
60
77
{
61
78
ConsoleExtensions . WriteLine ( "Run Thread and Run Testing is starting:" , ConsoleColor . Blue ) ;
62
79
await CreateThreadAndRun ( openAI ) ;
63
80
}
81
+
64
82
65
83
public static async Task CreateRunTest ( IOpenAIService openAI )
66
84
{
@@ -111,6 +129,65 @@ public static async Task CreateRunTest(IOpenAIService openAI)
111
129
}
112
130
}
113
131
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
+
114
191
public static async Task CreateToolRunTest ( IOpenAIService openAI )
115
192
{
116
193
ConsoleExtensions . WriteLine ( "Run Create Tool Testing is starting:" , ConsoleColor . Cyan ) ;
@@ -343,6 +420,55 @@ public static async Task SubmitToolOutputsToRunTest(IOpenAIService openAI)
343
420
}
344
421
}
345
422
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
+
346
472
public static async Task CancelRunTest ( IOpenAIService openAI )
347
473
{
348
474
ConsoleExtensions . WriteLine ( "Run Cancel Testing is starting:" , ConsoleColor . Cyan ) ;
@@ -488,6 +614,56 @@ public static async Task CreateThreadAndRun(IOpenAIService sdk)
488
614
}
489
615
}
490
616
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
+
491
667
public static async Task Cleanup ( IOpenAIService sdk )
492
668
{
493
669
ConsoleExtensions . WriteLine ( "Cleanup Testing is starting:" , ConsoleColor . Cyan ) ;
@@ -496,6 +672,7 @@ public static async Task Cleanup(IOpenAIService sdk)
496
672
var threadResult = await sdk . Beta . Threads . ThreadDelete ( CreatedThreadId ) ;
497
673
if ( threadResult . Successful )
498
674
{
675
+ CreatedThreadId = null ;
499
676
ConsoleExtensions . WriteLine ( "Thread Deleted Successfully." , ConsoleColor . Green ) ;
500
677
}
501
678
else
@@ -509,6 +686,7 @@ public static async Task Cleanup(IOpenAIService sdk)
509
686
var assistantResult = await sdk . Beta . Assistants . AssistantDelete ( CreatedAssistantId ) ;
510
687
if ( assistantResult . Successful )
511
688
{
689
+ CreatedAssistantId = null ;
512
690
ConsoleExtensions . WriteLine ( "Assistant Deleted Successfully." , ConsoleColor . Green ) ;
513
691
}
514
692
else
0 commit comments