Skip to content

Commit 10d163a

Browse files
add embeddings
1 parent 9b8eef9 commit 10d163a

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 EmbedContentRequest
13+
{
14+
public string Model { get; } = $"models/{GenerativeAI.Model.Embedding}";
15+
16+
/// <summary>
17+
///
18+
/// </summary>
19+
public Content Content { get; set; }
20+
// [JsonPropertyName("generation_config")]
21+
// public GenerationConfig? GenerationConfig { get; set; }
22+
// [JsonPropertyName("safety_settings")]
23+
// public List<SafetySetting>? SafetySettings { get; set; }
24+
// public List<Tool>? Tools { get; set; }
25+
26+
public EmbedContentRequest(string prompt, GenerationConfig? generationConfig = null, List<SafetySetting>? safetySettings = null, List<Tool>? tools = null)
27+
{
28+
Content = new Content
29+
{
30+
Parts = new List<IPart> { new TextData
31+
{
32+
Text = prompt
33+
}}
34+
};
35+
// if (generationConfig != null) GenerationConfig = generationConfig;
36+
// if (safetySettings != null) SafetySettings = safetySettings;
37+
// if (tools != null) Tools = tools;
38+
}
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 EmbedContentResponse
13+
{
14+
/// <summary>
15+
///
16+
/// </summary>
17+
public Embedding? Embedding { get; set; }
18+
public List<Embedding>? Embeddings { get; set; }
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Embedding
13+
{
14+
/// <summary>
15+
///
16+
/// </summary>
17+
public List<float> Values { get; set; }
18+
}
19+
}

tests/GoogleAi_Embedding_Should.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using FluentAssertions;
2+
using Mscc.GenerativeAI;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace Test.Mscc.GenerativeAI
9+
{
10+
[Collection(nameof(ConfigurationFixture))]
11+
public class GoogleAi_Embedding_Should
12+
{
13+
private readonly ITestOutputHelper output;
14+
private readonly ConfigurationFixture fixture;
15+
private readonly string model = Model.Embedding;
16+
17+
public GoogleAi_Embedding_Should(ITestOutputHelper output, ConfigurationFixture fixture)
18+
{
19+
this.output = output;
20+
this.fixture = fixture;
21+
}
22+
23+
[Fact]
24+
public void Initialize_Model()
25+
{
26+
// Arrange
27+
var expected = Model.Embedding;
28+
// Act
29+
var model = new GenerativeModel(apiKey: fixture.ApiKey, model: this.model);
30+
31+
// Assert
32+
model.Should().NotBeNull();
33+
model.Name().Should().Be(expected);
34+
}
35+
36+
[Fact]
37+
public async void Embed_Content()
38+
{
39+
// Arrange
40+
var prompt = "Write a story about a magic backpack.";
41+
var model = new GenerativeModel(apiKey: fixture.ApiKey, model: this.model);
42+
43+
// Act
44+
var response = await model.EmbedContent(prompt);
45+
46+
// Assert
47+
response.Should().NotBeNull();
48+
response.Embedding.Should().NotBeNull();
49+
response.Embedding.Values.Should().NotBeNull().And.HaveCountGreaterThanOrEqualTo(1);
50+
response.ForEach(x => output.WriteLine(x));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)