|
| 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 | +} |
0 commit comments