Code example (Python)

Updated at:

This topic describes how to use Python to access the Table Q&A service.

Make a common request

Common Requests Document

import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

# Create an AcsClient instance.
client = AcsClient(
    "<your-access-id-key>",
    "<your-access-id-secret>",
    "cn-hangzhou"
)

# Create a request to query service information and set its parameters.
service_request = CommonRequest()

# The domain and version are static fields.
service_request.set_domain('alinlp.cn-hangzhou.aliyuncs.com')
service_request.set_version('2020-06-29')

# The action to query service information: GetTableQAServiceInfoById.
service_request.set_action_name('GetTableQAServiceInfoById')

# For the parameters that you need to add, see the API documentation.
service_request.add_query_param('ServiceCode', 'alinlp')
# Service ID: The service ID displayed in the list on the console.
service_request.add_query_param('ServiceId', '95')
service_response = client.do_action_with_exception(service_request)
service_resp_obj = json.loads(service_response)
# Parse the latest bot_id that is attached to the service.
bot_id = json.loads(service_resp_obj['Data'])['data']['botId']
print(bot_id)

# Create a Table Q&A request and set its parameters.
tableqa_request = CommonRequest()
# The domain and version are static fields.
tableqa_request.set_domain('alinlp.cn-hangzhou.aliyuncs.com')
tableqa_request.set_version('2020-06-29')
tableqa_request.set_action_name('RequestTableQAOnline')
# For the parameters that you need to add, see the API documentation.
tableqa_request.add_query_param('ServiceCode', 'alinlp')
# For the bot_id parameter, use the bot_id that you queried in the previous step.
params = {
    "bot_id": bot_id,
    "question": "I want to see yesterday's fund quota",
}
tableqa_request.add_query_param('Params', json.dumps(params))
response = client.do_action_with_exception(tableqa_request)
resp_obj = json.loads(response)
print(resp_obj)

Note: The bot_id parameter is required for the RequestTableQAOnline operation. Before you use the Table Q&A service, call the GetTableQAServiceInfoById operation to retrieve the latest bot_id that is attached to the service.

Troubleshoot call exceptions

If an exception occurs during a call, see Troubleshoot call exceptions (error code summary) to find the description that matches your issue. The description provides the cause of the error and a solution.