最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java調(diào)用chatgpt接口來(lái)實(shí)現(xiàn)專(zhuān)屬于自己的人工智能助手

 更新時(shí)間:2023年03月27日 09:43:21   作者:禿頭披風(fēng)俠.  
這篇文章主要介紹了用java來(lái)調(diào)用chatget的接口,實(shí)現(xiàn)自己的聊天機(jī)器人,對(duì)人工智能感興趣的小伙伴可以參考閱讀

前言

今天突然突發(fā)奇想,就想要用java來(lái)調(diào)用chatget的接口,實(shí)現(xiàn)自己的聊天機(jī)器人,但是網(wǎng)上找文章,屬實(shí)是少的可憐(可能是不讓發(fā)吧)。找到了一些文章,但是基本都是通過(guò)調(diào)用別人的庫(kù)來(lái)完成的,導(dǎo)入其他的jar還有不低的學(xué)習(xí)成本,于是就自己使用HttpClient5寫(xiě)了一個(gè),在這里講解一下思路。

導(dǎo)包

對(duì)于http調(diào)用,我使用的是比較流行的httpclient5,然后直接創(chuàng)建了一個(gè)springboot項(xiàng)目,方便以后對(duì)外提供接口。

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.3</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.2.1</version>
        </dependency>

    </dependencies>

基本說(shuō)明

在編寫(xiě)代碼之前,這個(gè)先給出HttpClient的Api文檔 api文檔

我們?cè)诰帉?xiě)代碼之前需要了解官方提供的接口如何進(jìn)行訪(fǎng)問(wèn)以及返回的結(jié)果是什么

請(qǐng)求參數(shù)

官方文檔地址為文檔,請(qǐng)求參數(shù)必須填寫(xiě)的內(nèi)容如下

{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}

一個(gè)是model,一個(gè)是messages。model根據(jù)自己的情況來(lái)選擇,聊天的話(huà)就是gpt-3.5-turbo,下面的messages里面包含n個(gè)對(duì)象,每個(gè)對(duì)象有role和content,role表示角色,content表示內(nèi)容。
下面為官方文檔中的解釋

簡(jiǎn)單理解就是我們要問(wèn)問(wèn)題,role就是user。如果要實(shí)現(xiàn)連續(xù)對(duì)話(huà),那么就將返回的返回內(nèi)容設(shè)置到messages中,role設(shè)置為返回的role。

響應(yīng)參數(shù)

下面直接給出響應(yīng)的內(nèi)容

