Skip to content

Commit d9487bf

Browse files
Refactor and fix embedding functionality
1 parent 45ba54b commit d9487bf

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

src/Mscc.GenerativeAI.Web/GenerativeAIOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace Mscc.GenerativeAI.Web
1+
namespace Mscc.GenerativeAI.Web
42
{
53
public interface IGenerativeAIOptions
64
{

src/Mscc.GenerativeAI/Types/Content.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Mscc.GenerativeAI
77
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
88
public class Content
99
{
10+
private List<Part>? partTypes;
11+
1012
[JsonIgnore]
1113
public List<IPart>? Parts { get; set; }
1214
public string? Role { get; set; }
@@ -18,43 +20,40 @@ public virtual List<Part>? PartTypes
1820
get
1921
{
2022
SynchronizeParts();
21-
return PartTypes;
22-
}
23-
set
24-
{
25-
PartTypes = value;
23+
return partTypes;
2624
}
25+
set => partTypes = value;
2726
}
2827

2928
private void SynchronizeParts()
3029
{
30+
partTypes = null;
3131
if (Parts == null) return;
3232

33-
PartTypes = new List<Part>();
33+
partTypes = new List<Part>();
3434
foreach (var part in Parts)
3535
{
3636
if (part is TextData text)
3737
{
38-
PartTypes.Add(new Part { TextData = text });
38+
partTypes.Add(new Part { TextData = text });
3939
}
4040
if (part is InlineData inline)
4141
{
42-
PartTypes.Add(new Part { InlineData = inline });
42+
partTypes.Add(new Part { InlineData = inline });
4343
}
4444
if (part is FileData file)
4545
{
46-
PartTypes.Add(new Part { FileData = file });
46+
partTypes.Add(new Part { FileData = file });
4747
}
4848
if (part is FunctionResponse response)
4949
{
50-
PartTypes.Add(new Part { FunctionResponse = response });
50+
partTypes.Add(new Part { FunctionResponse = response });
5151
}
5252
if (part is FunctionCall call)
5353
{
54-
PartTypes.Add(new Part { FunctionCall = call });
54+
partTypes.Add(new Part { FunctionCall = call });
5555
}
5656
}
57-
5857
}
5958

6059
private string GetDebuggerDisplay()

src/Mscc.GenerativeAI/Types/EmbedContentRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Text.Json.Serialization;
33
#endif
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace Mscc.GenerativeAI
87
{

src/Mscc.GenerativeAI/Types/EmbedContentResponse.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Text.Json.Serialization;
33
#endif
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace Mscc.GenerativeAI
87
{

src/Mscc.GenerativeAI/Types/Embedding.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Text.Json.Serialization;
33
#endif
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace Mscc.GenerativeAI
87
{

tests/GoogleAi_Embedding_Should.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using FluentAssertions;
22
using Mscc.GenerativeAI;
3-
using System.Collections.Generic;
4-
using System.Linq;
53
using Xunit;
64
using Xunit.Abstractions;
75

@@ -25,6 +23,7 @@ public void Initialize_Model()
2523
{
2624
// Arrange
2725
var expected = Model.Embedding;
26+
2827
// Act
2928
var model = new GenerativeModel(apiKey: fixture.ApiKey, model: this.model);
3029

@@ -47,7 +46,10 @@ public async void Embed_Content()
4746
response.Should().NotBeNull();
4847
response.Embedding.Should().NotBeNull();
4948
response.Embedding.Values.Should().NotBeNull().And.HaveCountGreaterThanOrEqualTo(1);
50-
response.ForEach(x => output.WriteLine(x));
49+
response.Embedding.Values.ForEach(x =>
50+
{
51+
output.WriteLine(x.ToString());
52+
});
5153
}
5254
}
5355
}

0 commit comments

Comments
 (0)