Java and python call ChatGPT chat interface example

1. Interface information

接口地址:https://www.1bit.asia/openai/api/ask
类型:POST
参数:{
    "prompt":"写一个修仙小说目录",
    "userName":"apiuser002",
    "token":"链接页面获取"
}

Note: The userName parameter and token need to correspond. For multiple groups of different account tokens, please check

https://blog.csdn.net/liuhenghui5201/article/details/129721995

 Two, Java calling method

package com.xiaohui.bird;

import com.alibaba.fastjson2.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ApplicationMain {
    public static void main(String[] args) {
        String question = "李白的著名古诗有哪些";
        System.out.println("问: "+question);
        String respStr = ask(question);
        System.out.println("答: "+respStr);
    }
    public static String ask(String question) {
        try {
            String questionInfo = getQuestionInfo(question);
            JSONObject questionJson = JSONObject.parseObject(questionInfo);
            String questionId = questionJson.getString("data");
            String answer = null;
            do{
                try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); }
                URL url = new URL("https://www.1bit.asia/openai/api/answer/"+questionId);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                    response.append(System.lineSeparator());
                }
                in.close();
                String answerString =response.toString();
                if(!"思考中...".equals(answerString)){
                    answer = answerString;
                }
            } while (answer == null);
            return answer;
        } catch (Exception e) {
            e.printStackTrace();
            return "问答异常";
        }
    }

    private static String getQuestionInfo(String question) throws IOException {
        URL url = new URL("https://www.1bit.asia/openai/api/ask");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        // 设置请求头
        con.setRequestProperty("Content-Type", "application/json");
        // 设置请求体
        String requestBody = "{\"prompt\":\""+ question +"\",\"token\":\"6865725-27a9-4385-a7f5-97b09e434fw\",\"userName\":\"apiuser033\"}";
        con.setDoOutput(true);
        con.setDoInput(true);
        OutputStream os = con.getOutputStream();
        os.write(requestBody.getBytes());
        os.flush();
        os.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        String questionInfo = content.toString();
        return questionInfo;
    }
}

running result:

3. Python calling method

# coding=gbk
import requests
import json
 
prompt = input('请输入问题:')
url = 'https://www.1bit.asia/openai/api/ask'
data = {'prompt': prompt, 'token': '链接页面获取', 'userName':'apiuser002'}
headers = {'Content-Type':'application/json'}
response = requests.post(url, data= json.dumps(data), headers=headers)
 
print('答:'+response.text)

running result

 

Website experience address https://1bit.asia

Guess you like

Origin blog.csdn.net/liuhenghui5201/article/details/129722211