OpenAI注册以及Java代码调用

目录

一、OpenAI注册

1、注册微软账号

2、虚拟电话接收验证码

3、登录OpenAI

二、Java代码实现调用


基于Azure可移步:https://blog.csdn.net/qq_41061437/article/details/130927618

一、OpenAI注册

1、注册微软账号

Microsoft account

        OpenAI可以使用google账号登录,也可以使用微软账号登录,这里建议使用微软账号登录,因为一些原因,微软账号国内即可注册,用国内的邮箱和电话号码即可。

2、虚拟电话接收验证码

        OpenAI需要短信验证,需要购买一个虚拟电话:SMS-Activate - service for receiving virtual SMS online to virtual SIM,注册完成后,充值2美元,可以使用支付宝支付,然后搜索OpenAI:

        注意这个有效时间是20分钟,如果没有接收到短信会将购买费用返回,我使用阿根廷、印度的都能成功,可以根据实际情况选择。

3、登录OpenAI

         OpenAI地址:https://chat.openai.com/

        使用第一步注册的微软账号登录,接着会让提供电话号码,使用第二步购买的号码,等待验证码输入即可。

        登录完成后界面:

        在红色对话框即可进行对话,需要注意的是,因为受返回会话字数限制,超过一定数量就不会继续,可以在对话框输入continue继续进行。 

二、Java代码实现调用

        我这里Java使用的chatgpt-java这个jar包,截止更新文章,最新版本是1.0.8,该版本能支持代理、超时时长等设置。

        使用代调用需要用到OpenAI的key,获取地址:https://platform.openai.com/docs/introduction

        进去之后create new secret key,记得及时copy。

        我这里实现Java代码调用Chatgpt大部分是由AI完成,自己需要进行一些微调:

        

需要引入pom:

 <dependency>
            <groupId>com.unfbx</groupId>
            <artifactId>chatgpt-java</artifactId>
            <version>1.0.8</version>
        </dependency>

 代码:

import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.chat.Message;
import com.unfbx.chatgpt.interceptor.OpenAILogger;
import com.unfbx.chatgpt.interceptor.OpenAiResponseInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class ApiTestA {
    public static void main(String[] args) {
        //国内访问需要做代理,国外服务器不需要,host填入代理IP,如果本地开vpn,一般就是本机ip地址,port根据vpn的port填写,一般是7890
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", port));
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                //自定义代理
                .proxy(proxy)
                //自定义日志输出
                .addInterceptor(httpLoggingInterceptor)
                //自定义返回值拦截
                .addInterceptor(new OpenAiResponseInterceptor())
                //自定义超时时间
                .connectTimeout(10, TimeUnit.SECONDS)
                //自定义超时时间
                .writeTimeout(30, TimeUnit.SECONDS)
                //自定义超时时间
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        //构建客户端,apiKey中填入获取到的OpenAI的key
        OpenAiClient openAiClient = OpenAiClient.builder()
                .apiKey(Arrays.asList("sk-xxxxxxx"))
                .okHttpClient(okHttpClient)
                .build();

        List<Message> messages = new ArrayList<>();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入对话内容:");
            String input = sc.nextLine().trim();

            if (input.isEmpty()) {
                continue;
            }
            //聊天模型:gpt-3.5
            Message message = Message.builder().role(Message.Role.USER).content(input).build();
            messages.add(message);

            ChatCompletion chatCompletion = ChatCompletion.builder().messages(messages).build();
            ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
            System.out.println(chatCompletionResponse);
            chatCompletionResponse.getChoices().forEach(e -> {
                System.out.println("AI: " + e.getMessage().getContent());
            });
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41061437/article/details/129855629
今日推荐