文档

快速开始

更新时间:
一键部署

本文主要介绍如何使用API调用阿里云百炼的RAG检索增强应用,即从应用中心中创建的RAG检索增强应用。

快速开始

前提条件

示例代码

以下示例展示了调用RAG检索增强应用进行企业知识库问答的代码。

说明

需要使用您的API-KEY替换示例中的YOUR_API_KEY,并将APP-ID替换示例中的YOUR_APP_ID,代码才能正常运行。

python sdk version: dashscope>=1.10.0

java sdk version: >=2.5.0

设置API-KEY

export DASHSCOPE_API_KEY=YOUR_API_KEY

RAG调用示例

from http import HTTPStatus
from dashscope import Application


def rag_call():
    response = Application.call(app_id='YOUR_APP_ID',
                                prompt='API接口说明中, TopP参数改如何传递?',
                                )

    if response.status_code != HTTPStatus.OK:
        print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
    else:
        print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))


if __name__ == '__main__':
    rag_call()
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.List;


public class Main{
      public static void ragCall()
            throws ApiException, NoApiKeyException, InputRequiredException {
        RagApplicationParam param = RagApplicationParam.builder()
                .appId("YOUR_APP_ID")
                .prompt("API接口说明中, TopP参数改如何传递?")
                .build();

        Application application = new Application();
        ApplicationResult result = application.call(param);

        System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
                result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());

        if (result.getUsage() != null && result.getUsage().getModels() != null) {
            for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
                System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
                        usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
            }
        }
    }

    public static void main(String[] args) {
        try {
            ragCall();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.printf("Exception: %s", e.getMessage());
        }
        System.exit(0);
    }  
}

RAG调用结果示例

request_id=131c75de-b53d-9787-a301-1a841dc73eda
 output={"text": "在API接口说明中,TopP参数是一个可选的请求参数,用于控制生成过程中 nucleus 采样方法的概率阈值。当调用API时,您可以按照JSON格式将此参数放在请求体(Request Body)中进行传递,其具体格式如下:\n\n```json\n{\n  \"TopP\": <float_value>\n}\n```\n\n其中 `<float_value>` 应替换为实际要设置的浮点数数值,取值范围通常在0到1之间。这个参数的作用是,在模型生成文本时,只保留概率加起来大于等于TopP阈值的最可能token集合作为候选集,从而影响生成结果的随机性和多样性。取值越低,生成的文本确定性越高;取值越大,生成的随机性则会增加。", "finish_reason": "stop", "session_id": "25d6b00b574a4740b6e8a97500b556d5", "thoughts": null, "doc_references": null}
 usage={"models": [{"model_id": "qwen-max", "input_tokens": 1814, "output_tokens": 165}]}

了解更多

有关RAG检索增强应用API的详细调用文档可前往API详情页面进行了解。

  • 本页导读 (1)
文档反馈