人不能制情欲,则被情欲所制。——贺拉斯
今天使用forest
对接chatGPT
https://forest.dtflyx.com/
chatGPT的api文档:https://platform.openai.com/docs/api-reference/making-requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| 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;
@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(); } }
|