openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Streaming/StreamingUpdate.cs

107lines · modecode

1using System.ClientModel.Primitives;
2using System.Collections.Generic;
3using System.Diagnostics.CodeAnalysis;
4using System.Net.ServerSentEvents;
5using System.Text.Json;
6
7namespace OpenAI.Assistants;
8
9/// <summary>
10/// Represents a single item of streamed Assistants API data.
11/// </summary>
12/// <remarks>
13/// Please note that this is the abstract base type. To access data, downcast an instance of this type to an
14/// appropriate, derived update type:
15/// <para>
16/// For messages: <see cref="MessageStatusUpdate"/>, <see cref="MessageContentUpdate"/>
17/// </para>
18/// <para>
19/// For runs and run steps: <see cref="RunUpdate"/>, <see cref="RunStepUpdate"/>, <see cref="RunStepDetailsUpdate"/>,
20/// <see cref="RequiredActionUpdate"/>
21/// </para>
22/// <para>
23/// For threads: <see cref="ThreadUpdate"/>
24/// </para>
25/// </remarks>
26[Experimental("OPENAI001")]
27public abstract partial class StreamingUpdate
28{
29 /// <summary>
30 /// A value indicating what type of event this update represents.
31 /// </summary>
32 /// <remarks>
33 /// Many events share the same response type. For example, <see cref="StreamingUpdateReason.RunCreated"/> and
34 /// <see cref="StreamingUpdateReason.RunCompleted"/> are both associated with a <see cref="ThreadRun"/> instance.
35 /// You can use the value of <see cref="UpdateKind"/> to differentiate between these events when the type is not
36 /// sufficient to do so.
37 /// </remarks>
38 public StreamingUpdateReason UpdateKind { get; }
39
40 internal StreamingUpdate(StreamingUpdateReason updateKind)
41 {
42 UpdateKind = updateKind;
43 }
44
45 internal static IEnumerable<StreamingUpdate> FromSseItem(SseItem<byte[]> sseItem)
46 {
47 StreamingUpdateReason updateKind = StreamingUpdateReasonExtensions.FromSseEventLabel(sseItem.EventType);
48 using JsonDocument dataDocument = JsonDocument.Parse(sseItem.Data);
49 JsonElement e = dataDocument.RootElement;
50 ModelReaderWriterOptions serializationOptions = ModelReaderWriterOptions.Json;
51
52 return updateKind switch
53 {
54 StreamingUpdateReason.ThreadCreated => ThreadUpdate.DeserializeThreadCreationUpdates(e, updateKind, serializationOptions),
55 StreamingUpdateReason.RunCreated
56 or StreamingUpdateReason.RunQueued
57 or StreamingUpdateReason.RunInProgress
58 or StreamingUpdateReason.RunCompleted
59 or StreamingUpdateReason.RunIncomplete
60 or StreamingUpdateReason.RunFailed
61 or StreamingUpdateReason.RunCancelling
62 or StreamingUpdateReason.RunCancelled
63 or StreamingUpdateReason.RunExpired => RunUpdate.DeserializeRunUpdates(e, updateKind, serializationOptions),
64 StreamingUpdateReason.RunRequiresAction => RequiredActionUpdate.DeserializeRequiredActionUpdates(e),
65 StreamingUpdateReason.RunStepCreated
66 or StreamingUpdateReason.RunStepInProgress
67 or StreamingUpdateReason.RunStepCompleted
68 or StreamingUpdateReason.RunStepFailed
69 or StreamingUpdateReason.RunStepCancelled
70 or StreamingUpdateReason.RunStepExpired => RunStepUpdate.DeserializeRunStepUpdates(e, updateKind, serializationOptions),
71 StreamingUpdateReason.MessageCreated
72 or StreamingUpdateReason.MessageInProgress
73 or StreamingUpdateReason.MessageCompleted
74 or StreamingUpdateReason.MessageFailed => MessageStatusUpdate.DeserializeMessageStatusUpdates(e, updateKind, serializationOptions),
75 StreamingUpdateReason.RunStepUpdated => RunStepDetailsUpdate.DeserializeRunStepDetailsUpdates(e, updateKind, serializationOptions),
76 StreamingUpdateReason.MessageUpdated => MessageContentUpdate.DeserializeMessageContentUpdates(e, updateKind, serializationOptions),
77 _ => null,
78 };
79 }
80}
81
82/// <summary>
83/// Represents a single item of streamed data that encapsulates an underlying response value type.
84/// </summary>
85/// <typeparam name="T"> The response value type of the "delta" payload. </typeparam>
86[Experimental("OPENAI001")]
87public partial class StreamingUpdate<T> : StreamingUpdate
88 where T : class
89{
90 /// <summary>
91 /// The underlying response value received with the streaming event.
92 /// </summary>
93 public T Value { get; }
94
95 internal StreamingUpdate(T value, StreamingUpdateReason updateKind)
96 : base(updateKind)
97 {
98 Value = value;
99 }
100
101 /// <summary>
102 /// Implicit operator that allows the underlying value type of the <see cref="StreamingUpdate{T}"/> to be used
103 /// directly.
104 /// </summary>
105 /// <param name="update"></param>
106 public static implicit operator T(StreamingUpdate<T> update) => update.Value;
107}
108