Document AI Assistant plugin

更新时间:
复制 MD 格式

This topic introduces the Document AI Assistant plug-in for WebOffice and describes its features and usage.

The Document AI Assistant plug-in is an intelligent document creation tool powered by Alibaba Cloud's Tongyi Qianwen large language model. It helps you efficiently edit, polish, and translate documents, significantly improving the quality and efficiency of document processing.

Features

Feature

Description

Document continuation

Intelligently continues writing from your selected text, providing inspiration and ideas for content creation.

Document summarization

Generates a summary of your selected text to help you quickly grasp the main points.

Translation

Supports translation in 12 languages to meet your cross-lingual document processing needs.

Polishing

Improves the quality and readability of your text by optimizing its grammar, spelling, word choice, sentence structure, and overall style.

Enrichment

Expands on your selected text using AI to add more detail and depth to the content.

Tone rewriting

Rewrites the selected text to fit a different tone. You can choose from formal, casual, straightforward, confident, or friendly.

Limitations

  • The Document AI Assistant plug-in is available only for WebOffice on PC. It is not supported on mobile H5 pages or in mini-programs.

Integration

1. Encapsulate server-side APIs

The Document AI Assistant plug-in calls the x-oss-process API of Object Storage Service (OSS). Before you make the call, you must bind an IMM project to an OSS bucket.

In the management page for your bucket in the OSS console, choose Data Management > Data Processing > Document Processing from the left-side navigation pane. Verify that the IMM service has been enabled and authorized with the AliyunIMMDefaultRole, then click the Bucket binding IMM project button.

After the binding is complete, you can go to the Intelligent Document Processing tab to apply for a quota.

In the left-side navigation pane of the OSS console, choose Data Management > Data Processing > Document Processing. At the bottom of the page, click Apply for Quota Now.

On your server, encapsulate an API operation to obtain signed URLs for the Document AI Assistant APIs, such as /file/get_ai_process_url.

The following code provides an example of the main server-side logic:

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import java.net.URL;
import java.util.Date;
public class Demo {
    public static void main(String[] args) throws Throwable {
        // The following code uses China (Hangzhou) as an example. Replace the endpoint with the one for your region.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. For example, cn-hangzhou.
        String region = "cn-hangzhou";
        // We strongly recommend that you do not store access credentials in your code. Instead, use environment variables to obtain the access credentials.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. For example, examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the object. Do not include the bucket name in the full path.
        String objectName = "exampledir/exampleobject.docx";
        // Create an OSSClient instance.
        // When the OSSClient instance is no longer needed, call the shutdown method to release resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // Explicitly declare the use of the V4 signature algorithm.
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();
        try {
            // Set the expiration time for the signed URL to 10 minutes.
            Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10 );
            GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.POST);
            req.setExpiration(expiration);
            req.setContentType("text/plain; charset=UTF-8");
            // Note: Pass an empty string for the Process parameter.
            // Add the sub-resource parameter.
            req.addQueryParameter("x-oss-process", null);
            URL signedUrl = ossClient.generatePresignedUrl(req);
            System.out.println(signedUrl);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
# Specify the bucket name. For example, examplebucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the full path of the document to be previewed. The full path does not include the bucket name.
key = 'example.docx'
# Generate a signed URL that is valid for 10 minutes. The expiration time is in seconds.
# Note: Pass an empty string for the x-oss-process parameter.
url = bucket.sign_url('POST', key, 10 * 60, headers={'Content-Type': 'text/plain; charset=UTF-8'}, params={'x-oss-process': ''})
print(url)
package main
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
func main() {
    // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Create an OSSClient instance.
    // Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the bucket name. For example, examplebucket.
    bucketName := "examplebucket"
    bucket, err := client.Bucket(bucketName)
    if err != nil {
    fmt.Println("Error:", err); os.Exit(-1)
    }
    // Specify the full path of the document to be previewed. The full path does not include the bucket name.
    ossObjectName := "exampledir/exampleobject.docx"
    // Set the style, which includes document preview parameters.
    // Generate a signed URL that is valid for 600 seconds.
    // Note: Pass an empty string for the Process parameter.
    signedURL, err := bucket.SignURL(ossObjectName, oss.HTTPPost, 600, oss.ContentType("text/plain; charset=UTF-8"), oss.Process(""))
    if err != nil {
    fmt.Println("Error:", err); os.Exit(-1)
    } else {
    fmt.Println(signedURL)
    }
}
const OSS = require('ali-oss');
const client = new OSS({
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});
// Set parameters for the signed URL.
// The objectKey is the full path of the object in the bucket.
// Generate a signed URL that is valid for 10 minutes.
const signUrl = client.signatureUrl(objectKey, {
  method: 'POST',
  subResource: {
    'x-oss-process': '',  // Note: Pass an empty string here.
  },
  expires: 60*10, // Unit: seconds.
  'Content-Type': 'text/plain; charset=UTF-8',
});
console.log("signUrl="+signUrl);

The signed URL can be used for the following API operations:

2. Frontend plugin usage

