openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatClient.cs

208lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading;
7using System.Threading.Tasks;
8
9namespace OpenAI.Chat;
10
11[CodeGenClient("Chat")]
12[CodeGenSuppress("CreateChatCompletionAsync", typeof(ChatCompletionOptions))]
13[CodeGenSuppress("CreateChatCompletion", typeof(ChatCompletionOptions))]
14public partial class ChatClient
15{
16 private readonly string _model;
17
18 /// <summary>
19 /// Initializes a new instance of <see cref="ChatClient"/> that will use an API key when authenticating.
20 /// </summary>
21 /// <param name="model"> The model name for chat completions that the client should use. </param>
22 /// <param name="credential"> The API key used to authenticate with the service endpoint. </param>
23 /// <param name="options"> Additional options to customize the client. </param>
24 /// <exception cref="ArgumentNullException"> The provided <paramref name="credential"/> was null. </exception>
25 public ChatClient(string model, ApiKeyCredential credential, OpenAIClientOptions options = null)
26 : this(
27 OpenAIClient.CreatePipeline(OpenAIClient.GetApiKey(credential, requireExplicitCredential: true), options),
28 model,
29 OpenAIClient.GetEndpoint(options),
30 options)
31 { }
32
33 /// <summary>
34 /// Initializes a new instance of <see cref="ChatClient"/> that will use an API key from the OPENAI_API_KEY
35 /// environment variable when authenticating.
36 /// </summary>
37 /// <remarks>
38 /// To provide an explicit credential instead of using the environment variable, use an alternate constructor like
39 /// <see cref="ChatClient(string,ApiKeyCredential,OpenAIClientOptions)"/>.
40 /// </remarks>
41 /// <param name="model"> The model name for chat completions that the client should use. </param>
42 /// <param name="options"> Additional options to customize the client. </param>
43 /// <exception cref="InvalidOperationException"> The OPENAI_API_KEY environment variable was not found. </exception>
44 public ChatClient(string model, OpenAIClientOptions options = null)
45 : this(
46 OpenAIClient.CreatePipeline(OpenAIClient.GetApiKey(), options),
47 model,
48 OpenAIClient.GetEndpoint(options),
49 options)
50 { }
51
52 /// <summary>
53 /// Initializes a new instance of <see cref="ChatClient"/>.
54 /// </summary>
55 /// <param name="pipeline"> The <see cref="ClientPipeline"/> instance to use. </param>
56 /// <param name="model"> The model name to use. </param>
57 /// <param name="endpoint"> The endpoint to use. </param>
58 protected internal ChatClient(ClientPipeline pipeline, string model, Uri endpoint, OpenAIClientOptions options)
59 {
60 Argument.AssertNotNullOrEmpty(model, nameof(model));
61
62 _model = model;
63 _pipeline = pipeline;
64 _endpoint = endpoint;
65 }
66
67 /// <summary>
68 /// Generates a single chat completion result for a provided set of input chat messages.
69 /// </summary>
70 /// <param name="messages"> The messages to provide as input and history for chat completion. </param>
71 /// <param name="options"> Additional options for the chat completion request. </param>
72 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
73 /// <returns> A result for a single chat completion. </returns>
74 public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
75 {
76 Argument.AssertNotNullOrEmpty(messages, nameof(messages));
77
78 options ??= new();
79 CreateChatCompletionOptions(messages, ref options);
80
81 using BinaryContent content = options.ToBinaryContent();
82
83 ClientResult result = await CompleteChatAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
84 return ClientResult.FromValue(ChatCompletion.FromResponse(result.GetRawResponse()), result.GetRawResponse());
85 }
86
87 /// <summary>
88 /// Generates a single chat completion result for a provided set of input chat messages.
89 /// </summary>
90 /// <param name="messages"> The messages to provide as input and history for chat completion. </param>
91 /// <returns> A result for a single chat completion. </returns>
92 public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(params ChatMessage[] messages)
93 => await CompleteChatAsync(messages, default(ChatCompletionOptions)).ConfigureAwait(false);
94
95 /// <summary>
96 /// Generates a single chat completion result for a provided set of input chat messages.
97 /// </summary>
98 /// <param name="messages"> The messages to provide as input and history for chat completion. </param>
99 /// <param name="options"> Additional options for the chat completion request. </param>
100 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
101 /// <returns> A result for a single chat completion. </returns>
102 public virtual ClientResult<ChatCompletion> CompleteChat(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
103 {
104 Argument.AssertNotNullOrEmpty(messages, nameof(messages));
105
106 options ??= new();
107 CreateChatCompletionOptions(messages, ref options);
108
109 using BinaryContent content = options.ToBinaryContent();
110 ClientResult result = CompleteChat(content, cancellationToken.ToRequestOptions());
111 return ClientResult.FromValue(ChatCompletion.FromResponse(result.GetRawResponse()), result.GetRawResponse());
112
113 }
114
115 /// <summary>
116 /// Generates a single chat completion result for a provided set of input chat messages.
117 /// </summary>
118 /// <param name="messages"> The messages to provide as input and history for chat completion. </param>
119 /// <returns> A result for a single chat completion. </returns>
120 public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] messages)
121 => CompleteChat(messages, default(ChatCompletionOptions));
122
123 /// <summary>
124 /// Begins a streaming response for a chat completion request using the provided chat messages as input and
125 /// history.
126 /// </summary>
127 /// <remarks>
128 /// <see cref="AsyncResultCollection{T}"/> can be enumerated over using the <c>await foreach</c> pattern using the
129 /// <see cref="IAsyncEnumerable{T}"/> interface.
130 /// </remarks>
131 /// <param name="messages"> The messages to provide as input for chat completion. </param>
132 /// <param name="options"> Additional options for the chat completion request. </param>
133 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
134 /// <returns> A streaming result with incremental chat completion updates. </returns>
135 public virtual AsyncResultCollection<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
136 {
137 Argument.AssertNotNull(messages, nameof(messages));
138
139 options ??= new();
140 CreateChatCompletionOptions(messages, ref options, stream: true);
141
142 using BinaryContent content = options.ToBinaryContent();
143
144 async Task<ClientResult> getResultAsync() =>
145 await CompleteChatAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false);
146 return new AsyncStreamingChatCompletionUpdateCollection(getResultAsync);
147 }
148
149 /// <summary>
150 /// Begins a streaming response for a chat completion request using the provided chat messages as input and
151 /// history.
152 /// </summary>
153 /// <remarks>
154 /// <see cref="AsyncResultCollection{T}"/> can be enumerated over using the <c>await foreach</c> pattern using the
155 /// <see cref="IAsyncEnumerable{T}"/> interface.
156 /// </remarks>
157 /// <param name="messages"> The messages to provide as input for chat completion. </param>
158 /// <returns> A streaming result with incremental chat completion updates. </returns>
159 public virtual AsyncResultCollection<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(params ChatMessage[] messages)
160 => CompleteChatStreamingAsync(messages, default(ChatCompletionOptions));
161
162 /// <summary>
163 /// Begins a streaming response for a chat completion request using the provided chat messages as input and
164 /// history.
165 /// </summary>
166 /// <remarks>
167 /// <see cref="ResultCollection{T}"/> can be enumerated over using the <c>foreach</c> pattern using the
168 /// <see cref="IEnumerable{T}"/> interface.
169 /// </remarks>
170 /// <param name="messages"> The messages to provide as input for chat completion. </param>
171 /// <param name="options"> Additional options for the chat completion request. </param>
172 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
173 /// <returns> A streaming result with incremental chat completion updates. </returns>
174 public virtual ResultCollection<StreamingChatCompletionUpdate> CompleteChatStreaming(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
175 {
176 Argument.AssertNotNull(messages, nameof(messages));
177
178 options ??= new();
179 CreateChatCompletionOptions(messages, ref options, stream: true);
180
181 using BinaryContent content = options.ToBinaryContent();
182 ClientResult getResult() => CompleteChat(content, cancellationToken.ToRequestOptions(streaming: true));
183 return new StreamingChatCompletionUpdateCollection(getResult);
184 }
185
186 /// <summary>
187 /// Begins a streaming response for a chat completion request using the provided chat messages as input and
188 /// history.
189 /// </summary>
190 /// <remarks>
191 /// <see cref="ResultCollection{T}"/> can be enumerated over using the <c>foreach</c> pattern using the
192 /// <see cref="IEnumerable{T}"/> interface.
193 /// </remarks>
194 /// <param name="messages"> The messages to provide as input for chat completion. </param>
195 /// <returns> A streaming result with incremental chat completion updates. </returns>
196 public virtual ResultCollection<StreamingChatCompletionUpdate> CompleteChatStreaming(params ChatMessage[] messages)
197 => CompleteChatStreaming(messages, default(ChatCompletionOptions));
198
199 private void CreateChatCompletionOptions(IEnumerable<ChatMessage> messages, ref ChatCompletionOptions options, bool stream = false)
200 {
201 options.Messages = messages.ToList();
202 options.Model = _model;
203 options.Stream = stream
204 ? true
205 : null;
206 options.StreamOptions = stream ? options.StreamOptions : null;
207 }
208}