微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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”有了更深的了解,不妨来实际操作一番吧!这里是编程之家网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