  1. After you integrate the WebOffice SDK for JavaScript for document editing, import the following scripts:

<!-- Import the SDK for JavaScript -->
<script src="https://g.alicdn.com/IMM/office-js/1.1.19/aliyun-web-office-sdk.min.js"></script>
<!-- Import the plug-in SDK -->
<script src="https://g.alicdn.com/IMM/office-js-plugins/1.3.0/aliyun-web-office-plugins.min.js"></script>

In the example, 1.1.19 is the version of the SDK for JavaScript. Replace it with the actual version you are using. For the latest version, see SDK versions.

In the example, 1.3.0 is the version of the plug-in. Replace it with the actual version you are using. For the latest version, see Plug-in SDK versions.

  1. Initialize the plug-in.

After you call the aliyun.config method to initialize the SDK for JavaScript, call the aliyun.initAIPlugin method to initialize the Document AI Assistant plug-in.

let ins = aliyun.config({
  ....
})
// Call aliyun.initAIPlugin after the 'ready' event is triggered.
await ins.ready();
// Use the AI plug-in.
aliyun.initAIPlugin({
  sdkInstance: ins, // Pass the SDK instance.
  // aiHelperLink: 'https://help.aliyun.com/xxxx', // Link to the AI assistant help document on the panel.
  async onGetAIProcessUrl({ content_type, content_md5 }){
    /** Call the encapsulated server API to obtain the x-oss-process signed URL. */
    let opt = {
      content_type,
      // content_md5,
      file_name: "test-object.docx",
    };
    let { ai_process_url } = await $.post("/file/get_ai_process_url", opt);
    return {
      ai_process_url, // Required.
      // content_md5, // Optional. If this is not provided, the content_md5 item is not included in the signature.
    };
  },
  async onFeedback(info){
    // Record the feedback.
  }
})

2.1. initAIPlugin parameters

Parameter

Type

Required

Description

sdkInstance

Object

Yes

The SDK instance returned by aliyun.config().

onGetAIProcessUrl

({content_type: string, content_md5:string })=>Promise<{ai_process_url:string,content_md5?:string}>

Yes

A method to obtain the signed URL for the x-oss-process operation.

aiHelperLink

string

No

The link to the AI assistant help document on the panel.

onFeedback

(info:FeedbackInfo)=>void

No

A callback for handling user feedback. See the description of the FeedbackInfo parameter.

2.2. FeedbackInfo parameters

Parameter

Type

Description

type

string

Valid values: 'thumb-up' and 'thumb-down'.

data

object

The feedback details.

Usage

In the Word editing interface, click Document AI Assistant on the right side of the Start toolbar, or use the shortcut keys (Alt + / for Windows or Control + / for macOS). This opens the Document AI Assistant menu, which you can use to optimize the selected text or the text at the cursor.

The menu is divided into two groups. The Edit and adjust selected content group includes Polishing, Enrichment, and Tone rewriting. The Generate from selected content group includes Document summarization, Translation, and Document continuation.