spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

ollama+mcp不能流返回

Open benxiaohai061 opened this issue 1 year ago • 7 comments

1、配置文件 spring: ai: openai: base-url: http://192.168.1.123:11434 chat: options: model: qwen2.5:7b-instruct-fp16 stream-usage: true

mcp:
  client:
    type: async
    sse:
      connections:
        mcp-server-chart:
          url: http://localhost:1122/

2、代码 @Primary @Bean public ChatClient chatClient(ChatClient.Builder builder, ChatMemory chatMemory, ToolCallbackProvider tools) { return builder.defaultSystem(""" 你是一个智能人工助手 """) .defaultAdvisors( MessageChatMemoryAdvisor.builder(chatMemory).build(), new SimpleLoggerAdvisor( ModelOptionsUtils::toJsonStringPrettyPrinter, ModelOptionsUtils::toJsonStringPrettyPrinter, 0 ) ) .defaultToolCallbacks(tools) .build(); }

如果注释调.defaultToolCallbacks(tools) 这个。 就可以流返回。 如果加上后。AI回复的结果就变成了一次全返回

benxiaohai061 avatar Jun 06 '25 02:06 benxiaohai061

Hi @benxiaohai061 ! I have a very simple MCP server-side and client-side example that uses Ollama to serve the model just like you do: MCP-EXAMPLE, but in my example the streaming output is returned correctly with or without defaultToolCallbacks(tools), so maybe you can help you locate the problem by comparing it.If your problem persists, can you provide a minimized example project and maybe I can help you locate the problem.

sunyuhan1998 avatar Jun 06 '25 03:06 sunyuhan1998

我也遇到了同样的问题,如果不使用toos,向AI提问的时候是可以正常流式返回的。如果使用了toos,向AI提问的时候就不可以六十返回了。

jichengda avatar Jun 09 '25 01:06 jichengda

@GetMapping(path = "/stream", produces = "text/html;charset=utf-8")
public Flux<String> stream(String question, String chatId) {
    return chatClient.prompt().user(question).stream().content(); // 这样是正常的流式响应
    return chatClient.prompt().user(question).tools(new DataTools()).stream().content(); // 这样不是流式响应,阻塞式
}

jichengda avatar Jun 09 '25 03:06 jichengda

@GetMapping(path = "/stream", produces = "text/html;charset=utf-8")
public Flux<String> stream(String question, String chatId) {
    return chatClient.prompt().user(question).stream().content(); // 这样是正常的流式响应
    return chatClient.prompt().user(question).tools(new DataTools()).stream().content(); // 这样不是流式响应,阻塞式
}

我试了qwen3:1.7b 这个可以流返回。 qwen2.5不行。

benxiaohai061 avatar Jun 09 '25 07:06 benxiaohai061

Hi @benxiaohai061 ! I have a very simple MCP server-side and client-side example that uses Ollama to serve the model just like you do: MCP-EXAMPLE, but in my example the streaming output is returned correctly with or without defaultToolCallbacks(tools), so maybe you can help you locate the problem by comparing it.If your problem persists, can you provide a minimized example project and maybe I can help you locate the problem.

You can try the qwen2.5:7b-instruct-fp16 model @sunyuhan1998

benxiaohai061 avatar Jun 09 '25 07:06 benxiaohai061

@GetMapping(path = "/stream", produces = "text/html;charset=utf-8")
public Flux<String> stream(String question, String chatId) {
    return chatClient.prompt().user(question).stream().content(); // 这样是正常的流式响应
    return chatClient.prompt().user(question).tools(new DataTools()).stream().content(); // 这样不是流式响应,阻塞式
}

我试了qwen3:1.7b 这个可以流返回。 qwen2.5不行。

我用的是qwen3:32b,如果不用tools时候可以正常流式响应。用了tools就是阻塞式的了。我一会也尝试一下其它模型。 我找到原因了,是ollama版本的原因。升级ollama版本就好了。

jichengda avatar Jun 09 '25 11:06 jichengda

1、配置文件 spring: ai: openai: base-url: http://192.168.1.123:11434 chat: options: model: qwen2.5:7b-instruct-fp16 stream-usage: true

mcp:
  client:
    type: async
    sse:
      connections:
        mcp-server-chart:
          url: http://localhost:1122/

2、代码 @primary @bean public ChatClient chatClient(ChatClient.Builder builder, ChatMemory chatMemory, ToolCallbackProvider tools) { return builder.defaultSystem(""" 你是一个智能人工助手 """) .defaultAdvisors( MessageChatMemoryAdvisor.builder(chatMemory).build(), new SimpleLoggerAdvisor( ModelOptionsUtils::toJsonStringPrettyPrinter, ModelOptionsUtils::toJsonStringPrettyPrinter, 0 ) ) .defaultToolCallbacks(tools) .build(); }

如果注释调.defaultToolCallbacks(tools) 这个。 就可以流返回。 如果加上后。AI回复的结果就变成了一次全返回

ollama版本的事,早期的版本functioncall不支持流式响应。0.8.0后的版本才支持。

jichengda avatar Jun 09 '25 12:06 jichengda