Tongyi Law large language model

Updated at:

Tongyi Fa Rui is a large model for the legal industry, built on the Qwen foundation model and trained on specialized legal data and knowledge. It leverages techniques such as model fine-tuning, reinforcement learning, RAG, legal agents, and specialized small models for judicial applications. It can answer legal questions, determine which laws apply, assist with case analysis, generate legal documents, retrieve legal knowledge, and review contract clauses.

Model overview

Model name

Context length

Maximum input

Maximum output

Input cost

Output cost

(in tokens)

(per million tokens)

farui-plus

12k

12k

2k

CNY 20

For details on the rate limits for this model, see Rate Limiting.

Use the SDK

You can use the SDK to implement features such as single-turn conversation, multi-turn conversation, and streaming output.

Prerequisites

  • The DashScope SDK is available in Python and Java. Make sure you have the latest version installed: Install the SDK.

  • Activate the service and obtain an API key: Obtain an API key.

  • To reduce the risk of exposing your API key, we recommend configuring it as an environment variable. For more information, see Configure an API key as an environment variable. You can also configure the API key in your code, but this increases the risk of exposure.

NoteWhen using the DashScope Java SDK, you should reuse Generation and other request objects for efficiency. However, objects such as Generation are not thread-safe. You must ensure object safety by promptly closing processes and using synchronization mechanisms.

Single-turn conversation

You can use Tongyi Farui in scenarios such as legal consultation, document generation, and dispute focus identification. Run the following sample code to try a single-turn conversation with the Tongyi Farui large model.

Python

# coding=utf-8
import dashscope

messages = [{'role': 'system',
                'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': 'My brother owes me 10,000 yuan, generate a statement of claim for me.'}]
response = dashscope.Generation.call(
    model="farui-plus",
    messages=messages,
    result_format='message',
)
print(response)

Java

// We recommend using DashScope SDK version 2.12.0 or later.
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;

public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("My brother owes me 10,000 yuan, generate a statement of claim for me.")
                .build();
        GenerationParam param = GenerationParam.builder()
                // If you have not configured an environment variable, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("farui-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(JsonUtils.toJson(result));
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // Use a logging framework to record the exception information.
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

The following is an example of the output:

{
    "status_code": 200,
    "request_id": "32880e8b-dc0f-95e4-b88f-2ca0c41e8c7b",
    "code": "",
    "message": "",
    "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "[Civil Statement of Claim]\n\nPlaintiff: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\nAuthorized Agent: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\n\nClaims:\n1. An order for the defendant to repay the plaintiff the loan of 10,000 yuan plus interest;\n2. An order for the defendant to bear all court costs for this case.\n\nFacts and Reasons:\nThe plaintiff and the defendant are siblings. On XX/XX/XXXX, the defendant borrowed 10,000 yuan from the plaintiff, which was delivered in cash. The defendant promised to repay the loan by XX/XX/XXXX but failed to do so upon maturity. Despite multiple requests from the plaintiff, the defendant has provided various excuses and has not yet repaid the loan.\n\nList of Evidence:\n1. One IOU;\n2. Records of payment demands.\n\nTo:\n\nXXX People's Court\n\nPlaintiff: (Plaintiff's Signature)\n\nXX/XX/XXXX\n\nAttachments: 1. XXX copies of this statement of claim.\n  2. Table of contents for evidence."
                }
            }
        ]
    },
    "usage": {
        "input_tokens": 35,
        "output_tokens": 274,
        "total_tokens": 309
    }
}

Multi-turn conversation

Run the following sample code to try a multi-turn conversation with the Tongyi Farui large model.

from dashscope import Generation
# The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

messages = [{'role': 'system',
                'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': 'My brother owes me 10,000 yuan, generate a statement of claim for me.'}]
response = Generation.call(model="farui-plus",
                            messages=messages,
                            result_format='message')