{
 'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
 'object': 'chat.completion',
 'created': 1677649420,
 'model': 'gpt-3.5-turbo',
 'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
 'choices': [
   {
    'message': {
      'role': 'assistant',
      'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
    'finish_reason': 'stop',
    'index': 0
   }
  ]
}

我們問(wèn)問(wèn)題的答案就在choices.message下的content中,而role就代表了chatGpt扮演的角色??吹竭@我們就應(yīng)該知道該干嘛了吧肯定是創(chuàng)建對(duì)應(yīng)的VO類(lèi)啊。

創(chuàng)建請(qǐng)求和響應(yīng)的VO類(lèi)

下面5個(gè)類(lèi)就對(duì)應(yīng)了我們發(fā)送和接收的各種信息

ChatGptMessage類(lèi)

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptMessage {
        String role;
        String content;
}

ChatGptRequestParameter 類(lèi)

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptRequestParameter {

    String model = "gpt-3.5-turbo";

    List<ChatGptMessage> messages = new ArrayList<>();

    public void addMessages(ChatGptMessage message) {
        this.messages.add(message);
    }

}

ChatGptResponseParameter 類(lèi)

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptResponseParameter {

    String id;
    String object;
    String created;
    String model;
    Usage usage;
    List<Choices> choices;
}

Choices 類(lèi)

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Choices {

    ChatGptMessage message;
    String finish_reason;
    Integer index;
}

Usage 類(lèi)

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Usage {

    String prompt_tokens;
    String completion_tokens;
    String total_tokens;
}

代碼編寫(xiě)

不說(shuō)廢話(huà),首先創(chuàng)建一個(gè)CustomChatGpt類(lèi)

public class CustomChatGpt {

}

然后定義一些成員屬性

    /**
     * 自己chatGpt的ApiKey
     */
    private String apiKey;
    /**
     * 使用的模型
     */
    private String model = "gpt-3.5-turbo-0301";
    /**
     * 對(duì)應(yīng)的請(qǐng)求接口
     */
    private String url = "https://api.openai.com/v1/chat/completions";
    /**
     * 默認(rèn)編碼
     */
    private Charset charset = StandardCharsets.UTF_8;
        /**
     * 創(chuàng)建一個(gè)ChatGptRequestParameter,用于攜帶請(qǐng)求參數(shù)
     */
    private ChatGptRequestParameter chatGptRequestParameter = new ChatGptRequestParameter();

提供一個(gè)ApiKey的構(gòu)造器,創(chuàng)建該對(duì)象必須要傳入ApiKey

    public CustomChatGpt(String apiKey) {
        this.apiKey = apiKey;
    }

定義一個(gè)響應(yīng)超時(shí)時(shí)間

    /**
     * 響應(yīng)超時(shí)時(shí)間,毫秒
     */
    private int responseTimeout = 10000;

    public void setResponseTimeout(int responseTimeout) {
        this.responseTimeout = responseTimeout;
    }

編寫(xiě)一個(gè)getAnswer方法,要求傳入一個(gè)CloseableHttpClient和一個(gè)問(wèn)題

    public String getAnswer(CloseableHttpClient client, String question) {
    
    }

繼續(xù)實(shí)現(xiàn)方法,下面會(huì)完成一些參數(shù)的創(chuàng)建和設(shè)置

        // 創(chuàng)建一個(gè)HttpPost
        HttpPost httpPost = new HttpPost(url);
        // 創(chuàng)建一個(gè)ObjectMapper,用于解析和創(chuàng)建json
        ObjectMapper objectMapper = new ObjectMapper();
        // 設(shè)置請(qǐng)求參數(shù)
        chatGptRequestParameter.addMessages(new ChatGptMessage("user", question));
        HttpEntity httpEntity = null;
        try {
            // 對(duì)象轉(zhuǎn)換為json字符串
            httpEntity = new StringEntity(objectMapper.writeValueAsString(chatGptRequestParameter), charset);
        } catch (JsonProcessingException e) {
            System.out.println(question + "->json轉(zhuǎn)換異常");
            return null;
        }
        httpPost.setEntity(httpEntity);

下面會(huì)完成一些配置的設(shè)置

        // 設(shè)置請(qǐng)求頭
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // 設(shè)置登錄憑證
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);

        // 用于設(shè)置超時(shí)時(shí)間
        RequestConfig config = RequestConfig
                .custom()
                .setResponseTimeout(responseTimeout, TimeUnit.MILLISECONDS)
                .build();
        httpPost.setConfig(config);

