diff --git a/api/README_zh.md b/api/README_zh.md index 5414654..559d568 100644 --- a/api/README_zh.md +++ b/api/README_zh.md @@ -16,42 +16,4 @@ 在API信息中,可以查看代码生成/代码翻译的请求地址和使用文档: -根据文档中的描述使用API,参考文件``api/generation_example.py``: - -```python -# 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() -``` - +根据文档中的描述使用API,Python版参考目录``api/codegeex-api-example-python``;JAVA版参考工程:``api/codegeex-api-example-java`` diff --git a/api/codegeex-api-example-java/pom.xml b/api/codegeex-api-example-java/pom.xml new file mode 100644 index 0000000..03d01d8 --- /dev/null +++ b/api/codegeex-api-example-java/pom.xml @@ -0,0 +1,141 @@ + + + 4.0.0 + + cn.aminer + codegeex-api-example-java + 1.0-SNAPSHOT + + + + UTF-8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + 1.8 + 1.8 + UTF-8 + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + jar-with-dependencies + + + + + + package + + single + + + + + + + + + + com.fasterxml.jackson.module + jackson-module-parameter-names + 2.6.6 + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + 2.6.6 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.6.6 + + + com.squareup.okhttp3 + okhttp + + + org.slf4j + slf4j-log4j12 + + + log4j + log4j + + + org.projectlombok + lombok + provided + + + + + + + com.fasterxml.jackson.module + jackson-module-parameter-names + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + com.fasterxml.jackson.core + jackson-databind + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + org.projectlombok + lombok + 1.18.20 + provided + + + + + + + + central + ALiYun + http://maven.aliyun.com/nexus/content/groups/public + + + diff --git a/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/CodeGenerationExample.java b/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/CodeGenerationExample.java new file mode 100644 index 0000000..8a25d7a --- /dev/null +++ b/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/CodeGenerationExample.java @@ -0,0 +1,83 @@ +package cn.aminer.codegeex.example; + +import cn.aminer.codegeex.example.pojo.Payload; +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.*; + +import java.io.IOException; + +/** + * 调用 CodeGeeX API 生成代码的例子。 + * + * @author Darran Zhang @ codelast.com + * @version 2023-01-20 + */ +public class CodeGenerationExample { + public static final String API_KEY = "your_api_key"; // 在"天启开放平台"上申请到的API Key + public static final String API_SECRET = "your_api_secret"; // 在"天启开放平台"上申请到的API Secret + public static final int NUMBER = 3; // 生成几个候选 + public static final String LANGUAGE = "Java"; // 编程语言 + public static final String REQUEST_URL = "https://tianqi.aminer.cn/api/v2/multilingual_code_generate"; // 请求地址 + + public static void main(String[] args) throws Exception { + CodeGenerationExample example = new CodeGenerationExample(); + String prompt = "// use OkHttpClient library to write a function to perform http post request\n\n" + + "public class HttpPost {\n" + + " public static void main(String[] args) {\n"; + example.generateCode(prompt); + } + + /** + * 生成代码。 + * + * @param prompt 待补全的代码 + */ + public void generateCode(String prompt) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + Payload payload = new Payload().setApiKey(API_KEY).setApiSecret(API_SECRET).setPrompt(prompt).setNumber(NUMBER) + .setLanguage(LANGUAGE); + String response = performHttpPost(REQUEST_URL, objectMapper.writeValueAsString(payload)); + System.out.println(response); + } + + /** + * 发起 HTTP POST 请求。 + * + * @param url 请求的URL + * @param payload 请求的JSON数据 + * @return 请求返回的内容,若出错则返回 null。 + */ + public String performHttpPost(String url, String payload) { + HttpUrl.Builder builder = null; + try { + HttpUrl httpUrl = HttpUrl.parse(url); + if (httpUrl != null) { + builder = httpUrl.newBuilder(); + } + } catch (IllegalArgumentException e) { + System.out.println("failed to create HttpUrl.Builder from url " + url + ":" + e); + } + if (builder == null) { + return null; + } + OkHttpClient client = new OkHttpClient(); + RequestBody requestBody = RequestBody.create(payload, MediaType.parse("application/json; charset=utf-8")); + Request request = new Request.Builder() + .url(builder.build()) + .post(requestBody) + .build(); + + try { + Response response = client.newCall(request).execute(); + ResponseBody body = response.body(); + if (body == null) { + System.out.println("null response body"); + return null; + } + return body.string(); + } catch (IOException e) { + System.out.println("failed to send POST request: " + e); + } + return null; + } +} diff --git a/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/pojo/Payload.java b/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/pojo/Payload.java new file mode 100644 index 0000000..68f088a --- /dev/null +++ b/api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/pojo/Payload.java @@ -0,0 +1,31 @@ +package cn.aminer.codegeex.example.pojo; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 发送到 CodeGeex API 的请求中包含的JSON payload对象。 + * + * @author Darran Zhang @ codelast.com + * @version 2023-01-20 + */ +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Accessors(chain = true) +public class Payload { + @JsonProperty("apikey") + String apiKey; // 在"天启开放平台"上申请到的API Key + + @JsonProperty("apisecret") + String apiSecret; // 在"天启开放平台"上申请到的API Secret + + String prompt; // 待补全的代码 + + @JsonProperty("n") + int number; // 生成几个候选 + + @JsonProperty("lang") + String language; // 编程语言 +} diff --git a/api/generation_example.py b/api/codegeex-api-example-python/generation_example.py similarity index 100% rename from api/generation_example.py rename to api/codegeex-api-example-python/generation_example.py