您好,登錄后才能下訂單哦!
本篇內容主要講解“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。