全部產品
Search
文件中心

Object Storage Service:智能文檔語氣改寫

更新時間:May 27, 2025

智能文檔語氣改寫功能可以調整文檔語氣,最佳化表達,將文檔內容改寫為更專業、更輕鬆或者更直白等語氣。

前提條件

已綁定IMM Project。通過OSS控制台綁定的具體操作,請參見快速入門。通過API綁定的具體操作,請參見AttachOSSBucket - 綁定Object Storage Service桶

注意事項

  • 智能文檔語氣改寫功能僅支援同步處理(x-oss-process處理方式)。

  • 該介面需採用POST方式請求。

  • 不支援匿名訪問。

  • 必須擁有IMM處理所需的相關許可權。更多資訊,請參見許可權

參數說明

操作名稱:doc/rephrase

具體參數如下表所示。

參數

類型

是否必須

描述

tone

string

需要將文檔改寫的目標語氣。取值:

  • professional:更專業的

  • relaxed:更放鬆的

  • straightforward:更直白的

  • confident:更自信的

  • friendly:更友好的

content

string

需要語氣改寫的文檔內容,需經過URL安全的Base64編碼。

說明

最大支援長度為19500位元組。

format

string

指定返回資料的方式,取值:

  • json(預設值):普通模式,會返回一個資料包,其中包含完整的請求結果。

  • event-stream:SSE模式,會收到多個資料包,其中每次都包含全量的資料。

返回參數如下表所示:

參數

類型

描述

RequestId

string

當次請求的Request ID。

Output

struct

輸出的結果內容。

子節點:Text, FinishReason

Text

string

本次請求的處理得到的結果。

父節點:Output

FinishReason

string

表明當前產生結果的現狀。取值:

  • null:正在產生

  • stop:產生結束

父節點:Output

使用REST API

說明

使用REST API進行智能文檔語氣改寫時需要指定檔案名稱,但檔案名稱僅作為預留位置使用。使用智能文檔語氣改寫時,只識別content參數作為需要改寫語氣的文檔內容。

使用普通模式進行文檔智能語氣改寫

處理方式

  • 待處理檔案:example.doc

  • 待語氣改寫文本:“四大名著,即四大小說名著,是指《三國演義》、《西遊記》、《水滸傳》、《紅樓夢》4部中國古典章回小說。”

  • 需要改寫的語氣:straightforward

處理樣本

POST /example.doc?x-oss-process HTTP/1.1
Host: doc-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e

x-oss-process=doc/rephrase,tone_straightforward,content_5Zub5aSn5ZCN6JGX77yM5Y2z5Zub5aSn5bCP6K-05ZCN6JGX77yM5piv5oyH44CK5LiJ5Zu95ryU5LmJ44CL44CB44CK6KW_5ri46K6w44CL44CB44CK5rC05rWS5Lyg44CL44CB44CK57qi5qW85qKm44CLNOmDqOS4reWbveWPpOWFuOeroOWbnuWwj-ivtOOAgg==

返回樣本

HTTP/1.1 200 OK
Server: AliyunOSS
Date: Thu, 10 Aug 2023 11:09:00 GMT
Content-Type: application/json;charset=UTF-8
Connection: close
Vary: Accept-Encoding
x-oss-request-id: 65969DD74479D83039EBE00E
x-oss-server-time: 2010
Content-Encoding: gzip

{
  "RequestId":"65969DD74479D83039EBE00E",
  "Output":{
      "Text":"中國的四大名著,也就是四部非常著名的小說,指的是《三國演義》、《西遊記》、《水滸傳》和《紅樓夢》這四部中國古代長篇章回小說。",
      "FinishReason":"stop"
   }
}

使用SSE進行文檔智能語氣改寫

處理方式

  • 待處理檔案:example.doc

  • 待語氣改寫文本:“四大名著,即四大小說名著,是指《三國演義》、《西遊記》、《水滸傳》、《紅樓夢》4部中國古典章回小說。”

  • 需要改寫的語氣:straightforward

  • 返回結果方式:event-stream

處理樣本

POST /example.doc?x-oss-process HTTP/1.1
Host: doc-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e

x-oss-process=doc/rephrase,tone_straightforward,type_event-stream,content_5Zub5aSn5ZCN6JGX77yM5Y2z5Zub5aSn5bCP6K-05ZCN6JGX77yM5piv5oyH44CK5LiJ5Zu95ryU5LmJ44CL44CB44CK6KW_5ri46K6w44CL44CB44CK5rC05rWS5Lyg44CL44CB44CK57qi5qW85qKm44CLNOmDqOS4reWbveWPpOWFuOeroOWbnuWwj-ivtOOAgg==

返回樣本

HTTP/1.1 200 OK
Server: AliyunOSS
Date: Thu, 10 Aug 2023 11:20:11 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: close
x-oss-request-id: 65969D744479D83837AEE00E
x-oss-server-time: 587

id: 0
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的","FinishReason":"null"}}

id: 1
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大","FinishReason":"null"}}

id: 2
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名","FinishReason":"null"}}

id: 3
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本","FinishReason":"null"}}

id: 4
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國","FinishReason":"null"}}

id: 5
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國演義》、《西遊記","FinishReason":"null"}}

id: 6
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國演義》、《西遊記》、《水滸傳》和","FinishReason":"null"}}

id: 7
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國演義》、《西遊記》、《水滸傳》和《紅樓夢》這四部經典的中國古代","FinishReason":"null"}}

