对接chatGPT

简介: 对接chatGPT

人不能制情欲,则被情欲所制。——贺拉斯

今天使用forest对接chatGPT

https://forest.dtflyx.com/

chatGPT的api文档:https://platform.openai.com/docs/api-reference/making-requests

import com.dtflys.forest.Forest;
import com.dtflys.forest.http.ForestProxy;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.streamquery.stream.core.collection.Lists;
import org.dromara.streamquery.stream.core.collection.Maps;
import org.dromara.streamquery.stream.core.stream.Steam;
import java.util.Map;
import java.util.Objects;
/**
 * {@code
 * curl https://api.openai.com/v1/chat/completions \
 * -H "Content-Type: application/json" \
 * -H "Authorization: Bearer $APP_KEY" \
 * -d '{
 * "model": "gpt-3.5-turbo",
 * "messages": [{"role":"system","content":"translate english to chinese"},{"role": "user", "content": "Hello, my name is John. What is your name?"}],
 * "temperature": 0.7
 * }'
 * }
 */
@Slf4j
public class ChatGPTUtil {
    private static final ChatGPTProperties properties = SpringContextHolder.getBean(ChatGPTProperties.class);
    public static String translate(LanguageTypeEnum source, LanguageTypeEnum target, String content) {
        if (Objects.isNull(source) || Objects.isNull(target)) {
            return "";
        }
        if (StrUtil.isBlank(content)) {
            return "";
        }
        val prompt = StrUtil.format("translate {} to {}", source.getLocale().getLanguage(), target.getLocale().getLanguage());
        return request(prompt, content);
    }
    public static String request(String prompt, String completion) {
        if (StrUtil.isBlank(completion)) {
            return "";
        }
        val messages = Lists.<Map<String, Object>>of();
        if (StrUtil.isNotBlank(prompt)) {
            messages.add(Maps.of("role", "system", "content", prompt));
        }
        messages.add(Maps.of("role", "user", "content", completion));
        val request = Forest.post("https://api.openai.com/v1/chat/completions")
                .setConnectTimeout(properties.getTimeout()).setReadTimeout(properties.getTimeout())
                .addHeader("Authorization", "Bearer " + properties.getApiKey())
                .contentTypeJson()
                .addBody(Maps.of("model", properties.getModel(), "messages", messages));
        if (properties.getIsProxy()) {
            request.setProxy(new ForestProxy("127.0.0.1", 7890));
        }
        val chatCompletion = request.execute(ChatCompletion.class);
        log.info("{}", chatCompletion);
        return Steam.of(chatCompletion.getChoices())
                .map(ChatCompletion.Choice::getMessage).map(ChatCompletion.Message::getContent).join();
    }
}
相关文章
|
4月前
|
自然语言处理 Kubernetes 机器人
基于OpenIM 实现聊天机器人功能
基于OpenIM 实现聊天机器人功能
108 0
|
4月前
|
人工智能 自然语言处理 安全
【AI 现况分析】AI 如何帮助开发者完成自动化测试
【1月更文挑战第27天】【AI 现况分析】AI 如何帮助开发者完成自动化测试
|
2月前
|
人工智能 搜索推荐 机器人
Rasa: 帮助企业更快搭建“AI对话助手”的低代码平台
【2月更文挑战第24天】Rasa: 帮助企业更快搭建“AI对话助手”的低代码平台
37 2
Rasa: 帮助企业更快搭建“AI对话助手”的低代码平台
|
3月前
|
前端开发 JavaScript Java
千帆大模型平台多维度体验总结——平台使用以及接口调用小游戏开发
千帆大模型平台多维度体验总结——平台使用以及接口调用小游戏开发
103 0
|
10月前
|
人工智能 自然语言处理 API
如何调教一个定制化的ChatGPT私人助理,接入自家知识库
ChatGPT 虽然很能“聊”,但它并了解你的产品啊。就好比你把郭德纲喊来做你的客服,他也并不会比一个经过培训的客服人员做得更好。
|
8月前
|
SQL 弹性计算 自然语言处理
AIGC-知识库-LLM:从0开始搭建智能问答钉钉机器人
本文描述在阿里云上从0开始构建个人/企业专属,具备私域知识库+LLM智能问答钉钉机器人。知识库技术方案使用了Lindorm AI数据服务平台知识库能力,LLM使用了开源ChatGLM2-6B。
|
10月前
|
存储 人工智能 小程序
一个合格的 ChatGPT 应用需要具备什么?一文带你打通 GPT 产品功能!
一个合格的 ChatGPT 应用需要具备什么?一文带你打通 GPT 产品功能!
159 0
|
12月前
|
SQL 人工智能 数据挖掘
“提效”|教你用ChatGPT玩数据
“提效”|教你用ChatGPT玩数据
|
12月前
|
人工智能 算法 搜索推荐
70多种插件加持,联网版ChatGPT评测来了
70多种插件加持,联网版ChatGPT评测来了
148 0
70多种插件加持,联网版ChatGPT评测来了
|
数据采集 人工智能 自然语言处理
如何将最新的数据集成到 ChatGPT 智能对话中?
在金融领域,基础知识、公告数据、公开财报数据等是非常重要的数据来源。通过将这些数据集成到智能对话中,可以大大提高智能对话的准确性和可靠性。 其中,基础知识数据是指股票、基金、理财等投资产品的基本信息,如名称、代码、类型、净值等等。公告数据包括公司公告、重要新闻、财务数据等等。公开财报数据包括公司年报、季报等等。