refactor(llmx): 将 LLMProvider 接口中的 chat 方法返回类型改为 Publisher

修改了 LLMProvider 接口和 BaiLianProvider 实现类中的 chat 方法,将返回类型从 Flow 改为 Publisher,以适应 Spring WebFlux 的数据处理方式。这一变更使得系统能够更好地集成和使用 Spring WebFlux 的特性。
This commit is contained in:
gewuyou 2025-05-08 14:39:02 +08:00
parent 1ace055e37
commit 383533eb35
2 changed files with 9 additions and 9 deletions

View File

@ -1,12 +1,11 @@
package org.jcnc.llmx.core.spi.provider package org.jcnc.llmx.core.spi.provider
import kotlinx.coroutines.flow.Flow
import org.jcnc.llmx.core.spi.entities.request.ChatRequest import org.jcnc.llmx.core.spi.entities.request.ChatRequest
import org.jcnc.llmx.core.spi.entities.request.EmbeddingRequest import org.jcnc.llmx.core.spi.entities.request.EmbeddingRequest
import org.jcnc.llmx.core.spi.entities.response.ChatResponsePart import org.jcnc.llmx.core.spi.entities.response.ChatResponsePart
import org.jcnc.llmx.core.spi.entities.response.EmbeddingResponse import org.jcnc.llmx.core.spi.entities.response.EmbeddingResponse
import org.reactivestreams.Publisher
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
@ -31,7 +30,7 @@ interface LLMProvider {
* @return 返回一个Flow流通过该流可以接收到聊天响应的部分数据如消息状态更新等 * @return 返回一个Flow流通过该流可以接收到聊天响应的部分数据如消息状态更新等
*/ */
@PostMapping("/chat", produces = [MediaType.APPLICATION_NDJSON_VALUE]) @PostMapping("/chat", produces = [MediaType.APPLICATION_NDJSON_VALUE])
fun chat(request: ChatRequest): Flow<ChatResponsePart> fun chat(request: ChatRequest): Publisher<ChatResponsePart>
/** /**
* 嵌入功能方法 * 嵌入功能方法

View File

@ -1,16 +1,19 @@
package org.jcnc.llmx.impl.baiLian.controller package org.jcnc.llmx.impl.baiLian.controller
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.asPublisher
import org.jcnc.llmx.core.spi.entities.request.ChatRequest import org.jcnc.llmx.core.spi.entities.request.ChatRequest
import org.jcnc.llmx.core.spi.entities.request.EmbeddingRequest import org.jcnc.llmx.core.spi.entities.request.EmbeddingRequest
import org.jcnc.llmx.core.spi.entities.response.ChatResponsePart import org.jcnc.llmx.core.spi.entities.response.ChatResponsePart
import org.jcnc.llmx.core.spi.entities.response.EmbeddingResponse import org.jcnc.llmx.core.spi.entities.response.EmbeddingResponse
import org.jcnc.llmx.core.spi.provider.LLMProvider import org.jcnc.llmx.core.spi.provider.LLMProvider
import org.jcnc.llmx.impl.baiLian.service.BaiLianModelService import org.jcnc.llmx.impl.baiLian.service.BaiLianModelService
import org.reactivestreams.Publisher
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
/** /**
*百炼提供商 *百炼提供商
* *
@ -18,10 +21,9 @@ import org.springframework.web.bind.annotation.RestController
* @author gewuyou * @author gewuyou
*/ */
@RestController @RestController
//@RequestMapping("/provider")
class BaiLianProvider( class BaiLianProvider(
private val baiLianModelService: BaiLianModelService private val baiLianModelService: BaiLianModelService
): LLMProvider { ) : LLMProvider {
/** /**
* 初始化与聊天服务的连接以处理聊天请求 * 初始化与聊天服务的连接以处理聊天请求
* *
@ -31,9 +33,8 @@ class BaiLianProvider(
* @param request 聊天请求对象包含建立聊天所需的信息如用户标识会话标识等 * @param request 聊天请求对象包含建立聊天所需的信息如用户标识会话标识等
* @return 返回一个Flow流通过该流可以接收到聊天响应的部分数据如消息状态更新等 * @return 返回一个Flow流通过该流可以接收到聊天响应的部分数据如消息状态更新等
*/ */
// @PostMapping("/chat") override fun chat(@RequestBody request: ChatRequest): Publisher<ChatResponsePart> {
override fun chat(@RequestBody request: ChatRequest): Flow<ChatResponsePart> { return baiLianModelService.streamChat(request).asPublisher()
return baiLianModelService.streamChat(request)
} }
/** /**