Skip to content

Commit 87b240d

Browse files
committed
Add UpdateChatCompletion method to ChatClient
1 parent 3613b6a commit 87b240d

15 files changed

+336
-670
lines changed

api/OpenAI.net8.0.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,14 @@ public class ChatClient {
14581458
public virtual AsyncCollectionResult<ChatCompletion> GetChatCompletionsAsync(ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
14591459
[Experimental("OPENAI001")]
14601460
public virtual AsyncCollectionResult GetChatCompletionsAsync(string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options);
1461+
[Experimental("OPENAI001")]
1462+
public virtual ClientResult UpdateChatCompletion(string completionId, BinaryContent content, RequestOptions options = null);
1463+
[Experimental("OPENAI001")]
1464+
public virtual ClientResult<ChatCompletion> UpdateChatCompletion(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default);
1465+
[Experimental("OPENAI001")]
1466+
public virtual Task<ClientResult> UpdateChatCompletionAsync(string completionId, BinaryContent content, RequestOptions options = null);
1467+
[Experimental("OPENAI001")]
1468+
public virtual Task<ClientResult<ChatCompletion>> UpdateChatCompletionAsync(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default);
14611469
}
14621470
public class ChatCompletion : IJsonModel<ChatCompletion>, IPersistableModel<ChatCompletion> {
14631471
[Experimental("OPENAI001")]

api/OpenAI.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,10 @@ public class ChatClient {
13011301
public virtual CollectionResult GetChatCompletions(string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options);
13021302
public virtual AsyncCollectionResult<ChatCompletion> GetChatCompletionsAsync(ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
13031303
public virtual AsyncCollectionResult GetChatCompletionsAsync(string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options);
1304+
public virtual ClientResult UpdateChatCompletion(string completionId, BinaryContent content, RequestOptions options = null);
1305+
public virtual ClientResult<ChatCompletion> UpdateChatCompletion(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default);
1306+
public virtual Task<ClientResult> UpdateChatCompletionAsync(string completionId, BinaryContent content, RequestOptions options = null);
1307+
public virtual Task<ClientResult<ChatCompletion>> UpdateChatCompletionAsync(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default);
13041308
}
13051309
public class ChatCompletion : IJsonModel<ChatCompletion>, IPersistableModel<ChatCompletion> {
13061310
public IReadOnlyList<ChatMessageAnnotation> Annotations { get; }

specification/client/chat.client.tsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ using Azure.ClientGenerator.Core;
1717
@@visibility(ChatCompletionStreamResponseDelta.tool_calls, Lifecycle.Read);
1818

1919
@@clientName(Chat.createChatCompletion, "CompleteChat");
20+
21+
@@scope(Chat.getChatCompletionMessages, "!csharp");

src/Custom/Chat/ChatClient.Protocol.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Custom/Chat/ChatClient.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using OpenAI.Evals;
21
using OpenAI.Telemetry;
32
using System;
43
using System.ClientModel;
@@ -20,10 +19,6 @@ namespace OpenAI.Chat;
2019
[CodeGenSuppress("ChatClient", typeof(ClientPipeline), typeof(Uri))]
2120
[CodeGenSuppress("CompleteChat", typeof(ChatCompletionOptions), typeof(CancellationToken))]
2221
[CodeGenSuppress("CompleteChatAsync", typeof(ChatCompletionOptions), typeof(CancellationToken))]
23-
[CodeGenSuppress("GetChatCompletionMessages", typeof(string), typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(CancellationToken))]
24-
[CodeGenSuppress("GetChatCompletionMessagesAsync", typeof(string), typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(CancellationToken))]
25-
[CodeGenSuppress("UpdateChatCompletion", typeof(string), typeof(IDictionary<string, string>), typeof(CancellationToken))]
26-
[CodeGenSuppress("UpdateChatCompletionAsync", typeof(string), typeof(IDictionary<string, string>), typeof(CancellationToken))]
2722
public partial class ChatClient
2823
{
2924
private readonly string _model;
@@ -309,6 +304,32 @@ public virtual ClientResult<ChatCompletion> GetChatCompletion(string completionI
309304
return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
310305
}
311306

307+
// CUSTOM:
308+
// - Call FromClientResult.
309+
[Experimental("OPENAI001")]
310+
public virtual ClientResult<ChatCompletion> UpdateChatCompletion(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default)
311+
{
312+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
313+
Argument.AssertNotNull(metadata, nameof(metadata));
314+
315+
InternalUpdateChatCompletionRequest spreadModel = new InternalUpdateChatCompletionRequest(metadata, null);
316+
ClientResult result = this.UpdateChatCompletion(completionId, spreadModel, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
317+
return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
318+
}
319+
320+
// CUSTOM:
321+
// - Call FromClientResult.
322+
[Experimental("OPENAI001")]
323+
public virtual async Task<ClientResult<ChatCompletion>> UpdateChatCompletionAsync(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default)
324+
{
325+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
326+
Argument.AssertNotNull(metadata, nameof(metadata));
327+
328+
InternalUpdateChatCompletionRequest spreadModel = new InternalUpdateChatCompletionRequest(metadata, null);
329+
ClientResult result = await this.UpdateChatCompletionAsync(completionId, spreadModel, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
330+
return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
331+
}
332+
312333
// CUSTOM:
313334
// - Added Experimental attribute.
314335
// - Call FromClientResult.

src/Custom/Chat/Internal/GeneratorStubs.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Runtime.InteropServices;
1+
using System.ClientModel;
22

33
namespace OpenAI.Chat;
44

@@ -50,6 +50,19 @@ internal partial class InternalChatCompletionRequestMessageContentPartRefusal {
5050
[CodeGenType("CreateChatCompletionRequestModel")]
5151
internal readonly partial struct InternalCreateChatCompletionRequestModel { }
5252

53+
[CodeGenType("UpdateChatCompletionRequest")]
54+
internal partial class InternalUpdateChatCompletionRequest
55+
{
56+
public static implicit operator BinaryContent(InternalUpdateChatCompletionRequest internalUpdateChatCompletionRequest)
57+
{
58+
if (internalUpdateChatCompletionRequest == null)
59+
{
60+
return null;
61+
}
62+
return BinaryContent.Create(internalUpdateChatCompletionRequest, ModelSerializationExtensions.WireOptions);
63+
}
64+
}
65+
5366
[CodeGenType("CreateChatCompletionRequestToolChoice")]
5467
internal readonly partial struct InternalCreateChatCompletionRequestToolChoice { }
5568

src/Generated/ChatClient.RestClient.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,5 @@ internal virtual PipelineMessage CreateDeleteChatCompletionRequest(string comple
122122
message.Apply(options);
123123
return message;
124124
}
125-
126-
internal virtual PipelineMessage CreateGetChatCompletionMessagesRequest(string completionId, string after, int? limit, string order, RequestOptions options)
127-
{
128-
PipelineMessage message = Pipeline.CreateMessage();
129-
message.ResponseClassifier = PipelineMessageClassifier200;
130-
PipelineRequest request = message.Request;
131-
request.Method = "GET";
132-
ClientUriBuilder uri = new ClientUriBuilder();
133-
uri.Reset(_endpoint);
134-
uri.AppendPath("/chat/completions/", false);
135-
uri.AppendPath(completionId, true);
136-
uri.AppendPath("/messages", false);
137-
if (after != null)
138-
{
139-
uri.AppendQuery("after", after, true);
140-
}
141-
if (limit != null)
142-
{
143-
uri.AppendQuery("limit", TypeFormatters.ConvertToString(limit, null), true);
144-
}
145-
if (order != null)
146-
{
147-
uri.AppendQuery("order", order, true);
148-
}
149-
request.Uri = uri.ToUri();
150-
request.Headers.Set("Accept", "application/json");
151-
message.Apply(options);
152-
return message;
153-
}
154125
}
155126
}

src/Generated/ChatClient.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,61 @@ public virtual async Task<ClientResult> CompleteChatAsync(BinaryContent content,
9090
using PipelineMessage message = CreateCompleteChatRequest(content, options);
9191
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
9292
}
93+
94+
[Experimental("OPENAI001")]
95+
public virtual ClientResult GetChatCompletion(string completionId, RequestOptions options)
96+
{
97+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
98+
99+
using PipelineMessage message = CreateGetChatCompletionRequest(completionId, options);
100+
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
101+
}
102+
103+
[Experimental("OPENAI001")]
104+
public virtual async Task<ClientResult> GetChatCompletionAsync(string completionId, RequestOptions options)
105+
{
106+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
107+
108+
using PipelineMessage message = CreateGetChatCompletionRequest(completionId, options);
109+
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
110+
}
111+
112+
[Experimental("OPENAI001")]
113+
public virtual ClientResult UpdateChatCompletion(string completionId, BinaryContent content, RequestOptions options = null)
114+
{
115+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
116+
Argument.AssertNotNull(content, nameof(content));
117+
118+
using PipelineMessage message = CreateUpdateChatCompletionRequest(completionId, content, options);
119+
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
120+
}
121+
122+
[Experimental("OPENAI001")]
123+
public virtual async Task<ClientResult> UpdateChatCompletionAsync(string completionId, BinaryContent content, RequestOptions options = null)
124+
{
125+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
126+
Argument.AssertNotNull(content, nameof(content));
127+
128+
using PipelineMessage message = CreateUpdateChatCompletionRequest(completionId, content, options);
129+
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
130+
}
131+
132+
[Experimental("OPENAI001")]
133+
public virtual ClientResult DeleteChatCompletion(string completionId, RequestOptions options)
134+
{
135+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
136+
137+
using PipelineMessage message = CreateDeleteChatCompletionRequest(completionId, options);
138+
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
139+
}
140+
141+
[Experimental("OPENAI001")]
142+
public virtual async Task<ClientResult> DeleteChatCompletionAsync(string completionId, RequestOptions options)
143+
{
144+
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
145+
146+
using PipelineMessage message = CreateDeleteChatCompletionRequest(completionId, options);
147+
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
148+
}
93149
}
94150
}

0 commit comments

Comments
 (0)