forked from THUDM/CodeGeeX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JAVA version CodeGeeX API example.
- Loading branch information
Showing
5 changed files
with
256 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cn.aminer</groupId> | ||
<artifactId>codegeex-api-example-java</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<!-- | ||
如果没有下面的这段encoding配置,会导致编译的时候输出WARNING信息: | ||
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! | ||
--> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用 | ||
低版本jdk中不支持的语法),会存在target不同于source的情况 --> | ||
<source>1.8</source><!-- 源代码使用的JDK版本 --> | ||
<target>1.8</target><!-- 需要生成的目标class文件的编译版本 --> | ||
<encoding>UTF-8</encoding><!-- 字符集编码 --> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>3.3.0</version> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
<!-- 有下面这段executions,才能打出包含所有dependency的fat jar --> | ||
<executions> | ||
<execution> | ||
<phase>package</phase><!-- 指定在打包节点执行jar包合并操作 --> | ||
<goals> | ||
<goal>single</goal><!-- 该模块只运行一次 --> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-parameter-names</artifactId> | ||
<version>2.6.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jdk8</artifactId> | ||
<version>2.6.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
<version>2.6.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-parameter-names</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jdk8</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>4.10.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>1.7.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.20</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<repositories> | ||
<repository> | ||
<!-- Maven 自带的中央仓库使用的id为central,如果其他的仓库声明也是用该id,就会覆盖中央仓库的配置 --> | ||
<id>central</id> | ||
<name>ALiYun</name> | ||
<url>http://maven.aliyun.com/nexus/content/groups/public</url> | ||
</repository> | ||
</repositories> | ||
</project> |
83 changes: 83 additions & 0 deletions
83
...geex-api-example-java/src/main/java/cn/aminer/codegeex/example/CodeGenerationExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
api/codegeex-api-example-java/src/main/java/cn/aminer/codegeex/example/pojo/Payload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; // 编程语言 | ||
} |
File renamed without changes.