Spring?Boot項(xiàng)目中使用OpenAI-Java的示例詳解
前言
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過(guò)這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
準(zhǔn)備工作
1、初始化一個(gè)springboot項(xiàng)目
參考地址:http://www.dhdzp.com/article/232551.htm
2、訪問(wèn)OPENAI官網(wǎng)獲取API密鑰
地址:https://platform.openai.com/account/api-keys

3、通過(guò)OPENA開源的JAVA SDK (OpenAI-Java)訪問(wèn) API
地址:GitHub - TheoKanning/openai-java: OpenAI GPT-3 Api Client in Java
集成達(dá)芬奇模型
1、編寫SpringBoot項(xiàng)目中的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、配置密鑰、超時(shí)時(shí)間和使用的模型
#application.properties server.port=8081 #密鑰 open.ai.key=xxxxxxxx #超時(shí)時(shí)間 open.ai.request.timeout=100000 #達(dá)芬奇模型 open.ai.model=text-davinci-003
3、編寫訪問(wèn)業(yè)務(wù)類
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、編寫訪問(wèn)接口
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插件進(jìn)行測(cè)試

到此這篇關(guān)于Spring Boot項(xiàng)目中使用OpenAI-Java的示例詳解的文章就介紹到這了,更多相關(guān)Spring Boot使用OpenAI-Java內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot啟動(dòng)異常Exception in thread “main“ java.lang.UnsupportedClassVersionError
- Java SpringBoot集成ChatGPT實(shí)現(xiàn)AI聊天
- SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送的詳細(xì)過(guò)程
- Spring AI 入門學(xué)習(xí)指南
- Spring?AI?+?ollama?本地搭建聊天?AI?功能
- Spring?AI?+?混元帶你實(shí)現(xiàn)企業(yè)級(jí)穩(wěn)定可部署的AI業(yè)務(wù)智能體
- Spring?AI借助全局參數(shù)實(shí)現(xiàn)智能數(shù)據(jù)庫(kù)操作與個(gè)性化待辦管理
- Spring AI 文檔的提取、轉(zhuǎn)換、加載功能實(shí)現(xiàn)
- 深入解析Spring?AI框架如何在Java應(yīng)用中實(shí)現(xiàn)智能化交互的關(guān)鍵
相關(guān)文章
Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對(duì)于BigDecimal的大小比較,用equals方法的話會(huì)不僅會(huì)比較值的大小,還會(huì)比較兩個(gè)對(duì)象的精確度,而compareTo方法則不會(huì)比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11
SpringBoot實(shí)現(xiàn)驗(yàn)證碼的案例分享
驗(yàn)證碼可以有效防止其他人對(duì)某一個(gè)特定的注冊(cè)用戶用特定的程序,破解方式進(jìn)行不斷的登錄嘗試,我們其實(shí)很經(jīng)??吹?登錄一些網(wǎng)站其實(shí)是需要驗(yàn)證碼的,所以本文給大家分享了SpringBoot實(shí)現(xiàn)驗(yàn)證碼的案例,需要的朋友可以參考下2024-11-11
springboot使用redisTemplate操作lua腳本
本文主要介紹了springboot使用redisTemplate操作lua腳本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫(kù)實(shí)踐記錄
這篇文章主要介紹了Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫(kù)實(shí)踐記錄,需要的朋友可以參考下2022-11-11
Java解決No enclosing instance of type PrintListFromTailToHead
這篇文章主要介紹了Java解決No enclosing instance of type PrintListFromTailToHead is accessible問(wèn)題的兩種方案的相關(guān)資料,需要的朋友可以參考下2016-07-07

