我的编程空间,编程开发者的网络收藏夹
学习永远不晚

SpringBoot整合chatGPT的项目实践

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

SpringBoot整合chatGPT的项目实践

1 添加依赖

        <!-- 导入openai依赖 -->
        <dependency>
            <groupId>com.theokanning.openai-gpt3-java</groupId>
            <artifactId>client</artifactId>
            <version>0.8.1</version>
        </dependency>

2 创建相关文件

2.1 实体类:OpenAi.java

package com.wkf.workrecord.tools.openai;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OpenAi {
 
    String id;
 
    String name;
 
    String desc;
 
    String model;
 
    // 提示模板
    String prompt;
 
    // 创新采样
    Double temperature;
 
    // 情绪采样
    Double topP;
 
    // 结果条数
    Double n = 1d;
 
    // 频率处罚系数
    Double frequencyPenalty;
 
    // 重复处罚系数
    Double presencePenalty;
 
    // 停用词
    String stop;
 
}

2.2 配置类:OpenAiProperties.java

package com.wkf.workrecord.tools.openai;
 
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
 

 
@Data
@ConfigurationProperties(prefix = "openai")
public class OpenAiProperties implements InitializingBean {
    // 秘钥
    String token;
    // 超时时间
    Integer timeout;
 
    // 设置属性时同时设置给OpenAiUtils
    @Override
    public void afterPropertiesSet() throws Exception {
        OpenAiUtils.OPENAPI_TOKEN = token;
        OpenAiUtils.TIMEOUT = timeout;
    }
}

2.3 核心业务逻辑OpenAiUtils.java

package com.wkf.workrecord.tools.openai;
 
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import org.springframework.util.StringUtils;
import java.util.*;
 

public class OpenAiUtils {
    public static final Map<String, OpenAi> PARMS = new HashMap<>();
 
