Skip to content

Commit 7c95aa3

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add text generation samples (1) (#3256)
Create GenAI solution and add new samples for text generation: - Text generation with text - Text generation with Pdf --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent 3389a69 commit 7c95aa3

File tree

9 files changed

+300
-0
lines changed

9 files changed

+300
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
10+
<PackageReference Include="xunit" Version="2.9.3" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\GenAI.Samples\GenAI.Samples.csproj" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 Xunit;
19+
20+
[CollectionDefinition(nameof(GenAIFixture))]
21+
public class GenAIFixture : ICollectionFixture<GenAIFixture>
22+
{
23+
public string ProjectId { get; }
24+
25+
public GenAIFixture()
26+
{
27+
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
28+
if (string.IsNullOrEmpty(ProjectId))
29+
{
30+
throw new Exception("Missing GOOGLE_PROJECT_ID environment variable.");
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.Threading.Tasks;
18+
using Xunit;
19+
20+
[Collection(nameof(GenAIFixture))]
21+
public class TextGenWithPdfTest
22+
{
23+
private readonly GenAIFixture _fixture;
24+
private readonly TextGenWithPdf _sample;
25+
26+
public TextGenWithPdfTest(GenAIFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new TextGenWithPdf();
30+
}
31+
32+
[Fact]
33+
public async Task TestTextGenWithPdf()
34+
{
35+
var response = await _sample.GenerateContent(_fixture.ProjectId);
36+
Assert.NotEmpty(response);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.Threading.Tasks;
18+
using Xunit;
19+
20+
[Collection(nameof(GenAIFixture))]
21+
public class TextGenWithTxtTest
22+
{
23+
private readonly GenAIFixture _fixture;
24+
private readonly TextGenWithTxt _sample;
25+
26+
public TextGenWithTxtTest(GenAIFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new TextGenWithTxt();
30+
}
31+
32+
[Fact]
33+
public async Task TestTextGenWithTxt()
34+
{
35+
var response = await _sample.GenerateContent(_fixture.ProjectId);
36+
Assert.NotEmpty(response);
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Google.GenAI" Version="0.3.0" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
// [START googlegenaisdk_textgen_with_pdf]
18+
19+
using Google.GenAI;
20+
using Google.GenAI.Types;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Threading.Tasks;
24+
25+
public class TextGenWithPdf
26+
{
27+
public async Task<string> GenerateContent(
28+
string projectId = "your-project-id",
29+
string location = "global",
30+
string model = "gemini-2.5-flash")
31+
{
32+
await using var client = new Client(
33+
project: projectId,
34+
location: location,
35+
vertexAI: true,
36+
httpOptions: new HttpOptions { ApiVersion = "v1" });
37+
38+
string prompt = @"
39+
You are a highly skilled document summarization specialist.
40+
Your task is to provide a concise executive summary of no more than 300 words.
41+
Please summarize the given document for a general audience.";
42+
43+
var contents = new List<Content>
44+
{
45+
new Content
46+
{
47+
Role = "user",
48+
Parts = new List<Part>
49+
{
50+
new Part
51+
{
52+
FileData = new FileData
53+
{
54+
FileUri = "gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf",
55+
MimeType = "application/pdf",
56+
}
57+
},
58+
new Part { Text = prompt }
59+
}
60+
}
61+
};
62+
63+
GenerateContentResponse response = await client.Models.GenerateContentAsync(model: model, contents: contents);
64+
65+
string responseText = response.Candidates[0].Content.Parts[0].Text;
66+
Console.WriteLine(responseText);
67+
// Example reponse:
68+
// This paper introduces the Transformer, a novel neural network architecture designed
69+
// for processing sequences, such as those found in language translation. Traditionally,
70+
// such tasks have relied on complex recurrent or convolutional neural networks...
71+
return responseText;
72+
}
73+
}
74+
// [END googlegenaisdk_textgen_with_pdf]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// [START googlegenaisdk_textgen_with_txt]
18+
19+
using Google.GenAI;
20+
using Google.GenAI.Types;
21+
using System;
22+
using System.Threading.Tasks;
23+
24+
public class TextGenWithTxt
25+
{
26+
public async Task<string> GenerateContent(
27+
string projectId = "your-project-id",
28+
string location = "global",
29+
string model = "gemini-2.5-flash")
30+
{
31+
await using var client = new Client(
32+
project: projectId,
33+
location: location,
34+
vertexAI: true,
35+
httpOptions: new HttpOptions { ApiVersion = "v1" });
36+
37+
GenerateContentResponse response = await client.Models.GenerateContentAsync(model: model, contents: "How does AI work?");
38+
39+
string responseText = response.Candidates[0].Content.Parts[0].Text;
40+
Console.WriteLine(responseText);
41+
// Example reponse:
42+
// AI, or Artificial Intelligence, at its core, is about creating machines that can perform...
43+
// Here's a breakdown of how it generally works...
44+
return responseText;
45+
}
46+
}
47+
// [END googlegenaisdk_textgen_with_txt]

genai/api/GenAI.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenAI.Samples", "GenAI.Samples\GenAI.Samples.csproj", "{4CE7122F-BB91-4CDD-AE2A-50AE6F00ECF8}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenAI.Samples.Tests", "GenAI.Samples.Tests\GenAI.Samples.Tests.csproj", "{5381F18E-8B7E-4D71-A909-C936F784AB65}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{4CE7122F-BB91-4CDD-AE2A-50AE6F00ECF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{4CE7122F-BB91-4CDD-AE2A-50AE6F00ECF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{4CE7122F-BB91-4CDD-AE2A-50AE6F00ECF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{4CE7122F-BB91-4CDD-AE2A-50AE6F00ECF8}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{5381F18E-8B7E-4D71-A909-C936F784AB65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{5381F18E-8B7E-4D71-A909-C936F784AB65}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{5381F18E-8B7E-4D71-A909-C936F784AB65}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{5381F18E-8B7E-4D71-A909-C936F784AB65}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal

genai/api/runTests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
dotnet restore --force
16+
dotnet test --no-restore --test-adapter-path:. --logger:junit 2>&1 | %{ "$_" }

0 commit comments

Comments
 (0)