From 711cfcb3f6f3427258b03a62f2a82fe257eb7392 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Fri, 15 Mar 2024 19:45:52 +0800 Subject: [PATCH] =?UTF-8?q?client=20=E7=A7=BB=E9=99=A4=20AbstractFunctionC?= =?UTF-8?q?allSupport?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/ai/chatqianwen/QianWenApi.java | 2 + .../ai/chatqianwen/QianWenChatClient.java | 41 +--------------- .../ai/chatxinghuo/XingHuoChatClient.java | 33 +------------ .../ai/chatyiyan/YiYanChatClient.java | 47 ++----------------- .../function/AbstractFunctionCallSupport.java | 5 ++ .../ai/model/function/FunctionCallback.java | 7 +++ 6 files changed, 21 insertions(+), 114 deletions(-) diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenApi.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenApi.java index 93a92364c..08d3b8ca5 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenApi.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenApi.java @@ -78,6 +78,8 @@ public class QianWenApi { new CompletionsRequest() // 设置 appid .setAppId(appId) + // 开启 stream + .setStream(true) .setMessages(List.of(message)) //开启增量输出模式,后面输出不会包含已经输出的内容 .setParameters(new CompletionsRequest.Parameter().setIncrementalOutput(true)) diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java index 7b4a60baf..cf0eae0dd 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java @@ -5,13 +5,7 @@ import cn.iocoder.yudao.framework.ai.chat.ChatResponse; import cn.iocoder.yudao.framework.ai.chat.Generation; import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; -import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenChatCompletionMessage; -import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenChatCompletionRequest; -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletion; -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionRequest; import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; -import cn.iocoder.yudao.framework.ai.model.function.FunctionCallbackContext; import com.aliyun.broadscope.bailian.sdk.models.ChatRequestMessage; import com.aliyun.broadscope.bailian.sdk.models.ChatUserMessage; import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse; @@ -36,13 +30,11 @@ import java.util.stream.Collectors; * time: 2024/3/13 21:06 */ @Slf4j -public class QianWenChatClient extends AbstractFunctionCallSupport> - implements ChatClient, StreamingChatClient { +public class QianWenChatClient implements ChatClient, StreamingChatClient { private QianWenApi qianWenApi; public QianWenChatClient(QianWenApi qianWenApi) { - super(null); this.qianWenApi = qianWenApi; } @@ -61,10 +53,6 @@ public class QianWenChatClient extends AbstractFunctionCallSupport { @@ -72,7 +60,7 @@ public class QianWenChatClient extends AbstractFunctionCallSupport responseEntity = this.callWithFunctionSupport(request); + ResponseEntity responseEntity = qianWenApi.chatCompletionEntity(request); // 获取结果封装 chatCompletion CompletionsResponse response = responseEntity.getBody(); if (!response.isSuccess()) { @@ -100,29 +88,4 @@ public class QianWenChatClient extends AbstractFunctionCallSupport conversationHistory) { - return null; - } - - @Override - protected List doGetUserMessages(ChatRequestMessage request) { - return null; - } - - @Override - protected QianWenChatCompletionMessage doGetToolResponseMessage(ResponseEntity response) { - return null; - } - - @Override - protected ResponseEntity doChatCompletion(ChatRequestMessage request) { - return qianWenApi.chatCompletionEntity(request); - } - - @Override - protected boolean isToolFunctionCall(ResponseEntity response) { - return false; - } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatClient.java index a750e1c8f..23e85be6d 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatClient.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatClient.java @@ -6,10 +6,8 @@ import cn.iocoder.yudao.framework.ai.chat.Generation; import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletion; -import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletionMessage; import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletionRequest; import cn.iocoder.yudao.framework.ai.chatxinghuo.exception.XingHuoApiException; -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.retry.RetryCallback; @@ -29,8 +27,7 @@ import java.util.stream.Collectors; * time: 2024/3/11 10:19 */ @Slf4j -public class XingHuoChatClient extends AbstractFunctionCallSupport> - implements ChatClient, StreamingChatClient { +public class XingHuoChatClient implements ChatClient, StreamingChatClient { private XingHuoApi xingHuoApi; @@ -52,7 +49,6 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport response = this.callWithFunctionSupport(request); + ResponseEntity response = xingHuoApi.chatCompletionEntity(request); // 获取结果封装 ChatResponse return new ChatResponse(List.of(new Generation(response.getBody().getPayload().getChoices().getText().get(0).getContent()))); }); @@ -102,29 +98,4 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport conversationHistory) { - return null; - } - - @Override - protected List doGetUserMessages(XingHuoChatCompletionRequest request) { - return null; - } - - @Override - protected XingHuoChatCompletionMessage doGetToolResponseMessage(ResponseEntity response) { - return null; - } - - @Override - protected ResponseEntity doChatCompletion(XingHuoChatCompletionRequest request) { - return xingHuoApi.chatCompletionEntity(request); - } - - @Override - protected boolean isToolFunctionCall(ResponseEntity response) { - return false; - } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatClient.java index fb7f155b4..58cc622c4 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatClient.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatClient.java @@ -7,13 +7,9 @@ import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; import cn.iocoder.yudao.framework.ai.chat.messages.Message; import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletion; -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionMessage; import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionRequest; import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; -import cn.iocoder.yudao.framework.ai.model.function.FunctionCallbackContext; import lombok.extern.slf4j.Slf4j; - import org.springframework.http.ResponseEntity; import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; @@ -32,14 +28,11 @@ import java.util.List; * time: 2024/3/8 19:11 */ @Slf4j -public class YiYanChatClient - extends AbstractFunctionCallSupport> - implements ChatClient, StreamingChatClient { +public class YiYanChatClient implements ChatClient, StreamingChatClient { private YiYanApi yiYanApi; public YiYanChatClient(YiYanApi yiYanApi) { - super(new FunctionCallbackContext()); this.yiYanApi = yiYanApi; } @@ -70,7 +63,7 @@ public class YiYanChatClient // 创建 request 请求,stream模式需要供应商支持 YiYanChatCompletionRequest request = this.createRequest(prompt, false); // 调用 callWithFunctionSupport 发送请求 - ResponseEntity response = this.callWithFunctionSupport(request); + ResponseEntity response = yiYanApi.chatCompletionEntity(request); // 获取结果封装 ChatResponse YiYanChatCompletion chatCompletion = response.getBody(); return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); @@ -98,40 +91,6 @@ public class YiYanChatClient YiYanChatCompletionRequest request = this.createRequest(prompt, true); // 调用 callWithFunctionSupport 发送请求 Flux response = this.yiYanApi.chatCompletionStream(request); -// response.subscribe(new Consumer() { -// @Override -// public void accept(YiYanChatCompletion chatCompletion) { -// // {"id":"as-p0nfjuuasg","object":"chat.completion","created":1710033402,"sentence_id":0,"is_end":false,"is_truncated":false,"result":"编程语","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":5,"completion_tokens":0,"total_tokens":5}} -// System.err.println(chatCompletion); -// } -// }); - return response.map(res -> { - return new ChatResponse(List.of(new Generation(res.getResult()))); - }); - } - - @Override - protected YiYanChatCompletionRequest doCreateToolResponseRequest(YiYanChatCompletionRequest previousRequest, YiYanChatCompletionMessage responseMessage, List conversationHistory) { - return null; - } - - @Override - protected List doGetUserMessages(YiYanChatCompletionRequest request) { - return null; - } - - @Override - protected YiYanChatCompletionMessage doGetToolResponseMessage(ResponseEntity response) { - return null; - } - - @Override - protected ResponseEntity doChatCompletion(YiYanChatCompletionRequest request) { - return yiYanApi.chatCompletionEntity(request); - } - - @Override - protected boolean isToolFunctionCall(ResponseEntity response) { - return false; + return response.map(res -> new ChatResponse(List.of(new Generation(res.getResult())))); } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/AbstractFunctionCallSupport.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/AbstractFunctionCallSupport.java index d5becdbb5..33409f72d 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/AbstractFunctionCallSupport.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/AbstractFunctionCallSupport.java @@ -29,11 +29,16 @@ public abstract class AbstractFunctionCallSupport { protected final static boolean IS_RUNTIME_CALL = true; /** + * 函数回调寄存器用于按名称解析函数回调。 + * * The function callback register is used to resolve the function callbacks by name. */ protected final Map functionCallbackRegister = new ConcurrentHashMap<>(); /** + * 函数回调上下文用于按名称解析函数回调来自Spring上下文。 + * 它是可选的,通常与Spring一起使用自动配置。 + * * The function callback context is used to resolve the function callbacks by name * from the Spring context. It is optional and usually used with Spring * auto-configuration. diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/FunctionCallback.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/FunctionCallback.java index d644d517a..a14a59b8f 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/FunctionCallback.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/FunctionCallback.java @@ -17,6 +17,13 @@ package cn.iocoder.yudao.framework.ai.model.function; /** + * + * 表示模型函数调用处理程序。实现已向注册对触发函数调用的提示进行建模和调用。 + * + * https://blog.csdn.net/weixin_37546425/article/details/136402740 + * + * https://www.163.com/dy/article/ICE2S20P05119NPR.html + * * Represents a model function call handler. Implementations are registered with the * Models and called on prompts that trigger the function call. *