OpenAiModel

OpenAiModel class

Class representing OpenAi models integration within Aspose.Words.

public class OpenAiModel : AiModel

Constructors

NameDescription
OpenAiModel(string)Initializes a new instance of OpenAiModel class.
OpenAiModel(string, string)Initializes a new instance of OpenAiModel class.

Properties

NameDescription
Timeout { get; set; }Gets or sets the number of milliseconds to wait before the request to AI model times out. The default value is 100,000 milliseconds (100 seconds).
override Url { get; set; }Gets or sets a URL of the model. The default value is “https://api.openai.com/".

Methods

NameDescription
virtual CheckGrammar(DocumentCheckGrammarOptions)Checks grammar of the provided document. This operation leverages the connected AI model for checking grammar of document.
override Summarize(DocumentSummarizeOptions)Generates a summary of the specified document, with options to adjust the length of the summary. This operation leverages the connected AI model for content processing.
override Summarize(Document[], SummarizeOptions)Generates summaries for an array of documents, with options to control the summary length and other settings. This method utilizes the connected AI model for processing each document in the array.
override Translate(DocumentLanguage)Translates the provided document into the specified target language. This operation leverages the connected AI model for content translating.
WithApiKey(string)Sets a specified API key to the model.
WithOrganization(string)Sets a specified Organization to the model.
WithProject(string)Sets a specified Project to the model.

Remarks

Please refer to https://platform.openai.com/docs/models for OpenAi models details.

Examples

Shows how to use self-hosted AI model based on OpenAiModel.

public void SelfHostedModel()
{
    Document doc = new Document(MyDir + "Big document.docx");

    string apiKey = Environment.GetEnvironmentVariable("API_KEY");
    // Use OpenAI generative language models.
    AiModel model = new CustomAiModel("my-model-24b", "https://my.a.com/").WithApiKey(apiKey);

    Document translatedDoc = model.Translate(doc, Language.Russian);
    translatedDoc.Save(ArtifactsDir + "AI.SelfHostedModel.docx");
}

/// <summary>
/// Custom self-hosted AI model.
/// </summary>
internal class CustomAiModel : OpenAiModel
{
    internal CustomAiModel(string name, string url) : base(name)
    {
        mUrl = url;
    }

    public override string Url
    {
        get { return mUrl; }
    }

    private readonly string mUrl;
}

Shows how to summarize text using OpenAI and Google models.

Document firstDoc = new Document(MyDir + "Big document.docx");
Document secondDoc = new Document(MyDir + "Document.docx");

string apiKey = Environment.GetEnvironmentVariable("API_KEY");
// Use OpenAI or Google generative language models.
AiModel model = ((OpenAiModel)AiModel.Create(AiModelType.Gpt4OMini).WithApiKey(apiKey)).WithOrganization("Organization").WithProject("Project");

SummarizeOptions options = new SummarizeOptions();

options.SummaryLength = SummaryLength.Short;
Document oneDocumentSummary = model.Summarize(firstDoc, options);
oneDocumentSummary.Save(ArtifactsDir + "AI.AiSummarize.One.docx");

options.SummaryLength = SummaryLength.Long;
Document multiDocumentSummary = model.Summarize(new Document[] { firstDoc, secondDoc }, options);
multiDocumentSummary.Save(ArtifactsDir + "AI.AiSummarize.Multi.docx");

See Also