本篇内容主要讲解“Spring Boot项目中怎么使用OpenAI-Java”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Boot项目中怎么使用OpenAI-Java”吧!
1、初始化一个springboot项目
2、访问OPENAI官网获取API密钥
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”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。