|
| 1 | +--- |
| 2 | +title: AI Integration |
| 3 | +page_title: AI Integration |
| 4 | +description: "Learn how to utilize the AI Prompt integration in the Telerik UI Editor component for {{ site.framework }}." |
| 5 | +slug: htmlhelpers_editor_ai_integration_aspnetcore |
| 6 | +position: 5 |
| 7 | +--- |
| 8 | + |
| 9 | +# AI Integration |
| 10 | + |
| 11 | +The Editor features AI integration through the [AIPrompt]({% slug htmlhelpers_overview_aiprompt %}) component, enabling a smarter and more efficient content creation experience. |
| 12 | + |
| 13 | +You can enable the functionality by using the `AI()` configuration and specifying the endpoint that will request an answer from the desired LLM service. For that, use the `Service()` method and pass the respective Action and Controller. |
| 14 | + |
| 15 | +The following example demonstrates the basic AI configuration for the Editor. |
| 16 | + |
| 17 | +```HtmlHelper |
| 18 | + @(Html.Kendo().Editor() |
| 19 | + .Name("editor") |
| 20 | + .AI(ai => ai |
| 21 | + .AIPrompt(true) |
| 22 | + .Service(s => s.Url("AIChatCompletion", "AIPrompt")) |
| 23 | + .InlineAIPrompt(true) |
| 24 | + .Commands(c => |
| 25 | + { |
| 26 | + c.Add().Id("rewrite").Text("Rewrite").Icon("arrow-rotate-cw").Prompt(@<text>function(context) {return `Rewrite the selected text while preserving its original meaning and intent. Selected Text: ${context}` }</text>); |
| 27 | + c.Add().Id("fix-spelling").Text("Fix spelling").Icon("spell-checker").Prompt(@<text>function(context) {return `Correct any spelling, grammar, or punctuation mistakes in the selected text while preserving its original meaning and intent. Selected Text: ${context}` }</text>); |
| 28 | + c.Add().Id("change-tone-neutral").Text("Change to neutral tone").Icon("tell-a-friend").Prompt(@<text>function(context) {return `Adjust the tone of the following text to be more neutral while preserving its original meaning and intent. Selected Text: ${context}` }</text>); |
| 29 | + c.Add().Id("change-tone-friendly").Text("Change to friendly tone").Icon("tell-a-friend").Prompt(@<text>function(context) {return `Adjust the tone of the following text to be more friendly while preserving its original meaning and intent. Selected Text: ${context}` }</text>); |
| 30 | +
|
| 31 | + c.Add().Id("adjust-length-shorten").Text("Shorten Length").Icon("col-resize").Prompt(@<text>function(context) {return `Shorten the following text while preserving its original meaning and intent. Selected Text: ${context}` }</text>); |
| 32 | + }) |
| 33 | + |
| 34 | + ) |
| 35 | + ) |
| 36 | +``` |
| 37 | +{% if site.core %} |
| 38 | +```TagHelper |
| 39 | + <kendo-editor name="editor"> |
| 40 | + <ai ai-prompt="true" inline-ai-prompt="true"> |
| 41 | + <service action="AIChatCompletion" controller="AIPrompt" /> |
| 42 | + <commands> |
| 43 | + <command id="rewrite" text="Rewrite" icon="arrow-rotate-cw" prompt="function(context) {return `Rewrite the selected text while preserving its original meaning and intent. Selected Text: ${context}` }"></command> |
| 44 | + <command id="fix-spelling" text="Fix spelling" icon="spell-checker" prompt="function(context) {return `Correct any spelling, grammar, or punctuation mistakes in the selected text while preserving its original meaning and intent. Selected Text: ${context}` }"></command> |
| 45 | + <command id="change-tone-neutral" text="Change to neutral tone" icon="tell-a-friend" prompt="function(context) {return `Adjust the tone of the following text to be more neutral while preserving its original meaning and intent. Selected Text: ${context}` }"></command> |
| 46 | + <command id="change-tone-friendly" text="Change to friendly tone" icon="tell-a-friend" prompt="function(context) {return `Adjust the tone of the following text to be more friendly while preserving its original meaning and intent. Selected Text: ${context}` }"></command> |
| 47 | + <command id="adjust-length-shorten" text="Shorten Length" icon="col-resize" prompt="function(context) {return `Shorten the following text while preserving its original meaning and intent. Selected Text: ${context}` }"></command> |
| 48 | + </commands> |
| 49 | + </pane> |
| 50 | + </ai> |
| 51 | + </kendo-editor> |
| 52 | +``` |
| 53 | +{% endif %} |
| 54 | +```C# |
| 55 | + [HttpPost] |
| 56 | + [RateLimit(10, 60)] |
| 57 | + public async Task<IActionResult> AIChatCompletion() |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + var aiServiceUrl = "Link to LLM service"; |
| 62 | + var forwardedRequest = new HttpRequestMessage(HttpMethod.Post, aiServiceUrl); |
| 63 | + |
| 64 | + // pass the key for using the LLM service |
| 65 | + forwardedRequest.Headers.Add("X-Api-Key", _configuration["AI:ApiKey"]); |
| 66 | + |
| 67 | + using (var requestStream = new StreamReader(Request.Body)) |
| 68 | + { |
| 69 | + var body = await requestStream.ReadToEndAsync(); |
| 70 | + forwardedRequest.Content = new StringContent(body, System.Text.Encoding.UTF8, Request.ContentType); |
| 71 | + } |
| 72 | + |
| 73 | + var response = await _httpClient.SendAsync(forwardedRequest); |
| 74 | + var responseContent = await response.Content.ReadAsStringAsync(); |
| 75 | + |
| 76 | + |
| 77 | + return new ContentResult |
| 78 | + { |
| 79 | + Content = responseContent, |
| 80 | + ContentType = "application/json", |
| 81 | + StatusCode = (int)response.StatusCode |
| 82 | + }; |
| 83 | + } |
| 84 | + catch (System.Exception e) |
| 85 | + { |
| 86 | + |
| 87 | + return new ContentResult |
| 88 | + { |
| 89 | + Content = e.Message, |
| 90 | + ContentType = "application/json", |
| 91 | + StatusCode = 200 |
| 92 | + }; |
| 93 | + } |
| 94 | + } |
| 95 | +``` |
| 96 | +For a live example, visit the [AI Integration Demo of the Editor](https://demos.telerik.com/{{ site.platform }}/editor/ai-integration). |
| 97 | +## See Also |
| 98 | + |
| 99 | +* [AI Integration in Editor for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/editor/ai-integration) |
| 100 | +* [Server-Side API of the Editor](/api/editor) |
| 101 | +{% if site.core %}* [Server-Side API of the Editor TagHelper](/api/taghelpers/editor){% endif %} |
0 commit comments