|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using System.Threading.Tasks; |
| 21 | + |
| 22 | +using Google.GenAI; |
| 23 | +using Google.GenAI.Types; |
| 24 | + |
| 25 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 26 | + |
| 27 | +using TestServerSdk; |
| 28 | + |
| 29 | +[TestClass] |
| 30 | +public class EmbedContentTest { |
| 31 | + private static TestServerProcess? _server; |
| 32 | + private Client vertexClient; |
| 33 | + private Client geminiClient; |
| 34 | + private string modelName; |
| 35 | + public TestContext TestContext { get; set; } |
| 36 | + |
| 37 | + [ClassInitialize] |
| 38 | + public static void ClassInit(TestContext _) { |
| 39 | + _server = TestServer.StartTestServer(); |
| 40 | + } |
| 41 | + |
| 42 | + [ClassCleanup] |
| 43 | + public static void ClassCleanup() { |
| 44 | + TestServer.StopTestServer(_server); |
| 45 | + } |
| 46 | + |
| 47 | + [TestInitialize] |
| 48 | + public void TestInit() { |
| 49 | + // Test server specific setup. |
| 50 | + if (_server == null) { |
| 51 | + throw new InvalidOperationException("Test server is not initialized."); |
| 52 | + } |
| 53 | + var geminiClientHttpOptions = new HttpOptions { |
| 54 | + Headers = new Dictionary<string, string> { { "Test-Name", |
| 55 | + $"{GetType().Name}.{TestContext.TestName}" } }, |
| 56 | + BaseUrl = "http://localhost:1453" |
| 57 | + }; |
| 58 | + var vertexClientHttpOptions = new HttpOptions { |
| 59 | + Headers = new Dictionary<string, string> { { "Test-Name", |
| 60 | + $"{GetType().Name}.{TestContext.TestName}" } }, |
| 61 | + BaseUrl = "http://localhost:1454" |
| 62 | + }; |
| 63 | + |
| 64 | + // Common setup for both clients. |
| 65 | + string project = System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT"); |
| 66 | + string location = |
| 67 | + System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION") ?? "us-central1"; |
| 68 | + string apiKey = System.Environment.GetEnvironmentVariable("GOOGLE_API_KEY"); |
| 69 | + vertexClient = new Client(project: project, location: location, vertexAI: true, |
| 70 | + credential: TestServer.GetCredentialForTestMode(), |
| 71 | + httpOptions: vertexClientHttpOptions); |
| 72 | + geminiClient = |
| 73 | + new Client(apiKey: apiKey, vertexAI: false, httpOptions: geminiClientHttpOptions); |
| 74 | + |
| 75 | + // Specific setup for this test class |
| 76 | + modelName = "text-embedding-004"; |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public async Task EmbedContentSimpleTextVertexTest() { |
| 81 | + var contents = new List<Content> { |
| 82 | + new Content { Parts = new List<Part> { new Part { Text = "What is your name?" } } } |
| 83 | + }; |
| 84 | + var vertexResponse = await vertexClient.Models.EmbedContentAsync( |
| 85 | + model: modelName, contents: contents, config: null); |
| 86 | + |
| 87 | + Assert.IsNotNull(vertexResponse.Embeddings); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public async Task EmbedContentSimpleTextGeminiTest() { |
| 92 | + var contents = new List<Content> { |
| 93 | + new Content { Parts = new List<Part> { new Part { Text = "What is your name?" } } } |
| 94 | + }; |
| 95 | + var geminiResponse = await geminiClient.Models.EmbedContentAsync( |
| 96 | + model: modelName, contents: contents, config: null); |
| 97 | + |
| 98 | + Assert.IsNotNull(geminiResponse.Embeddings); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public async Task EmbedContentSingleStringVertexTest() { |
| 103 | + var vertexResponse = await vertexClient.Models.EmbedContentAsync( |
| 104 | + model: modelName, contents: "What is your name?", config: null); |
| 105 | + |
| 106 | + Assert.IsNotNull(vertexResponse.Embeddings); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public async Task EmbedContentSingleStringGeminiTest() { |
| 111 | + var geminiResponse = await geminiClient.Models.EmbedContentAsync( |
| 112 | + model: modelName, contents: "What is your name?", config: null); |
| 113 | + |
| 114 | + Assert.IsNotNull(geminiResponse.Embeddings); |
| 115 | + } |
| 116 | + |
| 117 | + [TestMethod] |
| 118 | + public async Task EmbedContentMultiTextVertexTest() { |
| 119 | + var contents = new List<Content> { |
| 120 | + new Content { Parts = new List<Part> { new Part { Text = "What is your name?" } } }, |
| 121 | + new Content { Parts = new List<Part> { new Part { Text = "I am a model." } } } |
| 122 | + }; |
| 123 | + var config = new EmbedContentConfig { OutputDimensionality = 10, Title = "test_title", |
| 124 | + TaskType = "RETRIEVAL_DOCUMENT" }; |
| 125 | + var vertexResponse = |
| 126 | + await vertexClient.Models.EmbedContentAsync(model: modelName, contents: contents, config: config); |
| 127 | + |
| 128 | + Assert.IsNotNull(vertexResponse.Embeddings); |
| 129 | + Assert.AreEqual(2, vertexResponse.Embeddings.Count); |
| 130 | + } |
| 131 | + |
| 132 | + [TestMethod] |
| 133 | + public async Task EmbedContentMultiTextGeminiTest() { |
| 134 | + var contents = new List<Content> { |
| 135 | + new Content { Parts = new List<Part> { new Part { Text = "What is your name?" } } }, |
| 136 | + new Content { Parts = new List<Part> { new Part { Text = "I am a model." } } } |
| 137 | + }; |
| 138 | + var config = new EmbedContentConfig { OutputDimensionality = 10, Title = "test_title", |
| 139 | + TaskType = "RETRIEVAL_DOCUMENT" }; |
| 140 | + var geminiResponse = |
| 141 | + await geminiClient.Models.EmbedContentAsync(model: modelName, contents: contents, config: config); |
| 142 | + |
| 143 | + Assert.IsNotNull(geminiResponse.Embeddings); |
| 144 | + Assert.AreEqual(2, geminiResponse.Embeddings.Count); |
| 145 | + } |
| 146 | +} |
0 commit comments