|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Gemini Integration with Blazor AI AssistView Component | Syncfusion |
| 4 | +description: Checkout and learn about gemini integration with Blazor AI AssistView component in Blazor WebAssembly Application. |
| 5 | +platform: Blazor |
| 6 | +control: AI AssistView |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Integration of Gemini AI With Blazor AI AssistView component |
| 11 | + |
| 12 | +The Syncfusion AI AssistView supports integration with [Gemini](Gemini API quickstart | Google AI for Developers), enabling advanced conversational AI features in your applications. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +* Google account to generate API key on accessing `Gemini AI` |
| 17 | +* Syncfusion AI AssistView for Blazor `Syncfusion.Blazor.InteractiveChat` installed in your project. |
| 18 | + |
| 19 | +## Getting Started with the AI AssistView Component |
| 20 | + |
| 21 | +Before integrating Gemini AI, ensure that the Syncfusion AI AssistView is correctly rendered in your application: |
| 22 | + |
| 23 | +[ Blazor Getting Started Guide](../getting-started) |
| 24 | + |
| 25 | +## Install Dependencies |
| 26 | + |
| 27 | +Install the Syncfusion Blazor package in the application. |
| 28 | + |
| 29 | +```bash |
| 30 | + |
| 31 | +Install-Package Syncfusion.Blazor.InteractiveChat |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +Install the Gemini AI package in the application. |
| 36 | + |
| 37 | +```bash |
| 38 | + |
| 39 | +Install-Package Mscc.GenerativeAI |
| 40 | + |
| 41 | +``` |
| 42 | + |
| 43 | +## Generate API Key |
| 44 | + |
| 45 | +1. Go to [Google AI Studio](https://aistudio.google.com/app/apikey) and sign in with your Google account. If you don’t have one, create a new account. |
| 46 | + |
| 47 | +2. Once logged in, click on `Get API Key` from the left-hand menu or the top-right corner of the dashboard. |
| 48 | + |
| 49 | +3. Click the `Create API Key` button. You’ll be prompted to either select an existing Google Cloud project or create a new one. Choose the appropriate option and proceed. |
| 50 | + |
| 51 | +4. After selecting or creating a project, your API key will be generated and displayed. Copy the key and store it securely, as it will only be shown once. |
| 52 | + |
| 53 | +> `Security Note`: Never commit the API key to version control. Use environment variables or a secret manager for production. |
| 54 | +
|
| 55 | +## Integration Gemini AI with AI AssistView |
| 56 | + |
| 57 | +> Add your generated `API Key` at the line |
| 58 | +
|
| 59 | +```bash |
| 60 | + |
| 61 | +const string GeminiApiKey = 'Place your API key here'; |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +{% tabs %} |
| 66 | +{% highlight razor %} |
| 67 | + |
| 68 | +<div class="aiassist-container" style="height: 350px; width: 650px;"> |
| 69 | + <SfAIAssistView @ref="sfAIAssistView" ID="aiAssistView" PromptSuggestions="@promptSuggestions" PromptRequested="@OnPromptRequest"> |
| 70 | + <AssistViews> |
| 71 | + <AssistView> |
| 72 | + <BannerTemplate> |
| 73 | + <div class="banner-content"> |
| 74 | + <div class="e-icons e-assistview-icon"></div> |
| 75 | + <h3>AI Assistance</h3> |
| 76 | + <i>To get started, provide input or choose a suggestion.</i> |
| 77 | + </div> |
| 78 | + </BannerTemplate> |
| 79 | + </AssistView> |
| 80 | + </AssistViews> |
| 81 | + <AssistViewToolbar ItemClicked="ToolbarItemClicked"> |
| 82 | + <AssistViewToolbarItem Type="ItemType.Spacer"></AssistViewToolbarItem> |
| 83 | + <AssistViewToolbarItem IconCss="e-icons e-refresh"></AssistViewToolbarItem> |
| 84 | + </AssistViewToolbar> |
| 85 | + </SfAIAssistView> |
| 86 | +</div> |
| 87 | + |
| 88 | +@code { |
| 89 | + private SfAIAssistView sfAIAssistView = new SfAIAssistView(); |
| 90 | + private List<string> promptSuggestions = new List<string> |
| 91 | + { |
| 92 | + "What are the best tools for organizing my tasks?", |
| 93 | + "How can I maintain work-life balance effectively?" |
| 94 | + }; |
| 95 | + private readonly string geminiApiKey = ""; |
| 96 | + private async Task OnPromptRequest(AssistViewPromptRequestedEventArgs args) |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + var gemini = new GoogleAI(apiKey: geminiApiKey); |
| 101 | + var model = gemini.GenerativeModel(model: "gemini-1.5-flash"); |
| 102 | + var response = await model.GenerateContent(args.Prompt); |
| 103 | + var responseText = response.Text; |
| 104 | + var pipeline = new MarkdownPipelineBuilder() |
| 105 | + .UseAdvancedExtensions() |
| 106 | + .UsePipeTables() |
| 107 | + .UseTaskLists() |
| 108 | + .Build(); |
| 109 | + // Add the response to the AIAssistView |
| 110 | + await Task.Delay(1000); // Simulate delay as in original code |
| 111 | + args.Response = Markdown.ToHtml(responseText, pipeline); |
| 112 | + } |
| 113 | + catch (Exception ex) |
| 114 | + { |
| 115 | + Console.WriteLine($"Error fetching Gemini response: {ex.Message}"); |
| 116 | + await Task.Delay(1000); |
| 117 | + args.Response = "⚠️ Something went wrong while connecting to the AI service. Please check your API key or try again later."; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void ToolbarItemClicked(AssistViewToolbarItemClickedEventArgs args) |
| 122 | + { |
| 123 | + sfAIAssistView.Prompts.Clear(); |
| 124 | + StateHasChanged(); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +{% endhighlight %} |
| 129 | +{% endtabs %} |
| 130 | + |
| 131 | + |
0 commit comments