OpenAI は、Java を通じて呼び出すことができる REST API を提供します。Java の HttpClient クラスを使用して HTTP リクエストを送信し、OpenAI API を呼び出すことができます。具体的な手順は次のとおりです。
1. HTTP リクエストを送信するための HttpClient オブジェクトを作成します。
2. HttpPost オブジェクトを構築し、OpenAI API の URL を指定します。
3. リクエストパラメータを HttpPost オブジェクトに追加します。
4. HttpClient を使用してリクエストを送信し、応答を取得します。
5. 応答から必要な結果を抽出します。
ポム:
<?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>org.example</groupId>
<artifactId>OpenAi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>adapter-rxjava2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>
public class OpenAI {
public static void main(String[] args){
OpenAiService service = new OpenAiService("你的KEY");
CompletionRequest completionRequest = CompletionRequest.builder()
.prompt("女朋友生气了怎么办")
.model("text-davinci-002")
.maxTokens(1000)
.temperature(0.2)
.build();
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
}
}
また、 github.com/liuhenghui/openai-javaから取得した上記 2 つの jar を導入する必要があります。
ChatGPT国内体験アドレスhttps://www.1bit.asia