参考:https://github.com/PlexPt/chatgpt-java
一、接口调用
-
posman 调用正常
-
java 调用报错:
ConnectException: Connection timed out: connect
cn.hutool.core.io.IORuntimeException: ConnectException: Connection timed out: connectat cn.hutool.http.HttpRequest.send(HttpRequest.java:1328)at cn.hutool.http.HttpRequest.doExecute(HttpRequest.java:1176)at cn.hutool.http.HttpRequest.execute(HttpRequest.java:1050)at cn.hutool.http.HttpRequest.execute(HttpRequest.java:1026)at com.feishu.chatgpt.controller.ChatGptDemo.completions(ChatGptDemo.java:43)at com.feishu.chatgpt.controller.ChatGptDemo.main(ChatGptDemo.java:14)
Caused by: java.net.ConnectException: Connection timed out: connectat java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)at java.net.Socket.connect(Socket.java:606)at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:303)at sun.net.NetworkClient.doConnect(NetworkClient.java:175)at sun.net.www.http.HttpClient.openServer(HttpClient.java:499)at sun.net.www.http.HttpClient.openServer(HttpClient.java:594)at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:263)at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:366)at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:207)at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1167)at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1061)at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:193)at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1354)at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1329)at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:264)at cn.hutool.http.HttpConnection.getOutputStream(HttpConnection.java:458)at cn.hutool.http.HttpRequest.sendFormUrlEncoded(HttpRequest.java:1351)at cn.hutool.http.HttpRequest.send(HttpRequest.java:1320)... 5 moreProcess finished with exit code 0
二、解决
- 在代码中设置
Proxy proxy = Proxys.http("127.0.0.1", 7890);
三、示例代码
- maven 坐标
<dependency><groupId>com.github.plexpt</groupId><artifactId>chatgpt</artifactId><version>4.0.5</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-http</artifactId><version>5.8.18</version></dependency>
- 详细代码
import cn.hutool.http.*;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.plexpt.chatgpt.util.Proxys;import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;public class ChatGptDemo {public static void main(String[] args) {completions();chat();}/*** v1/completions 下的模型调用方法*/public static void completions(){Map<String,String> headers = new HashMap<String,String>();headers.put("Content-Type","application/json");JSONObject json = new JSONObject();//选择模型json.set("model","text-davinci-003");//添加我们需要输入的内容json.set("prompt","推荐一部电影");json.set("temperature",0);json.set("max_tokens",2048);json.set("top_p",1);json.set("frequency_penalty",0.0);json.set("presence_penalty",0.0);try{Proxy proxy = Proxys.http("127.0.0.1", 7890); // 端口号不知道怎么来的私聊,说出来审核不通过HttpResponse response = HttpRequest.post("https://api.openai.com/v1/completions") // text-davinci-003.headerMap(headers, false).bearerAuth("sk-MMiKbgBZMbGIwPkgc6IIT3*******************") // 填写自己的 chatgpt API Keys.setProxy(proxy).body(String.valueOf(json)).timeout(600000).execute();System.out.println(response.body());}catch (Exception e){e.printStackTrace();}}/*** v1/chat/completions 下的模型调用方法*/public static void chat(){Map<String,String> headers = new HashMap<String,String>();headers.put("Content-Type","application/json");JSONObject json = new JSONObject();//选择模型json.set("model","gpt-3.5-turbo");//添加我们需要输入的内容JSONObject msg = new JSONObject();msg.set("role", "user");msg.set("content", "推荐1本余华的书");JSONArray array = new JSONArray();array.add(msg);json.set("messages", array);json.set("temperature",0);json.set("max_tokens",2048);json.set("top_p",1);json.set("frequency_penalty",0.0);json.set("presence_penalty",0.0);try{Proxy proxy = Proxys.http("127.0.0.1", 7890); // 端口号不知道怎么来的私聊,说出来审核不通过HttpResponse response = HttpRequest.post("https://api.openai.com/v1/chat/completions") //gpt-3.5-turbo.headerMap(headers, false).bearerAuth("sk-MMiKbgBZMbGIwPkgc6IIT3*******************") // 填写自己的 chatgpt API Keys.setProxy(proxy).body(String.valueOf(json)).timeout(600000).execute();System.out.println(response.body());}catch (Exception e){e.printStackTrace();}}
}
- 运行结果