Skip to main content
All docs
V24.2

TextResponse Class

A response that contains AI-generated or manipulated text (for example, text generation, translation, or content transformation).

Namespace: DevExpress.AIIntegration.Extensions

Assembly: DevExpress.AIIntegration.v24.2.dll

NuGet Package: DevExpress.AIIntegration

Declaration

public class TextResponse :
    BaseResponse,
    IEquatable<TextResponse>

Remarks

The following example registers an Azure OpenAI client and uses the AI-powered extension to change the writing style of originalText:

using Azure;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Extensions;

SetEnvironmentVariables();

// Register an Azure OpenAI client
AIExtensionsContainerDefault defaultAIExtensionsContainer = RegisterAzureOpenAIClient(
    Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"),
    Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY")
);

string originalText = "DevExpress released Universal v24.2 - the award-winning software development platform for .NET and Visual Studio developers.";
var response = await defaultAIExtensionsContainer.ChangeStyleAsync(
    new ChangeStyleRequest(originalText, WritingStyle.Academic)
);

Console.WriteLine(response);

/* Output:
 * DevExpress has announced the release of Universal v24.2, an acclaimed software development platform
 * designed specifically for .NET and Visual Studio developers.
 */

AIExtensionsContainerDefault RegisterAzureOpenAIClient(string azureOpenAIEndpoint, string azureOpenAIKey) {
    IChatClient client = new AzureOpenAIClient(
        new Uri(azureOpenAIEndpoint),
        new AzureKeyCredential(azureOpenAIKey))
            .AsChatClient("gpt-4o-mini");
    return AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(client);
}

void SetEnvironmentVariables() {
    Environment.SetEnvironmentVariable("AZURE_OPENAI_ENDPOINT", {SPECIFY_YOUR_AZURE_ENDPOINT});
    Environment.SetEnvironmentVariable("AZURE_OPENAI_APIKEY", {SPECIFY_YOU_AZURE_KEY});
}

Manage Long Input Text and AI Responses

AI-poweredextensions automatically split large content into manageable chunks (including paragraphs, sentences, words, punctuation marks, and other text elements) to ensure accurate processing. Use the ContinueAsync() method to start processing the next chunk asynchronously.

The following example registers an Azure OpenAI client and implements the Translate method. This method can manage large text blocks.

using Azure;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Extensions;

SetEnvironmentVariables();

// Register an Azure OpenAI client
AIExtensionsContainerDefault defaultAIExtensionsContainer = RegisterAzureOpenAIClient(
    Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"),
    Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY")
);

string sourceText = "A long text...";
async Task<string> Translate(string sourceText, string language) {
    TextResponse response = await defaultAIExtensionsContainer.TranslateAsync(
        new TranslateRequest(sourceText, language)
    );
    if (response.IsCompleted)
        return response.Response;
    if (!response.IsRestrictedOrFailed) {
        string translatedText = response.Response;
        while (response.IsContinuationRequired) {
            await response.ContinueAsync();
            translatedText += response.Response;
        }
        return translatedText;
    }
    switch (response.Status) {
        case ResponseStatus.MaxTokenLimitExceeded:
        case ResponseStatus.InputSizeLimitExceeded:
            return "The text you're trying to send within a request is too long and exceeds the allowed limit.";
        case ResponseStatus.ContentFiltered:
            return "Potentially harmful content was detected in your request.";
        case ResponseStatus.Error:
            return "An error occurred while processing the request.";
    }
    throw new NotSupportedException();
}

Console.WriteLine(await Translate(sourceText, "de"));

AIExtensionsContainerDefault RegisterAzureOpenAIClient(string azureOpenAIEndpoint, string azureOpenAIKey) {
    IChatClient client = new AzureOpenAIClient(
        new Uri(azureOpenAIEndpoint),
        new AzureKeyCredential(azureOpenAIKey))
            .AsChatClient("gpt-4o-mini");
    return AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(client);
}

void SetEnvironmentVariables() {
    Environment.SetEnvironmentVariable("AZURE_OPENAI_ENDPOINT", {SPECIFY_YOUR_AZURE_ENDPOINT});
    Environment.SetEnvironmentVariable("AZURE_OPENAI_APIKEY", {SPECIFY_YOU_AZURE_KEY});
}

Inheritance

Object
DevExpress.AIIntegration.Extensions.BaseResponse
TextResponse
See Also