地址:
https://tiger-sms.com/
我用支付宝充值了30多元(起充要30,加上手续费30多有点坑啊),买了一个印度尼西亚的号,美国的号用不了,提示虚拟手机号不允许注册。
3.用买到的手机号注册并获取短信验证码
4.登录OpenAI选择第一个开始聊天
代码写的挺不错的,可惜用的是gradle构建的,由于gradle本人几乎不会用,就没下载源码了。好在提供了maven依赖。
在maven仓库中可以查询到最新的jar包,引入项目即可
<!-- https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/service -->
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.12.0</version>
</dependency>
3.拷贝例子代码进行测试
由于缺少类等原因跑不起来,引入相关包
<!-- json数据解析工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
在国内无法直接调用OpenAI的接口,需要使用VPN代理访问。完整的代码测试代码如下:
package org.example.chatgpt;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theokanning.openai.OpenAiApi;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.chat.*;
import com.theokanning.openai.image.CreateImageRequest;
import com.theokanning.openai.service.OpenAiService;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
import static com.theokanning.openai.service.OpenAiService.*;
* 时间: 2023/6/5 17:07
public class Main {
public static void main(String[] args) {
String token = "sk-XXXXXXXXXXXX";
String proxyHost = "127.0.0.1";
int proxyPort = 7890;
//构建HTTP代理
Proxy proxy = null;
if (StrUtil.isNotBlank(proxyHost)) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
//构建HTTP客户端
OkHttpClient client = defaultClient(token, Duration.of(60, ChronoUnit.SECONDS))
.newBuilder()
.proxy(proxy)
.build();
ObjectMapper mapper = defaultObjectMapper();
Retrofit retrofit = defaultRetrofit(client, mapper);
OpenAiApi api = retrofit.create(OpenAiApi.class);
OpenAiService service = new OpenAiService(api, client.dispatcher().executorService());
//文本补全
System.out.println("\nCreating completion...");
CompletionRequest completionRequest = CompletionRequest.builder()
.model("ada")
.prompt("你叫什么?")
.echo(true)
.user("testing")
.n(3)
.build();
service.createCompletion(completionRequest).getChoices().forEach(new Consumer<CompletionChoice>() {
@Override
public void accept(CompletionChoice completionChoice) {
System.out.println(completionChoice.getText());
//图片生成
System.out.println("\nCreating Image...");
CreateImageRequest request = CreateImageRequest.builder()
.prompt("小猫钓鱼漫画")
.build();
System.out.println("\nImage is located at:");
System.out.println(service.createImage(request).getData().get(0).getUrl());
//对话
System.out.println("Creating chat completion...");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "你叫什么?");
messages.add(systemMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.n(1)
.maxTokens(500)
.logitBias(new HashMap<>())
.build();
//完整对话
service.createChatCompletion(chatCompletionRequest)
.getChoices().forEach(new Consumer<ChatCompletionChoice>() {
@Override
public void accept(ChatCompletionChoice chatCompletionChoice) {
System.out.println(chatCompletionChoice.getMessage());
//流式对话(逐Token返回)
// service.streamChatCompletion(chatCompletionRequest)
// .doOnError(Throwable::printStackTrace)
// .blockingForEach(System.out::println);
//立即释放连接
service.shutdownExecutor();
上述代码测试了:文本补全、绘图、聊天三个功能。
注意事项:
1.由于网络的原因,很多国外网站无法访问,需要先开通VPN,访问国外网站
2.国外手机号购买无法注册,多换几个号码试试
3.代码中需要设置HTTP代理,代理的地址就是VPN的本地地址