Closed huangpeide closed 1 hour ago
OllamaConfig aiConfig = new OllamaConfig();
aiConfig.setApiHost("http://172.17.12.72:11434/");
Configuration configuration = new Configuration();
configuration.setOllamaConfig(aiConfig);
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(new ErrorInterceptor())
.connectTimeout(300, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS)
.build();
configuration.setOkHttpClient(okHttpClient);
AiService aiService = new AiService(configuration);
chatService = aiService.getChatService(PlatformType.OLLAMA);
找到问题了,少加了.addInterceptor(new ContentTypeInterceptor()) 加上就可以了。
`"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\soft\IntelliJ IDEA 2023.1.2\lib\idea_rt.jar=49186:D:\soft\IntelliJ IDEA 2023.1.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\SKX010330\AppData\Local\Temp\classpath1443806535.jar com.d.yunt.llms.AiChatTest SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/apache-maven-3.6.3/repository/ch/qos/logback/logback-classic/1.2.12/logback-classic-1.2.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/apache-maven-3.6.3/repository/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 12:27:39.079 [main] INFO com.d.yunt.llms.platform.ollama.chat.OllamaAiChatService - EventSourceListener:com.d.yunt.llms.AiChatTest$1@a38c7fe 十一月 15, 2024 12:27:39 下午 okhttp3.internal.platform.Platform log 信息: --> POST http://172.17.12.72:11434/api/chat 十一月 15, 2024 12:27:39 下午 okhttp3.internal.platform.Platform log 信息: Content-Type: application/json; charset=utf-8 十一月 15, 2024 12:27:39 下午 okhttp3.internal.platform.Platform log 信息: Content-Length: 148 十一月 15, 2024 12:27:39 下午 okhttp3.internal.platform.Platform log 信息: Accept: text/event-stream 十一月 15, 2024 12:27:39 下午 okhttp3.internal.platform.Platform log 信息: --> END POST 十一月 15, 2024 12:28:00 下午 okhttp3.internal.platform.Platform log 信息: <-- 200 OK http://172.17.12.72:11434/api/chat (21305ms) 十一月 15, 2024 12:28:00 下午 okhttp3.internal.platform.Platform log 信息: Content-Type: application/x-ndjson 十一月 15, 2024 12:28:00 下午 okhttp3.internal.platform.Platform log 信息: Date: Fri, 15 Nov 2024 04:27:39 GMT 十一月 15, 2024 12:28:00 下午 okhttp3.internal.platform.Platform log 信息: Transfer-Encoding: chunked 十一月 15, 2024 12:28:00 下午 okhttp3.internal.platform.Platform log 信息: <-- END HTTP 12:28:00.407 [OkHttp http://172.17.12.72:11434/...] INFO com.d.yunt.llms.platform.ollama.chat.OllamaAiChatService - EventSourceListener.onFailure:okhttp3.internal.sse.RealEventSource@5b84d8 12:28:00.407 [OkHttp http://172.17.12.72:11434/...] INFO com.d.yunt.llms.listener.SseListener - onFailure;message:OK 12:28:00.410 [OkHttp http://172.17.12.72:11434/...] INFO com.d.yunt.llms.listener.SseListener - onFailure;response:Response{protocol=http/1.1, code=200, message=OK, url=http://172.17.12.72:11434/api/chat} java.lang.IllegalStateException: Invalid content-type: application/x-ndjson at okhttp3.internal.sse.RealEventSource.processResponse(RealEventSource.kt:60) at okhttp3.internal.sse.RealEventSource.onResponse(RealEventSource.kt:46) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) 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) 12:28:00.410 [main] INFO com.d.yunt.llms.platform.ollama.chat.OllamaAiChatService - ollamaChatCompletionMessages [OllamaMessage(role=user, content=鲁迅为什么打周树人, images=null, toolCalls=null)]
Process finished with exit code 0 `