java及python调用聊天接口示例

一、接口信息

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

说明:userName参数和token需要对应。同一时间多人调用会产生理解混乱,多组不同账号token请查看

二、Java调用方式

private static String getQuestionInfo(String question) {
    try {
        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":"链接页面获取","userName":"apiuser002"}";
        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;
    } catch (IOException e) {
        e.printStackTrace();
        return "请求异常";
    }
}

运行效果:

三、python 调用方式

# 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)

运行效果

经验分享 程序员 微信小程序 职场和发展