下面代碼會(huì)提交請(qǐng)求,解析響應(yīng),最后返回對(duì)應(yīng)問(wèn)題的答案

        try {
            // 提交請(qǐng)求
            return client.execute(httpPost, response -> {
                // 得到返回的內(nèi)容
                String resStr = EntityUtils.toString(response.getEntity(), charset);
                // 轉(zhuǎn)換為對(duì)象
                ChatGptResponseParameter responseParameter = objectMapper.readValue(resStr, ChatGptResponseParameter.class);
                String ans = "";
                // 遍歷所有的Choices(一般都只有一個(gè))
                for (Choices choice : responseParameter.getChoices()) {
                    ChatGptMessage message = choice.getMessage();
                    chatGptRequestParameter.addMessages(new ChatGptMessage(message.getRole(), message.getContent()));
                    String s = message.getContent().replaceAll("\n+", "\n");
                    ans += s;
                }
                // 返回信息
                return ans;
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 發(fā)生異常,移除剛剛添加的ChatGptMessage
        chatGptRequestParameter.getMessages().remove(chatGptRequestParameter.getMessages().size()-1);
        return "您當(dāng)前的網(wǎng)絡(luò)無(wú)法訪(fǎng)問(wèn)";

下面給出這個(gè)類(lèi)的完整代碼

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttpfx.vo.ChatGptMessage;
import com.ttpfx.vo.ChatGptRequestParameter;
import com.ttpfx.vo.ChatGptResponseParameter;
import com.ttpfx.vo.Choices;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * @author ttpfx
 * @date 2023/3/23
 */
public class CustomChatGpt {
    /**
     * 自己chatGpt的ApiKey
     */
    private String apiKey;
    /**
     * 使用的模型
     */
    private String model = "gpt-3.5-turbo-0301";
    /**
     * 對(duì)應(yīng)的請(qǐng)求接口
     */
    private String url = "https://api.openai.com/v1/chat/completions";
    /**
     * 默認(rèn)編碼
     */
    private Charset charset = StandardCharsets.UTF_8;


    /**
     * 創(chuàng)建一個(gè)ChatGptRequestParameter,用于攜帶請(qǐng)求參數(shù)
     */
    private ChatGptRequestParameter chatGptRequestParameter = new ChatGptRequestParameter();

    /**
     * 相應(yīng)超時(shí)時(shí)間,毫秒
     */
    private int responseTimeout = 1000;

    public void setResponseTimeout(int responseTimeout) {
        this.responseTimeout = responseTimeout;
    }

    public CustomChatGpt(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getAnswer(CloseableHttpClient client, String question) {

        // 創(chuàng)建一個(gè)HttpPost
        HttpPost httpPost = new HttpPost(url);
        // 創(chuàng)建一個(gè)ObjectMapper,用于解析和創(chuàng)建json
        ObjectMapper objectMapper = new ObjectMapper();

        // 設(shè)置請(qǐng)求參數(shù)
        chatGptRequestParameter.addMessages(new ChatGptMessage("user", question));
        HttpEntity httpEntity = null;
        try {
            // 對(duì)象轉(zhuǎn)換為json字符串
            httpEntity = new StringEntity(objectMapper.writeValueAsString(chatGptRequestParameter), charset);
        } catch (JsonProcessingException e) {
            System.out.println(question + "->json轉(zhuǎn)換異常");
            return null;
        }
        httpPost.setEntity(httpEntity);


        // 設(shè)置請(qǐng)求頭
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // 設(shè)置登錄憑證
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);

        // 用于設(shè)置超時(shí)時(shí)間
        RequestConfig config = RequestConfig
                .custom()
                .setResponseTimeout(responseTimeout, TimeUnit.MILLISECONDS)
                .build();
        httpPost.setConfig(config);
        try {
            // 提交請(qǐng)求
            return client.execute(httpPost, response -> {
                // 得到返回的內(nèi)容
                String resStr = EntityUtils.toString(response.getEntity(), charset);
                // 轉(zhuǎn)換為對(duì)象
                ChatGptResponseParameter responseParameter = objectMapper.readValue(resStr, ChatGptResponseParameter.class);
                String ans = "";
                // 遍歷所有的Choices(一般都只有一個(gè))
                for (Choices choice : responseParameter.getChoices()) {
                    ChatGptMessage message = choice.getMessage();
                    chatGptRequestParameter.addMessages(new ChatGptMessage(message.getRole(), message.getContent()));
                    String s = message.getContent().replaceAll("\n+", "\n");
                    ans += s;
                }
                // 返回信息
                return ans;
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 發(fā)生異常,移除剛剛添加的ChatGptMessage
        chatGptRequestParameter.getMessages().remove(chatGptRequestParameter.getMessages().size()-1);
        return "您當(dāng)前的網(wǎng)絡(luò)無(wú)法訪(fǎng)問(wèn)";
    }
}

使用

下面就是測(cè)試代碼,我們只需要傳入一個(gè)CloseableHttpClient 和 question 即可

public class Test {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String apiKey = "自己的ApiKey";
        CustomChatGpt customChatGpt = new CustomChatGpt(apiKey);
        // 根據(jù)自己的網(wǎng)絡(luò)設(shè)置吧
        customChatGpt.setResponseTimeout(20000);
        while (true) {
            System.out.print("\n請(qǐng)輸入問(wèn)題(q退出):");
            String question = new Scanner(System.in).nextLine();
            if ("q".equals(question)) break;
            long start = System.currentTimeMillis();
            String answer = customChatGpt.getAnswer(httpClient, question);
            long end = System.currentTimeMillis();
            System.out.println("該回答花費(fèi)時(shí)間為:" + (end - start) / 1000.0 + "秒");
            System.out.println(answer);
        }
        httpClient.close();
    }
}

最后說(shuō)明

對(duì)于ApiKey,只能說(shuō)難者不會(huì),會(huì)者不難,這個(gè)沒(méi)辦法教。

如果代碼無(wú)法運(yùn)行,或者運(yùn)行速度及其緩慢,請(qǐng)使用代理,在HttpClient里面可以很輕松的使用代理

        String proxyIp = "127.0.0.1";
        int proxyPort = 7890;
        HttpHost httpHost = new HttpHost(proxyIp, proxyPort);

上面就是一個(gè)示例,對(duì)于代理,這里也就無(wú)法繼續(xù)進(jìn)行說(shuō)明了。

如果我們完成了上面的功能,是不是就能夠?qū)ν馓峁┙涌?,然后?xiě)一個(gè)自己的網(wǎng)頁(yè)端的ChatGpt或者弄一個(gè)聊天機(jī)器人呢?當(dāng)然沒(méi)問(wèn)題啊

到此這篇關(guān)于ava調(diào)用chatgpt接口來(lái)實(shí)現(xiàn)專(zhuān)屬于自己的人工智能助手的文章就介紹到這了,更多相關(guān)java調(diào)用chatgpt實(shí)現(xiàn)人工智能助手內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java通過(guò)Socket實(shí)現(xiàn)簡(jiǎn)單多人聊天室

    Java通過(guò)Socket實(shí)現(xiàn)簡(jiǎn)單多人聊天室

    這篇文章主要為大家詳細(xì)介紹了Java通過(guò)Socket實(shí)現(xiàn)簡(jiǎn)單多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 詳解Java中“==”與equals()的區(qū)別

    詳解Java中“==”與equals()的區(qū)別

    這篇文章主要介紹了詳解Java中“==”與equals()的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • mybatis-plus插入失敗的問(wèn)題及解決

    mybatis-plus插入失敗的問(wèn)題及解決

    這篇文章主要介紹了mybatis-plus插入失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot開(kāi)發(fā)之整合Mybatis詳解

    SpringBoot開(kāi)發(fā)之整合Mybatis詳解

    這篇文章主要介紹了SpringBoot開(kāi)發(fā)之整合Mybatis詳解,MyBatis是一個(gè)半自動(dòng)的ORM框架,它允許我們通過(guò)編寫(xiě)SQL語(yǔ)句來(lái)操作數(shù)據(jù)庫(kù),使用MyBatis,我們可以通過(guò)定義映射文件(XML文件)或使用注解的方式將Java對(duì)象與數(shù)據(jù)庫(kù)表進(jìn)行映射,需要的朋友可以參考下
    2023-09-09
  • Java讀取InfluxDB數(shù)據(jù)庫(kù)的方法詳解

    Java讀取InfluxDB數(shù)據(jù)庫(kù)的方法詳解

    本文介紹基于Java語(yǔ)言,讀取InfluxDB數(shù)據(jù)庫(kù)的方法,包括讀取InfluxDB的所有數(shù)據(jù)庫(kù),以及指定數(shù)據(jù)庫(kù)中的measurement、field、tag等,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2025-01-01
  • 教你如何將Springboot項(xiàng)目成功部署到linux服務(wù)器

    教你如何將Springboot項(xiàng)目成功部署到linux服務(wù)器

    這篇文章主要介紹了如何將Springboot項(xiàng)目成功部署到linux服務(wù)器上,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • java獲取登錄者IP和登錄時(shí)間的兩種實(shí)現(xiàn)代碼詳解

    java獲取登錄者IP和登錄時(shí)間的兩種實(shí)現(xiàn)代碼詳解

    這篇文章主要介紹了java獲取登錄者IP和登錄時(shí)間的實(shí)現(xiàn)代碼,本文通過(guò)兩種結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 使用ftpClient下載ftp上所有文件解析

    使用ftpClient下載ftp上所有文件解析

    最近項(xiàng)目需要寫(xiě)個(gè)小功能,需求就是實(shí)時(shí)下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。今天小編給大家分享使用ftpClient下載ftp上所有文件的方法,需要的的朋友參考下吧
    2017-04-04
  • Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理

    Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理

    這篇文章主要介紹了Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java解析調(diào)用webservice服務(wù)的返回XML串詳解

    Java解析調(diào)用webservice服務(wù)的返回XML串詳解

    這篇文章主要介紹了Java解析調(diào)用webservice服務(wù)的返回XML串詳解的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07

最新評(píng)論

乌拉特中旗| 武汉市| 广西| 河池市| 永济市| 临江市| 若尔盖县| 太仆寺旗| 山阴县| 项城市| 响水县| 诸暨市| 屯留县| 香港 | 紫金县| 乐昌市| 射阳县| 中阳县| 富平县| 铅山县| 莫力| 潼南县| 繁峙县| 许昌市| 喀喇沁旗| 松潘县| 辽阳市| 新田县| 喜德县| 抚远县| 双峰县| 靖远县| 固镇县| 名山县| 图片| 新巴尔虎右旗| 抚宁县| 永新县| 彭州市| 广饶县| 郸城县|