print(response)
# Add the assistant's reply to the messages list
messages.append({'role': response.output.choices[0]['message']['role'],
                    'content': response.output.choices[0]['message']['content']})
# Add the user's new question to the messages list
messages.append({'role': 'user', 'content': 'If the loan interest rate is 4%, regenerate the statement of claim.'})
# Make a second call to the model for a response
response = Generation.call(model="farui-plus",
                            messages=messages,
                            result_format='message')
print(response)
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.dashscope.utils.Constants;

public class Main {
        // The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
    public static GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .model("farui-plus")
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
    }
    public static GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }
    public static void main(String[] args) {
        // The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        try {
            List<Message> messages = new ArrayList<>();
            messages.add(createMessage(Role.SYSTEM, "You are a helpful assistant."));
            messages.add(createMessage(Role.USER, "My brother owes me 10,000 yuan, generate a statement of claim for me."));

            GenerationParam param = createGenerationParam(messages);
            GenerationResult result = callGenerationWithMessages(param);
            printResultAsJson(result);
            // Add the assistant's response message to the list
            messages.add(result.getOutput().getChoices().get(0).getMessage());
            // Add the new user message
            messages.add(createMessage(Role.USER, "If the loan interest rate is 4%, regenerate the statement of claim."));
            result = callGenerationWithMessages(param);
            printResultAsJson(result);
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    private static Message createMessage(Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }

    private static void printResultAsJson(GenerationResult result) {
        System.out.println(JsonUtils.toJson(result));
    }
}

The following is an example of the output:

{
    "status_code": 200,
    "request_id": "0851caa1-232c-9b97-853f-be7810ecd36c",
    "code": "",
    "message": "",
    "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "[Civil Statement of Claim]\n\nPlaintiff: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\nAuthorized Agent: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\n\nClaims:\n1. An order for the defendant to repay the plaintiff the loan of 10,000 yuan;\n2. An order for the defendant to bear all court costs for this case.\n\nFacts and Reasons:\nThe plaintiff and the defendant are siblings. On September 10, 2019, the defendant borrowed 10,000 yuan from the plaintiff for an urgent need and issued an IOU, agreeing to a one-month loan term. However, after the loan term expired, the defendant did not repay the loan as agreed, and multiple demands from the plaintiff have been unsuccessful.\n\nList of Evidence:\n1. One IOU;\n2. One bank transfer record.\n\nTo:\n\nXXX People's Court\n\nPlaintiff: (Plaintiff's Signature)\n\nXX/XX/XXXX\n\nAttachments: 1. XXX copies of this statement of claim.\n  2. Table of contents for evidence.\n\nPlease note, this is only a template, and specific details may need to be adjusted based on the actual situation. In practice, we recommend consulting a professional lawyer or legal practitioner to ensure the accuracy and legality of the statement of claim."
                }
            }
        ]
    },
    "usage": {
        "input_tokens": 56,
        "output_tokens": 284,
        "total_tokens": 340
    }
}
{
    "status_code": 200,
    "request_id": "0d922f48-c975-965a-aab5-a3ec19191038",
    "code": "",
    "message": "",
    "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "[Civil Statement of Claim]\n\nPlaintiff: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\nAuthorized Agent: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\n\nClaims:\n1. An order for the defendant to repay the principal loan amount of 10,000 yuan;\n2. An order for the defendant to pay the plaintiff interest on the loan, calculated at an annual rate of 4% on the principal of 10,000 yuan, from September 10, 2019, until the date of actual settlement;\n3. An order for the defendant to bear all court costs for this case.\n\nFacts and Reasons:\nThe plaintiff and the defendant are siblings. On September 10, 2019, the defendant borrowed 10,000 yuan from the plaintiff for an urgent need and issued an IOU, agreeing to a one-month loan term with an annual interest rate of 4%. However, after the loan term expired, the defendant did not repay the loan as agreed, and multiple demands from the plaintiff have been unsuccessful.\n\nList of Evidence:\n1. One IOU;\n2. One bank transfer record.\n\nTo:\n\nXXX People's Court\n\nPlaintiff: (Plaintiff's Signature)\n\nXX/XX/XXXX\n\nAttachments: 1. XXX copies of this statement of claim.\n  2. Table of contents for evidence.\n\nPlease note, this is only a template, and specific details may need to be adjusted based on the actual situation. In practice, we recommend consulting a professional lawyer or legal practitioner to ensure the accuracy and legality of the statement of claim."
                }
            }
        ]
    },
    "usage": {
        "input_tokens": 22,
        "output_tokens": 338,
        "total_tokens": 360
    }
}

