溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring?Boot項目中怎么使用OpenAI-Java

發(fā)布時間:2023-04-27 10:06:43 來源:億速云 閱讀:246 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Spring Boot項目中怎么使用OpenAI-Java”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Spring Boot項目中怎么使用OpenAI-Java”吧!

準(zhǔn)備工作

1、初始化一個springboot項目

2、訪問OPENAI官網(wǎng)獲取API密鑰

Spring?Boot項目中怎么使用OpenAI-Java

3、通過OPENA開源的JAVA SDK (OpenAI-Java)訪問 API

集成達(dá)芬奇模型

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
#達(dá)芬奇模型
open.ai.model=text-davinci-003

3、編寫訪問業(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、編寫訪問接口

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)行測試

Spring?Boot項目中怎么使用OpenAI-Java

到此,相信大家對“Spring Boot項目中怎么使用OpenAI-Java”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI