Skip to content

Commit 9b8eef9

Browse files
refactor handling of parts
1 parent b0f0d99 commit 9b8eef9

File tree

2 files changed

+135
-130
lines changed

2 files changed

+135
-130
lines changed
Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,77 @@
1-
using System.Collections.Generic;
2-
using System.Diagnostics;
3-
using System.Text.Json.Serialization;
4-
5-
namespace Mscc.GenerativeAI
6-
{
7-
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
8-
public class Content
9-
{
10-
[JsonIgnore]
11-
public List<IPart>? Parts { get; set; }
12-
public string? Role { get; set; }
13-
14-
[DebuggerHidden]
15-
[JsonPropertyName("parts")]
16-
public virtual List<Part>? PartTypes { get; set; }
17-
18-
internal void SynchronizeParts()
19-
{
20-
if (Parts == null) return;
21-
22-
PartTypes = new List<Part>();
23-
foreach (var part in Parts)
24-
{
25-
if (part is TextData text)
26-
{
27-
PartTypes.Add(new Part { TextData = text });
28-
}
29-
if (part is InlineData inline)
30-
{
31-
PartTypes.Add(new Part { InlineData = inline });
32-
}
33-
if (part is FileData file)
34-
{
35-
PartTypes.Add(new Part { FileData = file });
36-
}
37-
if (part is FunctionResponse response)
38-
{
39-
PartTypes.Add(new Part { FunctionResponse = response });
40-
}
41-
if (part is FunctionCall call)
42-
{
43-
PartTypes.Add(new Part { FunctionCall = call });
44-
}
45-
}
46-
47-
}
48-
49-
private string GetDebuggerDisplay()
50-
{
51-
return $"Role: {Role} - Parts: {Parts?.Count}";
52-
}
53-
}
54-
55-
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
56-
public class ContentResponse
57-
{
58-
public List<Part> Parts { get; set; }
59-
public string Role { get; set; }
60-
61-
private string GetDebuggerDisplay()
62-
{
63-
return $"Role: {Role} - Parts: {Parts.Count}";
64-
}
65-
}
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Mscc.GenerativeAI
6+
{
7+
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
8+
public class Content
9+
{
10+
[JsonIgnore]
11+
public List<IPart>? Parts { get; set; }
12+
public string? Role { get; set; }
13+
14+
[DebuggerHidden]
15+
[JsonPropertyName("parts")]
16+
public virtual List<Part>? PartTypes
17+
{
18+
get
19+
{
20+
SynchronizeParts();
21+
return PartTypes;
22+
}
23+
set
24+
{
25+
PartTypes = value;
26+
}
27+
}
28+
29+
private void SynchronizeParts()
30+
{
31+
if (Parts == null) return;
32+
33+
PartTypes = new List<Part>();
34+
foreach (var part in Parts)
35+
{
36+
if (part is TextData text)
37+
{
38+
PartTypes.Add(new Part { TextData = text });
39+
}
40+
if (part is InlineData inline)
41+
{
42+
PartTypes.Add(new Part { InlineData = inline });
43+
}
44+
if (part is FileData file)
45+
{
46+
PartTypes.Add(new Part { FileData = file });
47+
}
48+
if (part is FunctionResponse response)
49+
{
50+
PartTypes.Add(new Part { FunctionResponse = response });
51+
}
52+
if (part is FunctionCall call)
53+
{
54+
PartTypes.Add(new Part { FunctionCall = call });
55+
}
56+
}
57+
58+
}
59+
60+
private string GetDebuggerDisplay()
61+
{
62+
return $"Role: {Role} - Parts: {Parts?.Count}";
63+
}
64+
}
65+
66+
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
67+
public class ContentResponse
68+
{
69+
public List<Part> Parts { get; set; }
70+
public string Role { get; set; }
71+
72+
private string GetDebuggerDisplay()
73+
{
74+
return $"Role: {Role} - Parts: {Parts.Count}";
75+
}
76+
}
6677
}
Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
1-
#if NET472_OR_GREATER || NETSTANDARD2_0
2-
using System.Text.Json.Serialization;
3-
#endif
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
7-
namespace Mscc.GenerativeAI
8-
{
9-
// Todo: Integrate GenerationConfig, SafetySettings, and Tools.
10-
public class GenerateContentRequest
11-
12-
{
13-
public List<Content>? Contents { get; set; }
14-
[JsonPropertyName("generation_config")]
15-
public GenerationConfig? GenerationConfig { get; set; }
16-
[JsonPropertyName("safety_settings")]
17-
public List<SafetySetting>? SafetySettings { get; set; }
18-
public List<Tool>? Tools { get; set; }
19-
20-
public GenerateContentRequest() { }
21-
22-
public GenerateContentRequest(string prompt, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
23-
{
24-
Contents = new List<Content> { new Content
25-
{
26-
Parts = new List<IPart> { new TextData
27-
{
28-
Text = prompt
29-
}}
30-
}};
31-
if (generationConfig != null) GenerationConfig = generationConfig;
32-
if (safetySettings != null) SafetySettings = safetySettings;
33-
if (tools != null) Tools = tools;
34-
}
35-
36-
public GenerateContentRequest(List<IPart> parts, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
37-
{
38-
Contents = new List<Content> { new Content
39-
{
40-
Parts = parts
41-
}};
42-
if (generationConfig != null) GenerationConfig = generationConfig;
43-
if (safetySettings != null) SafetySettings = safetySettings;
44-
if (tools != null) Tools = tools;
45-
}
46-
47-
public GenerateContentRequest(List<Part> parts, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
48-
{
49-
Contents = new List<Content> { new Content
50-
{
51-
Parts = parts.Select(p => (IPart)p).ToList()
52-
}};
53-
if (generationConfig != null) GenerationConfig = generationConfig;
54-
if (safetySettings != null) SafetySettings = safetySettings;
55-
if (tools != null) Tools = tools;
56-
}
57-
58-
internal void Synchronize()
59-
{
60-
foreach (var content in Contents)
61-
{
62-
content.SynchronizeParts();
63-
}
64-
}
65-
}
1+
#if NET472_OR_GREATER || NETSTANDARD2_0
2+
using System.Text.Json.Serialization;
3+
#endif
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace Mscc.GenerativeAI
8+
{
9+
/// <summary>
10+
///
11+
/// </summary>
12+
public class GenerateContentRequest
13+
14+
{
15+
public List<Content>? Contents { get; set; }
16+
[JsonPropertyName("generation_config")]
17+
public GenerationConfig? GenerationConfig { get; set; }
18+
[JsonPropertyName("safety_settings")]
19+
public List<SafetySetting>? SafetySettings { get; set; }
20+
public List<Tool>? Tools { get; set; }
21+
22+
public GenerateContentRequest() { }
23+
24+
public GenerateContentRequest(string prompt, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
25+
{
26+
Contents = new List<Content> { new Content
27+
{
28+
Parts = new List<IPart> { new TextData
29+
{
30+
Text = prompt
31+
}}
32+
}};
33+
if (generationConfig != null) GenerationConfig = generationConfig;
34+
if (safetySettings != null) SafetySettings = safetySettings;
35+
if (tools != null) Tools = tools;
36+
}
37+
38+
public GenerateContentRequest(List<IPart> parts, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
39+
{
40+
Contents = new List<Content> { new Content
41+
{
42+
Parts = parts
43+
}};
44+
if (generationConfig != null) GenerationConfig = generationConfig;
45+
if (safetySettings != null) SafetySettings = safetySettings;
46+
if (tools != null) Tools = tools;
47+
}
48+
49+
public GenerateContentRequest(List<Part> parts, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
50+
{
51+
Contents = new List<Content> { new Content
52+
{
53+
Parts = parts.Select(p => (IPart)p).ToList()
54+
}};
55+
if (generationConfig != null) GenerationConfig = generationConfig;
56+
if (safetySettings != null) SafetySettings = safetySettings;
57+
if (tools != null) Tools = tools;
58+
}
59+
}
6660
}

0 commit comments

Comments
 (0)