OpenAiModel

Inheritance: java.lang.Object, com.aspose.words.AiModel

All Implemented Interfaces: com.aspose.words.IAiModelText

public abstract class OpenAiModel extends AiModel implements IAiModelText

An abstract class representing the integration with OpenAI’s large language models within the Aspose.Words.

Examples:

Shows how to summarize text using OpenAI and Google models.


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

 String apiKey = System.getenv("API_KEY");
 // Use OpenAI or Google generative language models.
 IAiModelText model = (IAiModelText) AiModel.create(AiModelType.GPT_4_O_MINI).withApiKey(apiKey);

 SummarizeOptions options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.SHORT);
 Document oneDocumentSummary = model.summarize(firstDoc, options);
 oneDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.One.docx");

 options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.LONG);
 Document multiDocumentSummary = model.summarize(new Document[] { firstDoc, secondDoc }, options);
 multiDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.Multi.docx");
 

Constructors

ConstructorDescription
OpenAiModel()

Methods

MethodDescription
create(int modelType)
summarize(Document doc, SummarizeOptions options)
summarize(Document[] docs, SummarizeOptions options)
translate(Document doc, int language)
withApiKey(String apiKey)Sets a specified API key to the model.
withOrganization(String organizationId)Sets a specified Organization to the model.
withProject(String projectId)Sets a specified Project to the model.

OpenAiModel()

public OpenAiModel()

create(int modelType)

public static AiModel create(int modelType)

Parameters:

ParameterTypeDescription
modelTypeint

Returns: AiModel

summarize(Document doc, SummarizeOptions options)

public Document summarize(Document doc, SummarizeOptions options)

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.

Examples:

Shows how to summarize text using OpenAI and Google models.


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

 String apiKey = System.getenv("API_KEY");
 // Use OpenAI or Google generative language models.
 IAiModelText model = (IAiModelText) AiModel.create(AiModelType.GPT_4_O_MINI).withApiKey(apiKey);

 SummarizeOptions options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.SHORT);
 Document oneDocumentSummary = model.summarize(firstDoc, options);
 oneDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.One.docx");

 options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.LONG);
 Document multiDocumentSummary = model.summarize(new Document[] { firstDoc, secondDoc }, options);
 multiDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.Multi.docx");
 

Parameters:

ParameterTypeDescription
docDocument
optionsSummarizeOptions

Returns: Document

summarize(Document[] docs, SummarizeOptions options)

public Document summarize(Document[] docs, SummarizeOptions options)

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.

Examples:

Shows how to summarize text using OpenAI and Google models.


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

 String apiKey = System.getenv("API_KEY");
 // Use OpenAI or Google generative language models.
 IAiModelText model = (IAiModelText) AiModel.create(AiModelType.GPT_4_O_MINI).withApiKey(apiKey);

 SummarizeOptions options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.SHORT);
 Document oneDocumentSummary = model.summarize(firstDoc, options);
 oneDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.One.docx");

 options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.LONG);
 Document multiDocumentSummary = model.summarize(new Document[] { firstDoc, secondDoc }, options);
 multiDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.Multi.docx");
 

Parameters:

ParameterTypeDescription
docsDocument[]
optionsSummarizeOptions

Returns: Document

translate(Document doc, int language)

public Document translate(Document doc, int language)

Parameters:

ParameterTypeDescription
docDocument
languageint

Returns: Document

withApiKey(String apiKey)

public AiModel withApiKey(String apiKey)

Sets a specified API key to the model.

Examples:

Shows how to summarize text using OpenAI and Google models.


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

 String apiKey = System.getenv("API_KEY");
 // Use OpenAI or Google generative language models.
 IAiModelText model = (IAiModelText) AiModel.create(AiModelType.GPT_4_O_MINI).withApiKey(apiKey);

 SummarizeOptions options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.SHORT);
 Document oneDocumentSummary = model.summarize(firstDoc, options);
 oneDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.One.docx");

 options = new SummarizeOptions();
 options.setSummaryLength(SummaryLength.LONG);
 Document multiDocumentSummary = model.summarize(new Document[] { firstDoc, secondDoc }, options);
 multiDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.Multi.docx");
 

Parameters:

ParameterTypeDescription
apiKeyjava.lang.String

Returns: AiModel

withOrganization(String organizationId)

public OpenAiModel withOrganization(String organizationId)

Sets a specified Organization to the model.

Parameters:

ParameterTypeDescription
organizationIdjava.lang.String

Returns: OpenAiModel

withProject(String projectId)

public OpenAiModel withProject(String projectId)

Sets a specified Project to the model.

Parameters:

ParameterTypeDescription
projectIdjava.lang.String

Returns: OpenAiModel