Grt1228 / chatgpt-java

ChatGPT Java SDK支持流式输出、Gpt插件、联网。支持OpenAI官方所有接口。ChatGPT的Java客户端。OpenAI GPT-3.5-Turb GPT-4 Api Client for Java
https://chatgpt-java.unfbx.com
Apache License 2.0
3.35k stars 816 forks source link

使用流式客户端时报错: Exception in thread "OkHttp Dispatcher" java.lang.NoSuchFieldError: instance #278

Open retroxz opened 7 months ago

retroxz commented 7 months ago

使用流式客户端时报错: Exception in thread "OkHttp Dispatcher" java.lang.NoSuchFieldError: instance

猜测原因可能是OkHttp的依赖版本问题 请问应该使用哪个版本的OkHttp

maven依赖

        <!--    openai-java -->
        <dependency>
            <groupId>com.unfbx</groupId>
            <artifactId>chatgpt-java</artifactId>
            <version>1.1.5</version>
        </dependency>
        <!--    okhttp  -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okio</groupId>
            <artifactId>okio</artifactId>
            <version>3.9.0</version>
        </dependency>

测试代码

        //国内访问需要做代理,国外服务器不需要
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));//代理ip,端口
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                //自定义代理
                .proxy(proxy)
                .connectTimeout(30, TimeUnit.SECONDS)//自定义超时时间
                .writeTimeout(30, TimeUnit.SECONDS)//自定义超时时间
                .readTimeout(30, TimeUnit.SECONDS)//自定义超时时间
                .build();
        OpenAiStreamClient openAiClient = OpenAiStreamClient.builder()
                .apiKey(Arrays.asList(OPEN_AI_KEY))
                .okHttpClient(okHttpClient)
                .build();
        ContentKeywordsPrompt prompt = new ContentKeywordsPrompt();
        prompt.setContent("你好");
        log.info("本次提示词: \n {}", prompt.build());

        //聊天模型:gpt-3.5
        Message message = Message.builder().role(Message.Role.USER).content(prompt.build()).build();
        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
        ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();
        openAiClient.streamChatCompletion(chatCompletion, eventSourceListener);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

报错详情

Exception in thread "OkHttp Dispatcher" java.lang.NoSuchFieldError: instance
    at okhttp3.internal.sse.RealEventSource.processResponse(RealEventSource.java:77)
    at okhttp3.internal.sse.RealEventSource.onResponse(RealEventSource.java:56)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
JohnWayneHong commented 3 weeks ago

我也遇到一样的问题在Android项目中 gradle导入的okhttp用的是4.10.0,请问如何做到兼容这个库的3.14.9的okhttp