@@ -17,9 +17,9 @@ public class ApiRequestBuilder
17
17
private string ? _systemInstruction ;
18
18
private GenerationConfig ? _config ;
19
19
private bool _useGrounding = false ;
20
- private List < SafetySetting > ? _safetySettings ;
20
+ private IEnumerable < SafetySetting > ? _safetySettings ;
21
21
private List < Content > ? _chatHistory ;
22
- private List < ImageData > ? _images ;
22
+ private IEnumerable < ImageData > ? _images ;
23
23
24
24
/// <summary>
25
25
/// Sets the system instruction for the API request.
@@ -70,7 +70,7 @@ public ApiRequestBuilder EnableGrounding()
70
70
/// </summary>
71
71
/// <param name="safetySettings">The list of safety settings to set.</param>
72
72
/// <returns>The current instance of <see cref="ApiRequestBuilder"/>.</returns>
73
- public ApiRequestBuilder WithSafetySettings ( List < SafetySetting > safetySettings )
73
+ public ApiRequestBuilder WithSafetySettings ( IEnumerable < SafetySetting > safetySettings )
74
74
{
75
75
_safetySettings = safetySettings ;
76
76
return this ;
@@ -82,8 +82,8 @@ public ApiRequestBuilder WithSafetySettings(List<SafetySetting> safetySettings)
82
82
/// <returns>The current instance of <see cref="ApiRequestBuilder"/>.</returns>
83
83
public ApiRequestBuilder DisableAllSafetySettings ( )
84
84
{
85
- _safetySettings = new List < SafetySetting >
86
- {
85
+ _safetySettings =
86
+ [
87
87
new SafetySetting
88
88
{
89
89
Category = EnumHelper . GetDescription ( SafetySettingHarmCategory . DangerousContent ) ,
@@ -104,7 +104,7 @@ public ApiRequestBuilder DisableAllSafetySettings()
104
104
{
105
105
Category = EnumHelper . GetDescription ( SafetySettingHarmCategory . SexuallyExplicit ) ,
106
106
} ,
107
- } ;
107
+ ] ;
108
108
109
109
return this ;
110
110
}
@@ -155,9 +155,9 @@ public ApiRequestBuilder WithPrompt(string prompt)
155
155
/// <param name="chatMessages"></param>
156
156
/// <returns></returns>
157
157
/// <exception cref="ArgumentNullException"></exception>
158
- public ApiRequestBuilder WithChatHistory ( List < ChatMessage > chatMessages )
158
+ public ApiRequestBuilder WithChatHistory ( IEnumerable < ChatMessage > chatMessages )
159
159
{
160
- if ( chatMessages == null || chatMessages . Count == 0 )
160
+ if ( chatMessages == null || ! chatMessages . Any ( ) )
161
161
{
162
162
throw new ArgumentNullException ( nameof ( chatMessages ) , "Chat history can't be null or empty." ) ;
163
163
}
@@ -183,9 +183,9 @@ public ApiRequestBuilder WithChatHistory(List<ChatMessage> chatMessages)
183
183
/// Sets the Base64 images for the API request.
184
184
/// </summary>
185
185
/// <returns></returns>
186
- public ApiRequestBuilder WithBase64Images ( List < string > images )
186
+ public ApiRequestBuilder WithBase64Images ( IEnumerable < string > images )
187
187
{
188
- _images = images . Select ( ImageHelper . AsImageData ) . ToList ( ) ;
188
+ _images = images . Select ( ImageHelper . AsImageData ) ;
189
189
return this ;
190
190
}
191
191
@@ -201,11 +201,9 @@ public ApiRequest Build()
201
201
throw new ArgumentNullException ( nameof ( _prompt ) , "Prompt can't be an empty string." ) ;
202
202
}
203
203
204
- var contents = _chatHistory == null
205
- ? new List < Content > ( )
206
- : _chatHistory ;
204
+ var contents = _chatHistory ?? [ ] ;
207
205
208
- if ( _images != null )
206
+ if ( _images != null && _images . Any ( ) )
209
207
{
210
208
contents . Add ( new Content
211
209
{
@@ -239,27 +237,27 @@ public ApiRequest Build()
239
237
{
240
238
Contents = contents ,
241
239
GenerationConfig = _config ,
242
- SafetySettings = _safetySettings ,
240
+ SafetySettings = _safetySettings ? . ToList ( ) ,
243
241
Tools = _useGrounding
244
- ? new List < Tool >
245
- {
242
+ ?
243
+ [
246
244
new Tool
247
245
{
248
246
GoogleSearch = new GoogleSearch ( )
249
247
}
250
- }
248
+ ]
251
249
: null ,
252
250
SystemInstruction = string . IsNullOrEmpty ( _systemInstruction )
253
251
? null
254
252
: new SystemInstruction
255
253
{
256
- Parts = new List < Part >
257
- {
254
+ Parts =
255
+ [
258
256
new Part
259
257
{
260
258
Text = _systemInstruction
261
259
}
262
- }
260
+ ]
263
261
}
264
262
} ;
265
263
}
0 commit comments