|
| 1 | +package Examples; |
| 2 | + |
| 3 | +// Copyright (c) 2001-2024 Aspose Pty Ltd. All Rights Reserved. |
| 4 | +// |
| 5 | +// This file is part of Aspose.Words. The source code in this file |
| 6 | +// is only intended as a supplement to the documentation, and is provided |
| 7 | +// "as is", without warranty of any kind, either expressed or implied. |
| 8 | +////////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +import com.aspose.words.*; |
| 11 | +import org.testng.annotations.Test; |
| 12 | + |
| 13 | +@Test |
| 14 | +public class ExAI extends ApiExampleBase |
| 15 | +{ |
| 16 | + @Test (enabled = false, description = "This test should be run manually to manage API requests amount") |
| 17 | + public void aiSummarize() throws Exception |
| 18 | + { |
| 19 | + //ExStart:AiSummarize |
| 20 | + //GistId:72d57eeddb7fb342fd51b26e5fcf9642 |
| 21 | + //ExFor:GoogleAiModel |
| 22 | + //ExFor:OpenAiModel |
| 23 | + //ExFor:OpenAiModel.WithOrganization(String) |
| 24 | + //ExFor:OpenAiModel.WithProject(String) |
| 25 | + //ExFor:IAiModelText |
| 26 | + //ExFor:IAiModelText.Summarize(Document, SummarizeOptions) |
| 27 | + //ExFor:IAiModelText.Summarize(Document[], SummarizeOptions) |
| 28 | + //ExFor:SummarizeOptions |
| 29 | + //ExFor:SummarizeOptions.#ctor |
| 30 | + //ExFor:SummarizeOptions.SummaryLength |
| 31 | + //ExFor:SummaryLength |
| 32 | + //ExFor:AiModel |
| 33 | + //ExFor:AiModel.Create(AiModelType) |
| 34 | + //ExFor:AiModel.WithApiKey(String) |
| 35 | + //ExFor:AiModelType |
| 36 | + //ExSummary:Shows how to summarize text using OpenAI and Google models. |
| 37 | + Document firstDoc = new Document(getMyDir() + "Big document.docx"); |
| 38 | + Document secondDoc = new Document(getMyDir() + "Document.docx"); |
| 39 | + |
| 40 | + String apiKey = System.getenv("API_KEY"); |
| 41 | + // Use OpenAI or Google generative language models. |
| 42 | + IAiModelText model = ((OpenAiModel)AiModel.create(AiModelType.GPT_4_O_MINI).withApiKey(apiKey)).withOrganization("Organization").withProject("Project"); |
| 43 | + |
| 44 | + SummarizeOptions options = new SummarizeOptions(); |
| 45 | + options.setSummaryLength(SummaryLength.SHORT); |
| 46 | + Document oneDocumentSummary = model.summarize(firstDoc, options); |
| 47 | + oneDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.One.docx"); |
| 48 | + |
| 49 | + options = new SummarizeOptions(); |
| 50 | + options.setSummaryLength(SummaryLength.LONG); |
| 51 | + Document multiDocumentSummary = model.summarize(new Document[] { firstDoc, secondDoc }, options); |
| 52 | + multiDocumentSummary.save(getArtifactsDir() + "AI.AiSummarize.Multi.docx"); |
| 53 | + //ExEnd:AiSummarize |
| 54 | + } |
| 55 | + |
| 56 | + @Test (enabled = false, description = "This test should be run manually to manage API requests amount") |
| 57 | + public void aiTranslate() throws Exception |
| 58 | + { |
| 59 | + //ExStart:AiTranslate |
| 60 | + //GistId:93fefe5344a8337b931d0fed5c028225 |
| 61 | + //ExFor:IAiModelText.Translate(Document, AI.Language) |
| 62 | + //ExFor:AI.Language |
| 63 | + //ExSummary:Shows how to translate text using Google models. |
| 64 | + Document doc = new Document(getMyDir() + "Document.docx"); |
| 65 | + |
| 66 | + String apiKey = System.getenv("API_KEY"); |
| 67 | + // Use Google generative language models. |
| 68 | + IAiModelText model = (IAiModelText)AiModel.create(AiModelType.GEMINI_15_FLASH).withApiKey(apiKey); |
| 69 | + |
| 70 | + Document translatedDoc = model.translate(doc, Language.ARABIC); |
| 71 | + translatedDoc.save(getArtifactsDir() + "AI.AiTranslate.docx"); |
| 72 | + //ExEnd:AiTranslate |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments