seeyoulater1 2023-05-27 19:03 采纳率: 76.9%
浏览 44
已结题

Java 接入chatgpt 报错 Unexpected end of file from server] with root cause

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

  • 写回答

1条回答 默认 最新

  • BsonJ 2023-05-27 19:24
    关注

    启动参数的用意是啥

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月28日
  • 创建了问题 5月27日

悬赏问题

  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥20 python忆阻器数字识别
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api