当前位置:   article > 正文

SpringBoot整合chatGPT_com.theokanning.openai-gpt3-java

com.theokanning.openai-gpt3-java

1 添加依赖

  1. <!-- 导入openai依赖 -->
  2. <dependency>
  3. <groupId>com.theokanning.openai-gpt3-java</groupId>
  4. <artifactId>client</artifactId>
  5. <version>0.8.1</version>
  6. </dependency>

2 创建相关文件

2.1 实体类:OpenAi.java

  1. package com.wkf.workrecord.tools.openai;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. /**
  6. * @author wuKeFan
  7. * @date 2023-02-10 15:40:22
  8. */
  9. @Data
  10. @NoArgsConstructor
  11. @AllArgsConstructor
  12. public class OpenAi {
  13. String id;
  14. String name;
  15. String desc;
  16. String model;
  17. // 提示模板
  18. String prompt;
  19. // 创新采样
  20. Double temperature;
  21. // 情绪采样
  22. Double topP;
  23. // 结果条数
  24. Double n = 1d;
  25. // 频率处罚系数
  26. Double frequencyPenalty;
  27. // 重复处罚系数
  28. Double presencePenalty;
  29. // 停用词
  30. String stop;
  31. }

2.2 配置类:OpenAiProperties.java

  1. package com.wkf.workrecord.tools.openai;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.InitializingBean;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. /**
  6. * @author wuKeFan
  7. * @date 2023-02-10 15:25:32
  8. */
  9. @Data
  10. @ConfigurationProperties(prefix = "openai")
  11. public class OpenAiProperties implements InitializingBean {
  12. // 秘钥
  13. String token;
  14. // 超时时间
  15. Integer timeout;
  16. // 设置属性时同时设置给OpenAiUtils
  17. @Override
  18. public void afterPropertiesSet() throws Exception {
  19. OpenAiUtils.OPENAPI_TOKEN = token;
  20. OpenAiUtils.TIMEOUT = timeout;
  21. }
  22. }

