
# 单轮问答（即将下线）
#### 功能介绍
问答服务的输入为用户提问，输出是与输入最匹配的Top N(默认为top5)个知识点，知识点按得分从高到低排序。该接口即将下线，请优先使用[开启会话](https://support.huaweicloud.com/api-cbs/cbs_03_0112.html)、[处理会话](https://support.huaweicloud.com/api-cbs/cbs_03_0113.html)、[关闭会话](https://support.huaweicloud.com/api-cbs/cbs_03_0114.html)接口。
![](https://support.huaweicloud.com/api-cbs/public_sys-resources/note_3.0-zh-cn.png)
返回知识点如果含有答案字段（answer），则表示返回匹配成功结果，如果没有答案字段，则表示推荐结果。
#### URI
POST https://{endpoint}/v1/{project_id}/qabots/{qabot_id}/requests
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                               |
|:---|:---|:---|:---|
| endpoint   | 是    | String | 终端节点，即调用API的请求地址。 不同服务不同区域的endpoint不同，您可以从[终端节点](https://support.huaweicloud.com/api-cbs/cbs_03_0102.html)中获取。 例如，CBS服务在"华北-北京四"区域的"endpoint"为"cbs-ext.cn-north-4.myhuaweicloud.com"。 |
| project_id | 是    | String | 项目ID，用于资源隔离。请参见[获取项目ID](https://support.huaweicloud.com/api-cbs/cbs_03_0069.html)。                                                                                                                                                               |
| qabot_id   | 是    | String | qabot编号，UUID格式，如：303a0a00-c88a-43e3-aa2f-d5b8b9832b02。 进入问答机器人的Console界面，在"机器人名称/ID"列显示对应的qabot_id。                                                                                                                 |
   
#### 请求参数
表2请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                     |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | 用户Token。 用于获取操作API的权限。[获取Token接口](https://support.huaweicloud.com/api-cbs/cbs_03_0006.html)响应消息头中X-Subject-Token的值即为Token。 |
| Content-Type | 是    | String | 消息体的类型（格式），参数值为"application/json"。                                                                                                                     |
   
表3请求Body参数 
| 参数               | 是否必选 | 参数类型                                              | 说明                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|:---|
| session_id       | 是    | String                                            | 会话ID，由任意字符串组成，由用户自定义产生，用来区分不同的会话。 具体获取方式请参见[开启会话](https://support.huaweicloud.com/api-cbs/cbs_03_0112.html)章节。                                                                                                                                                                                                                                                               |
| user_id          | 否    | String                                            | 用户ID，用来区分唯一用户信息。                                                                                                                                                                                                                                                                                                                                                                                             |
| question         | 是    | String                                            | 用户问题。                                                                                                                                                                                                                                                                                                                                                                                                        |
| extends          | 否    | [Extends] object | 扩展参数项。                                                                                                                                                                                                                                                                                                                                                                                                       |
| threshold_enable | 否    | Boolean                                           | true：启动内部阈值，返回经过阈值处理之后的答案。 false：不启用内部阈值，返回原答案。                                                                                                                                                                                                                                                                                                                                |
| chat_enable      | 否    | Boolean                                           | 默认true true：使用内部闲聊语料进行兜底。 false：不使用闲聊兜底。                                                                                                                                                                                                                                                                                                         |
| operate_type     | 否    | Integer                                           | 调用接口时候传入，用以标记的问答的行为，默认为0，最终会展示在问答日志里。 - 0 用户输入  - 1 单击热点问题  - 3 单击推荐问题  - 4 单击问题提示   |
   
 表4Extends 
| 参数      | 是否必选 | 参数类型             | 描述                                                                                                    |
|:---|:---|:---|:---|
| domains | 否    | Array of strings | 领域列表，多个领域用分号隔开。如果设置了领域且领域不为空，就从这些领域中匹配答案，否则就从该用户的全部知识库匹配答案。 当前最多支持10个领域。 |
| top     | 否    | Integer          | 返回答案数量，默认为5，取值范围1\~10。                                                                                |
   
#### 响应消息
**状态码： 200**
表5响应Body参数 
| 参数         | 参数类型                                                         | 描述                                               |
|:---|:---|:---|
| request_id | String                                                       | 请求ID。 调用失败时无此字段。 |
| question   | String                                                       | 问题。 调用失败时无此字段。     |
| answers    | Array of [Answers] objects | 最相似的问题集。调用失败时无此字段。                               |
| extends    | [Extends] object         | 扩展参数项。调用失败时无此字段。                                 |
   
 表6Answers 
| 参数           | 参数类型                                                         | 描述               |
|:---|:---|:---|
| qa_pair_id   | String                                                       | 问答对ID。           |
| st_question  | String                                                       | 标准问题。            |
| ex_questions | Array of [ExQuestions] objects | 扩展问。             |
| answer       | String                                                       | 答案。              |
| score        | Float                                                        | 相似度得分，精确到小数点后3位。 |
| domain       | String                                                       | 问题类别。            |
   
 表7ExQuestions 
| 参数      | 参数类型   | 描述    |
|:---|:---|:---|
| content | String | 扩展问题。 |
   
 表8Extends 
| 参数      | 是否必选 | 参数类型             | 描述                                                                                                       |
|:---|:---|:---|:---|
| domains | 否    | Array of strings | 领域列表，多个领域用分号隔开。如果设置了领域且领域不为空，就从这些领域中匹配答案，否则就从该用户的全部知识库匹配答案。 当前最多支持10个领域。 |
| top     | 否    | Integer          | 返回答案数量，默认为5，取值范围1\~10。                                                                                   |
   
**状态码： 400**
表9响应Body参数 
| 参数         | 参数类型   | 描述                     |
|:---|:---|:---|
| error_code | String | 调用失败时的错误码。 调用成功时无此字段。  |
| error_msg  | String | 调用失败时的错误信息。 调用成功时无此字段。 |
   
#### 请求示例
输入问题"桌面云无法登录"，返回最匹配的top5个知识点
```
POST https://{endpoint}/v1/1604d8170cd84440a81281e5a3a04f8b/qabots/303a0a00-c88a-43e3-aa2f-d5b8b9832b02/requests
Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{
    "user_id": "z00123456",
    "session_id": "xxxxxxxxxxxxxxxxxx",
    "operate_type":3,      
    "question": "桌面云无法登录",
    "extends":{
"domains":["桌面云"],
"top":5
    },
    "threshold_enable":false,
    "chat_enable":true
}
```
#### 响应示例
**状态码：200**
成功响应示例
- 关闭内置阈值处理（设置threshold_enable = false），按top个数返回结果数量。
  ```
  { 
      "request_id": "303a0a00-c88a",
      "question": "桌面云打不开",
      "answers": [
          {
              "qa_pair_id":"123",
              "st_question":"桌面云打开不了",
              "answer":"用户账号不对",
              "score":0.952,
              "domain":"桌面云"
       },
       {
              "qa_pair_id":"123",
              "st_question":"桌面云打开不了了",
              "answer":"用户账号不对",        
              "score":0.901,
              "domain":"桌面云"
          },
          {
              "qa_pair_id":"123",
              "st_question":"桌面云是啥",
              "answer":"桌面云是可以远程办公的系统",      
              "score":0.352,
              "domain":"桌面云"
          },
       {
              "qa_pair_id":"123",
              "st_question":"电脑桌面打不开",
              "answer":"请检查下显示器是否开启",    
              "score":0.352,
              "domain":"桌面云"
          },
      ]
  }
  ```
  
- 开启内部阈值处理
  表10内部阈值处理 
  | 处理条件           | 结果                                                  | 备注                                                    |
  |:---|:---|:---|
  | 分值\>0.85       | 返回分值与最高分差距在0.05范围内所有命中语料。                           | 如果QaPairanswers对象的answer不为空，则为命中语料，如果answer为空，则为推荐语料。 |
  | 0.70\<分值\<0.85 | 如果存在这个区间的语料，返回最高分值处于这个区间的命中语料。如果不存在这个区间的语料，则返回推荐语料。 | 如果QaPairanswers对象的answer不为空，则为命中语料，如果answer为空，则为推荐语料。 |
  | 0.55\<分值\<0.70 | 返回推荐语料。                                             | 如果QaPairanswers对象的answer不为空，则为命中语料，如果answer为空，则为推荐语料。 |
  | 分值\<0.55       | 不返回语料。                                              | 如果QaPairanswers对象的answer不为空，则为命中语料，如果answer为空，则为推荐语料。 |
     
  - 示例1
    ```
    { 
        "request_id": "303a0a00-c88a",
        "question": "桌面云打不开",
        "answers": [
            {
                "qa_pair_id":"123",
                "st_question":"桌面云打开不了",
                "ex_questions": [
                    {
                        "content": "桌面云"
                    }
                ],
                "answer":"用户账号不对",
                "top_score_question":"桌面云打开不了",
                "score":0.952,
                "domain":"桌面云"
            }，
          {
                "qa_pair_id":"123",
                "st_question":"桌面云打开",
                "ex_questions": [
                    {
                        "content": "桌面"
                    }
                ],
                "answer":"打开桌面云",
                "top_score_question":"桌面云打开",
                "score":0.912,
                "domain":"桌面云"
            }
        ]
    }
    ```
    
  
  - 示例2
    ```
    { 
        "request_id": "303a0a00-c88a",
        "question": "桌面云打不开",
        "answers": [
            {
                "qa_pair_id":"123",
                "st_question":"桌面云打开不了",
                "top_score_question":"桌面云打开不了",
                "answer":"用户账号不对",
                "score":0.752,
                "domain":"桌面云"
            }
        {
                "qa_pair_id":"123",
                "st_question":"桌面云打开不了了",     
                "score":0.70,
                "domain":"桌面云"
            },
            {
                "qa_pair_id":"123",
                "st_question":"桌面云是啥",    
                "score":0.54,
                "domain":"桌面云"
            }
        ]
    }
    ```
    
  
  
  
  - 示例3
    ```
    { 
        "request_id": "303a0a00-c88a",
        "question": "桌面云打不开",
        "answers": [
        {
                "qa_pair_id":"123",
                "st_question":"桌面云打开不了了",     
                "score":0.291,
                "domain":"桌面云"
            },
            {
                "qa_pair_id":"123",
                "st_question":"桌面云是啥",    
                "score":0.252,
                "domain":"桌面云"
            }
        ]
    }
    ```
    
   
- 闲聊兜底或无答案回复
  ```
  { 
      "request_id": "303a0a00-c88a",
      "question": "你好",
      "answers": [
      {
              "answer":"您好，请问有什么可以帮您的吗?"
          }
      ]
  }
  ```
  
**状态码：400**
失败响应示例
```
{
  "error_code": "CBS.0022",
  "error_msg": "session_id can not be null"
}
```
#### 状态码
状态码请参见[状态码](https://support.huaweicloud.com/api-cbs/cbs_03_0054.html)。
#### 错误码
错误码请参见[错误码](https://support.huaweicloud.com/api-cbs/cbs_03_0053.html)。
