OpenAiModel
Contents
[
Hide
]OpenAiModel class
An abstract class representing the integration with OpenAI’s large language models within the Aspose.Words.
public abstract class OpenAiModel : AiModel, IAiModelText
Methods
Name | Description |
---|---|
virtual CheckGrammar(Document, CheckGrammarOptions) | Checks grammar of the provided document. This operation leverages the connected AI model for checking grammar of document. |
override Summarize(Document, SummarizeOptions) | 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(Document, Language) | 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. |
Examples
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");
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().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
{
/// <summary>
/// Gets custom URL of the model.
/// </summary>
protected override string Url
{
get { return "https://localhost/"; }
}
/// <summary>
/// Gets model name.
/// </summary>
protected override string Name
{
get { return "my-model-24b"; }
}
}
See Also
- class AiModel
- namespace Aspose.Words.AI
- assembly Aspose.Words