Streaming output

Large models generate results incrementally. By default, the API returns the full response only after generation is complete. Streaming output, in contrast, sends back results in real time as they are generated, reducing wait times. To enable streaming output, you need to configure it. In the DashScope Python SDK, set stream to True. In the DashScope Java SDK, use the streamCall method.

import os
import dashscope
# The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

messages = [
    {'role':'system','content':'you are a helpful assistant'},
    {'role': 'user','content': 'Who are you?'}
    ]
responses = dashscope.Generation.call(
    # If you have not configured an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model="farui-plus",
    messages=messages,
    result_format='message',
    stream=True,
    incremental_output=True
    )
for response in responses:
    print(response)
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
import io.reactivex.Flowable;
import java.lang.System;

public class Main {
        // The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private static void handleGenerationResult(GenerationResult message) {
        System.out.println(JsonUtils.toJson(message));
    }
    public static void streamCallWithMessage(Generation gen, Message userMsg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        GenerationParam param = buildGenerationParam(userMsg);
        Flowable<GenerationResult> result = gen.streamCall(param);
        result.blockingForEach(message -> handleGenerationResult(message));
    }
    private static GenerationParam buildGenerationParam(Message userMsg) {
        return GenerationParam.builder()
                // If you have not configured an environment variable, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("farui-plus")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();
    }
    public static void main(String[] args) {
        // The following configuration is for the China (Beijing) region. When making a call, replace {WorkspaceId} with your actual workspace ID. Configurations vary by region.
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content("Who are you?").build();
            streamCallWithMessage(gen, userMsg);
        } catch (ApiException | NoApiKeyException | InputRequiredException  e) {
            logger.error("An exception occurred: {}", e.getMessage());
        }
        System.exit(0);
    }
}

The following is an example of the streaming output:

{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "I am"}}]}, "usage": {"input_tokens": 21, "output_tokens": 1, "total_tokens": 22}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " Tong"}}]}, "usage": {"input_tokens": 21, "output_tokens": 2, "total_tokens": 23}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "yi"}}]}, "usage": {"input_tokens": 21, "output_tokens": 3, "total_tokens": 24}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " Far"}}]}, "usage": {"input_tokens": 21, "output_tokens": 4, "total_tokens": 25}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "ui, a virtual assistant developed by"}}]}, "usage": {"input_tokens": 21, "output_tokens": 8, "total_tokens": 29}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " Alibaba's Tongyi Lab"}}]}, "usage": {"input_tokens": 21, "output_tokens": 12, "total_tokens": 33}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ", designed to provide friendly"}}]}, "usage": {"input_tokens": 21, "output_tokens": 16, "total_tokens": 37}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " and helpful service"}}]}, "usage": {"input_tokens": 21, "output_tokens": 20, "total_tokens": 41}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ". I can help answer"}}]}, "usage": {"input_tokens": 21, "output_tokens": 24, "total_tokens": 45}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " legal questions, provide"}}]}, "usage": {"input_tokens": 21, "output_tokens": 28, "total_tokens": 49}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " information, and perform"}}]}, "usage": {"input_tokens": 21, "output_tokens": 32, "total_tokens": 53}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " simple logical reasoning"}}]}, "usage": {"input_tokens": 21, "output_tokens": 36, "total_tokens": 57}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ". If you have any questions"}}]}, "usage": {"input_tokens": 21, "output_tokens": 40, "total_tokens": 61}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ", feel free to ask"}}]}, "usage": {"input_tokens": 21, "output_tokens": 44, "total_tokens": 65}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ". Please note that my"}}]}, "usage": {"input_tokens": 21, "output_tokens": 48, "total_tokens": 69}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " answers are not a substitute"}}]}, "usage": {"input_tokens": 21, "output_tokens": 52, "total_tokens": 73}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " for professional"}}]}, "usage": {"input_tokens": 21, "output_tokens": 56, "total_tokens": 77}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " legal advice."}}]}, "usage": {"input_tokens": 21, "output_tokens": 59, "total_tokens": 80}}
{"status_code": 200, "request_id": "f741ea2e-e997-9d33-ae8d-da3c56b26a4e", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": ""}}]}, "usage": {"input_tokens": 21, "output_tokens": 59, "total_tokens": 80}}

Request parameters

Parameter

Type

Default

Description

model

string

None

Specifies the Tongyi Farui large model for the conversation. Currently, farui-plus is available. The maximum context length, including both input and output, is 14,000 tokens.

messages

array

None

  • messages: The conversation history between the user and the model. Each element in the list is in the format {"role": role, "content": content}. Valid roles are: system, user, and assistant.

  • system: Represents a system-level message. It is optional but, if used, must be the first message in the array (messages[0]).

  • user and assistant: Represent messages from the user and the model. These roles should alternate to simulate a conversation.

  • prompt: The current instruction from the user for the model to execute. It guides the model in generating a response.

You can use either the prompt or messages parameter. The prompt parameter is suitable for single-turn conversations.

For multi-turn conversations, use the messages parameter to provide conversation history, which helps the model understand context and maintain continuity. We recommend using messages for all conversational use cases.

prompt

string

None

max_tokens (optional)

int

2000

Specifies the maximum number of tokens the model can generate. For example, if a model's maximum output is 2,000 tokens, you can set this to 1,000 to limit the response length.

Different models have different output limits. Refer to the model list for details.

top_p (optional)

float

0.8

The probability threshold for nucleus sampling. For example, a value of 0.8 retains the smallest set of tokens whose cumulative probability is 0.8 or higher. The value must be in the range (0, 1.0). Higher values increase randomness, while lower values increase determinism.

top_k (optional)

int

None

The size of the candidate set for sampling. For example, a value of 50 means that only the 50 tokens with the highest scores are considered for sampling. Higher values increase randomness, while lower values increase determinism. If not specified, or if set to a value greater than 100, top_k sampling is disabled, and only top_p is used.

stream (optional)

bool

False

Specifies whether to use streaming output. When enabled, the API returns a generator or stream that you must iterate over to get the results. Each yielded output contains the next part of the generated content.

result_format (optional)

string

text

Specifies the output format. Valid values are text and message. The default is text. When set to message, the output structure matches the format shown in the response examples. The message format is recommended.

Response

The following is an example of the response when result_format is set to message:

{
    "status_code": 200,
    "request_id": "0bcab0eb-ee6b-983d-9479-9814cff59096",
    "code": "",
    "message": "",
    "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "[Civil Statement of Claim]\n\nPlaintiff: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\nAuthorized Agent: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on XX/XX/XXXX, ethnicity: XXX, residing at: XXX Road, XXX District, XXX City, contact number: XXX.\n\nClaims:\n1. An order for the defendant to repay the plaintiff the loan of 10,000 yuan;\n2. An order for the defendant to bear all court costs for this case.\n\nFacts and Reasons:\nThe plaintiff and the defendant are siblings. On September 10, 2019, the defendant borrowed 10,000 yuan from the plaintiff for an urgent need and issued an IOU, agreeing to a one-month loan term. However, after the loan term expired, the defendant did not repay the loan as agreed, and multiple demands from the plaintiff have been unsuccessful.\n\nList of Evidence:\n1. One IOU;\n2. One bank transfer record.\n\nTo:\n\nXXX People's Court\n\nPlaintiff: (Plaintiff's Signature)\n\nXX/XX/XXXX\n\nAttachments: 1. XXX copies of this statement of claim.\n  2. Table of contents for evidence.\n\nPlease note, this is only a template, and specific details may need to be adjusted based on the actual situation. In practice, we recommend consulting a professional lawyer or legal practitioner to ensure the accuracy and legality of the statement of claim."
                }
            }
        ]
    },
    "usage": {
        "input_tokens": 56,
        "output_tokens": 284,
        "total_tokens": 340
    }
}

