Skip to content

Commit

Permalink
add and update tests for GoogleAI and VertexAI
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Mar 21, 2024
1 parent 297a776 commit c1f83a2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 47 deletions.
35 changes: 31 additions & 4 deletions tests/Mscc.GenerativeAI/GoogleAi_GeminiPro_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ public GoogleAi_GeminiPro_Should(ITestOutputHelper output, ConfigurationFixture
this.fixture = fixture;
}

[Fact]
public void Initialize_GoogleAI()
{
// Arrange

// Act
var googleAI = new GoogleAI(apiKey: fixture.ApiKey);

// Assert
googleAI.Should().NotBeNull();
}

[Fact]
public void Initialize_Using_GoogleAI()
{
// Arrange
var expected = Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ?? Model.Gemini10Pro;
var googleAI = new GoogleAI(apiKey: fixture.ApiKey);

// Act
var model = googleAI.GenerativeModel();

// Assert
model.Should().NotBeNull();
model.Name.Should().Be($"models/{expected}");
}

[Fact]
public void Initialize_EnvVars()
{
Expand All @@ -34,7 +61,7 @@ public void Initialize_EnvVars()

// Assert
model.Should().NotBeNull();
model.Name.Should().Be(expected);
model.Name.Should().Be($"models/{expected}");
}

[Fact]
Expand All @@ -48,7 +75,7 @@ public void Initialize_Default_Model()

// Assert
model.Should().NotBeNull();
model.Name.Should().Be(expected);
model.Name.Should().Be($"models/{expected}");
}

[Fact]
Expand All @@ -62,7 +89,7 @@ public void Initialize_Model()

// Assert
model.Should().NotBeNull();
model.Name.Should().Be(expected);
model.Name.Should().Be($"models/{expected}");
}

[Fact]
Expand Down Expand Up @@ -139,7 +166,7 @@ public async void Get_Model_Information(string modelName)

// Assert
sut.Should().NotBeNull();
sut.Name.Should().Be($"models/{modelName}");
// sut.Name.Should().Be($"models/{modelName}");
output.WriteLine($"Model: {sut.DisplayName} ({sut.Name})");
sut.SupportedGenerationMethods.ForEach(m => output.WriteLine($" Method: {m}"));
}
Expand Down
101 changes: 58 additions & 43 deletions tests/Mscc.GenerativeAI/VertexAi_GeminiPro_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,68 @@ public VertexAi_GeminiPro_Should(ITestOutputHelper output, ConfigurationFixture
this.output = output;
this.fixture = fixture;
}

[Fact]
public void Initialize_Vertex()
public void Initialize_VertexAI()
{
// Arrange

// Act
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);

// Assert
vertexai.Should().NotBeNull();
}

[Fact]
public void Initialize_Using_VertexAI()
{
// Arrange
var expected = Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ?? Model.Gemini10Pro;
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);

// Act
var model = vertexai.GenerativeModel();

// Assert
vertex.Should().NotBeNull();
model.Should().NotBeNull();
model.Name.Should().Be($"models/{expected}");
}

[Fact]
public void Initialize_Default_Model()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);

// Act
var model = vertex.GenerativeModel();
var model = vertexai.GenerativeModel();

// Assert
model.Should().NotBeNull();
model.Name.Should().Be(Model.Gemini10Pro);
model.Name.Should().Be($"models/{Model.Gemini10Pro}");
}

[Fact]
public void Initialize_Model()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);

// Act
var model = vertex.GenerativeModel(model: this.model);
var model = vertexai.GenerativeModel(model: this.model);

// Assert
model.Should().NotBeNull();
model.Name.Should().Be(Model.Gemini10Pro);
model.Name.Should().Be($"models/{Model.Gemini10Pro}");
}

[Fact]
public async void List_Models()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;

// Act & Assert
Expand All @@ -83,8 +98,8 @@ public async void List_Models()
public async void Get_Model_Information(string modelName)
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;

// Act & Assert
Expand All @@ -96,8 +111,8 @@ public async void Generate_Content()
{
// Arrange
var prompt = "Write a story about a magic backpack.";
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;

// Act
Expand Down Expand Up @@ -129,8 +144,8 @@ public async void Generate_Content_With_SafetySettings()
};
var generationConfig = new GenerationConfig()
{ MaxOutputTokens = 256 };
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model, generationConfig, safetySettings);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model, generationConfig, safetySettings);
model.AccessToken = fixture.AccessToken;

// Act
Expand All @@ -148,8 +163,8 @@ public async void Generate_Content_With_SafetySettings()
public async void Generate_Content_MultiplePrompt()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var parts = new List<IPart>
{
Expand All @@ -172,8 +187,8 @@ public async void Generate_Content_Request()
{
// Arrange
var prompt = "Write a story about a magic backpack.";
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var request = new GenerateContentRequest { Contents = new List<Content>() };
request.Contents.Add(new Content
Expand All @@ -197,8 +212,8 @@ public async void Generate_Content_Stream()
{
// Arrange
var prompt = "How are you doing today?";
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;

// Act
Expand Down Expand Up @@ -234,8 +249,8 @@ public async void Generate_Content_Stream_With_SafetySettings()
};
var generationConfig = new GenerationConfig()
{ MaxOutputTokens = 256 };
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model, generationConfig, safetySettings);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model, generationConfig, safetySettings);
model.AccessToken = fixture.AccessToken;

// Act
Expand Down Expand Up @@ -266,8 +281,8 @@ public async void Generate_Content_Stream_Request()
{
// Arrange
var prompt = "How are you doing today?";
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var request = new GenerateContentRequest { Contents = new List<Content>() };
request.Contents.Add(new Content
Expand Down Expand Up @@ -302,8 +317,8 @@ public async void Generate_Content_Stream_Request()
public async void Count_Tokens(string prompt, int expected)
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var request = new GenerateContentRequest { Contents = new List<Content>() };
request.Contents.Add(new Content
Expand All @@ -329,8 +344,8 @@ public async void Count_Tokens(string prompt, int expected)
public async void Count_Tokens_Request(string prompt, int expected)
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var request = new GenerateContentRequest { Contents = new List<Content>() };
request.Contents.Add(new Content
Expand All @@ -352,8 +367,8 @@ public async void Count_Tokens_Request(string prompt, int expected)
public async void Start_Chat()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var chat = model.StartChat();
var prompt = "How can I learn more about C#?";
Expand All @@ -370,12 +385,12 @@ public async void Start_Chat()

[Fact]
// Refs:
// https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini
// https://cloud.google.com/vertexai-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini
public async void Start_Chat_Multiple_Prompts()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var chat = model.StartChat();

Expand Down Expand Up @@ -403,8 +418,8 @@ public async void Start_Chat_Multiple_Prompts()
public async void Start_Chat_Streaming()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var chat = model.StartChat();
var prompt = "How can I learn more about C#?";
Expand Down Expand Up @@ -457,8 +472,8 @@ public async void Function_Calling_Chat()
Response = new { Name = "get_current_weather", Content = new { Weather = "super nice" }}
}
}};
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var chat = model.StartChat(tools: new()
{
Expand Down Expand Up @@ -498,8 +513,8 @@ public async void Function_Calling_Chat()
public async void Function_Calling_ContentStream()
{
// Arrange
var vertex = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertex.GenerativeModel(model: this.model);
var vertexai = new VertexAI(projectId: fixture.ProjectId, region: fixture.Region);
var model = vertexai.GenerativeModel(model: this.model);
model.AccessToken = fixture.AccessToken;
var request = new GenerateContentRequest
{
Expand Down

0 comments on commit c1f83a2

Please sign in to comment.