3. How to Use AI API in Development Projects

The main function of the Agent API is to provide an interface that allows developers to interact with various features of Agent through a program. It also allows developers to deploy the completed Agent to the required platform, app, or program through the program.

Obtain the Required Information

First, to use the API, you need to test run, save, and publish Agent. After publishing, you can get the API Key (hereinafter referred to as AK) and API Secret (hereinafter referred to as AS) from the API option in the upper right corner of the following interface. You can get the Base URL and Agent ID from the API Document. Image Image

Using the API

Understanding Methods

Before using the API, we need to understand several methods used when using openAPI:

  • Get method, used to retrieve resources, such as getting user information, device status, etc.
  • Post method, used to send data to the server to update resources on the server.
  • Put method, used to update existing resources.
  • Delete method, used to delete specific resources.

Obtain and Connect to the API

After understanding the methods, we can input the AK, AS, Agent ID, etc., obtained earlier into the program.

ENDPOINT = "https://openapi.tuyacn.com"
AK = "your API Key"
SK = "your API Secret"

agent_id = 'your agent id'

run_path = f"/v1.0/ai-agent/agent/run/{agent_id}"

In actual use, you may need to configure the ENDPOINT according to the actual API server address. The API endpoints of Tuya Cloud usually vary by region. Here are some common Tuya Cloud API endpoints:

China Region: https://openapi.tuyacn.com US Region: https://openapi.tuyaus.com Europe Region: https://openapi.tuyaeu.com India Region: https://openapi.tuyain.com

Users need the Endpoint, AK, and SK to use the API.

api = openapi.TuyaOpenAPI(ENDPOINT, AK, SK)
token_resp = api.connect()

Obtain Task ID

Now we can get the task ID through a post request.

resp = api.post(run_path, [
    {
        "name": "aaa",
        "type": "string",
        "value": "bbb"
    },
])
print(resp)

By printing the return value of the Post, we can find the position of the task ID and extract it separately.

Reference return value:

{'client_id': 'mf4r9n77t4vwfuf53qws54yq5', 'sign': '0CB35B3F3E2D2F0495A2F1D79A57A827B6B7605E4FAE536084E9E642F7990914', 'sign_method': 'HMAC-SHA256', 't': '1726121829597', 'lang': 'en'}
{'client_id': 'mf4r9n77t4vwfuf53qws54yq5', 'sign': 'DF3768304545214EDAAEF410C206D9C8F4B7C668742C64066388918A97691CC4', 'sign_method': 'HMAC-SHA256', 't': '1726121829685', 'lang': 'en', 'access_token': 'cn_35ec01bc95ccde1c9ec98563c9b9d310'}
{'result': {'is_test': False, 'task': '65605f75-889b-4a9d-84e0-7cd60a7b6cb6', 'workflow': 'f31d90c2-d77c-44eb-bef4-ca4ea3bba34b'}, 'success': True, 't': 1726121830018

Extract the task ID using the following command:

task_id = resp['result']['task']
get_ret_path = f"/v1.0/ai-agent/agent/result/{agent_id}/{task_id}"

Get Status for Testing

Using the get command, we can obtain the connection status and know whether the connection is successful. The complete code is as follows:

ENDPOINT = "http://10.202.244.29:8080" # Base URL
AK = "your API Key"
SK = "your API Secret"

agent_id = 'your agent id'

run_path = f"/v1.0/ai-agent/agent/run/{agent_id}"
api = openapi.TuyaOpenAPI(ENDPOINT, AK, SK)
token_resp = api.connect()

resp = api.post(run_path, [
    {
        "name": "aaa",
        "type": "string",
        "value": "bbb"
    },
])
print(resp)
task_id = resp['result']['task']
get_ret_path = f"/v1.0/ai-agent/agent/result/{agent_id}/{task_id}"

while True:
    ret_resp = api.get(get_ret_path)
    print(ret_resp)
    if ret_resp['result']['status'] == 'FAILURE' or ret_resp['result']['status'] == 'SUCCESS':
        break

    print('wait for 1s')

    time.sleep(1)

We can check the success or failure in the status of the Result return value.

Precautions

  • Security: Securely store and transmit the base URL and Agent ID.
  • Access Interval: Carefully set the interval for obtaining the result endpoint to avoid excessive requests. Adjust according to the expected completion time of the task.

results matching ""

    No results matching ""