Skip to content

Commit 2adcf39

Browse files
MarkDaoustcopybara-github
authored andcommitted
docs: Add interactions examples.
Fixes: #159 PiperOrigin-RevId: 904120026
1 parent d244d90 commit 2adcf39

332 files changed

Lines changed: 31263 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. Perform these steps when you are in the directory of this README file.
2+
2. Set the `GEMINI_API_KEY` environment variable to your API key.
3+
3. Run `dotnet run` to run the demo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Google.GenAI\Google.GenAI.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>net8.0</TargetFramework>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
14+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2026 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 Google.GenAI;
18+
using Google.GenAI.Gaos.Models.Interactions;
19+
using Google.GenAI.Gaos.Models.Requests;
20+
21+
bool isVertex = args.Contains("--vertex", StringComparer.OrdinalIgnoreCase);
22+
Console.WriteLine(isVertex ? "Running in Vertex AI mode." : "Running in Gemini API mode.");
23+
24+
Client client = new Client(vertexAI: isVertex);
25+
26+
if (isVertex)
27+
{
28+
Console.WriteLine("Interactions API is not yet supported on Vertex AI");
29+
return;
30+
}
31+
32+
CreateModelInteraction params1 = new()
33+
{
34+
Input = InteractionsInput.CreateStr("Why is the sky blue?"),
35+
Model = Model.Gemini25Flash,
36+
};
37+
38+
var response_interaction = await client.Interactions.CreateAsync(CreateInteractionRequestBody.CreateCreateModelInteraction(params1));
39+
var interaction = response_interaction.Interaction;
40+
41+
Console.WriteLine($"Interaction ID: {interaction.Id}");
42+
Console.WriteLine($"Status: {interaction.Status}");
43+
44+
Console.WriteLine($"Output: {interaction.OutputText}");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Google.GenAI\Google.GenAI.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>net8.0</TargetFramework>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
14+
</Project>
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 Google.GenAI;
18+
using Google.GenAI.Gaos.Models.Interactions;
19+
using Google.GenAI.Gaos.Models.Requests;
20+
21+
bool isVertex = args.Contains("--vertex", StringComparer.OrdinalIgnoreCase);
22+
Console.WriteLine(isVertex ? "Running in Vertex AI mode." : "Running in Gemini API mode.");
23+
24+
Client client = new Client(vertexAI: isVertex);
25+
26+
CreateModelInteraction parameters = new()
27+
{
28+
Input = InteractionsInput.CreateStr("What is your name?"),
29+
Model = Model.Gemini25Flash,
30+
};
31+
32+
var response_interaction = await client.Interactions.CreateAsync(CreateInteractionRequestBody.CreateCreateModelInteraction(parameters));
33+
var interaction = response_interaction.Interaction;
34+
35+
Console.WriteLine($"Interaction ID: {interaction.Id}");
36+
Console.WriteLine($"Status: {interaction.Status}");
37+
38+
Console.WriteLine($"Output: {interaction.OutputText}");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. Perform these steps when you are in the directory of this README file.
2+
2. Set the `GEMINI_API_KEY` environment variable to your API key.
3+
3. Run `dotnet run` to run the demo
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
4+
<ItemGroup>
5+
<ProjectReference Include="..\..\..\Google.GenAI\Google.GenAI.csproj" />
6+
</ItemGroup>
7+
8+
<PropertyGroup>
9+
<OutputType>Exe</OutputType>
10+
<TargetFramework>net8.0</TargetFramework>
11+
<ImplicitUsings>enable</ImplicitUsings>
12+
<Nullable>enable</Nullable>
13+
</PropertyGroup>
14+
15+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 Google.GenAI;
18+
using Google.GenAI.Gaos.Models.Interactions;
19+
using Google.GenAI.Gaos.Models.Requests;
20+
21+
bool isVertex = args.Contains("--vertex", StringComparer.OrdinalIgnoreCase);
22+
Console.WriteLine(isVertex ? "Running in Vertex AI mode." : "Running in Gemini API mode.");
23+
24+
Client client = new Client(vertexAI: isVertex);
25+
26+
CreateModelInteraction parameters = new()
27+
{
28+
Input = InteractionsInput.CreateStr("Tell me a story in 300 words."),
29+
Model = Model.Gemini25Flash,
30+
Stream = true,
31+
};
32+
33+
var response = await client.Interactions.CreateAsync(
34+
CreateInteractionRequestBody.CreateCreateModelInteraction(parameters)
35+
);
36+
37+
var stream = response.InteractionSSEStreamEvent;
38+
if (stream != null)
39+
{
40+
while (true)
41+
{
42+
var sseEvent = await stream.Next();
43+
if (sseEvent == null) break;
44+
45+
var stepDelta = sseEvent.GetDataStepDelta();
46+
var textDelta = stepDelta?.GetDeltaText();
47+
if (textDelta != null)
48+
{
49+
Console.Write(textDelta.Text);
50+
}
51+
}
52+
}
53+
54+
Console.WriteLine();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. Perform these steps when you are in the directory of this README file.
2+
2. Set the `GEMINI_API_KEY` environment variable to your API key.
3+
3. Run `dotnet run` to run the demo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Google.GenAI\Google.GenAI.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>net8.0</TargetFramework>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)