Adding custom AI options
Developers have the capability to create customized AI options and display them on the UI.
Create custom tone
- Create a class that implements
IAiToneinterface.
public class ProfessionalTone : IAiTone
{
public string Name => "Professional";
public int Order => 10;
}
- Register the new tone in Startup.cs
services
.AddAiAssistant()
.AddTone<ProfessionalTone>();
Create custom format
- Create a class that implements
IAiFormatinterface.
public class SalePitchFormat : IAiFormat
{
public string Name => "Sale Pitch";
public int Order => 10;
}
- Register the new format in Startup.cs
services
.AddAiAssistant()
.AddFormat<SalePitchFormat>();
Create custom length
- Create a class that implements
IAiLengthinterface.
public class MediumLength : IAiLength
{
public string Name => "Medium";
public int Order => 5;
}
- Register the new length in Startup.cs
services
.AddAiAssistant()
.AddLength<MediumLength>();
Creating custom prompts
To create a custom AI prompt, follow these steps:
1. Implement the IAiPrompt interface
Create a class that implements IAiPrompt. This class defines the AI service type, prompt behavior, and UI options.
Example: IntroductionPrompt implementation
using EPiServer.ServiceLocation;
...
[ServiceConfiguration(typeof(IAiPrompt), Lifecycle = ServiceInstanceScope.Scoped)]
public class IntroductionPrompt : IAiPrompt
{
public AiServiceType AiServiceType => AiServiceType.TextGeneration;
public string InstructionType => AiInstructionTypes.SuggestionFromSources;
public string Instruction => "[Unique instruction name]";
/// <summary>
/// Show as Summarize on the UI.
/// </summary>
public string InstructionDisplayName => AiInstructions.Summarize;
public InstructionAvailability InstructionAvailability => InstructionAvailability.ShowOnFocus;
public bool ShowTextArea => false;
public bool ShowTone => true;
public bool ShowFormat => false;
public bool ShowLength => false;
public virtual string BuildPromptText(PromptModel model)
{
var sb = new StringBuilder("Write a concise ");
if (!string.IsNullOrEmpty(model.Tone))
{
sb.Append($"and {model.Tone} ");
}
sb.Append($"introduction in {model.Language} ");
if (model.MaxLength is not null)
{
sb.Append($"under {model.MaxLength} characters ");
}
sb.Append("based on the following text: ");
return sb.ToString();
}
}
-
Use
[ServiceConfiguration]attribute to register the prompt generator in the service container with a lifecycle ofTransient,ScopedorSingleton. -
AiServiceType (enum): Defines the types of AI services available for processing user requests.
TextGeneration: Used for generating, summarizing, or transforming text-based content.Vision: Used for analyzing and interpreting images.
-
InstructionType (string): Defines different AI instruction types for generating content.
AiInstructionTypes.Suggestion: Generates text with configurable tone, format, and length.AiInstructionTypes.SuggestionFromSources: Generates text based on specified source properties from the model.AiInstructionTypes.SuggestionFromImages: Generates text based on analyzed content from images.AiInstructionTypes.FixSpellingAndGrammar: Corrects spelling and grammar errors in the provided text.AiInstructionTypes.Translation: Translates text into a selected language.
-
Instruction (string): The unique name of the instruction, ensuring distinct identification.
-
InstructionAvailability (enum): Defines UI behavior for displaying instructions.
InstructionAvailability.Show: The instruction is always visible in the UI.InstructionAvailability.ShowOnFocus: The instruction is visible only when the user focuses on the associated field.InstructionAvailability.Hidden: The instruction is hidden and not displayed in the UI.
-
ShowTextArea (bool): Determines whether the input field is displayed.
-
ShowTone (bool): Determines whether the tone selection is displayed.
-
ShowFormat (bool): Determines whether the format selection is displayed.
-
ShowLength (bool): Determines whether the length selection is displayed.
Implements the BuildPromptText method to construct AI prompt dynamically. The PromptModel contains the data submitted by the user, triggered when the user clicks the Generate Draft button.
-
Text (string): The input text used as the basis for AI-generated content.
-
Language (string): The language in which the content should be generated.
-
Tone (string?): The tone of the generated content (e.g., Professional, Informational).
-
Format (string?): The format of the generated content (e.g., Paragraph, Summary, Report).
-
Length (string?): The length of the generated content (e.g., < 150 chars, 500-1000 chars, or < 4000 chars).
-
MaxLength (int?): The maximum number of characters allowed for the generated content.
-
IsCustomPrompt (bool): Indicates whether the prompt is custom, identified by a
#at the beginning of the prompt text.
2. Create a Custom Attribute that inherits AiAssistantAttribute
Extend AiAssistantAttribute to define the source properties (AI input) and target properties (UI activation upon user focus).
Example: AiIntroductionGeneratorAttribute implementation
public class AiIntroductionGeneratorAttribute : AiAssistantAttribute
{
public AiIntroductionGeneratorAttribute(
int maxLength,
PropertyCombinationType sourcePropertyCombinationType,
params string[] sourcePropertyNames)
{
MaxLength = maxLength;
SourcePropertyCombinationType = sourcePropertyCombinationType;
SourcePropertyNames = sourcePropertyNames;
Prompt = typeof(IntroductionPrompt);
}
public AiIntroductionGeneratorAttribute(
int maxLength,
params string[] sourcePropertyNames) : this(maxLength, PropertyCombinationType.And, sourcePropertyNames)
{
}
public AiIntroductionGeneratorAttribute(
params string[] sourcePropertyNames) : this(500, PropertyCombinationType.And, sourcePropertyNames)
{
}
}
- MaxLength (int): Allows setting a maximum character length for the generated text.
- SourcePropertyCombinationType (PropertyCombinationType): Defines how multiple source properties are combined when generating content. Supports
PropertyCombinationTypevalues such as AND and OR logic. For more details, refer to the Configure the property for generating the meta title and meta description section. - SourcePropertyNames (string[]): Specifies the properties used as input for AI-generated introductions.
- Prompt (Type): Associates with a specific AI prompt for text generation.