mirror of https://github.com/THUDM/CodeGeeX.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.8 KiB
1.8 KiB
创建CodeGeeX API
使用天启 · API开放平台申请CodeGeeX API:

点击首页中的天启平台体验入口:
点击API应用:
输入任意名称,创建API应用。创建后会得到API Key/Secret,用于调用API:
在API信息中,可以查看代码生成/代码翻译的请求地址和使用文档:
根据文档中的描述使用API,参考文件api/generation_example.py
:
# encoding:utf-8
import requests
import json
'''
Code Generation
'''
API_KEY = "" # Get from Tianqi console. 从控制台获取
API_SECRET = "" # Get from Tianqi console. 从控制台获取
PROMPT = "from typing import List\n\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n"
NUMBER = 3
LANG = "Python"
request_url = "https://tianqi.aminer.cn/api/v2/"
api = 'multilingual_code_generate'
# Request is in json format. 指定请求参数格式为json
headers = {'Content-Type': 'application/json'}
request_url = request_url + api
data = {
"apikey": API_KEY,
"apisecret": API_SECRET,
"prompt":PROMPT,
"n":NUMBER,
"lang":LANG
}
def main():
response = requests.post(request_url, headers=headers, data=json.dumps(data))
if response:
print(response.json())
if __name__ == '__main__':
main()