Skip to content

Commit df1aab0

Browse files
committed
hide convenience APIs
1 parent 09b98ef commit df1aab0

13 files changed

+320
-382
lines changed

api/OpenAI.net8.0.cs

Lines changed: 16 additions & 89 deletions
Large diffs are not rendered by default.

api/OpenAI.netstandard2.0.cs

Lines changed: 16 additions & 85 deletions
Large diffs are not rendered by default.

src/Custom/Responses/CreateResponseOptions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal CreateResponseOptions(IDictionary<string, string> metadata, float? temp
8686

8787
public ResponseTruncationMode? Truncation { get; set; }
8888

89-
public IList<ResponseItem> Input { get; }
89+
public IList<ResponseItem> Input { get; internal set; }
9090

9191
public IList<Includable> Include { get; set; }
9292

@@ -125,5 +125,13 @@ [.. responseCreationOptions.Include.Select(x => x.ToIncludable())],
125125
responseCreationOptions.Stream,
126126
new JsonPatch());
127127
}
128+
129+
internal CreateResponseOptions GetClone()
130+
{
131+
CreateResponseOptions copiedOptions = (CreateResponseOptions)this.MemberwiseClone();
132+
copiedOptions.Patch = _patch;
133+
134+
return copiedOptions;
135+
}
128136
}
129137
}

src/Custom/Responses/OpenAIResponseClient.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ protected internal OpenAIResponseClient(ClientPipeline pipeline, string model, O
117117
/// Gets the endpoint URI for the service.
118118
/// </summary>
119119
[Experimental("OPENAI001")]
120-
public Uri Endpoint => _endpoint;
120+
public virtual Uri Endpoint => _endpoint;
121121

122122
/// <summary>
123123
/// Gets the name of the model used in requests sent to the service.
124124
/// </summary>
125125
[Experimental("OPENAI001")]
126-
public string Model => _model;
126+
public virtual string Model => _model;
127127

128128
internal virtual Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
129129
{
@@ -180,15 +180,15 @@ public virtual ClientResult<ResponseResult> CreateResponse(CreateResponseOptions
180180
{
181181
Argument.AssertNotNull(options, nameof(options));
182182

183-
ClientResult result = this.CreateResponse(options, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
183+
ClientResult result = this.CreateResponse(CreatePerCallOptions(options), cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
184184
return ClientResult.FromValue((ResponseResult)result.GetRawResponse().Content, result.GetRawResponse());
185185
}
186186

187187
public virtual async Task<ClientResult<ResponseResult>> CreateResponseAsync(CreateResponseOptions options, CancellationToken cancellationToken = default)
188188
{
189189
Argument.AssertNotNull(options, nameof(options));
190190

191-
ClientResult result = await this.CreateResponseAsync(options, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
191+
ClientResult result = await this.CreateResponseAsync(CreatePerCallOptions(options), cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
192192
return ClientResult.FromValue((ResponseResult)result.GetRawResponse().Content, result.GetRawResponse());
193193
}
194194

@@ -215,7 +215,7 @@ internal AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingA
215215

216216
public virtual AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(CreateResponseOptions options, CancellationToken cancellationToken = default)
217217
{
218-
return CreateResponseStreamingAsync(options, cancellationToken.ToRequestOptions(streaming: true));
218+
return CreateResponseStreamingAsync(CreatePerCallOptions(options, true), cancellationToken.ToRequestOptions(streaming: true));
219219
}
220220

221221
internal AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(CreateResponseOptions options, RequestOptions requestOptions)
@@ -249,7 +249,7 @@ public virtual CollectionResult<StreamingResponseUpdate> CreateResponseStreaming
249249
Argument.AssertNotNull(options, nameof(options));
250250

251251
return new SseUpdateCollection<StreamingResponseUpdate>(
252-
() => CreateResponse(options, cancellationToken.ToRequestOptions(streaming: true)),
252+
() => CreateResponse(CreatePerCallOptions(options, true), cancellationToken.ToRequestOptions(streaming: true)),
253253
StreamingResponseUpdate.DeserializeStreamingResponseUpdate,
254254
cancellationToken);
255255
}
@@ -438,7 +438,26 @@ internal virtual ResponseCreationOptions CreatePerCallOptions(ResponseCreationOp
438438
: userOptions.GetClone();
439439

440440
copiedOptions.Input = inputItems.ToList();
441-
copiedOptions.Model = _model;
441+
copiedOptions.Model = Model;
442+
443+
if (stream)
444+
{
445+
copiedOptions.Stream = true;
446+
}
447+
448+
return copiedOptions;
449+
}
450+
451+
internal virtual CreateResponseOptions CreatePerCallOptions(CreateResponseOptions userOptions, bool stream = false)
452+
{
453+
CreateResponseOptions copiedOptions = userOptions is null
454+
? new()
455+
: userOptions.GetClone();
456+
457+
if (copiedOptions.Model is null)
458+
{
459+
copiedOptions.Model = Model;
460+
}
442461

443462
if (stream)
444463
{

src/Custom/Responses/ResponseResult.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,14 @@ internal ResponseResult(IDictionary<string, string> metadata, float? temperature
118118
public ResponseTokenUsage Usage { get; }
119119

120120
public bool ParallelToolCalls { get; }
121+
122+
public string GetOutputText()
123+
{
124+
IEnumerable<string> outputTextSegments = Output.Where(item => item is InternalResponsesAssistantMessage)
125+
.Select(item => item as InternalResponsesAssistantMessage)
126+
.SelectMany(message => message.Content.Where(contentPart => contentPart.Kind == ResponseContentPartKind.OutputText)
127+
.Select(outputTextPart => outputTextPart.Text));
128+
return outputTextSegments.Any() ? string.Concat(outputTextSegments) : null;
129+
}
121130
}
122131
}

tests/Responses/ResponsesTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ public async Task StreamingResponses()
488488
}
489489
else if (update is StreamingResponseCompletedUpdate responseCompletedUpdate)
490490
{
491-
finalResponseText = responseCompletedUpdate.Response.OutputText;
491+
finalResponseText = responseCompletedUpdate.Response.Output[0] is MessageResponseItem messageItem
492+
? messageItem.Content[0].Text
493+
: null;
492494
}
493495
}
494496
Assert.That(deltaTextSegments, Has.Count.GreaterThan(0));
@@ -664,12 +666,13 @@ public async Task OutputTextMethod()
664666
{
665667
OpenAIResponseClient client = GetTestClient();
666668
ResponseResult response = await client.CreateResponseAsync(
667-
new ([ResponseItem.CreateUserMessageItem("Respond with only the word hello.")]));
668-
Assert.That(response?.OutputText?.Length, Is.GreaterThan(0).And.LessThan(7));
669-
Assert.That(response?.OutputText?.ToLower(), Does.Contain("hello"));
669+
new([ResponseItem.CreateUserMessageItem("Respond with only the word hello.")]));
670+
var outputText = response.GetOutputText();
671+
Assert.That(outputText.Length, Is.GreaterThan(0).And.LessThan(7));
672+
Assert.That(outputText.ToLower(), Does.Contain("hello"));
670673

671674
response.Output.Add(ResponseItem.CreateAssistantMessageItem("More text!"));
672-
Assert.That(response?.OutputText?.ToLower(), Does.EndWith("more text!"));
675+
Assert.That(response.GetOutputText().ToLower(), Does.EndWith("more text!"));
673676

674677
response = await client.CreateResponseAsync(
675678
new ([ResponseItem.CreateUserMessageItem("How's the weather?")])
@@ -751,7 +754,7 @@ public async Task FileInputFromIdWorks()
751754

752755
ResponseResult response = await client.CreateResponseAsync(new([messageItem]));
753756

754-
Assert.That(response?.OutputText?.ToLower(), Does.Contain("pizza"));
757+
Assert.That(response?.GetOutputText().ToLower(), Does.Contain("pizza"));
755758
}
756759

757760
[RecordedTest]
@@ -771,7 +774,7 @@ public async Task FileInputFromBinaryWorks()
771774

772775
ResponseResult response = await client.CreateResponseAsync(new([messageItem]));
773776

774-
Assert.That(response?.OutputText?.ToLower(), Does.Contain("pizza"));
777+
Assert.That(response?.GetOutputText(), Does.Contain("pizza"));
775778
}
776779

777780
[RecordedTest]

0 commit comments

Comments
 (0)