X

Tencent Python Package Contributing General Guidelines

In this blog, we will give you a brief introduction of how to contribute to the "tencent" python package third party APIs and help developers integrate future tencent product and services API easier, such as backend for "Wechat public account Automatic Reply". The guidelines are also listed and any violation of the guidelines will not be accepted in the MR request of Github.

1. Introduction to python "tencent" package

1.1 Install

Users can install the tencent python package by running the scripts "pip install tencent", and once the tencent logo or CEO grayscale picture is displayed, the package is successfully installed.

    pip install tencent

1.2 Usage

You can register your API and in the package and access your API through tencent.api() method. And you can also pass the positional arguments *args and key-values arguments **kwargs to the APIs as def api (api_name, *args, **kwargs):

    import tencent

    ## result = tencent.api("{your_api_name}")

    ## pass in api name
    result1 = tencent.api("api_base")

    ## positional args
    result_dict = tencent.api("api_base", param1, param2, param3="value3", param4="value4")


    ## positional args and keyvalue args kwargs

    result_dict = tencent.api("api_base", param1, param2, param3="value3", param4="value4")

You can also pass the positional arguments *args and key-values arguments **kwargs to the APIs

1.3 Contribute

You can create Merge Request on the Github repo (https://github.com/AI-Hub-Admin/tencent) and the repo admin will examine the safety and relevance of the contributed APIs to the package and retain the rights to decide if it's relevant to the purpose of this public repos.

2. Guidelines for Developing third party APIs

You can create Merge Request on the Github repo (https://github.com/AI-Hub-Admin/tencent) and the repo admin will examine the safety and relevance of the contributed APIs and have the rights to decide it's relevant to the purpose of this public repos.

2.1 Contributed API Should extend the BaseAPI Class

We will use a simple API function (DemoAPI class) as an example to illustrate how to contribute the APIs. Suppose your API name is "api_demo" and you implement your API class as "DemoAPI", which should extends the base class BaseAPI.

Input arguments include: "args", which is tuple of args, and "kwargs" which is key-value argument dict The function returns a dict type result.


class BaseAPI(object):
    """docstring for ClassName"""
    def __init__(self, configs):
        self.configs = configs
        self.name = API_NAME_BASE


    def api(self, args, kwargs):
        """
        """


class DemoAPI(BaseAPI):

    """docstring for ClassName"""
    def __init__(self, configs):
        super(DemoAPI, self).__init__(configs)
        self.name = API_NAME_DEMO

    def api(self, args, kwargs):
        """
            Args:
                args: tuple of args,  (input)
                kwargs: key value dict
            Return:
                res_dict: dict, multi-modal text text, image, audio and video
        """
        api_input = ""
        if len(args) > 0:
            api_input = args[0]

        res_dict = {}
        res_dict["output"] = api_input
        return res_dict

2.2 Register Your API

In the file of path "tencent/src/tencent/__init__.py", you can import your method and register your APIs in a dict. The API class is imported from base module and you need to add one new line of code to register your API. DemoAPI.name is the unique identifier of all the APIs, which should not collide with other API names.


## register your DemoAPI class


from .base import *

SUPPORTED_APIS[DemoAPI(None).name] = {KEY_IMPL: DemoAPI}


2.3 Usage of Your APIs



import tencent

your_api_name="xxx"
res = tencent.api(your_api_name)

2.4 UnitTest

In the folder "tencent/tests", you can add your unit test to see if it's imported correctly. See "/tencent/tests/*.py" for more examples. You can visit github repo (https://github.com/AI-Hub-Admin/tencent/tree/main/tests)

Related

Comments

Write Your Comment

Upload Pictures and Videos