id: 8
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國演義》、《西遊記》、《水滸傳》和《紅樓夢》這四部經典的中國古代長篇小說。","FinishReason":"null"}}

id: 9
event: Result
data: {"RequestId":"65969D744479D83837AEE00E","Output":{"Text":"中國的四大名著,也就是四本非常著名的小說,指的是《三國演義》、《西遊記》、《水滸傳》和《紅樓夢》這四部經典的中國古代長篇小說。","FinishReason":"stop"}}

使用SDK

以下僅列舉常見SDK通過處理參數的方式使用智能文檔語氣改寫的程式碼範例。如需使用其他SDK的智能文檔語氣改寫的程式碼範例,請參見以下常見SDK自行調整。

Java

要求使用3.17.4及以上版本的Java SDK。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.GenericResult;
import com.aliyun.oss.model.ProcessObjectRequest;

import java.io.IOException;
import java.util.Formatter;

public class Demo {
    public static void main(String[] args) throws ClientException, com.aliyuncs.exceptions.ClientException {
        // yourEndpoint填寫Bucket所在地區對應的Endpoint。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 填寫阿里雲通用Region ID,例如cn-hangzhou。
        String region = "cn-hangzhou";
        // 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 指定Bucket名稱。
        String bucketName = "examplebucket";
        // 指定檔案名稱,作為預留位置使用。使用智能文檔語氣改寫時,不讀取該檔案的內容。
        String key ="example.docx";
        // 指定待改寫語氣的常值內容。
        String content = "yourTxt";
        String encodeContent = BinaryUtil.toBase64String(content.getBytes()).replaceAll("\\+","-")
                .replaceAll("/","_").replaceAll("=","");

        // 建立OSSClient執行個體。
        // 當OSSClient執行個體不再使用時,調用shutdown方法以釋放資源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {

            StringBuilder sbStyle = new StringBuilder();
            Formatter styleFormatter = new Formatter(sbStyle);
            // 構建智能文檔語氣改寫的處理指示。
            styleFormatter.format("doc/rephrase,tone_straightforward,content_%s",
                    encodeContent);
            System.out.println(sbStyle.toString());
            ProcessObjectRequest request = new ProcessObjectRequest(bucketName, key, sbStyle.toString());
            GenericResult processResult = ossClient.processObject(request);
            String json = IOUtils.readStreamAsString(processResult.getResponse().getContent(), "UTF-8");
            processResult.getResponse().getContent().close();
            System.out.println(json);
        } 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());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

PHP

要求使用PHP SDK 2.7.0及以上版本。

<?php
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 
$ak = getenv('OSS_ACCESS_KEY_ID');
$sk = getenv('OSS_ACCESS_KEY_SECRET');
// 指定Bucket名稱,例如examplebucket。
$bucket = 'examplebucket';
// 指定檔案名稱,僅作為預留位置使用。使用智能文檔語氣改寫時,不讀取該檔案的內容。
$objectKey = 'example.docx';
// 指定待改寫語氣的常值內容。
$txt = "yourTxt";

$base64url = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($txt));
// 構建智能文檔語氣改寫的指令。
$body = sprintf("x-oss-process=doc/rephrase,tone_straightforward,content_%s", $base64url);

$httpVerb = 'POST';
$contentMd5 = base64_encode(md5($body, true));
$contentType = '';
$date = gmdate('D, d M Y H:i:s T');
$stringToSign = $httpVerb . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" . "/{$bucket}/{$objectKey}?x-oss-process";
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $sk, true));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://{$bucket}.oss-cn-hangzhou.aliyuncs.com/{$objectKey}?x-oss-process");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Date: ' . $date,
    'Authorization: OSS ' . $ak . ':' . $signature,
    'Content-Type: ' . $contentType,
    'Content-Md5:' . $contentMd5,
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false) {
    echo "Error: " . curl_error($ch);
} else {
    if ($httpcode == 200) {
        var_dump($response);
    } else {
        echo "Error: HTTP code " . $httpcode;
    }
}

Go

要求使用Go SDK 3.0.2及以上版本。

package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

type TextData struct {
	RequestId string `json:"RequestId"`
	Output    struct {
		Text         string `json:"Text"`
		FinishReason string `json:"FinishReason"`
	} `json:"Output"`
}

func main() {
	// 從環境變數中擷取臨時訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID、OSS_ACCESS_KEY_SECRET、OSS_SESSION_TOKEN。
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// 建立OSSClient執行個體。
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// 填寫Bucket名稱,例如examplebucket。
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	params := make(map[string]interface{})
	params["x-oss-process"] = nil
        // 指定待改寫語氣的常值內容。
	txt := "yourTxt"
        // 構建智能文檔語氣改寫的處理指示。
	data := fmt.Sprintf("x-oss-process=doc/rephrase,tone_straightforward,content_%v", base64.URLEncoding.EncodeToString([]byte(txt)))
        // example.docx是Bucket中的某個檔案,作為預留位置使用。使用智能文檔語氣改寫時,不讀取該檔案的內容。
	response, err := bucket.Do("POST", "example.docx", params, nil, strings.NewReader(data), nil)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	defer response.Body.Close()
	jsonData, err := io.ReadAll(response.Body)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	var text TextData
	err = json.Unmarshal(jsonData, &text)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Printf("RequestId:%v\n", text.RequestId)
	fmt.Printf("Text:%v\n", text.Output.Text)
	fmt.Printf("FinishReason:%v\n", text.Output.FinishReason)
}