Skip to content

Commit 26f12ea

Browse files
Added a model factory to Responses namespace (#566)
* added a model factory to responses * pr feedbcak * moved attribute
1 parent 27ac530 commit 26f12ea

File tree

5 files changed

+279
-2
lines changed

5 files changed

+279
-2
lines changed

api/OpenAI.net8.0.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ public class AudioClient {
10561056
public AudioClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
10571057
public AudioClient(string model, ApiKeyCredential credential);
10581058
public AudioClient(string model, string apiKey);
1059+
public string Model { get; }
10591060
public ClientPipeline Pipeline { get; }
10601061
public virtual ClientResult GenerateSpeech(BinaryContent content, RequestOptions options = null);
10611062
public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -1403,6 +1404,7 @@ public class ChatClient {
14031404
public ChatClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
14041405
public ChatClient(string model, ApiKeyCredential credential);
14051406
public ChatClient(string model, string apiKey);
1407+
public string Model { get; }
14061408
public ClientPipeline Pipeline { get; }
14071409
public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] messages);
14081410
public virtual ClientResult CompleteChat(BinaryContent content, RequestOptions options = null);
@@ -2277,6 +2279,7 @@ public class EmbeddingClient {
22772279
public EmbeddingClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
22782280
public EmbeddingClient(string model, ApiKeyCredential credential);
22792281
public EmbeddingClient(string model, string apiKey);
2282+
public string Model { get; }
22802283
public ClientPipeline Pipeline { get; }
22812284
public virtual ClientResult<OpenAIEmbedding> GenerateEmbedding(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default);
22822285
public virtual Task<ClientResult<OpenAIEmbedding>> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -3239,6 +3242,7 @@ public class ImageClient {
32393242
public ImageClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
32403243
public ImageClient(string model, ApiKeyCredential credential);
32413244
public ImageClient(string model, string apiKey);
3245+
public string Model { get; }
32423246
public ClientPipeline Pipeline { get; }
32433247
public virtual ClientResult<GeneratedImage> GenerateImage(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default);
32443248
public virtual Task<ClientResult<GeneratedImage>> GenerateImageAsync(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -3425,6 +3429,7 @@ public class ModerationClient {
34253429
public ModerationClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
34263430
public ModerationClient(string model, ApiKeyCredential credential);
34273431
public ModerationClient(string model, string apiKey);
3432+
public string Model { get; }
34283433
public ClientPipeline Pipeline { get; }
34293434
public virtual ClientResult ClassifyText(BinaryContent content, RequestOptions options = null);
34303435
public virtual ClientResult<ModerationResultCollection> ClassifyText(IEnumerable<string> inputs, CancellationToken cancellationToken = default);
@@ -4605,6 +4610,13 @@ public class OpenAIResponseClient {
46054610
public virtual CollectionResult<StreamingResponseUpdate> GetResponseStreaming(string responseId, int? startingAfter = null, CancellationToken cancellationToken = default);
46064611
public virtual AsyncCollectionResult<StreamingResponseUpdate> GetResponseStreamingAsync(string responseId, int? startingAfter = null, CancellationToken cancellationToken = default);
46074612
}
4613+
public static class OpenAIResponsesModelFactory {
4614+
public static MessageResponseItem MessageResponseItem(string id = null, MessageRole role = MessageRole.Assistant, MessageStatus? status = null);
4615+
public static OpenAIResponse OpenAIResponse(string id = null, DateTimeOffset createdAt = default, ResponseStatus? status = null, ResponseError error = null, ResponseTokenUsage usage = null, string endUserId = null, ResponseReasoningOptions reasoningOptions = null, int? maxOutputTokenCount = null, ResponseTextOptions textOptions = null, ResponseTruncationMode? truncationMode = null, ResponseIncompleteStatusDetails incompleteStatusDetails = null, IEnumerable<ResponseItem> outputItems = null, bool parallelToolCallsEnabled = false, ResponseToolChoice toolChoice = null, string model = null, IDictionary<string, string> metadata = null, float? temperature = null, float? topP = null, string previousResponseId = null, bool? background = null, string instructions = null, IEnumerable<ResponseTool> tools = null);
4616+
public static ReasoningResponseItem ReasoningResponseItem(string id = null, string encryptedContent = null, ReasoningStatus? status = null, IEnumerable<ReasoningSummaryPart> summaryParts = null);
4617+
public static ReasoningResponseItem ReasoningResponseItem(string id = null, string encryptedContent = null, ReasoningStatus? status = null, string summaryText = null);
4618+
public static ReferenceResponseItem ReferenceResponseItem(string id = null);
4619+
}
46084620
[Experimental("OPENAI001")]
46094621
public class ReasoningResponseItem : ResponseItem, IJsonModel<ReasoningResponseItem>, IPersistableModel<ReasoningResponseItem> {
46104622
public ReasoningResponseItem(IEnumerable<ReasoningSummaryPart> summaryParts);

api/OpenAI.netstandard2.0.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ public class AudioClient {
967967
public AudioClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
968968
public AudioClient(string model, ApiKeyCredential credential);
969969
public AudioClient(string model, string apiKey);
970+
public string Model { get; }
970971
public ClientPipeline Pipeline { get; }
971972
public virtual ClientResult GenerateSpeech(BinaryContent content, RequestOptions options = null);
972973
public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -1266,6 +1267,7 @@ public class ChatClient {
12661267
public ChatClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
12671268
public ChatClient(string model, ApiKeyCredential credential);
12681269
public ChatClient(string model, string apiKey);
1270+
public string Model { get; }
12691271
public ClientPipeline Pipeline { get; }
12701272
public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] messages);
12711273
public virtual ClientResult CompleteChat(BinaryContent content, RequestOptions options = null);
@@ -1980,6 +1982,7 @@ public class EmbeddingClient {
19801982
public EmbeddingClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
19811983
public EmbeddingClient(string model, ApiKeyCredential credential);
19821984
public EmbeddingClient(string model, string apiKey);
1985+
public string Model { get; }
19831986
public ClientPipeline Pipeline { get; }
19841987
public virtual ClientResult<OpenAIEmbedding> GenerateEmbedding(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default);
19851988
public virtual Task<ClientResult<OpenAIEmbedding>> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -2840,6 +2843,7 @@ public class ImageClient {
28402843
public ImageClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
28412844
public ImageClient(string model, ApiKeyCredential credential);
28422845
public ImageClient(string model, string apiKey);
2846+
public string Model { get; }
28432847
public ClientPipeline Pipeline { get; }
28442848
public virtual ClientResult<GeneratedImage> GenerateImage(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default);
28452849
public virtual Task<ClientResult<GeneratedImage>> GenerateImageAsync(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default);
@@ -2995,6 +2999,7 @@ public class ModerationClient {
29952999
public ModerationClient(string model, ApiKeyCredential credential, OpenAIClientOptions options);
29963000
public ModerationClient(string model, ApiKeyCredential credential);
29973001
public ModerationClient(string model, string apiKey);
3002+
public string Model { get; }
29983003
public ClientPipeline Pipeline { get; }
29993004
public virtual ClientResult ClassifyText(BinaryContent content, RequestOptions options = null);
30003005
public virtual ClientResult<ModerationResultCollection> ClassifyText(IEnumerable<string> inputs, CancellationToken cancellationToken = default);
@@ -4081,6 +4086,13 @@ public class OpenAIResponseClient {
40814086
public virtual CollectionResult<StreamingResponseUpdate> GetResponseStreaming(string responseId, int? startingAfter = null, CancellationToken cancellationToken = default);
40824087
public virtual AsyncCollectionResult<StreamingResponseUpdate> GetResponseStreamingAsync(string responseId, int? startingAfter = null, CancellationToken cancellationToken = default);
40834088
}
4089+
public static class OpenAIResponsesModelFactory {
4090+
public static MessageResponseItem MessageResponseItem(string id = null, MessageRole role = MessageRole.Assistant, MessageStatus? status = null);
4091+
public static OpenAIResponse OpenAIResponse(string id = null, DateTimeOffset createdAt = default, ResponseStatus? status = null, ResponseError error = null, ResponseTokenUsage usage = null, string endUserId = null, ResponseReasoningOptions reasoningOptions = null, int? maxOutputTokenCount = null, ResponseTextOptions textOptions = null, ResponseTruncationMode? truncationMode = null, ResponseIncompleteStatusDetails incompleteStatusDetails = null, IEnumerable<ResponseItem> outputItems = null, bool parallelToolCallsEnabled = false, ResponseToolChoice toolChoice = null, string model = null, IDictionary<string, string> metadata = null, float? temperature = null, float? topP = null, string previousResponseId = null, bool? background = null, string instructions = null, IEnumerable<ResponseTool> tools = null);
4092+
public static ReasoningResponseItem ReasoningResponseItem(string id = null, string encryptedContent = null, ReasoningStatus? status = null, IEnumerable<ReasoningSummaryPart> summaryParts = null);
4093+
public static ReasoningResponseItem ReasoningResponseItem(string id = null, string encryptedContent = null, ReasoningStatus? status = null, string summaryText = null);
4094+
public static ReferenceResponseItem ReferenceResponseItem(string id = null);
4095+
}
40844096
public class ReasoningResponseItem : ResponseItem, IJsonModel<ReasoningResponseItem>, IPersistableModel<ReasoningResponseItem> {
40854097
public ReasoningResponseItem(IEnumerable<ReasoningSummaryPart> summaryParts);
40864098
public ReasoningResponseItem(string summaryText);

src/Custom/Responses/Items/ReasoningResponseItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Diagnostics.CodeAnalysis;
32
using System.Linq;
43

54
namespace OpenAI.Responses;
@@ -12,7 +11,7 @@ public partial class ReasoningResponseItem
1211
{
1312
// CUSTOM: Retain optionality of OpenAPI read-only property value
1413
[CodeGenMember("Status")]
15-
public ReasoningStatus? Status { get; }
14+
public ReasoningStatus? Status { get; internal set; }
1615

1716
// CUSTOM: Rename for collection clarity
1817
[CodeGenMember("Summary")]
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
5+
6+
namespace OpenAI.Responses;
7+
8+
/// <summary> Model factory for models. </summary>
9+
[Experimental("OPENAI001")]
10+
public static partial class OpenAIResponsesModelFactory
11+
{
12+
/// <summary> Initializes a new instance of <see cref="OpenAI.Responses.OpenAIResponse"/>. </summary>
13+
/// <returns> A new <see cref="OpenAI.Responses.OpenAIResponse"/> instance for mocking. </returns>
14+
public static OpenAIResponse OpenAIResponse(
15+
string id = null,
16+
DateTimeOffset createdAt = default,
17+
ResponseStatus? status = null,
18+
ResponseError error = null,
19+
ResponseTokenUsage usage = null,
20+
string endUserId = null,
21+
ResponseReasoningOptions reasoningOptions = null,
22+
int? maxOutputTokenCount = null,
23+
ResponseTextOptions textOptions = null,
24+
ResponseTruncationMode? truncationMode = null,
25+
ResponseIncompleteStatusDetails incompleteStatusDetails = null,
26+
IEnumerable<ResponseItem> outputItems = null,
27+
bool parallelToolCallsEnabled = default,
28+
ResponseToolChoice toolChoice = null,
29+
string model = null,
30+
IDictionary<string, string> metadata = null,
31+
float? temperature = null,
32+
float? topP = null,
33+
string previousResponseId = null,
34+
bool? background = null,
35+
string instructions = null,
36+
IEnumerable<ResponseTool> tools = null)
37+
{
38+
outputItems ??= new List<ResponseItem>();
39+
tools ??= new List<ResponseTool>();
40+
metadata ??= new Dictionary<string, string>();
41+
42+
return new OpenAIResponse(
43+
metadata: metadata,
44+
temperature: temperature,
45+
topP: topP,
46+
serviceTier: null,
47+
previousResponseId: previousResponseId,
48+
background: background,
49+
instructions: instructions,
50+
tools: tools.ToList(),
51+
id: id,
52+
status: status,
53+
createdAt: createdAt,
54+
error: error,
55+
usage: usage,
56+
endUserId: endUserId,
57+
reasoningOptions: reasoningOptions,
58+
maxOutputTokenCount: maxOutputTokenCount,
59+
textOptions: textOptions,
60+
truncationMode: truncationMode,
61+
incompleteStatusDetails: incompleteStatusDetails,
62+
outputItems: outputItems.ToList(),
63+
parallelToolCallsEnabled: parallelToolCallsEnabled,
64+
toolChoice: toolChoice,
65+
model: model,
66+
@object: "response",
67+
additionalBinaryDataProperties: null);
68+
}
69+
70+
/// <summary> Initializes a new instance of <see cref="OpenAI.Responses.MessageResponseItem"/>. </summary>
71+
/// <returns> A new <see cref="OpenAI.Responses.MessageResponseItem"/> instance for mocking. </returns>
72+
public static MessageResponseItem MessageResponseItem(
73+
string id = null,
74+
MessageRole role = MessageRole.Assistant,
75+
MessageStatus? status = null)
76+
{
77+
// Convert the public MessageRole to the internal role type
78+
InternalResponsesMessageRole internalRole = role.ToSerialString();
79+
80+
return new MessageResponseItem(
81+
id: id,
82+
internalRole: internalRole,
83+
status: status);
84+
}
85+
86+
/// <summary> Initializes a new instance of <see cref="OpenAI.Responses.ReasoningResponseItem"/>. </summary>
87+
/// <param name="id">The ID of the reasoning response item.</param>
88+
/// <param name="encryptedContent">The encrypted reasoning content.</param>
89+
/// <param name="status">The status of the reasoning response item.</param>
90+
/// <param name="summaryParts">The collection of summary parts.</param>
91+
/// <returns> A new <see cref="OpenAI.Responses.ReasoningResponseItem"/> instance for mocking. </returns>
92+
public static ReasoningResponseItem ReasoningResponseItem(
93+
string id = null,
94+
string encryptedContent = null,
95+
ReasoningStatus? status = null,
96+
IEnumerable<ReasoningSummaryPart> summaryParts = null)
97+
{
98+
summaryParts ??= new List<ReasoningSummaryPart>();
99+
100+
var item = new ReasoningResponseItem(
101+
kind: InternalItemType.Reasoning,
102+
id: id,
103+
additionalBinaryDataProperties: null,
104+
encryptedContent: encryptedContent,
105+
summaryParts: summaryParts.ToList());
106+
107+
item.Status = status;
108+
return item;
109+
}
110+
111+
/// <summary> Initializes a new instance of <see cref="OpenAI.Responses.ReasoningResponseItem"/> with summary text. </summary>
112+
/// <param name="id">The ID of the reasoning response item.</param>
113+
/// <param name="encryptedContent">The encrypted reasoning content.</param>
114+
/// <param name="status">The status of the reasoning response item.</param>
115+
/// <param name="summaryText">The summary text to create a ReasoningSummaryTextPart from.</param>
116+
/// <returns> A new <see cref="OpenAI.Responses.ReasoningResponseItem"/> instance for mocking. </returns>
117+
public static ReasoningResponseItem ReasoningResponseItem(
118+
string id = null,
119+
string encryptedContent = null,
120+
ReasoningStatus? status = null,
121+
string summaryText = null)
122+
{
123+
var summaryParts = !string.IsNullOrEmpty(summaryText)
124+
? new List<ReasoningSummaryPart> { new ReasoningSummaryTextPart(summaryText) }
125+
: new List<ReasoningSummaryPart>();
126+
127+
var item = new ReasoningResponseItem(
128+
kind: InternalItemType.Reasoning,
129+
id: id,
130+
additionalBinaryDataProperties: null,
131+
encryptedContent: encryptedContent,
132+
summaryParts: summaryParts);
133+
134+
item.Status = status;
135+
return item;
136+
}
137+
138+
/// <summary> Initializes a new instance of <see cref="OpenAI.Responses.ReferenceResponseItem"/>. </summary>
139+
/// <returns> A new <see cref="OpenAI.Responses.ReferenceResponseItem"/> instance for mocking. </returns>
140+
public static ReferenceResponseItem ReferenceResponseItem(
141+
string id = null)
142+
{
143+
return new ReferenceResponseItem(
144+
kind: InternalItemType.ItemReference,
145+
id: id,
146+
additionalBinaryDataProperties: null);
147+
}
148+
}

0 commit comments

Comments
 (0)