一文读懂Springboot如何使用ChatGPT【OpenAI官方Springboot依赖,极强接口封装】

ChatGPT使用步骤

SpringBoot使用

依赖

	<dependency>
            <groupId>com.theokanning.openai-gpt3-java</groupId>
            <artifactId>service</artifactId>
            <version>${gpt.server.version}</version>
        </dependency>

封装了丰富的OpenAI 接口可直接使用

实例

    private final ChatGptProperties config;
    private OpenAiService service;
    @PostConstruct
    public void init() {
        ObjectMapper mapper = defaultObjectMapper();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(config.getProxyDomain(), config.getProxyPort()));
        OkHttpClient client = defaultClient(config.getApiKey(), Duration.ofMinutes(1))
                .newBuilder()
                .proxy(proxy)
                .build();
        Retrofit retrofit = defaultRetrofit(client, mapper);
        OpenAiApi api = retrofit.create(OpenAiApi.class);
        service = new OpenAiService(api);
    }
    @PostMapping("/completions/chat")
    public R chatCompletions(@RequestBody(required = false) FnRequest param) {
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.USER.value(), param.getData().toString());
        messages.add(systemMessage);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
                .builder()
                .model("gpt-3.5-turbo")
                .messages(messages)
                .temperature(0.6d)
                .n(1)
                .frequencyPenalty(1.2d)
                .presencePenalty(1.6d)
//                .stream(true)
//                .user("testing")
                .maxTokens(1024)
                .logitBias(new HashMap<>())
                .build();
        StringBuffer stringBuffer = new StringBuffer();
        String br = System.getProperty("line.separator");
        service.createChatCompletion(chatCompletionRequest).getChoices()
                .forEach(c -> stringBuffer.append(c.getMessage().getContent()).append(br));
        return R.ok(stringBuffer.toString());
    }

接口参数含义

temperature: 0.0 to 2.0 (默认 1.0) 温度,越高越随机,越低越有规律(或确定性)。

top_p: 0.0 to 1.0 (默认 1.0) 使用温度的另一种选择,也叫核采样(nucleus sampling),建议不要同时使用 temperature 和 top_p。top_p 表示模型只考虑概率最高的 top_p 的 token,比如 top_p=0.1,表示模型只考虑概率最高的 10% 的 token。

n: number (默认 1) 生成的回复数量。

stream: boolean (默认 False) 是否使用流式模式,如果设置为 True,将发送部分消息增量,就像在 ChatGPT 中一样。什么意思捏,就是每次单独给你蹦几个词,好让你动态的去更新文本,像你在 ChatGPT 中等待完整的回复一样。

stop: string or array (默认 None) 用来停止生成的 token,可以是一个字符串,也可以是一个字符串列表,如果是字符串列表,那么只要其中一个 token 出现,就会停止生成,最多 4 个。

max_tokens: inf (默认 4096-prompt_token) 生成的最大 token 数量。

frequency_penalty 和 presence_penalty: -2.0 to 2.0 (默认 0) 用来惩罚重复的 token。关于此参数的更多细节在 4 中有介绍,看起来一个是处理的频率,一个是处理的存在次数(整数)。这两个参数的值越大,生成的文本越不会重复。

公式是这样的:

mu[j] -> mu[j] - c[j] * alpha_frequency - float(c[j] > 0) * alpha_presence
logit_bias: dict (默认 None) 用来调整 token 的概率,可以接受 json。数值是 -100 to 100,-100 相当于直接禁用这个词,100 相当于如果相关就必须使用。

user: dict (默认 None) 用来设置用户的信息,具体内容可以参考 5,主要是为了防止滥用。

升级ChatGPT PLUS

步骤

  • 申请外国虚拟信用卡【Depay】
  • 充值USTD虚拟货币【欧易】
  • USTD充值到Depay
  • Depay 的USTD 转 USD虚拟货币
  • 将USD货币存入虚拟信用卡
  • 通过虚拟信用卡充值到ChatGPT

优点

  • 优先ChatGPT试用用户
  • 畅享丝滑的响应速度
  • 优先体验新功能

原文

鸣谢

  • 非常感谢你从头到尾阅读了这篇文章,希望其中的内容对你有所启发和帮助。如果你还有其他问题或需要进一步的了解,欢迎随时关注我的动态并留言
  • 最后可以给作者点个关注和小赞赞嘛,谢谢!
  • 觉得有收藏价值可以进行收藏
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幽·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值