Java仿照ChatGPT聊天源码实现

446 阅读1分钟

📺1.实现效果图

这里我们采用一个公共免费的API接口

地址 : api.qingyunke.com/api.php?key…要发送的信息

📝2.实现源码

🌟(1)HttpUtil工具类请求聊天机器人接口

package com.learning;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by zf on 2017/2/27.
 */
public class HttpUtil {
    private static final String API = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=";
    private static String MSG;
    private static HttpUtil INSTANCE;

    public static HttpUtil getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new HttpUtil();
        }
        return INSTANCE;
    }

    private HttpUtil() {
    }

    public String sendRequest2API(String msg) {
        if (msg.length() > 0) {
            this.MSG = msg;
            HttpURLConnection connection = null;
            StringBuilder response = new StringBuilder();
            try {
                URL url = new URL(API + MSG);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                InputStream in = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                return response.toString();
            }
        }
        return null;
    }
}

🌟(2)采用JFrame窗口实现页面的布局

package com.learning;


import com.google.gson.Gson;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by zf on 2017/2/27.
 */
public class MainUI {
    private JFrame jFrame;
    private JPanel jPanel;
    private JButton sendMsgBtn;
    private JTextArea msgTextArea;
    private JTextArea historyTextArea;
    private static String MSG;
    private static StringBuilder history = new StringBuilder();

    public MainUI() {
        jFrame = new JFrame("低配版chatGPT");
        jPanel = new JPanel();
        sendMsgBtn = new JButton("发送");
        msgTextArea = new JTextArea("这里发生消息");
        historyTextArea = new JTextArea(20, 20);
        historyTextArea.setBackground(new Color(255, 255, 255));
        jPanel.add(historyTextArea);
        jPanel.add(msgTextArea);
        jPanel.add(sendMsgBtn);
        jFrame.add(jPanel);
        jFrame.setSize(500, 500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

        sendMsgBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MSG = msgTextArea.getText();
                history.append("我:" + "\n" + MSG + "\n");
                Gson gson = new Gson();
                RobotAnswer robotAnswer = gson.fromJson(HttpUtil.getInstance().sendRequest2API(MSG), RobotAnswer.class);
                history.append(robotAnswer.getAnswer());
                historyTextArea.setText(history.toString());
                System.out.println(history);
            }
        });
    }

    public static void main(String[] args) {
        new MainUI();
    }
}

🌟(3)创建机器人类

package com.learning;

/**
 * Created by zf on 2017/2/27.
 */
public class RobotAnswer {
    private int result;
    private String content;
    private String answer;

    public RobotAnswer() {
    }

    public String getAnswer() {
        if (result == 0) {
            answer = "AI:" + "\n" + content;
        } else {
            answer = ".....";
        }
        return answer;
    }
}

💰3.chatGPT国内镜像

扫描下方公众号二维码回复 chatgpt 领取镜像地址 👇 👇 👇

8108aba5d35540d29a5af51fb3c9e900.jpeg