    static {
        PARMS.put("OpenAi01", new OpenAi("OpenAi01", "问&答", "依据现有知识库问&答", "text-davinci-003", "Q: %s\nA:", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
        PARMS.put("OpenAi02", new OpenAi("OpenAi02", "语法纠正", "将句子转换成标准的英语,输出结果始终是英文", "text-davinci-003", "%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        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, ""));
        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, "\"\"\""));
        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, ""));
        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, ""));
        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, "\"\"\""));
        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, "# ;"));
        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, ""));
        PARMS.put("OpenAi10", new OpenAi("OpenAi10", "信息分类", "把一段信息继续分类", "text-davinci-003", "%s\n分类:", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        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, ""));
        PARMS.put("OpenAi12", new OpenAi("OpenAi12", "文字转表情符号", "将文本编码成表情服务", "text-davinci-003", "转换文字为表情。\n%s:", 0.8, 1.0, 1.0, 0.0, 0.0, "\n"));
        PARMS.put("OpenAi13", new OpenAi("OpenAi13", "时间复杂度计算", "求一段代码的时间复杂度", "text-davinci-003", "%s\n\"\"\"\n函数的时间复杂度是", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
        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, "###"));
        PARMS.put("OpenAi15", new OpenAi("OpenAi15", "高级情绪评分", "支持批量列表的方式检查情绪", "text-davinci-003", "对下面内容进行情感分类:\n%s\"\n情绪评级:", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi16", new OpenAi("OpenAi16", "代码解释", "对一段代码进行解释", "code-davinci-002", "代码:\n%s\n\"\"\"\n上面的代码在做什么:\n1. ", 0.0, 1.0, 1.0, 0.0, 0.0, "\"\"\""));
        PARMS.put("OpenAi17", new OpenAi("OpenAi17", "关键字提取", "提取一段文本中的关键字", "text-davinci-003", "抽取下面内容的关键字:\n%s", 0.5, 1.0, 1.0, 0.8, 0.0, ""));
        PARMS.put("OpenAi18", new OpenAi("OpenAi18", "问题解答", "类似解答题", "text-davinci-003", "Q: %s\nA: ?", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi19", new OpenAi("OpenAi19", "广告设计", "给一个产品设计一个广告", "text-davinci-003", "为下面的产品创作一个创业广告,用于投放到抖音上:\n产品:%s.", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi20", new OpenAi("OpenAi20", "产品取名", "依据产品描述和种子词语,给一个产品取一个好听的名字", "text-davinci-003", "产品描述: %s.\n种子词: %s.\n产品名称: ", 0.8, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi21", new OpenAi("OpenAi21", "句子简化", "把一个长句子简化成一个短句子", "text-davinci-003", "%s\nTl;dr: ", 0.7, 1.0, 1.0, 0.0, 1.0, ""));
        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, "###"));
        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, ""));
        PARMS.put("OpenAi24", new OpenAi("OpenAi24", "语言聊天机器人", "各种开发语言的两天机器人", "code-davinci-002", "You: %s\n%s机器人:", 0.0, 1.0, 1.0, 0.5, 0.0, "You: "));
        PARMS.put("OpenAi25", new OpenAi("OpenAi25", "机器学习机器人", "机器学习模型方面的机器人", "text-davinci-003", "You: %s\nML机器人:", 0.3, 1.0, 1.0, 0.5, 0.0, "You: "));
        PARMS.put("OpenAi26", new OpenAi("OpenAi26", "清单制作", "可以列出各方面的分类列表,比如歌单", "text-davinci-003", "列出10%s:", 0.5, 1.0, 1.0, 0.52, 0.5, "11.0"));
        PARMS.put("OpenAi27", new OpenAi("OpenAi27", "文本情绪分析", "对一段文字进行情绪分析", "text-davinci-003", "推断下面文本的情绪是积极的, 中立的, 还是消极的.\n文本: \"%s\"\n观点:", 0.0, 1.0, 1.0, 0.5, 0.0, ""));
        PARMS.put("OpenAi28", new OpenAi("OpenAi28", "航空代码抽取", "抽取文本中的航空diam信息", "text-davinci-003", "抽取下面文本中的航空代码:\n文本:\"%s\"\n航空代码:", 0.0, 1.0, 1.0, 0.0, 0.0, "\n"));
        PARMS.put("OpenAi29", new OpenAi("OpenAi29", "生成SQL语句", "无上下文,语句描述生成SQL", "text-davinci-003", "%s", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi30", new OpenAi("OpenAi30", "抽取联系信息", "从文本中抽取联系方式", "text-davinci-003", "从下面文本中抽取%s:\n%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        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, ""));
        PARMS.put("OpenAi32", new OpenAi("OpenAi32", "好友聊天", "模仿好友聊天", "text-davinci-003", "You: %s\n好友:", 0.5, 1.0, 1.0, 0.5, 0.0, "You:"));
        PARMS.put("OpenAi33", new OpenAi("OpenAi33", "颜色生成", "依据描述生成对应颜色", "text-davinci-003", "%s:\nbackground-color: ", 0.0, 1.0, 1.0, 0.0, 0.0, ";"));
        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, "#\"\"\""));
        PARMS.put("OpenAi35", new OpenAi("OpenAi35", "段落创作", "依据短语生成相关文短", "text-davinci-003", "为下面短语创建一个中文段:\n%s:\n", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi36", new OpenAi("OpenAi36", "代码压缩", "把多行代码简单的压缩成一行", "code-davinci-002", "将下面%s代码转成一行:\n%s\n%s一行版本:", 0.0, 1.0, 1.0, 0.0, 0.0, ";"));
        PARMS.put("OpenAi37", new OpenAi("OpenAi37", "故事创作", "依据一个主题创建一个故事", "text-davinci-003", "主题: %s\n故事创作:", 0.8, 1.0, 1.0, 0.5, 0.0, ""));
        PARMS.put("OpenAi38", new OpenAi("OpenAi38", "人称转换", "第一人称转第3人称", "text-davinci-003", "把下面内容从第一人称转为第三人称 (性别女):\n%s\n", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi39", new OpenAi("OpenAi39", "摘要说明", "依据笔记生成摘要说明", "text-davinci-003", "将下面内容转换成将下%s摘要:\n%s", 0.0, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi40", new OpenAi("OpenAi40", "头脑风暴", "给定一个主题,让其生成一些主题相关的想法", "text-davinci-003", "头脑风暴一些关于%s的想法:", 0.6, 1.0, 1.0, 1.0, 1.0, ""));
        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"));
        PARMS.put("OpenAi42", new OpenAi("OpenAi42", "提纲生成", "按照提示为相关内容生成提纲", "text-davinci-003", "为%s提纲:", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
        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, ""));
        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:"));
        PARMS.put("OpenAi45", new OpenAi("OpenAi45", "摆烂聊天", "与讽刺机器进行聊天", "text-davinci-003", "Marv不情愿的回答问题.\nYou:%s\nMarv:", 0.5, 0.3, 1.0, 0.5, 0.0, ""));
        PARMS.put("OpenAi46", new OpenAi("OpenAi46", "分解步骤", "把一段文本分解成几步来完成", "text-davinci-003", "为下面文本生成次序列表,并增加列表数子: \n%s\n", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi47", new OpenAi("OpenAi47", "点评生成", "依据文本内容自动生成点评", "text-davinci-003", "依据下面内容,进行点评:\n%s\n点评:", 0.5, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi48", new OpenAi("OpenAi48", "知识学习", "可以为学习知识自动解答", "text-davinci-003", "%s", 0.3, 1.0, 1.0, 0.0, 0.0, ""));
        PARMS.put("OpenAi49", new OpenAi("OpenAi49", "面试", "生成面试题", "text-davinci-003", "创建10道%s相关的面试题(中文):\n", 0.5, 1.0, 10.0, 0.0, 0.0, ""));
    }
 
    public static String OPENAPI_TOKEN = "";
    public static Integer TIMEOUT = null;
 
    
    public static List<CompletionChoice> getAiResult(OpenAi openAi, String prompt) {
        if (TIMEOUT == null || TIMEOUT < 1000) {
            TIMEOUT = 3000;
        }
        OpenAiService service = new OpenAiService(OPENAPI_TOKEN, TIMEOUT);
        CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder()
                .model(openAi.getModel())
                .prompt(prompt)
                .temperature(openAi.getTemperature())
                .maxTokens(1000)
                .topP(openAi.getTopP())
                .frequencyPenalty(openAi.getFrequencyPenalty())
                .presencePenalty(openAi.getPresencePenalty());
        if (!StringUtils.isEmpty(openAi.getStop())) {
            builder.stop(Arrays.asList(openAi.getStop().split(",")));
        }
        CompletionRequest completionRequest = builder.build();
        return service.createCompletion(completionRequest).getChoices();
    }
 
    
    public static List<CompletionChoice> getQuestionAnswer(String question) {
        OpenAi openAi = PARMS.get("OpenAi01");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question));
    }
 
    
    public static List<CompletionChoice> getGrammarCorrection(String text) {
        OpenAi openAi = PARMS.get("OpenAi02");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getSummarize(String text) {
        OpenAi openAi = PARMS.get("OpenAi03");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getOpenAiApi(String text) {
        OpenAi openAi = PARMS.get("OpenAi04");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getTextToCommand(String text) {
        OpenAi openAi = PARMS.get("OpenAi05");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getTranslatesLanguages(String text, String translatesLanguages) {
        if (StringUtils.isEmpty(translatesLanguages)) {
            translatesLanguages = "  1. French, 2. Spanish and 3. English";
        }
        OpenAi openAi = PARMS.get("OpenAi06");
        return getAiResult(openAi, String.format(openAi.getPrompt(), translatesLanguages, text));
    }
 
    
    public static List<CompletionChoice> getStripeApi(String text) {
        OpenAi openAi = PARMS.get("OpenAi07");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
 
    
    public static List<CompletionChoice> getStripeApi(String databaseType, List<String> tables, String text, String sqlType) {
        OpenAi openAi = PARMS.get("OpenAi08");
        StringJoiner joiner = new StringJoiner("\n");
        for (int i = 0; i < tables.size(); i++) {
            joiner.add("# " + tables);
        }
        return getAiResult(openAi, String.format(openAi.getPrompt(), databaseType, joiner.toString(), text, sqlType));
    }
 
    
    public static List<CompletionChoice> getUnstructuredData(String text) {
        OpenAi openAi = PARMS.get("OpenAi09");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getTextCategory(String text) {
        OpenAi openAi = PARMS.get("OpenAi10");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getCodeExplain(String codeType, String code) {
        OpenAi openAi = PARMS.get("OpenAi11");
        return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code));
    }
 
    
    public static List<CompletionChoice> getTextEmoji(String text) {
        OpenAi openAi = PARMS.get("OpenAi12");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getTimeComplexity(String code) {
        OpenAi openAi = PARMS.get("OpenAi13");
        return getAiResult(openAi, String.format(openAi.getPrompt(), code));
    }
 
 
    
    public static List<CompletionChoice> getTranslateProgramming(String fromLanguage, String toLanguage, String code) {
        OpenAi openAi = PARMS.get("OpenAi14");
        return getAiResult(openAi, String.format(openAi.getPrompt(), fromLanguage, toLanguage, fromLanguage, code, toLanguage));
    }
 
    
    public static List<CompletionChoice> getBatchTweetClassifier(List<String> texts) {
        OpenAi openAi = PARMS.get("OpenAi15");
        StringJoiner stringJoiner = new StringJoiner("\n");
        for (int i = 0; i < texts.size(); i++) {
            stringJoiner.add((i + 1) + ". " + texts.get(i));
        }
        return getAiResult(openAi, String.format(openAi.getPrompt(), stringJoiner.toString()));
    }
 
    
    public static List<CompletionChoice> getExplainCOde(String code) {
        OpenAi openAi = PARMS.get("OpenAi16");
        return getAiResult(openAi, String.format(openAi.getPrompt(), code));
    }
 
    
    public static List<CompletionChoice> getTextKeywords(String text) {
        OpenAi openAi = PARMS.get("OpenAi17");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getFactualAnswering(String text) {
        OpenAi openAi = PARMS.get("OpenAi18");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getAd(String text) {
        OpenAi openAi = PARMS.get("OpenAi19");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getProductName(String productDescription, String seedWords) {
        OpenAi openAi = PARMS.get("OpenAi20");
        return getAiResult(openAi, String.format(openAi.getPrompt(), productDescription, seedWords));
    }
 
    
    public static List<CompletionChoice> getProductName(String text) {
        OpenAi openAi = PARMS.get("OpenAi21");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getBugFixer(String codeType, String code) {
        OpenAi openAi = PARMS.get("OpenAi22");
        return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code, codeType));
    }
 
    
    public static List<CompletionChoice> getFillData(int rows, String headers) {
        OpenAi openAi = PARMS.get("OpenAi23");
        return getAiResult(openAi, String.format(openAi.getPrompt(), rows, headers));
    }
 
    
    public static List<CompletionChoice> getProgrammingLanguageChatbot(String question, String programmingLanguages) {
        OpenAi openAi = PARMS.get("OpenAi24");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question, programmingLanguages));
    }
 
    
    public static List<CompletionChoice> getMLChatbot(String question) {
        OpenAi openAi = PARMS.get("OpenAi25");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question));
    }
 
    
    public static List<CompletionChoice> getListMaker(String text) {
        OpenAi openAi = PARMS.get("OpenAi26");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getTweetClassifier(String text) {
        OpenAi openAi = PARMS.get("OpenAi27");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getAirportCodeExtractor(String text) {
        OpenAi openAi = PARMS.get("OpenAi28");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getSQL(String text) {
        OpenAi openAi = PARMS.get("OpenAi29");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getExtractContactInformation(String extractContent, String text) {
        OpenAi openAi = PARMS.get("OpenAi30");
        return getAiResult(openAi, String.format(openAi.getPrompt(), extractContent, text));
    }
 
    
    public static List<CompletionChoice> getTransformationCode(String fromCodeType, String toCodeType, String code) {
        OpenAi openAi = PARMS.get("OpenAi31");
        return getAiResult(openAi, String.format(openAi.getPrompt(), fromCodeType, toCodeType, fromCodeType, code, toCodeType));
    }
 
    
    public static List<CompletionChoice> getFriendChat(String question) {
        OpenAi openAi = PARMS.get("OpenAi32");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question));
    }
 
    
    public static List<CompletionChoice> getMoodToColor(String text) {
        OpenAi openAi = PARMS.get("OpenAi33");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getCodeDocument(String codeType, String code) {
        OpenAi openAi = PARMS.get("OpenAi34");
        return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code));
    }
 
    
    public static List<CompletionChoice> getCreateAnalogies(String text) {
        OpenAi openAi = PARMS.get("OpenAi35");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getCodeLine(String codeType, String code) {
        OpenAi openAi = PARMS.get("OpenAi36");
        return getAiResult(openAi, String.format(openAi.getPrompt(), codeType, code, codeType));
    }
 
    
    public static List<CompletionChoice> getStory(String topic) {
        OpenAi openAi = PARMS.get("OpenAi37");
        return getAiResult(openAi, String.format(openAi.getPrompt(), topic));
    }
 
    
    public static List<CompletionChoice> getStoryCreator(String text) {
        OpenAi openAi = PARMS.get("OpenAi38");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getNotesToSummary(String scene, String note) {
        OpenAi openAi = PARMS.get("OpenAi39");
        return getAiResult(openAi, String.format(openAi.getPrompt(), note));
    }
 
    
    public static List<CompletionChoice> getIdeaGenerator(String topic) {
        OpenAi openAi = PARMS.get("OpenAi40");
        return getAiResult(openAi, String.format(openAi.getPrompt(), topic));
    }
 
    
    public static List<CompletionChoice> getESRBRating(String text) {
        OpenAi openAi = PARMS.get("OpenAi41");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getEssayOutline(String text) {
        OpenAi openAi = PARMS.get("OpenAi42");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getRecipeCreator(String name, List<String> ingredients) {
        OpenAi openAi = PARMS.get("OpenAi43");
        StringJoiner joiner = new StringJoiner("\n");
        for (String ingredient : ingredients) {
            joiner.add(ingredient);
        }
        return getAiResult(openAi, String.format(openAi.getPrompt(), name, joiner.toString()));
    }
 
    
    public static List<CompletionChoice> getAiChatbot(String question) {
        OpenAi openAi = PARMS.get("OpenAi44");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question));
    }
 
    
    public static List<CompletionChoice> getMarvChatbot(String question) {
        OpenAi openAi = PARMS.get("OpenAi45");
        return getAiResult(openAi, String.format(openAi.getPrompt(), question));
    }
 
    
    public static List<CompletionChoice> getTurnDirection(String text) {
        OpenAi openAi = PARMS.get("OpenAi46");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getReviewCreator(String text) {
        OpenAi openAi = PARMS.get("OpenAi47");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getStudyNote(String text) {
        OpenAi openAi = PARMS.get("OpenAi48");
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
    
    public static List<CompletionChoice> getInterviewQuestion(String text) {
        OpenAi openAi = PARMS.get("OpenAi49");
        System.out.println(String.format(openAi.getPrompt(), text));
        return getAiResult(openAi, String.format(openAi.getPrompt(), text));
    }
 
}

2.4 自动配置类OpenAiAutoConfiguration.java

package com.wkf.workrecord.tools.openai;
 
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 

@Configuration
@EnableConfigurationProperties(OpenAiProperties.class)
public class OpenAiAutoConfiguration {
}

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

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

2.6 在yml文件上配置token

openai:
  token: 你的token
  timeout: 5000

3 编写测试类

package com.wkf.workrecord.study;
 
import com.theokanning.openai.completion.CompletionChoice;
import com.wkf.workrecord.tools.openai.OpenAiUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
import java.util.List;
 

@Slf4j
@SpringBootTest
public class OpenAiTest {
 
    
    @Test
    public void test() {
        List<CompletionChoice> questionAnswer = OpenAiUtils.getQuestionAnswer("使用SpringBoot框架进行Http请求的详细代码");
        for (CompletionChoice completionChoice : questionAnswer) {
            System.out.println(completionChoice.getText());
        }
        List<CompletionChoice> openAiApi = OpenAiUtils.getOpenAiApi("使用SpringBoot框架进行Http请求");
        for (CompletionChoice completionChoice : openAiApi) {
            System.out.println(completionChoice.getText());
        }
    }
 
}

4 补充

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

4.1 添加依赖

        <!-- openAi 最新版依赖 -->
        <dependency>
            <groupId>com.unfbx</groupId>
            <artifactId>chatgpt-java</artifactId>
            <version>1.0.6</version>
        </dependency>

4.2 添加代码

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

5 总结

以上就是SpringBoot整合chatGPT的项目实践的详细内容,更多关于SpringBoot整合chatGPT的资料请关注编程网其它相关文章!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

SpringBoot整合chatGPT的项目实践

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

SpringBoot整合chatGPT的项目实践

本文主要介绍了SpringBoot整合chatGPT的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
2023-03-24

SpringBoot 整合ChatGPT API项目实战教程

这篇文章主要介绍了SpringBoot整合ChatGPTAPI项目实战教程,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
2023-05-20

SpringBoot整合Mybatis实现商品评分的项目实践

本文详细介绍了使用SpringBoot集成Mybatis实现商品评分功能的项目实践。该项目实现了用户对商品评分和查看评价的功能。技术栈包括SpringBoot、Mybatis、MySQL和Thymeleaf。本文涵盖了环境配置、数据表设计、MyBatis模型、Controller、Service和Thymeleaf模板的创建,最后通过测试验证了项目的实现。该项目展示了使用SpringBoot和Mybatis框架进行Web应用开发的高效流程。
SpringBoot整合Mybatis实现商品评分的项目实践
2024-04-02

springboot整合quartz项目使用案例

quartz是一个定时调度的框架,就目前市场上来说,其实有比quartz更优秀的一些定时调度框架,不但性能比quartz好,学习成本更低,而且还提供可视化操作定时任务,这篇文章主要介绍了springboot整合quartz项目使用(含完整代码),需要的朋友可以参考下
2023-05-20

SpringBoot项目整合Redis教程详解

这篇文章主要介绍了SpringBoot项目整合Redis教程详解,Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。感兴趣的小伙伴可以参考阅读本文
2023-05-13

java SpringBoot项目整合Redis的方法是什么

这篇文章主要介绍了java SpringBoot项目整合Redis的方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java SpringBoot项目整合Redis的方法是什么文章都会有所收获,下面我们
2023-07-05

Springboot+redis+Vue实现秒杀的项目实践

本文主要介绍了Springboot+redis+Vue实现秒杀的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
2022-11-13

springboot项目整合注册功能模块开发实战

这篇文章主要介绍了springboot项目整合注册功能模块开发实战,在用户的注册是首先需要查询当前的用户名是否存在,如果存在则不能进行注册,相当于一个查询语句,本文通过实例代码详细讲解,需要的朋友可以参考下
2022-11-13

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录