Spring Boot项目中怎么使用OpenAI-Java

本篇内容主要讲解“Spring Boot项目中怎么使用OpenAI-Java”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Boot项目中怎么使用OpenAI-Java”吧!

准备工作

1、初始化一个springboot项目

2、访问OPENAI官网获取API密钥

Spring Boot项目中怎么使用OpenAI-Java

3、通过OPENA开源的JAVA SDK (OpenAI-Java)访问 API

集成达芬奇模型

1、编写SpringBoot项目中的pom文件

<dependency>
   <groupId>com.theokanning.openai-gpt3-java</groupId>
   <artifactId>client</artifactId>
   <version>0.9.0</version>
</dependency>

2、初始化OpenAiService类

import com.theokanning.openai.OpenAiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.time.Duration;
 
/**
 * openai 配置类
 */
@Configuration
public class OpenAiConfiguration {
 
    @Value("${open.ai.key}")
    private String openAiKey;
    @Value("${open.ai.request.timeout}")
    private long timeout;
    
    @Bean
    public OpenAiService openAiService(){
        return new OpenAiService(openAiKey, Duration.ofSeconds(timeout));
    }
}

3、配置密钥、超时时间和使用的模型

#application.properties
server.port=8081
#密钥
open.ai.key=xxxxxxxx
#超时时间
open.ai.request.timeout=100000
#达芬奇模型
open.ai.model=text-davinci-003

3、编写访问业务类

import com.google.common.collect.Maps;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.util.Arrays;
import java.util.Map;
 
@Slf4j
@Service
public class OpenAiChatBiz {
 
    @Value("${open.ai.model}")
    private String openaimodel;
    @Autowired
    private OpenAiService openAiService;
    /**
     * 聊天
     * @param prompt
     * @return
     */
    public String chat(String prompt){
        CompletionRequest completionRequest = CompletionRequest.builder()
                .prompt(prompt)
                .model(openaimodel)
                .echo(true)
                .temperature(0.7)
                .topP(1d)
                .frequencyPenalty(0d)
                .presencePenalty(0d)
                .maxTokens(1000)
                .build();
        CompletionResult completionResult = openAiService.createCompletion(completionRequest);
        String text = completionResult.getChoices().get(0).getText();
        return text;
    }
}

4、编写访问接口

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class OpenAiChatApi {
 
    @Autowired
    private OpenAiChatBiz openAiChatBiz;
 
    @RequestMapping(path = "/chat/question",method = RequestMethod.GET)
    public String openAiChat(@RequestParam("question")String question){
        if(StringUtils.isBlank(question)){
            return "Please Input";
        }
        return openAiChatBiz.chat(question);
    }
}

效果展示

使用google的API Tester插件进行测试

Spring Boot项目中怎么使用OpenAI-Java

到此,相信大家对“Spring Boot项目中怎么使用OpenAI-Java”有了更深的了解,不妨来实际操作一番吧!这里是编程之家网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...