Simple Steps to Integrate OpenAI (ChatGPT) with the Syncfusion’s Blazor Rich Text Editor
Tags: blazor, webdev, openai, chatgpt
Published: 2023-03-22 01:54:00

---
title: Simple Steps to Integrate OpenAI (ChatGPT) with the Syncfusion’s Blazor Rich Text Editor
published: true
date: 2023-03-21 11:05:24 UTC
tags: blazor, webdev, openai, chatgpt
canonical_url: https://www.syncfusion.com/blogs/post/integrate-chatgpt-blazor-rich-text-editor.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtpz6u48mikkewubv98i.png
---
[ChatGPT](https://en.wikipedia.org/wiki/ChatGPT "Wikipedia link for ChatGPT") is an AI language model created by OpenAI. It can deliver human-like responses to a wide variety of prompts and questions. It can be used in chatbots, language translation, content creation, and more.

The Syncfusion [Blazor Rich Text Editor](https://www.syncfusion.com/blazor-components/blazor-wysiwyg-rich-text-editor "Syncfusion Blazor Rich Text Editor Component") is a feature-rich WYSIWYG HTML editor and WYSIWYG Markdown editor. It is used to create blogs, forum posts, note sections, support tickets (incidents), messaging applications, and more. It allows users to insert images, links, tables, and lists with modular architecture.

In this article, we’ll see how to integrate OpenAI with the Blazor Rich Text Editor component to enhance content generation and review.

## Prerequisites

- [Visual Studio 2022 with ASP.NET and web development environment](https://visualstudio.microsoft.com/vs/ "Visual Studio 2022")
- [.NET 6.0 SDK or above](https://dotnet.microsoft.com/en-us/download/dotnet/6.0 ".NET 6.0 SDK")
- OpenAI API key: You can get one by [signing up](https://platform.openai.com/signup "ChartGPT Signing Up Portal") for an account.

## Integrating ChatGPT with Blazor Rich Text Editor

Let’s see how to integrate ChatGPT with the Syncfusion Blazor Rich Text Editor component for better productivity!

### Step 1: Creating the Blazor Rich Text Editor with toolbar and AI options.

1.Follow the [getting started](https://blazor.syncfusion.com/documentation/rich-text-editor/getting-started "Getting Started with Syncfusion Blazor Rich Text Editor") guide to create a Blazor server app and add the Syncfusion Blazor Rich Text Editor component.
2.After the sample creation, add the toolbar items to the Blazor Rich Text Editor.
  a.Add a custom toolbar item named **AI** and set a dropdown button (using the Syncfusion [Blazor Dropdown Menu](https://blazor.syncfusion.com/documentation/drop-down-menu/getting-started "Getting Started with Syncfusion Blazor Dropdown Menu Component")) as the custom toolbar item content.
  b.Customize the dropdown button pop-up to show AI menu items (using [Blazor Context Menu](https://blazor.syncfusion.com/documentation/context-menu/getting-started "Getting started with Syncfusion Blazor Context Menu component")), explained in the next step.

Refer to the following code example.

**Pages/Index.razor**

```csharp
@page "/"
@using Syncfusion.Blazor.RichTextEditor
@using Syncfusion.Blazor.SplitButtons
@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.Spinner
@using BlazorRTEWithOpenAI.Data
 
<div class="control-section">
    <SfRichTextEditor @bind-Value="@Value">
        <RichTextEditorInlineMode Enable="false" ShowOnSelection="true" />
        <RichTextEditorToolbarSettings Items="@Tools">
            <RichTextEditorCustomToolbarItems>
                <RichTextEditorCustomToolbarItem Name="AI">
                    <Template>
                        <SfDropDownButton @ref="@dropDownButton" Content="AI">
                            <PopupContent>
                                <SfContextMenu @ref="@contextMenu" Items="AIMenuItems" TValue="ContextMenuItem" ShowItemOnClick="true">
                                    <MenuFieldSettings Text="Content"/>
                                    <MenuEvents TValue="ContextMenuItem" Created="OnMenuCreated" ItemSelected="ItemSelected" />
                                </SfContextMenu>
                            </PopupContent>
                        </SfDropDownButton>
                    </Template>
                </RichTextEditorCustomToolbarItem>
            </RichTextEditorCustomToolbarItems>
        </RichTextEditorToolbarSettings>
    </SfRichTextEditor>
    <SfSpinner @bind-Visible="@showSpinner"/>
</div>

```

**Pages/Index.razor.cs**

```csharp
using BlazorRTEWithOpenAI.Data;
using System.Text;
using Microsoft.AspNetCore.Components;
using Syncfusion.Blazor.Navigations;
using Syncfusion.Blazor.RichTextEditor;
using Syncfusion.Blazor.SplitButtons;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.RegularExpressions;
 
namespace BlazorRTEWithOpenAI.Pages
{
    public partial class Index
    {
        [Inject]
        private HttpClient httpClient { get; set; }
 
        private SfDropDownButton? dropDownButton;
        private SfContextMenu<ContextMenuItem>? contextMenu;
        private bool showSpinner = false;
        private string Value { get; set; } = "<p>The Olympics is a global multisport event that takes place every four years. Thousands of athletes from all over the world come together to compete in various sports and represent their countries. The Olympics is a celebration of athleticism, sportsmanship, and international unity, and it continues to inspire people all over the world.</p>";
 
        private List<ToolbarItemModel> Tools = new List<ToolbarItemModel>()
        {
            new ToolbarItemModel() { Command = ToolbarCommand.Bold },
            new ToolbarItemModel() { Command = ToolbarCommand.Italic },
            new ToolbarItemModel() { Command = ToolbarCommand.Underline },
            new ToolbarItemModel() { Command = ToolbarCommand.Separator },
            new ToolbarItemModel() { Command = ToolbarCommand.Formats },
            new ToolbarItemModel() { Command = ToolbarCommand.Alignments },
            new ToolbarItemModel() { Command = ToolbarCommand.Separator },
            new ToolbarItemModel() { Name = "AI", TooltipText = "Edit with AI" },
            new ToolbarItemModel() { Command = ToolbarCommand.Separator },
            new ToolbarItemModel() { Command = ToolbarCommand.SourceCode },
            new ToolbarItemModel() { Command = ToolbarCommand.Separator },
            new ToolbarItemModel() { Command = ToolbarCommand.Undo },
            new ToolbarItemModel() { Command = ToolbarCommand.Redo }
        };
 
        public void OnMenuCreated()
        {
            contextMenu?.Open();
        }
    }
}

```
c.Now, create a model class for menu items and define the items with options to edit the Rich Text Editor’s content using OpenAI. This item collection will be bound to the context menu of the dropdown button pop-up created in the previous step.  
  
In this demo, I have added rephrasing (standard, formal, expand, shorten), changed tone (professional, casual, straightforward, confident, friendly), fix spelling and grammar, and convert to article options to the context menu of the toolbar item for integrating ChatGPT into the Rich Text Editor component.

**Data/ ContextMenuItem.cs**

```csharp
namespace BlazorRTEWithOpenAI.Data
{
    public class ContextMenuItem
    {
        public List<ContextMenuItem> Items { get; set; }
        public string Content { get; set; }
        public string Id { get; set; }
        public string Prompt { get; set; }
        public bool Separator { get; set; }
    }
}
```

**Pages/Index.razor.cs**

```csharp
namespace BlazorRTEWithOpenAI.Pages
{
    public partial class Index
    {
        . . .
        . . .
 
        private List<ContextMenuItem> AIMenuItems = new List<ContextMenuItem>{
            new ContextMenuItem { Id ="RephraseContent", Content = "Rephrase" , Items = new List<ContextMenuItem> {
                new ContextMenuItem { Content="Standard", Prompt = Constants.STANDARD },
                new ContextMenuItem { Content="Formal", Prompt = Constants.FORMAL },
                new ContextMenuItem { Content="Expand", Prompt = Constants.EXPAND },
                new ContextMenuItem { Content="Shorten", Prompt = Constants.SHORTEN}
            }},
            new ContextMenuItem { Id ="ChangeContentTone", Content = "Change tone", Items = new List<ContextMenuItem> {
                new ContextMenuItem { Content="Professional", Prompt = Constants.PROFESSIONAL },
                new ContextMenuItem { Content="Casual", Prompt = Constants.CASUAL },
                new ContextMenuItem { Content="Straightforward", Prompt = Constants.STRAIGHTFORWARD },
                new ContextMenuItem { Content="Confident", Prompt = Constants.CONFIDENT},
                new ContextMenuItem { Content="Friendly", Prompt = Constants.FRIENDLY },
            }},
            new ContextMenuItem { Separator = true },
            new ContextMenuItem { Id ="CheckGrammar", Content = "Fix spelling & grammar", Prompt = Constants.GRAMMARCHECK},
            new ContextMenuItem { Id ="Article", Content = "Convert to article", Prompt = Constants.CONVERTTOARTICLE }
        };
    }
```

### Step 2: Defining model and prompts for OpenAI.

a.Create a class named **Constants.cs** inside the **Data** folder of your Blazor server application.
b.Inside the **Constants.cs** file, add the following constants:
  - OpenAI model: The name of the [OpenAI API model](https://platform.openai.com/docs/models/overview "ChartGPT OpenAI API Model") you want to use. Here, we are using text-davinci-003 as the OpenAI API model name.
  - OpenAI key: You can get one by [signing up](https://platform.openai.com/signup "ChartGPT Signing Up Portal") for an account in OpenAI.
  - API endpoint: The URL for the [OpenAI API endpoint](https://platform.openai.com/docs/api-reference/completions/create "ChartGPT OpenAI API EndPoint").
  - The text prompts to instruct the OpenAI to generate the completion result.

Refer to the following code example.

**Data/Constants.cs**

```csharp
namespace BlazorRTEWithOpenAI.Data
{
    internal static class Constants
    {
        // The OpenAI API model name.
        internal const string OPENAI_MODEL = "text-davinci-003";
        // The OpenAI API key.
        internal const string OPENAI_KEY = "";
        // The OpenAI API endpoint.
        internal const string API_ENDPOINT = "https://api.openai.com/v1/completions";
        internal const string STANDARD = "Rewrite the below text in a reliable manner to maintain meaning and provide the result as html content.\n\n";
        internal const string FORMAL = "Present the below text in a more sophisticated and professional way content and provide the result as html content.\n\n";
        internal const string EXPAND = "Adds more detail and depth to increase sentence length of the below html content and provide the result as html content.\n\n";
        internal const string SHORTEN = "Strips away extra words in the below html content and provide a clear message as html content.\n\n";
        internal const string PROFESSIONAL = "Change the below html content tone to professional content and provide the result as html content.\n\n";
        internal const string CASUAL = "Change the below html content tone to casual content and provide the result as html content.\n\n";
        internal const string STRAIGHTFORWARD = "Change the below html content tone to straightforward content and provide the result as html content.\n\n";
        internal const string CONFIDENT = "Change the below html content tone to confident content and provide the result as html content.\n\n";
        internal const string FRIENDLY = "Change the below html content tone to friendly content and provide the result as html content.\n\n";
        internal const string GRAMMARCHECK = "Check grammar and spelling in the below html content and provide the result as html content.\n\n";
        internal const string CONVERTTOARTICLE = "Convert to article using the below html content and provide the result as html content.\n\n";
    }
}

```

### **Step 3: Create an API endpoint and get a response from OpenAI.**

Next, create an HTTP client to make a [Completion API](https://platform.openai.com/docs/api-reference/completions/create "ChartGPT Completion API") call to OpenAI with the user-selected toolbar prompt.

In OpenAI’s Completion API, the prompt is the input text we provide to the API, and the completion is the response generated based on the input prompt.

The Completion API is designed to generate natural-language text based on the input prompt using advanced machine-learning techniques. The provided prompt input text can be a partial sentence, a question, a keyword, or any other type of text you want the API to complete.

The Completion API returns a response with a suitable answer for the prompt. Then, we process the Completion API’s response and replace the given prompt with the completion result (answer for the given prompt) in the Rich Text Editor.

Refer to the following code example.

**Pages/Index.razor.cs**

```csharp
namespace BlazorRTEWithOpenAI.Pages
{
    public partial class Index
    {
       
        . . .
        . . .
 
        private async Task ItemSelected(MenuEventArgs<ContextMenuItem> args)
        {
            if (string.IsNullOrEmpty(args.Item.Prompt))
                return;
 
            var prompt = args.Item.Prompt + Value;
           
            dropDownButton?.Toggle();
            this.showSpinner = true;
            Value = await createCompletion(prompt);
            this.showSpinner = false;
        }
 
        // Method to get the completion result from OpenAI.
        private async Task<string> createCompletion(string prompt)
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Constants.OPENAI_KEY);
            var openAIPrompt = new
            {
                model = Constants.OPENAI_MODEL,
                prompt,
                temperature = 0.5,
                max_tokens = 1500,
                top_p = 1, 
                frequency_penalty = 0,
                presence_penalty = 0
            };
 
            var content = new StringContent(JsonSerializer.Serialize(openAIPrompt), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(Constants.API_ENDPOINT, content);
            var jsonContent = await response.Content.ReadAsStringAsync();
            var choices = JsonDocument.Parse(jsonContent).RootElement.GetProperty("choices").GetRawText();
            var result = JsonDocument.Parse(Regex.Replace(choices, @"[\[\]]", string.Empty)).RootElement;
            #pragma warning disable CS8603 // Possible null reference return.
            return result.GetProperty("text").GetString();
            #pragma warning restore CS8603 // Possible null reference return.
        }
    }
}
```

### **Step 4: Run the application.** 

You can see the demo of the runnable app in the following GIF image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/03/Integrate-OpenAI-ChatGPT-with-the-Syncfusions-Blazor-Rich-Text-Editor.gif" alt="Integrating OpenAI (ChatGPT) with the Syncfusion’s Blazor Rich Text Editor" style="width:100%">

  <figcaption>Integrating OpenAI (ChatGPT) with the Syncfusion Blazor Rich Text Editor</figcaption>

</figure>

## GitHub reference

You can download the complete source code for [integrating OpenAI (ChatGPT) with Syncfusion’s Blazor Rich Text Editor on GitHub](https://github.com/SyncfusionExamples/Integrating-OpenAI-with-Blazor-RichTextEditor "Integrating OpenAI (ChatGPT) with Syncfusion’s Blazor Rich Text Editor demo on GitHub").

## Conclusion

Thanks for reading! In this blog, we have learned how to integrate [OpenAI (ChatGPT)](https://openai.com/blog/chatgpt "OpenAI (ChatGPT)") with the Syncfusion [Blazor Rich Text Editor](https://www.syncfusion.com/blazor-components/blazor-wysiwyg-rich-text-editor "Syncfusion Blazor Rich Text Editor Compnent") to enhance content generation and review. In this way, you can customize your app using OpenAI with minimal lines of code. If you have any questions, please let us know in the comments section below.

Syncfusion provides over 65 high-performance, lightweight, modular, and responsive [Blazor UI controls](https://www.syncfusion.com/blazor-components "Syncfusion Blazor UI components") such as [DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid "Blazor DataGrid"), [Charts](https://www.syncfusion.com/blazor-components/blazor-charts "Blazor Charts"), and [Scheduler](https://www.syncfusion.com/blazor-components/blazor-scheduler "Blazor Scheduler") to ease the work of developers. Use them to build elite apps!

You can also contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/blazor-components "Syncfusion Feedback Portal"). We are always happy to assist you! Have a good day!

## Related blogs

- [Load on Demand: A Solution to Handling Large Data Sets Efficiently in Blazor Gantt Chart](https://www.syncfusion.com/blogs/post/load-on-demand-blazor-gantt-chart.aspx "Blog: Load on Demand: A Solution to Handling Large Data Sets Efficiently in Blazor Gantt Chart")
- [Interview with Blazor WebAssembly Succinctly Author Michael Washington](https://www.syncfusion.com/blogs/post/interview-with-blazor-webassembly-succinctly-author-michael-washington.aspx "Blog: Interview with Blazor WebAssembly Succinctly Author Michael Washington")
- [Instantly Update a Real-Time Chart with SignalR in Blazor Server-Side App](https://www.syncfusion.com/blogs/post/instantly-update-a-real-time-chart-with-signalr-in-blazor-server-side-app.aspx "Blog: Instantly Update a Real-Time Chart with SignalR in Blazor Server-Side App")
- [Easily Integrate Blazor Components into Any SPA Framework as Custom Elements](https://www.syncfusion.com/blogs/post/integrate-blazor-components-into-any-spa-framework.aspx "Blog: Easily Integrate Blazor Components into Any SPA Framework as Custom Elements")