-
Notifications
You must be signed in to change notification settings - Fork 6
/
GenerativeModel.cs
1012 lines (911 loc) · 43.2 KB
/
GenerativeModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
#endif
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Text;
namespace Mscc.GenerativeAI
{
public class GenerativeModel
{
private const string EndpointGoogleAi = "generativelanguage.googleapis.com";
private const string UrlGoogleAi = "https://{endpointGoogleAI}/{version}/{model}:{method}";
private const string UrlParameterKey = "?key={apiKey}"; // Or in the x-goog-api-key header
private const string UrlVertexAi = "https://{region}-aiplatform.googleapis.com/{version}/projects/{projectId}/locations/{region}/publishers/{publisher}/models/{model}:{method}";
private const string MediaType = "application/json";
private readonly bool _useVertexAi;
private readonly string _region = "us-central1";
private readonly string _publisher = "google";
private readonly JsonSerializerOptions _options;
private readonly Credentials? _credentials;
private string _model;
private bool _useHeaderApiKey;
private string? _apiKey;
private string? _accessToken;
private bool _useHeaderProjectId;
private string? _projectId;
private List<SafetySetting>? _safetySettings;
private GenerationConfig? _generationConfig;
private List<Tool>? _tools;
#if NET472_OR_GREATER || NETSTANDARD2_0
private static readonly Version _httpVersion = HttpVersion.Version11;
private static readonly HttpClient Client = new HttpClient();
#else
private static readonly Version _httpVersion = HttpVersion.Version30;
private static readonly HttpClient Client = new HttpClient(new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(30),
EnableMultipleHttp2Connections = true,
})
{
DefaultRequestVersion = _httpVersion
};
#endif
private string Url
{
get
{
var url = UrlGoogleAi;
if (!string.IsNullOrEmpty(_apiKey) && !_useHeaderApiKey)
{
url += UrlParameterKey;
}
if (_useVertexAi)
{
url = UrlVertexAi;
}
return url;
}
}
private string Version
{
get
{
if (_useVertexAi)
{
return ApiVersion.V1;
}
return ApiVersion.V1Beta;
}
}
private string Method
{
get
{
switch (_model)
{
case GenerativeAI.Model.BisonChat:
return GenerativeAI.Method.GenerateMessage;
case GenerativeAI.Model.BisonText:
return GenerativeAI.Method.GenerateText;
case GenerativeAI.Model.GeckoEmbedding:
return GenerativeAI.Method.EmbedText;
case GenerativeAI.Model.Embedding:
return GenerativeAI.Method.EmbedContent;
case GenerativeAI.Model.AttributedQuestionAnswering:
return GenerativeAI.Method.GenerateAnswer;
}
if (_useVertexAi)
{
return GenerativeAI.Method.StreamGenerateContent;
}
return GenerativeAI.Method.GenerateContent;
}
}
/// <summary>
/// Returns the name of the model.
/// </summary>
/// <returns>Name of the model.</returns>
public string Name => _model;
public string Model
{
set
{
if (value != null)
{
_model = value.SanitizeModelName();
}
}
}
public string? ApiKey
{
set
{
_apiKey = value;
if (!string.IsNullOrEmpty(_apiKey))
{
_useHeaderApiKey = Client.DefaultRequestHeaders.Contains("x-goog-api-key");
if (!_useHeaderApiKey)
{
Client.DefaultRequestHeaders.Add("x-goog-api-key", _apiKey);
}
_useHeaderApiKey = Client.DefaultRequestHeaders.Contains("x-goog-api-key");
}
}
}
public string? AccessToken
{
set
{
_accessToken = value;
if (value != null)
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
}
}
public string? ProjectId
{
set
{
_projectId = value;
if (!string.IsNullOrEmpty(_projectId))
{
_useHeaderProjectId = Client.DefaultRequestHeaders.Contains("x-goog-user-project");
if (!_useHeaderProjectId)
{
Client.DefaultRequestHeaders.Add("x-goog-user-project", _projectId);
}
_useHeaderProjectId = Client.DefaultRequestHeaders.Contains("x-goog-user-project");
}
}
}
/// <summary>
/// Default constructor attempts to read environment variables and
/// sets default values, if available
/// </summary>
public GenerativeModel()
{
_options = DefaultJsonSerializerOptions();
GenerativeModelExtensions.ReadDotEnv();
var credentialsFile =
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ??
Environment.GetEnvironmentVariable("GOOGLE_WEB_CREDENTIALS") ??
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "gcloud",
"application_default_credentials.json");
_credentials = GetCredentialsFromFile(credentialsFile);
ApiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN"); // ?? GetAccessTokenFromAdc();
Model = Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ??
GenerativeAI.Model.Gemini10Pro;
_region = Environment.GetEnvironmentVariable("GOOGLE_REGION") ?? _region;
}
/// <summary>
/// Constructor to initialize access to Google AI Gemini API.
/// </summary>
/// <param name="apiKey">API key provided by Google AI Studio</param>
/// <param name="model">Model to use (default: "gemini-pro")</param>
/// <param name="generationConfig">Optional. Configuration options for model generation and outputs.</param>
/// <param name="safetySettings">Optional. A list of unique SafetySetting instances for blocking unsafe content.</param>
public GenerativeModel(string? apiKey = null,
string? model = null,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null) : this()
{
ApiKey = apiKey ?? _apiKey;
Model = model ?? _model;
_generationConfig ??= generationConfig;
_safetySettings ??= safetySettings;
}
/// <summary>
/// Constructor to initialize access to Vertex AI Gemini API.
/// </summary>
/// <param name="projectId">Identifier of the Google Cloud project</param>
/// <param name="region">Region to use</param>
/// <param name="model">Model to use</param>
/// <param name="generationConfig">Optional. Configuration options for model generation and outputs.</param>
/// <param name="safetySettings">Optional. A list of unique SafetySetting instances for blocking unsafe content.</param>
internal GenerativeModel(string? projectId = null, string? region = null,
string? model = null,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null) : this()
{
_useVertexAi = true;
AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN") ??
GetAccessTokenFromAdc();
ProjectId = projectId ??
Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID") ??
_credentials?.ProjectId ??
_projectId;
_region = region ?? _region;
Model = model ?? _model;
_generationConfig = generationConfig;
_safetySettings = safetySettings;
}
/// <summary>
/// Lists models available through the API.
/// </summary>
/// <returns>List of available models.</returns>
/// <param name="tuned">Flag, whether models or tuned models shall be returned.</param>
/// <param name="pageSize">The maximum number of Models to return (per page).</param>
/// <param name="pageToken">A page token, received from a previous models.list call. Provide the pageToken returned by one request as an argument to the next request to retrieve the next page.</param>
/// <param name="filter">Optional. A filter is a full text search over the tuned model's description and display name. By default, results will not include tuned models shared with everyone. Additional operators: - owner:me - writers:me - readers:me - readers:everyone</param>
/// <exception cref="NotSupportedException"></exception>
public async Task<List<ModelResponse>> ListModels(bool tuned = false,
int? pageSize = null,
string? pageToken = null,
string? filter = null)
{
if (tuned)
{
return await ListTunedModels(pageSize, pageToken, filter);
}
if (_useVertexAi)
{
throw new NotSupportedException();
}
var url = "https://{endpointGoogleAI}/{Version}/models";
var queryStringParams = new Dictionary<string, string?>()
{
[nameof(pageSize)] = Convert.ToString(pageSize),
[nameof(pageToken)] = pageToken
};
if (!string.IsNullOrEmpty(_apiKey) && !_useHeaderApiKey)
{
queryStringParams.Add("key", _apiKey);
}
url = ParseUrl(url).AddQueryString(queryStringParams);
var response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
var models = await Deserialize<ListModelsResponse>(response);
return models?.Models!;
}
/// <summary>
/// Get a list of available tuned models and description.
/// </summary>
/// <returns>List of available tuned models.</returns>
/// <param name="pageSize">The maximum number of Models to return (per page).</param>
/// <param name="pageToken">A page token, received from a previous models.list call. Provide the pageToken returned by one request as an argument to the next request to retrieve the next page.</param>
/// <param name="filter">Optional. A filter is a full text search over the tuned model's description and display name. By default, results will not include tuned models shared with everyone. Additional operators: - owner:me - writers:me - readers:me - readers:everyone</param>
/// <exception cref="NotSupportedException"></exception>
private async Task<List<ModelResponse>> ListTunedModels(int? pageSize = null,
string? pageToken = null,
string? filter = null)
{
if (_useVertexAi)
{
throw new NotSupportedException();
}
if (!string.IsNullOrEmpty(_apiKey))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var url = "https://{endpointGoogleAI}/{Version}/tunedModels"; // v1beta3
var queryStringParams = new Dictionary<string, string?>()
{
[nameof(pageSize)] = Convert.ToString(pageSize),
[nameof(pageToken)] = pageToken,
[nameof(filter)] = filter
};
if (!string.IsNullOrEmpty(_apiKey) && !_useHeaderApiKey)
{
queryStringParams.Add("key", _apiKey);
}
url = ParseUrl(url).AddQueryString(queryStringParams);
var response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
var models = await Deserialize<ListTunedModelResponse>(response);
return models?.TunedModels!;
}
/// <summary>
/// Creates a tuned model.
/// </summary>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public async Task<CreateTunedModelResponse> CreateTunedModel(CreateTunedModelRequest request)
{
if (!(_model is (string)GenerativeAI.Model.BisonText001 ||
_model is (string)GenerativeAI.Model.Gemini10Pro001))
{
throw new NotSupportedException();
}
if (!string.IsNullOrEmpty(_apiKey))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var method = GenerativeAI.Method.TunedModels;
// var method = "createTunedModel";
// if (_model is (string)Model.BisonText001)
// method = "createTunedTextModel";
var url = "https://{endpointGoogleAI}/{Version}/{method}"; // v1beta3
url = ParseUrl(url, method);
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
return await Deserialize<CreateTunedModelResponse>(response);
}
/// <summary>
/// Deletes a tuned model.
/// </summary>
/// <param name="model">Required. The resource name of the model. Format: tunedModels/my-model-id</param>
/// <returns>If successful, the response body is empty.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<string> DeleteTunedModel(string model)
{
if (string.IsNullOrEmpty(model))
{
throw new ArgumentNullException(nameof(model));
}
if (_useVertexAi)
{
throw new NotSupportedException();
}
model = model.SanitizeModelName();
if (!string.IsNullOrEmpty(_apiKey) && model.StartsWith("tunedModel", StringComparison.InvariantCultureIgnoreCase))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var url = $"https://{EndpointGoogleAi}/{Version}/{model}"; // v1beta3
url = ParseUrl(url);
var response = await Client.DeleteAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Updates a tuned model.
/// </summary>
/// <param name="model">Required. The resource name of the model. Format: tunedModels/my-model-id</param>
/// <param name="tunedModel">The tuned model to update.</param>
/// <param name="updateMask">Required. The list of fields to update. This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo".</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<ModelResponse> PatchTunedModel(string model, ModelResponse tunedModel, string? updateMask = null)
{
if (string.IsNullOrEmpty(model))
{
throw new ArgumentNullException(nameof(model));
}
if (_useVertexAi)
{
throw new NotSupportedException();
}
#if NET472_OR_GREATER || NETSTANDARD2_0
throw new NotSupportedException();
#else
model = model.SanitizeModelName();
if (!string.IsNullOrEmpty(_apiKey) && model.StartsWith("tunedModel", StringComparison.InvariantCultureIgnoreCase))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var url = $"https://{EndpointGoogleAi}/{Version}/{model}"; // v1beta3
var queryStringParams = new Dictionary<string, string?>()
{
[nameof(updateMask)] = updateMask
};
if (!string.IsNullOrEmpty(_apiKey) && !_useHeaderApiKey)
{
queryStringParams.Add("key", _apiKey);
}
url = ParseUrl(url).AddQueryString(queryStringParams);
string json = Serialize(tunedModel);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PatchAsync(url, payload);
response.EnsureSuccessStatusCode();
return await Deserialize<ModelResponse>(response);
#endif
}
/// <summary>
/// Transfers ownership of the tuned model. This is the only way to change ownership of the tuned model. The current owner will be downgraded to writer role.
/// </summary>
/// <param name="model">Required. The resource name of the tuned model to transfer ownership. Format: tunedModels/my-model-id</param>
/// <param name="emailAddress">Required. The email address of the user to whom the tuned model is being transferred to.</param>
/// <returns>If successful, the response body is empty.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<string> TransferOwnership(string model, string emailAddress)
{
if (model == null) throw new ArgumentNullException(nameof(model));
if (emailAddress == null) throw new ArgumentNullException(nameof(emailAddress));
if (_useVertexAi)
{
throw new NotSupportedException();
}
model = model.SanitizeModelName();
if (!string.IsNullOrEmpty(_apiKey) && model.StartsWith("tunedModel", StringComparison.InvariantCultureIgnoreCase))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var method = GenerativeAI.Method.TransferOwnership;
var url = ParseUrl(Url, method);
string json = Serialize(new { EmailAddress = emailAddress });
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Gets information about a specific Model.
/// </summary>
/// <param name="model">Required. The resource name of the model. This name should match a model name returned by the models.list method. Format: models/model-id or tunedModels/my-model-id</param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public async Task<ModelResponse> GetModel(string model = GenerativeAI.Model.GeminiPro)
{
if (_useVertexAi)
{
throw new NotSupportedException();
}
model = model.SanitizeModelName();
if (!string.IsNullOrEmpty(_apiKey) && model.StartsWith("tunedModel", StringComparison.InvariantCultureIgnoreCase))
{
throw new NotSupportedException("Accessing tuned models via API key is not provided. Setup OAuth for your project.");
}
var url = $"https://{EndpointGoogleAi}/{Version}/{model}";
if (!string.IsNullOrEmpty(_apiKey) && !_useHeaderApiKey)
{
url += UrlParameterKey;
}
url = ParseUrl(url);
var response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await Deserialize<ModelResponse>(response);
}
/// <summary>
/// Generates a response from the model given an input GenerateContentRequest.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public async Task<GenerateContentResponse> GenerateContent(GenerateContentRequest? request)
{
if (request == null) throw new ArgumentNullException(nameof(request));
var url = ParseUrl(Url, Method);
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
if (_useVertexAi)
{
var fullText = new StringBuilder();
var contentResponseVertex = await Deserialize<List<GenerateContentResponse>>(response);
foreach (var item in contentResponseVertex)
{
switch (item.Candidates[0].FinishReason)
{
case FinishReason.Safety:
return item;
break;
case FinishReason.Unspecified:
default:
fullText.Append(item.Text);
break;
}
}
var result = contentResponseVertex.LastOrDefault();
result.Candidates[0].Content.Parts[0].Text = fullText.ToString();
return result;
}
return await Deserialize<GenerateContentResponse>(response);
}
/// <remarks/>
public async Task<GenerateContentResponse> GenerateContent(string? prompt,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null)
{
if (prompt == null) throw new ArgumentNullException(nameof(prompt));
var request = new GenerateContentRequest(prompt,
generationConfig ?? _generationConfig,
safetySettings ?? _safetySettings,
tools ?? _tools);
return await GenerateContent(request);
}
/// <remarks/>
public async Task<GenerateContentResponse> GenerateContent(List<IPart>? parts,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null)
{
if (parts == null) throw new ArgumentNullException(nameof(parts));
var request = new GenerateContentRequest(parts,
generationConfig ?? _generationConfig,
safetySettings ?? _safetySettings,
tools ?? _tools);
request.Contents[0].Role = Role.User;
return await GenerateContent(request);
}
/// <summary>
/// Generates a streamed response from the model given an input GenerateContentRequest.
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public async IAsyncEnumerable<GenerateContentResponse> GenerateContentStream(GenerateContentRequest? request,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (request == null) throw new ArgumentNullException(nameof(request));
var method = "streamGenerateContent";
var url = ParseUrl(Url, method);
// Ref: https://code-maze.com/using-streams-with-httpclient-to-improve-performance-and-memory-usage/
// Ref: https://www.stevejgordon.co.uk/using-httpcompletionoption-responseheadersread-to-improve-httpclient-performance-dotnet
var ms = new MemoryStream();
await JsonSerializer.SerializeAsync(ms, request, _options, cancellationToken);
ms.Seek(0, SeekOrigin.Begin);
var message = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url),
Version = _httpVersion
};
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType));
using (var payload = new StreamContent(ms))
{
message.Content = payload;
payload.Headers.ContentType = new MediaTypeHeaderValue(MediaType);
using (var response = await Client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
response.EnsureSuccessStatusCode();
if (response.Content is not null)
{
using var stream = await response.Content.ReadAsStreamAsync();
await foreach (var item in JsonSerializer.DeserializeAsyncEnumerable<GenerateContentResponse>(
stream, _options, cancellationToken))
{
if (cancellationToken.IsCancellationRequested)
yield break;
yield return item;
}
}
}
}
}
/// <remarks/>
public IAsyncEnumerable<GenerateContentResponse> GenerateContentStream(string? prompt,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null)
{
if (prompt == null) throw new ArgumentNullException(nameof(prompt));
var request = new GenerateContentRequest(prompt,
generationConfig ?? _generationConfig,
safetySettings ?? _safetySettings,
tools ?? _tools);
return GenerateContentStream(request);
}
/// <remarks/>
public IAsyncEnumerable<GenerateContentResponse> GenerateContentStream(List<IPart>? parts,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null)
{
if (parts == null) throw new ArgumentNullException(nameof(parts));
var request = new GenerateContentRequest(parts,
generationConfig ?? _generationConfig,
safetySettings ?? _safetySettings,
tools ?? _tools);
request.Contents[0].Role = Role.User;
return GenerateContentStream(request);
}
/// <summary>
/// Generates a grounded answer from the model given an input GenerateAnswerRequest.
/// </summary>
/// <param name="request"></param>
/// <returns>Response from the model for a grounded answer.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<GenerateAnswerResponse> GenerateAnswer(GenerateAnswerRequest? request)
{
if (request == null) throw new ArgumentNullException(nameof(request));
if (_model != (string)GenerativeAI.Model.AttributedQuestionAnswering)
{
throw new NotSupportedException();
}
var url = ParseUrl(Url, Method);
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
// if (_useVertexAi)
// {
// var fullText = new StringBuilder();
// var contentResponseVertex = await Deserialize<List<GenerateAnswerResponse>>(response);
// foreach (var item in contentResponseVertex)
// {
// switch (item.Candidates[0].FinishReason)
// {
// case FinishReason.Safety:
// return item;
// break;
// case FinishReason.Unspecified:
// default:
// fullText.Append(item.Text);
// break;
// }
// }
// var result = contentResponseVertex.LastOrDefault();
// result.Candidates[0].Content.Parts[0].Text = fullText.ToString();
// return result;
// }
return await Deserialize<GenerateAnswerResponse>(response);
}
/// <summary>
/// Generates an embedding from the model given an input Content.
/// </summary>
/// <param name="request">Required. EmbedContentRequest to process. The content to embed. Only the parts.text fields will be counted.</param>
/// <param name="taskType">Optional. Optional task type for which the embeddings will be used. Can only be set for models/embedding-001.</param>
/// <param name="title">Optional. An optional title for the text. Only applicable when TaskType is RETRIEVAL_DOCUMENT. Note: Specifying a title for RETRIEVAL_DOCUMENT provides better quality embeddings for retrieval.</param>
/// <returns>Embeddings of the content as a list of floating numbers.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<EmbedContentResponse> EmbedContent(EmbedContentRequest request, TaskType? taskType = null, string? title = null)
{
if (request == null) throw new ArgumentNullException(nameof(request));
if (_model != (string)GenerativeAI.Model.Embedding)
{
throw new NotSupportedException();
}
var url = ParseUrl(Url, Method);
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
return await Deserialize<EmbedContentResponse>(response);
}
/// <summary>
/// Generates an embedding from the model given an input Content.
/// </summary>
/// <param name="prompt">Required. String to process. The content to embed. Only the parts.text fields will be counted.</param>
/// <param name="taskType">Optional. Optional task type for which the embeddings will be used. Can only be set for models/embedding-001.</param>
/// <param name="title">Optional. An optional title for the text. Only applicable when TaskType is RETRIEVAL_DOCUMENT. Note: Specifying a title for RETRIEVAL_DOCUMENT provides better quality embeddings for retrieval.</param>
/// <returns>Embeddings of the content as a list of floating numbers.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<EmbedContentResponse> EmbedContent(string? prompt, TaskType? taskType = null, string? title = null)
{
if (prompt == null) throw new ArgumentNullException(nameof(prompt));
if (_model != (string)GenerativeAI.Model.Embedding)
{
throw new NotSupportedException();
}
var request = new EmbedContentRequest(prompt)
{
TaskType = taskType,
Title = title
};
return await EmbedContent(request);
}
/// <summary>
/// Generates multiple embeddings from the model given input text in a synchronous call.
/// </summary>
/// <param name="requests">Required. Embed requests for the batch. The model in each of these requests must match the model specified BatchEmbedContentsRequest.model.</param>
/// <returns>List of Embeddings of the content as a list of floating numbers.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
public async Task<EmbedContentResponse> BatchEmbedContent(List<EmbedContentRequest> requests)
{
if (requests == null) throw new ArgumentNullException(nameof(requests));
if (_model != (string)GenerativeAI.Model.Embedding)
{
throw new NotSupportedException();
}
var method = GenerativeAI.Method.BatchEmbedContent;
var url = ParseUrl(Url, method);
string json = Serialize(requests);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
return await Deserialize<EmbedContentResponse>(response);
}
/// <summary>
/// Counts the number of tokens in the content.
/// </summary>
/// <param name="request"></param>
/// <returns>Number of tokens.</returns>
/// <exception cref="ArgumentNullException"></exception>
public async Task<CountTokensResponse> CountTokens(GenerateContentRequest? request)
{
if (request == null) throw new ArgumentNullException(nameof(request));
var method = GenerativeAI.Method.CountTokens;
var url = ParseUrl(Url, method);
string json = Serialize(request);
var payload = new StringContent(json, Encoding.UTF8, MediaType);
var response = await Client.PostAsync(url, payload);
response.EnsureSuccessStatusCode();
return await Deserialize<CountTokensResponse>(response);
}
/// <remarks/>
public async Task<CountTokensResponse> CountTokens(string? prompt)
{
if (prompt == null) throw new ArgumentNullException(nameof(prompt));
var request = new GenerateContentRequest(prompt, _generationConfig, _safetySettings, _tools);
return await CountTokens(request);
}
/// <remarks/>
public async Task<CountTokensResponse> CountTokens(List<IPart>? parts)
{
if (parts == null) throw new ArgumentNullException(nameof(parts));
var request = new GenerateContentRequest(parts, _generationConfig, _safetySettings, _tools);
return await CountTokens(request);
}
// Todo: Implementation missing
/// <summary>
/// Starts a chat session.
/// </summary>
/// <param name="history"></param>
/// <param name="generationConfig">Optional. Configuration options for model generation and outputs.</param>
/// <param name="safetySettings">Optional. A list of unique SafetySetting instances for blocking unsafe content.</param>
/// <param name="tools">Optional. A list of Tools the model may use to generate the next response.</param>
/// <returns></returns>
public ChatSession StartChat(List<ContentResponse>? history = null,
GenerationConfig? generationConfig = null,
List<SafetySetting>? safetySettings = null,
List<Tool>? tools = null)
{
var config = generationConfig ?? _generationConfig;
var safety = safetySettings ?? _safetySettings;
var tool = tools ?? _tools;
return new ChatSession(this, history, config, safety, tool);
}
/// <summary>
/// Parses the URL template and replaces the placeholder with current values.
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <returns></returns>
private string ParseUrl(string url, string? method = default)
{
var replacements = GetReplacements();
replacements.Add("method", method);
var urlParsed = Regex.Replace(url, @"\{(?<name>.*?)\}",
match => replacements.TryGetValue(match.Groups["name"].Value, out var value) ? value : "");
return urlParsed;
Dictionary<string, string> GetReplacements()
{
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "endpointGoogleAI", EndpointGoogleAi },
{ "version", Version },
{ "model", _model },
{ "apikey", _apiKey },
{ "projectid", _projectId },
{ "region", _region },
{ "location", _region },
{ "publisher", _publisher }
};
}
}
/// <summary>
/// Return serialized JSON string of request payload.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private string Serialize<T>(T request)
{
return JsonSerializer.Serialize(request, _options);
}
/// <summary>
/// Return deserialized object from JSON response.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="response"></param>
/// <returns></returns>
private async Task<T> Deserialize<T>(HttpResponseMessage? response)
{
#if NET472_OR_GREATER || NETSTANDARD2_0
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(json, _options);
#else
return await response.Content.ReadFromJsonAsync<T>(_options);
#endif
}
/// <summary>
/// Get default options for JSON serialization.
/// </summary>
/// <returns></returns>
internal JsonSerializerOptions DefaultJsonSerializerOptions()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
//WriteIndented = true,
};
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper));
return options;
}
/// <summary>
///
/// </summary>
/// <param name="credentialsFile"></param>
/// <returns></returns>
private Credentials? GetCredentialsFromFile(string credentialsFile)
{
Credentials? credentials = null;
if (File.Exists(credentialsFile))
{
var options = DefaultJsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
using (var stream = new FileStream(credentialsFile, FileMode.Open, FileAccess.Read))
{
credentials = JsonSerializer.Deserialize<Credentials>(stream, options);
}
}
return credentials;
}
/// <summary>
/// Retrieve access token from Application Default Credentials (ADC)
/// </summary>
/// <returns>The access token.</returns>
// Reference: https://cloud.google.com/docs/authentication
private string GetAccessTokenFromAdc()
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
return RunExternalExe("cmd.exe", "/c gcloud auth application-default print-access-token").TrimEnd();
}
else
{
return RunExternalExe("gcloud", "auth application-default print-access-token").TrimEnd();
}
}
private string RunExternalExe(string filename, string arguments = null)
{
var process = new Process();
process.StartInfo.FileName = filename;
if (!string.IsNullOrEmpty(arguments))
{
process.StartInfo.Arguments = arguments;
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
var stdOutput = new StringBuilder();
process.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data); // Use AppendLine rather than Append since args.Data is one line of output, not including the newline character.
string stdError = null;
try
{
process.Start();
process.BeginOutputReadLine();
stdError = process.StandardError.ReadToEnd();
process.WaitForExit();
}
catch (Exception e)
{
throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
}
if (process.ExitCode == 0)
{
return stdOutput.ToString();
}
else
{
var message = new StringBuilder();
if (!string.IsNullOrEmpty(stdError))
{
message.AppendLine(stdError);
}
if (stdOutput.Length != 0)
{
message.AppendLine("Std output:");
message.AppendLine(stdOutput.ToString());
}