java接入chatgpt 报错 网页上能够正常访问
以下是java代码
@RestController
public class ChatgptController {
@RequestMapping("/test")
public String test(){
Map<String,Object> requestMap = new HashMap<>();
requestMap.put("model", "gpt-3.5-turbo");
List<Map<String, String>> dataList = new ArrayList<>();
dataList.add(new HashMap<String, String>(){{
put("role", "user");
put("content", "Say this is a test");
}});
requestMap.put("messages",dataList);
requestMap.put("temperature", "0.7");
String body = HttpUtils.post(HttpUtils.CHAT_END_POINT,requestMap)
.body();
return body;
}
}
// utils 工具类 使用的 hutool 的 http 工具
public class HttpUtils {
/**
* token
*/
public static final String OPEN_API_KEY ="值";
/**
* 组织key
*/
public static final String ORG_KEY = "值";
/**
* 聊天端点
*/
public static final String CHAT_END_POINT = "https://api.openai.com/v1/chat/completions";
public static final String TEST_GOOGLE_ADDR = "https://www.google.com";
public static HttpResponse post(String url, Map<String, Object> requestBody){
return baseRequest(Method.POST,url)
.body(JSONObject.toJSONBytes(requestBody))
.keepAlive(false)
.execute();
}
public static HttpResponse get(String url){
return baseRequest(Method.GET,url)
.execute();
}
private static HttpRequest baseRequest(Method method, String url){
HttpRequest request = new HttpRequest(url);
request.method(method)
.header("Authorization",OPEN_API_KEY)
.header("OpenAI-Organization",ORG_KEY);
return request;
}
}
jvm启动参数
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=1090
报错信息
2023-05-27 19:00:54.571 ERROR 26012 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is cn.hutool.core.io.IORuntimeException: SocketException: Unexpected end of file from server] with root cause
启动参数的用意是啥