Skip to content

Commit 57066fb

Browse files
authored
[Firebase AI] Change public fields to easier types (#1290)
* [Firebase AI] Change public fields to easier types * Update ModelContent.cs
1 parent 8d436f6 commit 57066fb

File tree

9 files changed

+76
-98
lines changed

9 files changed

+76
-98
lines changed

docs/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Release Notes
123123
- Firebase AI: Add support for defining a Thinking budget.
124124
- Firebase AI: Deprecated `CountTokensResponse.TotalBillableCharacters`, use
125125
`CountTokensResponse.TotalTokens` instead.
126+
- Firebase AI: Changed public field types for ReadOnlyMemory<byte> to byte[],
127+
and IEnumerable to IReadOnlyList.
126128
- Messaging: Removed deprecated `FirebaseMessage.Dispose`,
127129
`FirebaseNotification.Dispose`, and `MessagingOptions.Dispose` methods.
128130

firebaseai/src/Candidate.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
using System;
1817
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
20-
using System.Linq;
2118
using Firebase.AI.Internal;
2219

2320
namespace Firebase.AI {
@@ -73,7 +70,7 @@ public enum FinishReason {
7370
/// Each content generation prompt may produce multiple candidate responses.
7471
/// </summary>
7572
public readonly struct Candidate {
76-
private readonly ReadOnlyCollection<SafetyRating> _safetyRatings;
73+
private readonly IReadOnlyList<SafetyRating> _safetyRatings;
7774

7875
/// <summary>
7976
/// The response’s content.
@@ -83,9 +80,9 @@ public readonly struct Candidate {
8380
/// <summary>
8481
/// The safety rating of the response content.
8582
/// </summary>
86-
public IEnumerable<SafetyRating> SafetyRatings {
83+
public IReadOnlyList<SafetyRating> SafetyRatings {
8784
get {
88-
return _safetyRatings ?? new ReadOnlyCollection<SafetyRating>(new List<SafetyRating>());
85+
return _safetyRatings ?? new List<SafetyRating>();
8986
}
9087
}
9188

@@ -110,7 +107,7 @@ private Candidate(ModelContent content, List<SafetyRating> safetyRatings,
110107
FinishReason? finishReason, CitationMetadata? citationMetadata,
111108
GroundingMetadata? groundingMetadata) {
112109
Content = content;
113-
_safetyRatings = new ReadOnlyCollection<SafetyRating>(safetyRatings ?? new List<SafetyRating>());
110+
_safetyRatings = safetyRatings ?? new List<SafetyRating>();
114111
FinishReason = finishReason;
115112
CitationMetadata = citationMetadata;
116113
GroundingMetadata = groundingMetadata;

firebaseai/src/Chat.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
using System.Collections.Generic;
18-
using System.Collections.ObjectModel;
1918
using System.Linq;
2019
using System.Runtime.CompilerServices;
2120
using System.Threading;
@@ -36,11 +35,7 @@ public class Chat {
3635
/// The previous content from the chat that has been successfully sent and received from the
3736
/// model. This will be provided to the model for each message sent as context for the discussion.
3837
/// </summary>
39-
public IEnumerable<ModelContent> History {
40-
get {
41-
return new ReadOnlyCollection<ModelContent>(chatHistory);
42-
}
43-
}
38+
public IReadOnlyList<ModelContent> History => chatHistory;
4439

4540
// Note: No public constructor, get one through GenerativeModel.StartChat
4641
private Chat(GenerativeModel model, IEnumerable<ModelContent> initialHistory) {

firebaseai/src/Citation.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
2019
using Firebase.AI.Internal;
2120

2221
namespace Firebase.AI {
@@ -25,20 +24,20 @@ namespace Firebase.AI {
2524
/// A collection of source attributions for a piece of content.
2625
/// </summary>
2726
public readonly struct CitationMetadata {
28-
private readonly ReadOnlyCollection<Citation> _citations;
27+
private readonly IReadOnlyList<Citation> _citations;
2928

3029
/// <summary>
3130
/// A list of individual cited sources and the parts of the content to which they apply.
3231
/// </summary>
33-
public IEnumerable<Citation> Citations {
32+
public IReadOnlyList<Citation> Citations {
3433
get {
35-
return _citations ?? new ReadOnlyCollection<Citation>(new List<Citation>());
34+
return _citations ?? new List<Citation>();
3635
}
3736
}
3837

3938
// Hidden constructor, users don't need to make this.
4039
private CitationMetadata(List<Citation> citations) {
41-
_citations = new ReadOnlyCollection<Citation>(citations ?? new List<Citation>());
40+
_citations = citations;
4241
}
4342

4443
/// <summary>

firebaseai/src/CountTokensResponse.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
2019
using Google.MiniJSON;
2120
using Firebase.AI.Internal;
2221

@@ -44,13 +43,13 @@ public readonly struct CountTokensResponse {
4443
[Obsolete("Use TotalTokens instead; Gemini 2.0 series models and newer are always billed by token count.")]
4544
public int? TotalBillableCharacters { get; }
4645

47-
private readonly ReadOnlyCollection<ModalityTokenCount> _promptTokensDetails;
46+
private readonly IReadOnlyList<ModalityTokenCount> _promptTokensDetails;
4847
/// <summary>
4948
/// The breakdown, by modality, of how many tokens are consumed by the prompt.
5049
/// </summary>
51-
public IEnumerable<ModalityTokenCount> PromptTokensDetails {
50+
public IReadOnlyList<ModalityTokenCount> PromptTokensDetails {
5251
get {
53-
return _promptTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
52+
return _promptTokensDetails ?? new List<ModalityTokenCount>();
5453
}
5554
}
5655

@@ -62,8 +61,7 @@ private CountTokensResponse(int totalTokens,
6261
#pragma warning disable CS0618
6362
TotalBillableCharacters = totalBillableCharacters;
6463
#pragma warning restore CS0618
65-
_promptTokensDetails =
66-
new ReadOnlyCollection<ModalityTokenCount>(promptTokensDetails ?? new List<ModalityTokenCount>());
64+
_promptTokensDetails = promptTokensDetails;
6765
}
6866

6967
/// <summary>

firebaseai/src/GenerateContentResponse.cs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
2019
using System.Linq;
2120
using Google.MiniJSON;
2221
using Firebase.AI.Internal;
@@ -27,14 +26,14 @@ namespace Firebase.AI {
2726
/// The model's response to a generate content request.
2827
/// </summary>
2928
public readonly struct GenerateContentResponse {
30-
private readonly ReadOnlyCollection<Candidate> _candidates;
29+
private readonly IReadOnlyList<Candidate> _candidates;
3130

3231
/// <summary>
3332
/// A list of candidate response content, ordered from best to worst.
3433
/// </summary>
35-
public IEnumerable<Candidate> Candidates {
34+
public IReadOnlyList<Candidate> Candidates {
3635
get {
37-
return _candidates ?? new ReadOnlyCollection<Candidate>(new List<Candidate>());
36+
return _candidates ?? new List<Candidate>();
3837
}
3938
}
4039

@@ -64,16 +63,16 @@ public string Text {
6463
/// <summary>
6564
/// Returns function calls found in any `Part`s of the first candidate of the response, if any.
6665
/// </summary>
67-
public IEnumerable<ModelContent.FunctionCallPart> FunctionCalls {
66+
public IReadOnlyList<ModelContent.FunctionCallPart> FunctionCalls {
6867
get {
69-
return Candidates.FirstOrDefault().Content.Parts.OfType<ModelContent.FunctionCallPart>();
68+
return Candidates.FirstOrDefault().Content.Parts.OfType<ModelContent.FunctionCallPart>().ToList();
7069
}
7170
}
7271

7372
// Hidden constructor, users don't need to make this.
7473
private GenerateContentResponse(List<Candidate> candidates, PromptFeedback? promptFeedback,
7574
UsageMetadata? usageMetadata) {
76-
_candidates = new ReadOnlyCollection<Candidate>(candidates ?? new List<Candidate>());
75+
_candidates = candidates;
7776
PromptFeedback = promptFeedback;
7877
UsageMetadata = usageMetadata;
7978
}
@@ -132,7 +131,7 @@ public enum BlockReason {
132131
/// A metadata struct containing any feedback the model had on the prompt it was provided.
133132
/// </summary>
134133
public readonly struct PromptFeedback {
135-
private readonly ReadOnlyCollection<SafetyRating> _safetyRatings;
134+
private readonly IReadOnlyList<SafetyRating> _safetyRatings;
136135

137136
/// <summary>
138137
/// The reason a prompt was blocked, if it was blocked.
@@ -145,9 +144,9 @@ public readonly struct PromptFeedback {
145144
/// <summary>
146145
/// The safety ratings of the prompt.
147146
/// </summary>
148-
public IEnumerable<SafetyRating> SafetyRatings {
147+
public IReadOnlyList<SafetyRating> SafetyRatings {
149148
get {
150-
return _safetyRatings ?? new ReadOnlyCollection<SafetyRating>(new List<SafetyRating>());
149+
return _safetyRatings ?? new List<SafetyRating>();
151150
}
152151
}
153152

@@ -156,7 +155,7 @@ private PromptFeedback(BlockReason? blockReason, string blockReasonMessage,
156155
List<SafetyRating> safetyRatings) {
157156
BlockReason = blockReason;
158157
BlockReasonMessage = blockReasonMessage;
159-
_safetyRatings = new ReadOnlyCollection<SafetyRating>(safetyRatings ?? new List<SafetyRating>());
158+
_safetyRatings = safetyRatings;
160159
}
161160

162161
private static BlockReason ParseBlockReason(string str) {
@@ -191,37 +190,37 @@ internal static PromptFeedback FromJson(Dictionary<string, object> jsonDict) {
191190
/// section within the Service Specific Terms).
192191
/// </summary>
193192
public readonly struct GroundingMetadata {
194-
private readonly ReadOnlyCollection<string> _webSearchQueries;
195-
private readonly ReadOnlyCollection<GroundingChunk> _groundingChunks;
196-
private readonly ReadOnlyCollection<GroundingSupport> _groundingSupports;
193+
private readonly IReadOnlyList<string> _webSearchQueries;
194+
private readonly IReadOnlyList<GroundingChunk> _groundingChunks;
195+
private readonly IReadOnlyList<GroundingSupport> _groundingSupports;
197196

198197
/// <summary>
199198
/// A list of web search queries that the model performed to gather the grounding information.
200199
/// These can be used to allow users to explore the search results themselves.
201200
/// </summary>
202-
public IEnumerable<string> WebSearchQueries {
201+
public IReadOnlyList<string> WebSearchQueries {
203202
get {
204-
return _webSearchQueries ?? new ReadOnlyCollection<string>(new List<string>());
203+
return _webSearchQueries ?? new List<string>();
205204
}
206205
}
207206

208207
/// <summary>
209208
/// A list of `GroundingChunk` structs. Each chunk represents a piece of retrieved content
210209
/// (e.g., from a web page) that the model used to ground its response.
211210
/// </summary>
212-
public IEnumerable<GroundingChunk> GroundingChunks {
211+
public IReadOnlyList<GroundingChunk> GroundingChunks {
213212
get {
214-
return _groundingChunks ?? new ReadOnlyCollection<GroundingChunk>(new List<GroundingChunk>());
213+
return _groundingChunks ?? new List<GroundingChunk>();
215214
}
216215
}
217216

218217
/// <summary>
219218
/// A list of `GroundingSupport` structs. Each object details how specific segments of the
220219
/// model's response are supported by the `groundingChunks`.
221220
/// </summary>
222-
public IEnumerable<GroundingSupport> GroundingSupports {
221+
public IReadOnlyList<GroundingSupport> GroundingSupports {
223222
get {
224-
return _groundingSupports ?? new ReadOnlyCollection<GroundingSupport>(new List<GroundingSupport>());
223+
return _groundingSupports ?? new List<GroundingSupport>();
225224
}
226225
}
227226

@@ -234,9 +233,9 @@ public IEnumerable<GroundingSupport> GroundingSupports {
234233

235234
private GroundingMetadata(List<string> webSearchQueries, List<GroundingChunk> groundingChunks,
236235
List<GroundingSupport> groundingSupports, SearchEntryPoint? searchEntryPoint) {
237-
_webSearchQueries = new ReadOnlyCollection<string>(webSearchQueries ?? new List<string>());
238-
_groundingChunks = new ReadOnlyCollection<GroundingChunk>(groundingChunks ?? new List<GroundingChunk>());
239-
_groundingSupports = new ReadOnlyCollection<GroundingSupport>(groundingSupports ?? new List<GroundingSupport>());
236+
_webSearchQueries = webSearchQueries;
237+
_groundingChunks = groundingChunks;
238+
_groundingSupports = groundingSupports;
240239
SearchEntryPoint = searchEntryPoint;
241240
}
242241

@@ -347,7 +346,7 @@ internal static WebGroundingChunk FromJson(Dictionary<string, object> jsonDict)
347346
/// retrieved grounding chunks.
348347
/// </summary>
349348
public readonly struct GroundingSupport {
350-
private readonly ReadOnlyCollection<int> _groundingChunkIndices;
349+
private readonly IReadOnlyList<int> _groundingChunkIndices;
351350

352351
/// <summary>
353352
/// Specifies the segment of the model's response content that this grounding support pertains
@@ -363,15 +362,15 @@ public readonly struct GroundingSupport {
363362
/// means that `groundingChunks[1]`, `groundingChunks[3]`, `groundingChunks[4]` are the
364363
/// retrieved content supporting this part of the response.
365364
/// </summary>
366-
public IEnumerable<int> GroundingChunkIndices {
365+
public IReadOnlyList<int> GroundingChunkIndices {
367366
get {
368-
return _groundingChunkIndices ?? new ReadOnlyCollection<int>(new List<int>());
367+
return _groundingChunkIndices ?? new List<int>();
369368
}
370369
}
371370

372371
private GroundingSupport(Segment segment, List<int> groundingChunkIndices) {
373372
Segment = segment;
374-
_groundingChunkIndices = new ReadOnlyCollection<int>(groundingChunkIndices ?? new List<int>());
373+
_groundingChunkIndices = groundingChunkIndices;
375374
}
376375

377376
internal static GroundingSupport FromJson(Dictionary<string, object> jsonDict) {
@@ -459,17 +458,17 @@ public readonly struct UsageMetadata {
459458
/// </summary>
460459
public int TotalTokenCount { get; }
461460

462-
private readonly ReadOnlyCollection<ModalityTokenCount> _promptTokensDetails;
463-
public IEnumerable<ModalityTokenCount> PromptTokensDetails {
461+
private readonly IReadOnlyList<ModalityTokenCount> _promptTokensDetails;
462+
public IReadOnlyList<ModalityTokenCount> PromptTokensDetails {
464463
get {
465-
return _promptTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
464+
return _promptTokensDetails ?? new List<ModalityTokenCount>();
466465
}
467466
}
468467

469-
private readonly ReadOnlyCollection<ModalityTokenCount> _candidatesTokensDetails;
470-
public IEnumerable<ModalityTokenCount> CandidatesTokensDetails {
468+
private readonly IReadOnlyList<ModalityTokenCount> _candidatesTokensDetails;
469+
public IReadOnlyList<ModalityTokenCount> CandidatesTokensDetails {
471470
get {
472-
return _candidatesTokensDetails ?? new ReadOnlyCollection<ModalityTokenCount>(new List<ModalityTokenCount>());
471+
return _candidatesTokensDetails ?? new List<ModalityTokenCount>();
473472
}
474473
}
475474

@@ -480,11 +479,8 @@ private UsageMetadata(int promptTC, int candidatesTC, int thoughtsTC, int totalT
480479
CandidatesTokenCount = candidatesTC;
481480
ThoughtsTokenCount = thoughtsTC;
482481
TotalTokenCount = totalTC;
483-
_promptTokensDetails =
484-
new ReadOnlyCollection<ModalityTokenCount>(promptDetails ?? new List<ModalityTokenCount>());
485-
_candidatesTokensDetails =
486-
new ReadOnlyCollection<ModalityTokenCount>(candidateDetails ?? new List<ModalityTokenCount>());
487-
482+
_promptTokensDetails = promptDetails;
483+
_candidatesTokensDetails = candidateDetails;
488484
}
489485

490486
/// <summary>

0 commit comments

Comments
 (0)