2.3 核心业务逻辑OpenAiUtils.java

  1. package com.wkf.workrecord.tools.openai;
  2. import com.theokanning.openai.OpenAiService;
  3. import com.theokanning.openai.completion.CompletionChoice;
  4. import com.theokanning.openai.completion.CompletionRequest;
  5. import org.springframework.util.StringUtils;
  6. import java.util.*;
  7. /**
  8. * @author wuKeFan
  9. * @date 2023-02-10 15:32:18
  10. */
  11. public class OpenAiUtils {
  12. public static final Map<String, OpenAi> PARMS = new HashMap<>();
  13. static {
  14. PARMS.put("OpenAi01", new OpenAi("OpenAi01", "问&答", "依据现有知识库问&答", "text-davinci-003", "Q: %s\nA:", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
  15. PARMS.put("OpenAi02", new OpenAi("OpenAi02", "语法纠正", "将句子转换成标准的英语,输出结果始终是英文", "text-davinci-003", "%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  16. PARMS.put("OpenAi03", new OpenAi("OpenAi03", "内容概况", "将一段话,概况中心", "text-davinci-003", "Summarize this for a second-grade student:\n%s", 0.7, 1.0, 1.0, 0.0, 0.0, ""));
  17. PARMS.put("OpenAi04", new OpenAi("OpenAi04", "生成OpenAi的代码", "一句话生成OpenAi的代码", "code-davinci-002", "\"\"\"\nUtil exposes the following:\nutil.openai() -> authenticates & returns the openai module, which has the following functions:\nopenai.Completion.create(\n prompt=\"<my prompt>\", # The prompt to start completing from\n max_tokens=123, # The max number of tokens to generate\n temperature=1.0 # A measure of randomness\n echo=True, # Whether to return the prompt in addition to the generated completion\n)\n\"\"\"\nimport util\n\"\"\"\n%s\n\"\"\"\n\n", 0.0, 1.0, 1.0, 0.0, 0.0, "\"\"\""));
  18. PARMS.put("OpenAi05", new OpenAi("OpenAi05", "程序命令生成", "一句话生成程序的命令,目前支持操作系统指令比较多", "text-davinci-003", "Convert this text to a programmatic command:\n\nExample: Ask Constance if we need some bread\nOutput: send-msg `find constance` Do we need some bread?\n\n%s", 0.0, 1.0, 1.0, 0.2, 0.0, ""));
  19. PARMS.put("OpenAi06", new OpenAi("OpenAi06", "语言翻译", "把一种语法翻译成其它几种语言", "text-davinci-003", "Translate this into %s:\n%s", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  20. PARMS.put("OpenAi07", new OpenAi("OpenAi07", "Stripe国际API生成", "一句话生成Stripe国际支付API", "code-davinci-002", "\"\"\"\nUtil exposes the following:\n\nutil.stripe() -> authenticates & returns the stripe module; usable as stripe.Charge.create etc\n\"\"\"\nimport util\n\"\"\"\n%s\n\"\"\"", 0.0, 1.0, 1.0, 0.0, 0.0, "\"\"\""));
  21. PARMS.put("OpenAi08", new OpenAi("OpenAi08", "SQL语句生成", "依据上下文中的表信息,生成SQL语句", "code-davinci-002", "### %s SQL tables, 表字段信息如下:\n%s\n#\n### %s\n %s", 0.0, 1.0, 1.0, 0.0, 0.0, "# ;"));
  22. PARMS.put("OpenAi09", new OpenAi("OpenAi09", "结构化生成", "对于非结构化的数据抽取其中的特征生成结构化的表格", "text-davinci-003", "A table summarizing, use Chinese:\n%s\n", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  23. PARMS.put("OpenAi10", new OpenAi("OpenAi10", "信息分类", "把一段信息继续分类", "text-davinci-003", "%s\n分类:", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  24. PARMS.put("OpenAi11", new OpenAi("OpenAi11", "Python代码解释", "把代码翻译成文字,用来解释程序的作用", "code-davinci-002", "# %s \n %s \n\n# 解释代码作用\n\n#", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  25. PARMS.put("OpenAi12", new OpenAi("OpenAi12", "文字转表情符号", "将文本编码成表情服务", "text-davinci-003", "转换文字为表情。\n%s:", 0.8, 1.0, 1.0, 0.0, 0.0, "\n"));
  26. PARMS.put("OpenAi13", new OpenAi("OpenAi13", "时间复杂度计算", "求一段代码的时间复杂度", "text-davinci-003", "%s\n\"\"\"\n函数的时间复杂度是", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
  27. PARMS.put("OpenAi14", new OpenAi("OpenAi14", "程序代码翻译", "把一种语言的代码翻译成另外一种语言的代码", "code-davinci-002", "##### 把这段代码从%s翻译成%s\n### %s\n \n %s\n \n### %s", 0.0, 1.0, 1.0, 0.0, 0.0, "###"));
  28. PARMS.put("OpenAi15", new OpenAi("OpenAi15", "高级情绪评分", "支持批量列表的方式检查情绪", "text-davinci-003", "对下面内容进行情感分类:\n%s\"\n情绪评级:", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  29. PARMS.put("OpenAi16", new OpenAi("OpenAi16", "代码解释", "对一段代码进行解释", "code-davinci-002", "代码:\n%s\n\"\"\"\n上面的代码在做什么:\n1. ", 0.0, 1.0, 1.0, 0.0, 0.0, "\"\"\""));
  30. PARMS.put("OpenAi17", new OpenAi("OpenAi17", "关键字提取", "提取一段文本中的关键字", "text-davinci-003", "抽取下面内容的关键字:\n%s", 0.5, 1.0, 1.0, 0.8, 0.0, ""));
  31. PARMS.put("OpenAi18", new OpenAi("OpenAi18", "问题解答", "类似解答题", "text-davinci-003", "Q: %s\nA: ?", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  32. PARMS.put("OpenAi19", new OpenAi("OpenAi19", "广告设计", "给一个产品设计一个广告", "text-davinci-003", "为下面的产品创作一个创业广告,用于投放到抖音上:\n产品:%s.", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
  33. PARMS.put("OpenAi20", new OpenAi("OpenAi20", "产品取名", "依据产品描述和种子词语,给一个产品取一个好听的名字", "text-davinci-003", "产品描述: %s.\n种子词: %s.\n产品名称: ", 0.8, 1.0, 1.0, 0.0, 0.0, ""));
  34. PARMS.put("OpenAi21", new OpenAi("OpenAi21", "句子简化", "把一个长句子简化成一个短句子", "text-davinci-003", "%s\nTl;dr: ", 0.7, 1.0, 1.0, 0.0, 1.0, ""));
  35. PARMS.put("OpenAi22", new OpenAi("OpenAi22", "修复代码Bug", "自动修改代码中的bug", "code-davinci-002", "##### 修复下面代码的bug\n### %s\n %s\n### %s\n", 0.0, 1.0, 1.0, 0.0, 0.0, "###"));
  36. PARMS.put("OpenAi23", new OpenAi("OpenAi23", "表格填充数据", "自动为一个表格生成数据", "text-davinci-003", "spreadsheet ,%s rows:\n%s\n", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
  37. PARMS.put("OpenAi24", new OpenAi("OpenAi24", "语言聊天机器人", "各种开发语言的两天机器人", "code-davinci-002", "You: %s\n%s机器人:", 0.0, 1.0, 1.0, 0.5, 0.0, "You: "));
  38. PARMS.put("OpenAi25", new OpenAi("OpenAi25", "机器学习机器人", "机器学习模型方面的机器人", "text-davinci-003", "You: %s\nML机器人:", 0.3, 1.0, 1.0, 0.5, 0.0, "You: "));
  39. PARMS.put("OpenAi26", new OpenAi("OpenAi26", "清单制作", "可以列出各方面的分类列表,比如歌单", "text-davinci-003", "列出10%s:", 0.5, 1.0, 1.0, 0.52, 0.5, "11.0"));
  40. PARMS.put("OpenAi27", new OpenAi("OpenAi27", "文本情绪分析", "对一段文字进行情绪分析", "text-davinci-003", "推断下面文本的情绪是积极的, 中立的, 还是消极的.\n文本: \"%s\"\n观点:", 0.0, 1.0, 1.0, 0.5, 0.0, ""));
  41. PARMS.put("OpenAi28", new OpenAi("OpenAi28", "航空代码抽取", "抽取文本中的航空diam信息", "text-davinci-003", "抽取下面文本中的航空代码:\n文本:\"%s\"\n航空代码:", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
  42. PARMS.put("OpenAi29", new OpenAi("OpenAi29", "生成SQL语句", "无上下文,语句描述生成SQL", "text-davinci-003", "%s", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  43. PARMS.put("OpenAi30", new OpenAi("OpenAi30", "抽取联系信息", "从文本中抽取联系方式", "text-davinci-003", "从下面文本中抽取%s:\n%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  44. PARMS.put("OpenAi31", new OpenAi("OpenAi31", "程序语言转换", "把一种语言转成另外一种语言", "code-davinci-002", "#%s to %s:\n%s:%s\n\n%s:", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  45. PARMS.put("OpenAi32", new OpenAi("OpenAi32", "好友聊天", "模仿好友聊天", "text-davinci-003", "You: %s\n好友:", 0.5, 1.0, 1.0, 0.5, 0.0, "You:"));
  46. PARMS.put("OpenAi33", new OpenAi("OpenAi33", "颜色生成", "依据描述生成对应颜色", "text-davinci-003", "%s:\nbackground-color: ", 0.0, 1.0, 1.0, 0.0, 0.0, ";"));
  47. PARMS.put("OpenAi34", new OpenAi("OpenAi34", "程序文档生成", "自动为程序生成文档", "code-davinci-002", "# %s\n \n%s\n# 上述代码的详细、高质量文档字符串:\n\"\"\"", 0.0, 1.0, 1.0, 0.0, 0.0, "#\"\"\""));
  48. PARMS.put("OpenAi35", new OpenAi("OpenAi35", "段落创作", "依据短语生成相关文短", "text-davinci-003", "为下面短语创建一个中文段:\n%s:\n", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
  49. PARMS.put("OpenAi36", new OpenAi("OpenAi36", "代码压缩", "把多行代码简单的压缩成一行", "code-davinci-002", "将下面%s代码转成一行:\n%s\n%s一行版本:", 0.0, 1.0, 1.0, 0.0, 0.0, ";"));
  50. PARMS.put("OpenAi37", new OpenAi("OpenAi37", "故事创作", "依据一个主题创建一个故事", "text-davinci-003", "主题: %s\n故事创作:", 0.8, 1.0, 1.0, 0.5, 0.0, ""));
  51. PARMS.put("OpenAi38", new OpenAi("OpenAi38", "人称转换", "第一人称转第3人称", "text-davinci-003", "把下面内容从第一人称转为第三人称 (性别女):\n%s\n", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  52. PARMS.put("OpenAi39", new OpenAi("OpenAi39", "摘要说明", "依据笔记生成摘要说明", "text-davinci-003", "将下面内容转换成将下%s摘要:\n%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
  53. PARMS.put("OpenAi40", new OpenAi("OpenAi40", "头脑风暴", "给定一个主题,让其生成一些主题相关的想法", "text-davinci-003", "头脑风暴一些关于%s的想法:", 0.6, 1.0, 1.0, 1.0, 1.0, ""));
  54. PARMS.put("OpenAi41", new OpenAi("OpenAi41", "ESRB文本分类", "按照ESRB进行文本分类", "text-davinci-003", "Provide an ESRB rating for the following text:\\n\\n\\\"%s\"\\n\\nESRB rating:", 0.3, 1.0, 1.0, 0.0, 0.0, "\n"));
  55. PARMS.put("OpenAi42", new OpenAi("OpenAi42", "提纲生成", "按照提示为相关内容生成提纲", "text-davinci-003", "为%s提纲:", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  56. PARMS.put("OpenAi43", new OpenAi("OpenAi43", "美食制作(后果自负)", "依据美食名称和材料生成美食的制作步骤", "text-davinci-003", "依据下面成分和美食,生成制作方法:\n%s\n成分:\n%s\n制作方法:", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  57. PARMS.put("OpenAi44", new OpenAi("OpenAi44", "AI聊天", "与AI机器进行聊天", "text-davinci-003", "Human: %s", 0.9, 1.0, 1.0, 0.0, 0.6, "Human:AI:"));
  58. PARMS.put("OpenAi45", new OpenAi("OpenAi45", "摆烂聊天", "与讽刺机器进行聊天", "text-davinci-003", "Marv不情愿的回答问题.\nYou:%s\nMarv:", 0.5, 0.3, 1.0, 0.5, 0.0, ""));
  59. PARMS.put("OpenAi46", new OpenAi("OpenAi46", "分解步骤", "把一段文本分解成几步来完成", "text-davinci-003", "为下面文本生成次序列表,并增加列表数子: \n%s\n", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  60. PARMS.put("OpenAi47", new OpenAi("OpenAi47", "点评生成", "依据文本内容自动生成点评", "text-davinci-003", "依据下面内容,进行点评:\n%s\n点评:", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
  61. PARMS.put("OpenAi48", new OpenAi("OpenAi48", "知识学习", "可以为学习知识自动解答", "text-davinci-003", "%s", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
  62. PARMS.put("OpenAi49", new OpenAi("OpenAi49", "面试", "生成面试题", "text-davinci-003", "创建10道%s相关的面试题(中文):\n", 0.5, 1.0, 10.0, 0.0, 0.0, ""));
  63. }
  64. public static String OPENAPI_TOKEN = "";
  65. public static Integer TIMEOUT = null;
  66. /**
  67. * 获取ai
  68. *
  69. * @param openAi
  70. * @param prompt
  71. * @return
  72. */
  73. public static List<CompletionChoice> getAiResult(OpenAi openAi, String prompt) {
  74. if (TIMEOUT == null || TIMEOUT < 1000) {
  75. TIMEOUT = 3000;
  76. }
  77. OpenAiService service = new OpenAiService(OPENAPI_TOKEN, TIMEOUT);
  78. CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder()
  79. .model(openAi.getModel())
  80. .prompt(prompt)
  81. .temperature(openAi.getTemperature())
  82. .maxTokens(1000)
  83. .topP(openAi.getTopP())
  84. .frequencyPenalty(openAi.getFrequencyPenalty())
  85. .presencePenalty(openAi.getPresencePenalty());
  86. if (!StringUtils.isEmpty(openAi.getStop())) {
  87. builder.stop(Arrays.asList(openAi.getStop().split(",")));
  88. }
  89. CompletionRequest completionRequest = builder.build();
  90. return service.createCompletion(completionRequest).getChoices();
  91. }
  92. /**
  93. * 问答
  94. *
  95. * @param question
  96. * @return
  97. */
  98. public static List<CompletionChoice> getQuestionAnswer(String question) {
  99. OpenAi openAi = PARMS.get("OpenAi01");
  100. return getAiResult(openAi, String.format(openAi.getPrompt(), question));
  101. }
  102. /**
  103. * 语法纠错
  104. *
  105. * @param text
  106. * @return
  107. */
  108. public static List<CompletionChoice> getGrammarCorrection(String text) {
  109. OpenAi openAi = PARMS.get("OpenAi02");
  110. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  111. }
  112. /**
  113. * 将一段话,概况中心
  114. *
  115. * @param text
  116. * @return
  117. */
  118. public static List<CompletionChoice> getSummarize(String text) {
  119. OpenAi openAi = PARMS.get("OpenAi03");
  120. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  121. }
  122. /**
  123. * 一句话生成OpenAi的代码
  124. *
  125. * @param text
  126. * @return
  127. */
  128. public static List<CompletionChoice> getOpenAiApi(String text) {
  129. OpenAi openAi = PARMS.get("OpenAi04");
  130. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  131. }
  132. /**
  133. * 一句话生成程序的命令,目前支持操作系统指令比较多
  134. *
  135. * @param text
  136. * @return
  137. */
  138. public static List<CompletionChoice> getTextToCommand(String text) {
  139. OpenAi openAi = PARMS.get("OpenAi05");
  140. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  141. }
  142. /**
  143. * 把一种语法翻译成其它几种语言
  144. *
  145. * @param text
  146. * @return
  147. */
  148. public static List<CompletionChoice> getTranslatesLanguages(String text, String translatesLanguages) {
  149. if (StringUtils.isEmpty(translatesLanguages)) {
  150. translatesLanguages = " 1. French, 2. Spanish and 3. English";
  151. }
  152. OpenAi openAi = PARMS.get("OpenAi06");
  153. return getAiResult(openAi, String.format(openAi.getPrompt(), translatesLanguages, text));
  154. }
  155. /**
  156. * 一句话生成Stripe国际支付API
  157. *
  158. * @param text
  159. * @return
  160. */
  161. public static List<CompletionChoice> getStripeApi(String text) {
  162. OpenAi openAi = PARMS.get("OpenAi07");
  163. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  164. }
  165. /**
  166. * 依据上下文中的表信息,生成SQL语句
  167. *
  168. * @param databaseType 数据库类型
  169. * @param tables 上午依赖的表和字段 Employee(id, name, department_id)
  170. * @param text SQL描述
  171. * @param sqlType sql类型,比如SELECT
  172. * @return
  173. */
  174. public static List<CompletionChoice> getStripeApi(String databaseType, List<String> tables, String text, String sqlType) {
  175. OpenAi openAi = PARMS.get("OpenAi08");
  176. StringJoiner joiner = new StringJoiner("\n");
  177. for (int i = 0; i < tables.size(); i++) {
  178. joiner.add("# " + tables);
  179. }
  180. return getAiResult(openAi, String.format(openAi.getPrompt(), databaseType, joiner.toString(), text, sqlType));
  181. }
  182. /**
  183. * 对于非结构化的数据抽取其中的特征生成结构化的表格
  184. *
  185. * @param text 非结构化的数据
  186. * @return
  187. */
  188. public static List<CompletionChoice> getUnstructuredData(String text) {
  189. OpenAi openAi = PARMS.get("OpenAi09");
  190. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  191. }
  192. /**
  193. * 把一段信息继续分类
  194. *
  195. * @param text 要分类的文本
  196. * @return
  197. */
  198. public static List<CompletionChoice> getTextCategory(String text) {
  199. OpenAi openAi = PARMS.get("OpenAi10");
  200. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  201. }
  202. /**
  203. * 把一段信息继续分类
  204. *
  205. * @param codeType 代码类型,比如Python
  206. * @param code 要解释的代码
  207. * @return
  208. */
  209. public static List<CompletionChoice> getCodeExplain(String codeType, String code) {
  210. OpenAi openAi = PARMS.get("OpenAi11");
  211. return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code));
  212. }
  213. /**
  214. * 将文本编码成表情服务
  215. *
  216. * @param text 文本
  217. * @return
  218. */
  219. public static List<CompletionChoice> getTextEmoji(String text) {
  220. OpenAi openAi = PARMS.get("OpenAi12");
  221. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  222. }
  223. /**
  224. * 求一段代码的时间复杂度
  225. *
  226. * @param code 代码
  227. * @return
  228. */
  229. public static List<CompletionChoice> getTimeComplexity(String code) {
  230. OpenAi openAi = PARMS.get("OpenAi13");
  231. return getAiResult(openAi, String.format(openAi.getPrompt(), code));
  232. }
  233. /**
  234. * 把一种语言的代码翻译成另外一种语言的代码
  235. *
  236. * @param fromLanguage 要翻译的代码语言
  237. * @param toLanguage 要翻译成的代码语言
  238. * @param code 代码
  239. * @return
  240. */
  241. public static List<CompletionChoice> getTranslateProgramming(String fromLanguage, String toLanguage, String code) {
  242. OpenAi openAi = PARMS.get("OpenAi14");
  243. return getAiResult(openAi, String.format(openAi.getPrompt(), fromLanguage, toLanguage, fromLanguage, code, toLanguage));
  244. }
  245. /**
  246. * 支持批量列表的方式检查情绪
  247. *
  248. * @param texts 文本
  249. * @return
  250. */
  251. public static List<CompletionChoice> getBatchTweetClassifier(List<String> texts) {
  252. OpenAi openAi = PARMS.get("OpenAi15");
  253. StringJoiner stringJoiner = new StringJoiner("\n");
  254. for (int i = 0; i < texts.size(); i++) {
  255. stringJoiner.add((i + 1) + ". " + texts.get(i));
  256. }
  257. return getAiResult(openAi, String.format(openAi.getPrompt(), stringJoiner.toString()));
  258. }
  259. /**
  260. * 对一段代码进行解释
  261. *
  262. * @param code 文本
  263. * @return
  264. */
  265. public static List<CompletionChoice> getExplainCOde(String code) {
  266. OpenAi openAi = PARMS.get("OpenAi16");
  267. return getAiResult(openAi, String.format(openAi.getPrompt(), code));
  268. }
  269. /**
  270. * 提取一段文本中的关键字
  271. *
  272. * @param text 文本
  273. * @return
  274. */
  275. public static List<CompletionChoice> getTextKeywords(String text) {
  276. OpenAi openAi = PARMS.get("OpenAi17");
  277. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  278. }
  279. /**
  280. * 事实回答答题
  281. *
  282. * @param text 文本
  283. * @return
  284. */
  285. public static List<CompletionChoice> getFactualAnswering(String text) {
  286. OpenAi openAi = PARMS.get("OpenAi18");
  287. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  288. }
  289. /**
  290. * 给一个产品设计一个广告
  291. *
  292. * @param text 文本
  293. * @return
  294. */
  295. public static List<CompletionChoice> getAd(String text) {
  296. OpenAi openAi = PARMS.get("OpenAi19");
  297. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  298. }
  299. /**
  300. * 依据产品描述和种子词语,给一个产品取一个好听的名字
  301. *
  302. * @param productDescription 产品描述
  303. * @param seedWords 种子词语
  304. * @return
  305. */
  306. public static List<CompletionChoice> getProductName(String productDescription, String seedWords) {
  307. OpenAi openAi = PARMS.get("OpenAi20");
  308. return getAiResult(openAi, String.format(openAi.getPrompt(), productDescription, seedWords));
  309. }
  310. /**
  311. * 把一个长句子简化成一个短句子
  312. *
  313. * @param text 长句子
  314. * @return
  315. */
  316. public static List<CompletionChoice> getProductName(String text) {
  317. OpenAi openAi = PARMS.get("OpenAi21");
  318. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  319. }
  320. /**
  321. * 自动修改代码中的bug
  322. *
  323. * @param codeType 语言类型
  324. * @param code 代码
  325. * @return
  326. */
  327. public static List<CompletionChoice> getBugFixer(String codeType, String code) {
  328. OpenAi openAi = PARMS.get("OpenAi22");
  329. return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code, codeType));
  330. }
  331. /**
  332. * 自动为一个表格生成数据
  333. *
  334. * @param rows 生成的行数
  335. * @param headers 数据表头,格式如:姓名| 年龄|性别|生日
  336. * @return
  337. */
  338. public static List<CompletionChoice> getFillData(int rows, String headers) {
  339. OpenAi openAi = PARMS.get("OpenAi23");
  340. return getAiResult(openAi, String.format(openAi.getPrompt(), rows, headers));
  341. }
  342. /**
  343. * 各种开发语言的两天机器人
  344. *
  345. * @param question 你的问题
  346. * @param programmingLanguages 语言 比如Java JavaScript
  347. * @return
  348. */
  349. public static List<CompletionChoice> getProgrammingLanguageChatbot(String question, String programmingLanguages) {
  350. OpenAi openAi = PARMS.get("OpenAi24");
  351. return getAiResult(openAi, String.format(openAi.getPrompt(), question, programmingLanguages));
  352. }
  353. /**
  354. * 机器学习模型方面的机器人
  355. *
  356. * @param question 你的问题
  357. * @return
  358. */
  359. public static List<CompletionChoice> getMLChatbot(String question) {
  360. OpenAi openAi = PARMS.get("OpenAi25");
  361. return getAiResult(openAi, String.format(openAi.getPrompt(), question));
  362. }
  363. /**
  364. * 可以列出各方面的分类列表,比如歌单
  365. *
  366. * @param text 清单描述
  367. * @return
  368. */
  369. public static List<CompletionChoice> getListMaker(String text) {
  370. OpenAi openAi = PARMS.get("OpenAi26");
  371. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  372. }
  373. /**
  374. * 对一段文字进行情绪分析
  375. *
  376. * @param text
  377. * @return
  378. */
  379. public static List<CompletionChoice> getTweetClassifier(String text) {
  380. OpenAi openAi = PARMS.get("OpenAi27");
  381. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  382. }
  383. /**
  384. * 抽取文本中的航空代码信息
  385. *
  386. * @param text
  387. * @return
  388. */
  389. public static List<CompletionChoice> getAirportCodeExtractor(String text) {
  390. OpenAi openAi = PARMS.get("OpenAi28");
  391. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  392. }
  393. /**
  394. * 无上下文,语句描述生成SQL
  395. *
  396. * @param text
  397. * @return
  398. */
  399. public static List<CompletionChoice> getSQL(String text) {
  400. OpenAi openAi = PARMS.get("OpenAi29");
  401. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  402. }
  403. /**
  404. * 从文本中抽取联系方式
  405. *
  406. * @param extractContent 抽取内容描述
  407. * @param text
  408. * @return 从下面文本中抽取邮箱和电话:\n教育行业A股IPO第一股(股票代码 003032)\n全国咨询/投诉热线:400-618-4000 举报邮箱:mc@itcast.cn
  409. */
  410. public static List<CompletionChoice> getExtractContactInformation(String extractContent, String text) {
  411. OpenAi openAi = PARMS.get("OpenAi30");
  412. return getAiResult(openAi, String.format(openAi.getPrompt(), extractContent, text));
  413. }
  414. /**
  415. * 把一种语言转成另外一种语言代码
  416. *
  417. * @param fromCodeType 当前代码类型
  418. * @param toCodeType 转换的代码类型
  419. * @param code
  420. * @return
  421. */
  422. public static List<CompletionChoice> getTransformationCode(String fromCodeType, String toCodeType, String code) {
  423. OpenAi openAi = PARMS.get("OpenAi31");
  424. return getAiResult(openAi, String.format(openAi.getPrompt(), fromCodeType, toCodeType, fromCodeType, code, toCodeType));
  425. }
  426. /**
  427. * 模仿好友聊天
  428. *
  429. * @param question
  430. * @return
  431. */
  432. public static List<CompletionChoice> getFriendChat(String question) {
  433. OpenAi openAi = PARMS.get("OpenAi32");
  434. return getAiResult(openAi, String.format(openAi.getPrompt(), question));
  435. }
  436. /**
  437. * 依据描述生成对应颜色
  438. *
  439. * @param text
  440. * @return
  441. */
  442. public static List<CompletionChoice> getMoodToColor(String text) {
  443. OpenAi openAi = PARMS.get("OpenAi33");
  444. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  445. }
  446. /**
  447. * 自动为程序生成文档
  448. *
  449. * @param codeType 语言
  450. * @param code
  451. * @return
  452. */
  453. public static List<CompletionChoice> getCodeDocument(String codeType, String code) {
  454. OpenAi openAi = PARMS.get("OpenAi34");
  455. return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code));
  456. }
  457. /**
  458. * 依据短语生成相关文短
  459. *
  460. * @param text 短语
  461. * @return
  462. */
  463. public static List<CompletionChoice> getCreateAnalogies(String text) {
  464. OpenAi openAi = PARMS.get("OpenAi35");
  465. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  466. }
  467. /**
  468. * 把多行代码简单的压缩成一行
  469. *
  470. * @param codeType 语言
  471. * @param code
  472. * @return
  473. */
  474. public static List<CompletionChoice> getCodeLine(String codeType, String code) {
  475. OpenAi openAi = PARMS.get("OpenAi36");
  476. return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code, codeType));
  477. }
  478. /**
  479. * 依据一个主题创建一个故事
  480. *
  481. * @param topic 创作主题
  482. * @return
  483. */
  484. public static List<CompletionChoice> getStory(String topic) {
  485. OpenAi openAi = PARMS.get("OpenAi37");
  486. return getAiResult(openAi, String.format(openAi.getPrompt(), topic));
  487. }
  488. /**
  489. * 第一人称转第3人称
  490. *
  491. * @param text
  492. * @return
  493. */
  494. public static List<CompletionChoice> getStoryCreator(String text) {
  495. OpenAi openAi = PARMS.get("OpenAi38");
  496. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  497. }
  498. /**
  499. * 依据笔记生成摘要说明
  500. *
  501. * @param scene 生成的摘要场景
  502. * @param note 记录的笔记
  503. * @return
  504. */
  505. public static List<CompletionChoice> getNotesToSummary(String scene, String note) {
  506. OpenAi openAi = PARMS.get("OpenAi39");
  507. return getAiResult(openAi, String.format(openAi.getPrompt(), note));
  508. }
  509. /**
  510. * 给定一个主题,让其生成一些主题相关的想法
  511. *
  512. * @param topic 头脑风暴关键词
  513. * @return
  514. */
  515. public static List<CompletionChoice> getIdeaGenerator(String topic) {
  516. OpenAi openAi = PARMS.get("OpenAi40");
  517. return getAiResult(openAi, String.format(openAi.getPrompt(), topic));
  518. }
  519. /**
  520. * 按照ESRB进行文本分类
  521. *
  522. * @param text 文本
  523. * @return
  524. */
  525. public static List<CompletionChoice> getESRBRating(String text) {
  526. OpenAi openAi = PARMS.get("OpenAi41");
  527. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  528. }
  529. /**
  530. * 按照提示为相关内容生成提纲
  531. *
  532. * @param text 场景,比如 数据库软件生成大学毕业论文
  533. * @return
  534. */
  535. public static List<CompletionChoice> getEssayOutline(String text) {
  536. OpenAi openAi = PARMS.get("OpenAi42");
  537. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  538. }
  539. /**
  540. * 依据美食名称和材料生成美食的制作步骤
  541. *
  542. * @param name 美食名称
  543. * @param ingredients 美食食材
  544. * @return
  545. */
  546. public static List<CompletionChoice> getRecipeCreator(String name, List<String> ingredients) {
  547. OpenAi openAi = PARMS.get("OpenAi43");
  548. StringJoiner joiner = new StringJoiner("\n");
  549. for (String ingredient : ingredients) {
  550. joiner.add(ingredient);
  551. }
  552. return getAiResult(openAi, String.format(openAi.getPrompt(), name, joiner.toString()));
  553. }
  554. /**
  555. * 与AI机器进行聊天
  556. *
  557. * @param question
  558. * @return
  559. */
  560. public static List<CompletionChoice> getAiChatbot(String question) {
  561. OpenAi openAi = PARMS.get("OpenAi44");
  562. return getAiResult(openAi, String.format(openAi.getPrompt(), question));
  563. }
  564. /**
  565. * 与讽刺机器进行聊天,聊天的机器人是一种消极情绪
  566. *
  567. * @param question
  568. * @return
  569. */
  570. public static List<CompletionChoice> getMarvChatbot(String question) {
  571. OpenAi openAi = PARMS.get("OpenAi45");
  572. return getAiResult(openAi, String.format(openAi.getPrompt(), question));
  573. }
  574. /**
  575. * 把一段文本分解成几步来完成
  576. *
  577. * @param text
  578. * @return
  579. */
  580. public static List<CompletionChoice> getTurnDirection(String text) {
  581. OpenAi openAi = PARMS.get("OpenAi46");
  582. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  583. }
  584. /**
  585. * 依据文本内容自动生成点评
  586. *
  587. * @param text
  588. * @return
  589. */
  590. public static List<CompletionChoice> getReviewCreator(String text) {
  591. OpenAi openAi = PARMS.get("OpenAi47");
  592. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  593. }
  594. /**
  595. * 可以为学习知识自动解答
  596. *
  597. * @param text
  598. * @return
  599. */
  600. public static List<CompletionChoice> getStudyNote(String text) {
  601. OpenAi openAi = PARMS.get("OpenAi48");
  602. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  603. }
  604. /**
  605. * 生成面试题
  606. *
  607. * @param text
  608. * @return
  609. */
  610. public static List<CompletionChoice> getInterviewQuestion(String text) {
  611. OpenAi openAi = PARMS.get("OpenAi49");
  612. System.out.println(String.format(openAi.getPrompt(), text));
  613. return getAiResult(openAi, String.format(openAi.getPrompt(), text));
  614. }
  615. }

2.4 自动配置类OpenAiAutoConfiguration.java

  1. package com.wkf.workrecord.tools.openai;
  2. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. /**
  5. * 自动配置类
  6. * @author wuKeFan
  7. * @date 2023-02-10 15:34:01
  8. */
  9. @Configuration
  10. @EnableConfigurationProperties(OpenAiProperties.class)
  11. public class OpenAiAutoConfiguration {
  12. }

2.5 在resources文件夹下的META-INF/spring.factories文件中增加配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wkf.workrecord.tools.openai.OpenAiAutoConfiguration

2.6 在yml文件上配置token

  1. openai:
  2. token: 你的token
  3. timeout: 5000

3 编写测试类

  1. package com.wkf.workrecord.study;
  2. import com.theokanning.openai.completion.CompletionChoice;
  3. import com.wkf.workrecord.tools.openai.OpenAiUtils;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import java.util.List;
  8. /**
  9. * openAi测试类
  10. * @author wuKeFan
  11. * @date 2023-02-10 15:37:01
  12. */
  13. @Slf4j
  14. @SpringBootTest
  15. public class OpenAiTest {
  16. /**
  17. * openAi接口请求API
  18. */
  19. @Test
  20. public void test() {
  21. List<CompletionChoice> questionAnswer = OpenAiUtils.getQuestionAnswer("使用SpringBoot框架进行Http请求的详细代码");
  22. for (CompletionChoice completionChoice : questionAnswer) {
  23. System.out.println(completionChoice.getText());
  24. }
  25. List<CompletionChoice> openAiApi = OpenAiUtils.getOpenAiApi("使用SpringBoot框架进行Http请求");
  26. for (CompletionChoice completionChoice : openAiApi) {
  27. System.out.println(completionChoice.getText());
  28. }
  29. }
  30. }

4 补充

如果使用上述方法出现超时错误的,可以使用这个方法

4.1 添加依赖

  1. <!-- openAi 最新版依赖 -->
  2. <dependency>
  3. <groupId>com.unfbx</groupId>
  4. <artifactId>chatgpt-java</artifactId>
  5. <version>1.0.6</version>
  6. </dependency>

4.2 添加代码

  1. Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 7890));
  2. //日志输出可以不添加
  3. //HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
  4. //httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  5. OpenAiClient openAiClient = OpenAiClient.builder()
  6. .apiKey("sk-***********************************************")
  7. .connectTimeout(50)
  8. .writeTimeout(50)
  9. .readTimeout(50)
  10. .proxy(proxy)
  11. //.interceptor(Collections.singletonList(httpLoggingInterceptor))
  12. .apiHost("https://api.openai.com/")
  13. .build();
  14. CompletionResponse completions = openAiClient.completions("你是openAi吗");
  15. Arrays.stream(completions.getChoices()).forEach(System.out::println);

5 总结

到此这篇关于SpringBoot整合chatGPT的文章就介绍到这了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/937582
推荐阅读
相关标签
  

闽ICP备14008679号