Skip to content

Commit 548adc3

Browse files
authored
Merge branch 'main' into main
2 parents 44bb4b4 + b03c3d0 commit 548adc3

File tree

9 files changed

+553
-131
lines changed

9 files changed

+553
-131
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.SemanticKernel;
4+
using Microsoft.SemanticKernel.ChatCompletion;
5+
using Resources;
6+
7+
namespace ChatCompletion;
8+
9+
/// <summary>
10+
/// This sample shows how to use binary file and inline Base64 inputs, like PDFs, with Google Gemini's chat completion.
11+
/// </summary>
12+
public class Google_GeminiChatCompletionWithFile(ITestOutputHelper output) : BaseTest(output)
13+
{
14+
[Fact]
15+
public async Task GoogleAIChatCompletionWithLocalFile()
16+
{
17+
Console.WriteLine("============= Google AI - Gemini Chat Completion With Local File =============");
18+
19+
Assert.NotNull(TestConfiguration.GoogleAI.ApiKey);
20+
Assert.NotNull(TestConfiguration.GoogleAI.Gemini.ModelId);
21+
22+
Kernel kernel = Kernel.CreateBuilder()
23+
.AddGoogleAIGeminiChatCompletion(TestConfiguration.GoogleAI.Gemini.ModelId, TestConfiguration.GoogleAI.ApiKey)
24+
.Build();
25+
26+
var fileBytes = await EmbeddedResource.ReadAllAsync("employees.pdf");
27+
28+
var chatHistory = new ChatHistory("You are a friendly assistant.");
29+
chatHistory.AddUserMessage(
30+
[
31+
new TextContent("What's in this file?"),
32+
new BinaryContent(fileBytes, "application/pdf")
33+
]);
34+
35+
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
36+
37+
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);
38+
39+
Console.WriteLine(reply.Content);
40+
}
41+
42+
[Fact]
43+
public async Task VertexAIChatCompletionWithLocalFile()
44+
{
45+
Console.WriteLine("============= Vertex AI - Gemini Chat Completion With Local File =============");
46+
47+
Assert.NotNull(TestConfiguration.VertexAI.BearerKey);
48+
Assert.NotNull(TestConfiguration.VertexAI.Location);
49+
Assert.NotNull(TestConfiguration.VertexAI.ProjectId);
50+
Assert.NotNull(TestConfiguration.VertexAI.Gemini.ModelId);
51+
52+
Kernel kernel = Kernel.CreateBuilder()
53+
.AddVertexAIGeminiChatCompletion(
54+
modelId: TestConfiguration.VertexAI.Gemini.ModelId,
55+
bearerKey: TestConfiguration.VertexAI.BearerKey,
56+
location: TestConfiguration.VertexAI.Location,
57+
projectId: TestConfiguration.VertexAI.ProjectId)
58+
.Build();
59+
60+
var fileBytes = await EmbeddedResource.ReadAllAsync("employees.pdf");
61+
62+
var chatHistory = new ChatHistory("You are a friendly assistant.");
63+
chatHistory.AddUserMessage(
64+
[
65+
new TextContent("What's in this file?"),
66+
new BinaryContent(fileBytes, "application/pdf"),
67+
]);
68+
69+
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
70+
71+
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);
72+
73+
Console.WriteLine(reply.Content);
74+
}
75+
76+
[Fact]
77+
public async Task GoogleAIChatCompletionWithBase64DataUri()
78+
{
79+
Console.WriteLine("============= Google AI - Gemini Chat Completion With Base64 Data Uri =============");
80+
81+
Assert.NotNull(TestConfiguration.GoogleAI.ApiKey);
82+
Assert.NotNull(TestConfiguration.GoogleAI.Gemini.ModelId);
83+
84+
Kernel kernel = Kernel.CreateBuilder()
85+
.AddGoogleAIGeminiChatCompletion(TestConfiguration.GoogleAI.Gemini.ModelId, TestConfiguration.GoogleAI.ApiKey)
86+
.Build();
87+
88+
var fileBytes = await EmbeddedResource.ReadAllAsync("employees.pdf");
89+
var fileBase64 = Convert.ToBase64String(fileBytes.ToArray());
90+
var dataUri = $"data:application/pdf;base64,{fileBase64}";
91+
92+
var chatHistory = new ChatHistory("You are a friendly assistant.");
93+
chatHistory.AddUserMessage(
94+
[
95+
new TextContent("What's in this file?"),
96+
new BinaryContent(dataUri)
97+
// Google AI Gemini AI does not support arbitrary URIs but we can convert a Base64 URI into InlineData with the correct mimeType.
98+
]);
99+
100+
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
101+
102+
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);
103+
104+
Console.WriteLine(reply.Content);
105+
}
106+
107+
[Fact]
108+
public async Task VertexAIChatCompletionWithBase64DataUri()
109+
{
110+
Console.WriteLine("============= Vertex AI - Gemini Chat Completion With Base64 Data Uri =============");
111+
112+
Assert.NotNull(TestConfiguration.VertexAI.BearerKey);
113+
Assert.NotNull(TestConfiguration.VertexAI.Location);
114+
Assert.NotNull(TestConfiguration.VertexAI.ProjectId);
115+
Assert.NotNull(TestConfiguration.VertexAI.Gemini.ModelId);
116+
117+
Kernel kernel = Kernel.CreateBuilder()
118+
.AddVertexAIGeminiChatCompletion(
119+
modelId: TestConfiguration.VertexAI.Gemini.ModelId,
120+
bearerKey: TestConfiguration.VertexAI.BearerKey,
121+
location: TestConfiguration.VertexAI.Location,
122+
projectId: TestConfiguration.VertexAI.ProjectId)
123+
.Build();
124+
125+
var fileBytes = await EmbeddedResource.ReadAllAsync("employees.pdf");
126+
var fileBase64 = Convert.ToBase64String(fileBytes.ToArray());
127+
var dataUri = $"data:application/pdf;base64,{fileBase64}";
128+
129+
var chatHistory = new ChatHistory("You are a friendly assistant.");
130+
chatHistory.AddUserMessage(
131+
[
132+
new TextContent("What's in this file?"),
133+
new BinaryContent(dataUri)
134+
// Vertex AI API does not support URIs outside of inline Base64 or GCS buckets within the same project. The bucket that stores the file must be in the same Google Cloud project that's sending the request. You must always provide the mimeType via the metadata property.
135+
// var content = new BinaryContent(gs://generativeai-downloads/files/employees.pdf);
136+
// content.Metadata = new Dictionary<string, object?> { { "mimeType", "application/pdf" } };
137+
]);
138+
139+
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
140+
141+
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);
142+
143+
Console.WriteLine(reply.Content);
144+
}
145+
}

dotnet/samples/Concepts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom
6464
- [Google_GeminiChatCompletion](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletion.cs)
6565
- [Google_GeminiChatCompletionStreaming](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletionStreaming.cs)
6666
- [Google_GeminiChatCompletionWithThinkingBudget](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletionWithThinkingBudget.cs)
67+
- [Google_GeminiChatCompletionWithFile.cs](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletionWithFile.cs)
6768
- [Google_GeminiGetModelResult](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiGetModelResult.cs)
6869
- [Google_GeminiStructuredOutputs](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiStructuredOutputs.cs)
6970
- [Google_GeminiVision](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiVision.cs)

dotnet/samples/Demos/ProcessWithCloudEvents/ProcessWithCloudEvents.Client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"globals": "^15.15.0",
3333
"typescript": "~5.7.2",
3434
"typescript-eslint": "^8.24.1",
35-
"vite": "^6.2.7"
35+
"vite": "^6.4.1"
3636
}
3737
}

0 commit comments

Comments
 (0)