HTTP API

Description

The Tongyi FaRui model supports HTTP calls to generate responses. The API provides both standard HTTP and HTTP SSE protocols, allowing you to choose the one that best suits your requirements.

Prerequisites

Activate the service and obtain an API key: Get API Key.

Request

POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

Request parameters

Location

Parameter

Type

Required

Description

Example

Header

Content-Type

string

Yes

The format of the request body. Must be application/json.

application/json

Accept

string

No

Set this to text/event-stream to enable HTTP SSE streaming responses. If omitted, the default is */*.

text/event-stream

Authorization

string

Yes

Your API key, prefixed with Bearer.

Bearer d1**2a

X-DashScope-WorkSpace

string

No

Specifies the workspace for the call. This parameter is required for API calls made with a sub-account API key, as the sub-account must belong to a workspace. It is optional for a primary account API key. If provided, the call uses the workspace's identity; otherwise, it uses the primary account's identity.

ws_QTggmeAxxxxx

Body

model

string

Yes

The ID of the Tongyi FaRui model.

farui-plus

input.prompt

string

No

The prompt for the model. Supports both Chinese and English.

My brother owes me 10,000 yuan. Generate a statement of claim for me.

input.messages

list

No

The conversation history. While prompt is supported for backward compatibility, using messages is recommended. Each object in the list must have a role (system, user, or assistant) and content. More roles may be added in the future.

[{'role': 'system',

'content': 'You are a helpful assistant.'},

{'role': 'user', 'content': 'My brother owes me 10,000 yuan. Generate a statement of claim for me.'}]

input.messages.role

string

Required when messages is used.

input.messages.content

string

parameters.result_format

string

No

Specifies the response format. Set to text for a legacy text format, or message for a structured format compatible with OpenAI.

"message" refers to a message that is compatible with OpenAI.

"text"

parameters.max_tokens

integer

No

The maximum number of tokens to generate. For example, if a model's maximum output length is 2,000 tokens, you can set this to 1,000 to prevent excessively long responses.

Each model has a different maximum limit. See the model list for details.

2000

parameters.top_p

float

No

The probability threshold for nucleus sampling. For example, a value of 0.8 considers only the tokens that make up the top 80% of the probability mass for sampling. The value must be in the range (0, 1.0). Higher values increase randomness; lower values decrease it. The default is 0.8. Do not set this value to 1.0 or greater.

0.8

parameters.top_k

float

No

The number of top-scoring tokens to consider for sampling. For example, a value of 50 restricts sampling to the 50 highest-scoring tokens. Higher values increase randomness; lower values increase determinism. Note: If this parameter is omitted or set to a value greater than 100, top-k sampling is disabled, and only top-p sampling applies. The default is null (disabled).

50

Response parameters

Parameter

Type

Output format

Description

Example

output.text

string

Returned when result_format is text.

The content generated by the model.

output.finish_reason

string

The reason the model stopped generating tokens. null indicates that generation is in progress. stop indicates that the model reached a stop sequence. length indicates that the output reached the maximum token limit.

stop

output.choices[list]

list

Returned when result_format is message.

A list of generated choices.

{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"[Civil Statement of Claim]\n\nPlaintiff: XXX, male/female, born on YYYY-MM-DD, XXX ethnicity, residing at XXX Road, XXX City, contact number: XXX.\nAuthorized Agent: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on YYYY-MM-DD, XXX ethnicity, residing at XXX Road, XXX City, contact number: XXX.\n\nClaims:\n1. An order for the defendant to repay the debt of 10,000 yuan;\n2. An order for the defendant to bear all court costs for this case.\n\nFacts and Reasons:\nMy brother, XXX, due to financial difficulties, borrowed 10,000 yuan from me on YYYY-MM-DD and wrote an IOU, promising to repay by YYYY-MM-DD. However, after the loan matured, my brother did not repay as agreed. Despite my repeated requests, he has consistently made excuses.\n\nList of Evidence:\n1. One IOU;\n2. Several records of payment demands.\n\nTo:\n\nXXX People's Court\n\nPlaintiff: (Plaintiff's Signature)\n\nYYYY-MM-DD\n\nAttachments: 1. XXX copies of this statement of claim.\n 2. Table of contents for evidence."}}]}

output.choices[x].finish_reason

string

The reason generation stopped.

  • null: Generation is in progress.

  • stop: The model reached a stop sequence.

  • length: The output reached its maximum length.

output.choices[x].message

string

A message object with a role and content. The role can be system, user, or assistant. content contains the text generated by the model.

output.choices[x].message.role

string

output.choices[x].message.content

string

usage.output_tokens

integer

General

The number of tokens in the generated output.

236

usage.input_tokens

integer

The number of tokens in the input.

56

usage.total_tokens

integer

The total number of tokens used in the request (input + output).

292

request_id

string

A unique identifier for the request.

2ae6671b-9373-9c7d-a407-af2029b51659

Example request (streaming disabled)

This example shows a cURL command to call the Tongyi FaRui model with streaming disabled.

curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
--data '{
    "model": "farui-plus",
    "input": {
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "My brother owes me 10,000 yuan. Generate a statement of claim for me."
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Response example (SSE disabled)

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "[Civil Complaint]\n\nPlaintiff: XXX, male/female, born on YYYY-MM-DD, XXX ethnicity, residing at XXX Road, XXX City, Contact Number: XXX.\nCounsel: XXX, (Law Firm Name).\n\nDefendant: XXX, male/female, born on YYYY-MM-DD, XXX ethnicity, residing at XXX Road, XXX City, Contact Number: XXX.\n\nClaims:\n1. An order compelling the defendant to repay the CNY 10,000 loan;\n2. An order compelling the defendant to bear all costs of this action.\n\nFacts and Grounds:\nOn YYYY-MM-DD, the defendant, XXX, who is the plaintiff's brother, borrowed CNY 10,000 from the plaintiff due to financial difficulties. He signed an IOU promising repayment by YYYY-MM-DD. However, after the loan became due, the defendant did not repay as agreed. Despite the plaintiff's repeated requests for payment, the defendant has consistently offered excuses and failed to repay the loan.\n\nExhibits:\n1. IOU;\n2. Records of payment demands.\n\nPlaintiff: (Plaintiff's Signature)\n\nYYYY-MM-DD\n\nAttachments: 1. XXX copies of this complaint.\n  2. List of Exhibits."
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 292,
        "output_tokens": 236,
        "input_tokens": 56
    },
    "request_id": "2ae6671b-9373-9c7d-a407-af2029b51659"
}

Request example (SSE enabled)

The following cURL command calls the Tongyi Farui model with SSE enabled.

curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--header 'X-DashScope-SSE: enable' \
--data '{
    "model": "farui-plus",
    "input": {
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "My brother owes me CNY 10,000. Generate a statement of claim for me."
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Response example (SSE enabled)

id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"[Civil Complaint","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":59,"input_tokens":56,"output_tokens":3},"request_id":"a074989b-d320-908a-9f87-fd597426933f"}

id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"[Civil Complaint]\n\nPlaintiff: XXX, Male/Female,","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":69,"input_tokens":56,"output_tokens":13},"request_id":"a074989b-d320-908a-9f87-fd597426933f"}

... ... ... ...
... ... ... ...
id:27
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"[Civil Complaint]\n\nPlaintiff: XXX, Male/Female, born on YYYY-MM-DD, Ethnicity: XXX, residing at XXX XXX Road, XXX City, Contact: XXX.\nAuthorized Litigation Representative: XXX, (Law Firm Name).\n\nDefendant: XXX, Male/Female, born on YYYY-MM-DD, Ethnicity: XXX, residing at XXX XXX Road, XXX City, Contact: XXX.\n\nClaims:\n1. Order the defendant to repay the debt of CNY 10,000.\n2. Order the defendant to bear the costs of this suit.\n\nFacts and Reasons:\nDue to financial hardship, my brother, XXX, borrowed CNY 10,000 from me on YYYY-MM-DD. He issued an IOU, promising to repay the amount in full by YYYY-MM-DD. However, after the loan became due, my brother failed to repay it as agreed. Despite my repeated requests for payment, he has consistently made excuses to delay repayment.\n\nList of Evidence:\n1. One (1) IOU.\n2. Records of payment requests.\n\nRespectfully submitted,\n\nPlaintiff: (Signature)\n\nYYYY-MM-DD\n\nAttachments:\n1. XXX copies of this complaint.\n2. List of evidence.","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":292,"input_tokens":56,"output_tokens":236},"request_id":"a074989b-d320-908a-9f87-fd597426933f"}

id:28
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"[Civil Complaint]\n\nPlaintiff: XXX, Male/Female, born on YYYY-MM-DD, Ethnicity: XXX, residing at XXX XXX Road, XXX City, Contact: XXX.\nAuthorized Litigation Representative: XXX, (Law Firm Name).\n\nDefendant: XXX, Male/Female, born on YYYY-MM-DD, Ethnicity: XXX, residing at XXX XXX Road, XXX City, Contact: XXX.\n\nClaims:\n1. Order the defendant to repay the debt of CNY 10,000.\n2. Order the defendant to bear the costs of this suit.\n\nFacts and Reasons:\nDue to financial hardship, my brother, XXX, borrowed CNY 10,000 from me on YYYY-MM-DD. He issued an IOU, promising to repay the amount in full by YYYY-MM-DD. However, after the loan became due, my brother failed to repay it as agreed. Despite my repeated requests for payment, he has consistently made excuses to delay repayment.\n\nList of Evidence:\n1. One (1) IOU.\n2. Records of payment requests.\n\nRespectfully submitted,\n\nPlaintiff: (Signature)\n\nYYYY-MM-DD\n\nAttachments:\n1. XXX copies of this complaint.\n2. List of evidence.","role":"assistant"},"finish_reason":"stop"}]},"usage":{"total_tokens":292,"input_tokens":56,"output_tokens":236},"request_id":"a074989b-d320-908a-9f87-fd597426933f"}

Error response

If an access request fails, the response includes a code and message to explain the error.

{
    "code":"InvalidApiKey",
    "message":"Invalid API-key provided.",
    "request_id":"fb53c4ec-1c12-4fc4-a580-cdb7c3261fc1"
}
The provided cn_doc contains a JSON code block showing an API error response.
The items "code", "message", "request_id", and "InvalidApiKey" are fields and values in an API response. These are classified as code or log examples, not user-facing UI elements (e.g., buttons, menus, tabs).
Consequently, the provided document contains no standard UI terms.
To generate a bilingual terminology list, please provide a document that includes console procedures or UI element names.

Status codes

For details about common Model Studio status codes, see error codes.