Commit 711cfcb3f6f3427258b03a62f2a82fe257eb7392
1 parent
26afa04e
client 移除 AbstractFunctionCallSupport
Showing
6 changed files
with
21 additions
and
114 deletions
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 { |
| 78 | 78 | new CompletionsRequest() |
| 79 | 79 | // 设置 appid |
| 80 | 80 | .setAppId(appId) |
| 81 | + // 开启 stream | |
| 82 | + .setStream(true) | |
| 81 | 83 | .setMessages(List.of(message)) |
| 82 | 84 | //开启增量输出模式,后面输出不会包含已经输出的内容 |
| 83 | 85 | .setParameters(new CompletionsRequest.Parameter().setIncrementalOutput(true)) | ... | ... |
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; |
| 5 | 5 | import cn.iocoder.yudao.framework.ai.chat.Generation; |
| 6 | 6 | import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; |
| 7 | 7 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; |
| 8 | -import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenChatCompletionMessage; | |
| 9 | -import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenChatCompletionRequest; | |
| 10 | -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletion; | |
| 11 | -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionRequest; | |
| 12 | 8 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; |
| 13 | -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; | |
| 14 | -import cn.iocoder.yudao.framework.ai.model.function.FunctionCallbackContext; | |
| 15 | 9 | import com.aliyun.broadscope.bailian.sdk.models.ChatRequestMessage; |
| 16 | 10 | import com.aliyun.broadscope.bailian.sdk.models.ChatUserMessage; |
| 17 | 11 | import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse; |
| ... | ... | @@ -36,13 +30,11 @@ import java.util.stream.Collectors; |
| 36 | 30 | * time: 2024/3/13 21:06 |
| 37 | 31 | */ |
| 38 | 32 | @Slf4j |
| 39 | -public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCompletionMessage, ChatRequestMessage, ResponseEntity<CompletionsResponse>> | |
| 40 | - implements ChatClient, StreamingChatClient { | |
| 33 | +public class QianWenChatClient implements ChatClient, StreamingChatClient { | |
| 41 | 34 | |
| 42 | 35 | private QianWenApi qianWenApi; |
| 43 | 36 | |
| 44 | 37 | public QianWenChatClient(QianWenApi qianWenApi) { |
| 45 | - super(null); | |
| 46 | 38 | this.qianWenApi = qianWenApi; |
| 47 | 39 | } |
| 48 | 40 | |
| ... | ... | @@ -61,10 +53,6 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo |
| 61 | 53 | }) |
| 62 | 54 | .build(); |
| 63 | 55 | |
| 64 | - public QianWenChatClient(FunctionCallbackContext functionCallbackContext) { | |
| 65 | - super(functionCallbackContext); | |
| 66 | - } | |
| 67 | - | |
| 68 | 56 | @Override |
| 69 | 57 | public ChatResponse call(Prompt prompt) { |
| 70 | 58 | return this.retryTemplate.execute(ctx -> { |
| ... | ... | @@ -72,7 +60,7 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo |
| 72 | 60 | // 创建 request 请求,stream模式需要供应商支持 |
| 73 | 61 | ChatRequestMessage request = this.createRequest(prompt, false); |
| 74 | 62 | // 调用 callWithFunctionSupport 发送请求 |
| 75 | - ResponseEntity<CompletionsResponse> responseEntity = this.callWithFunctionSupport(request); | |
| 63 | + ResponseEntity<CompletionsResponse> responseEntity = qianWenApi.chatCompletionEntity(request); | |
| 76 | 64 | // 获取结果封装 chatCompletion |
| 77 | 65 | CompletionsResponse response = responseEntity.getBody(); |
| 78 | 66 | if (!response.isSuccess()) { |
| ... | ... | @@ -100,29 +88,4 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo |
| 100 | 88 | return new ChatResponse(List.of(new Generation(res.getData().getText()))); |
| 101 | 89 | }); |
| 102 | 90 | } |
| 103 | - | |
| 104 | - @Override | |
| 105 | - protected QianWenChatCompletionRequest doCreateToolResponseRequest(ChatRequestMessage previousRequest, QianWenChatCompletionMessage responseMessage, List<QianWenChatCompletionMessage> conversationHistory) { | |
| 106 | - return null; | |
| 107 | - } | |
| 108 | - | |
| 109 | - @Override | |
| 110 | - protected List<QianWenChatCompletionMessage> doGetUserMessages(ChatRequestMessage request) { | |
| 111 | - return null; | |
| 112 | - } | |
| 113 | - | |
| 114 | - @Override | |
| 115 | - protected QianWenChatCompletionMessage doGetToolResponseMessage(ResponseEntity<CompletionsResponse> response) { | |
| 116 | - return null; | |
| 117 | - } | |
| 118 | - | |
| 119 | - @Override | |
| 120 | - protected ResponseEntity<CompletionsResponse> doChatCompletion(ChatRequestMessage request) { | |
| 121 | - return qianWenApi.chatCompletionEntity(request); | |
| 122 | - } | |
| 123 | - | |
| 124 | - @Override | |
| 125 | - protected boolean isToolFunctionCall(ResponseEntity<CompletionsResponse> response) { | |
| 126 | - return false; | |
| 127 | - } | |
| 128 | 91 | } | ... | ... |
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; |
| 6 | 6 | import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; |
| 7 | 7 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; |
| 8 | 8 | import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletion; |
| 9 | -import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletionMessage; | |
| 10 | 9 | import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoChatCompletionRequest; |
| 11 | 10 | import cn.iocoder.yudao.framework.ai.chatxinghuo.exception.XingHuoApiException; |
| 12 | -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; | |
| 13 | 11 | import lombok.extern.slf4j.Slf4j; |
| 14 | 12 | import org.springframework.http.ResponseEntity; |
| 15 | 13 | import org.springframework.retry.RetryCallback; |
| ... | ... | @@ -29,8 +27,7 @@ import java.util.stream.Collectors; |
| 29 | 27 | * time: 2024/3/11 10:19 |
| 30 | 28 | */ |
| 31 | 29 | @Slf4j |
| 32 | -public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCompletionMessage, XingHuoChatCompletionRequest, ResponseEntity<XingHuoChatCompletion>> | |
| 33 | - implements ChatClient, StreamingChatClient { | |
| 30 | +public class XingHuoChatClient implements ChatClient, StreamingChatClient { | |
| 34 | 31 | |
| 35 | 32 | private XingHuoApi xingHuoApi; |
| 36 | 33 | |
| ... | ... | @@ -52,7 +49,6 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo |
| 52 | 49 | .build(); |
| 53 | 50 | |
| 54 | 51 | public XingHuoChatClient(XingHuoApi xingHuoApi) { |
| 55 | - super(null); | |
| 56 | 52 | this.xingHuoApi = xingHuoApi; |
| 57 | 53 | } |
| 58 | 54 | |
| ... | ... | @@ -64,7 +60,7 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo |
| 64 | 60 | // 创建 request 请求,stream模式需要供应商支持 |
| 65 | 61 | XingHuoChatCompletionRequest request = this.createRequest(prompt, false); |
| 66 | 62 | // 调用 callWithFunctionSupport 发送请求 |
| 67 | - ResponseEntity<XingHuoChatCompletion> response = this.callWithFunctionSupport(request); | |
| 63 | + ResponseEntity<XingHuoChatCompletion> response = xingHuoApi.chatCompletionEntity(request); | |
| 68 | 64 | // 获取结果封装 ChatResponse |
| 69 | 65 | return new ChatResponse(List.of(new Generation(response.getBody().getPayload().getChoices().getText().get(0).getContent()))); |
| 70 | 66 | }); |
| ... | ... | @@ -102,29 +98,4 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo |
| 102 | 98 | return new ChatResponse(List.of(new Generation(content))); |
| 103 | 99 | }); |
| 104 | 100 | } |
| 105 | - | |
| 106 | - @Override | |
| 107 | - protected XingHuoChatCompletionRequest doCreateToolResponseRequest(XingHuoChatCompletionRequest previousRequest, XingHuoChatCompletionMessage responseMessage, List<XingHuoChatCompletionMessage> conversationHistory) { | |
| 108 | - return null; | |
| 109 | - } | |
| 110 | - | |
| 111 | - @Override | |
| 112 | - protected List<XingHuoChatCompletionMessage> doGetUserMessages(XingHuoChatCompletionRequest request) { | |
| 113 | - return null; | |
| 114 | - } | |
| 115 | - | |
| 116 | - @Override | |
| 117 | - protected XingHuoChatCompletionMessage doGetToolResponseMessage(ResponseEntity<XingHuoChatCompletion> response) { | |
| 118 | - return null; | |
| 119 | - } | |
| 120 | - | |
| 121 | - @Override | |
| 122 | - protected ResponseEntity<XingHuoChatCompletion> doChatCompletion(XingHuoChatCompletionRequest request) { | |
| 123 | - return xingHuoApi.chatCompletionEntity(request); | |
| 124 | - } | |
| 125 | - | |
| 126 | - @Override | |
| 127 | - protected boolean isToolFunctionCall(ResponseEntity<XingHuoChatCompletion> response) { | |
| 128 | - return false; | |
| 129 | - } | |
| 130 | 101 | } | ... | ... |
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; |
| 7 | 7 | import cn.iocoder.yudao.framework.ai.chat.messages.Message; |
| 8 | 8 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; |
| 9 | 9 | import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletion; |
| 10 | -import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionMessage; | |
| 11 | 10 | import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanChatCompletionRequest; |
| 12 | 11 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; |
| 13 | -import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport; | |
| 14 | -import cn.iocoder.yudao.framework.ai.model.function.FunctionCallbackContext; | |
| 15 | 12 | import lombok.extern.slf4j.Slf4j; |
| 16 | - | |
| 17 | 13 | import org.springframework.http.ResponseEntity; |
| 18 | 14 | import org.springframework.retry.RetryCallback; |
| 19 | 15 | import org.springframework.retry.RetryContext; |
| ... | ... | @@ -32,14 +28,11 @@ import java.util.List; |
| 32 | 28 | * time: 2024/3/8 19:11 |
| 33 | 29 | */ |
| 34 | 30 | @Slf4j |
| 35 | -public class YiYanChatClient | |
| 36 | - extends AbstractFunctionCallSupport<YiYanChatCompletionMessage, YiYanChatCompletionRequest, ResponseEntity<YiYanChatCompletion>> | |
| 37 | - implements ChatClient, StreamingChatClient { | |
| 31 | +public class YiYanChatClient implements ChatClient, StreamingChatClient { | |
| 38 | 32 | |
| 39 | 33 | private YiYanApi yiYanApi; |
| 40 | 34 | |
| 41 | 35 | public YiYanChatClient(YiYanApi yiYanApi) { |
| 42 | - super(new FunctionCallbackContext()); | |
| 43 | 36 | this.yiYanApi = yiYanApi; |
| 44 | 37 | } |
| 45 | 38 | |
| ... | ... | @@ -70,7 +63,7 @@ public class YiYanChatClient |
| 70 | 63 | // 创建 request 请求,stream模式需要供应商支持 |
| 71 | 64 | YiYanChatCompletionRequest request = this.createRequest(prompt, false); |
| 72 | 65 | // 调用 callWithFunctionSupport 发送请求 |
| 73 | - ResponseEntity<YiYanChatCompletion> response = this.callWithFunctionSupport(request); | |
| 66 | + ResponseEntity<YiYanChatCompletion> response = yiYanApi.chatCompletionEntity(request); | |
| 74 | 67 | // 获取结果封装 ChatResponse |
| 75 | 68 | YiYanChatCompletion chatCompletion = response.getBody(); |
| 76 | 69 | return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); |
| ... | ... | @@ -98,40 +91,6 @@ public class YiYanChatClient |
| 98 | 91 | YiYanChatCompletionRequest request = this.createRequest(prompt, true); |
| 99 | 92 | // 调用 callWithFunctionSupport 发送请求 |
| 100 | 93 | Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request); |
| 101 | -// response.subscribe(new Consumer<YiYanChatCompletion>() { | |
| 102 | -// @Override | |
| 103 | -// public void accept(YiYanChatCompletion chatCompletion) { | |
| 104 | -// // {"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}} | |
| 105 | -// System.err.println(chatCompletion); | |
| 106 | -// } | |
| 107 | -// }); | |
| 108 | - return response.map(res -> { | |
| 109 | - return new ChatResponse(List.of(new Generation(res.getResult()))); | |
| 110 | - }); | |
| 111 | - } | |
| 112 | - | |
| 113 | - @Override | |
| 114 | - protected YiYanChatCompletionRequest doCreateToolResponseRequest(YiYanChatCompletionRequest previousRequest, YiYanChatCompletionMessage responseMessage, List<YiYanChatCompletionMessage> conversationHistory) { | |
| 115 | - return null; | |
| 116 | - } | |
| 117 | - | |
| 118 | - @Override | |
| 119 | - protected List<YiYanChatCompletionMessage> doGetUserMessages(YiYanChatCompletionRequest request) { | |
| 120 | - return null; | |
| 121 | - } | |
| 122 | - | |
| 123 | - @Override | |
| 124 | - protected YiYanChatCompletionMessage doGetToolResponseMessage(ResponseEntity<YiYanChatCompletion> response) { | |
| 125 | - return null; | |
| 126 | - } | |
| 127 | - | |
| 128 | - @Override | |
| 129 | - protected ResponseEntity<YiYanChatCompletion> doChatCompletion(YiYanChatCompletionRequest request) { | |
| 130 | - return yiYanApi.chatCompletionEntity(request); | |
| 131 | - } | |
| 132 | - | |
| 133 | - @Override | |
| 134 | - protected boolean isToolFunctionCall(ResponseEntity<YiYanChatCompletion> response) { | |
| 135 | - return false; | |
| 94 | + return response.map(res -> new ChatResponse(List.of(new Generation(res.getResult())))); | |
| 136 | 95 | } |
| 137 | 96 | } | ... | ... |
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<Msg, Req, Resp> { |
| 29 | 29 | protected final static boolean IS_RUNTIME_CALL = true; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | + * 函数回调寄存器用于按名称解析函数回调。 | |
| 33 | + * | |
| 32 | 34 | * The function callback register is used to resolve the function callbacks by name. |
| 33 | 35 | */ |
| 34 | 36 | protected final Map<String, FunctionCallback> functionCallbackRegister = new ConcurrentHashMap<>(); |
| 35 | 37 | |
| 36 | 38 | /** |
| 39 | + * 函数回调上下文用于按名称解析函数回调来自Spring上下文。 | |
| 40 | + * 它是可选的,通常与Spring一起使用自动配置。 | |
| 41 | + * | |
| 37 | 42 | * The function callback context is used to resolve the function callbacks by name |
| 38 | 43 | * from the Spring context. It is optional and usually used with Spring |
| 39 | 44 | * auto-configuration. | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/model/function/FunctionCallback.java
| ... | ... | @@ -17,6 +17,13 @@ |
| 17 | 17 | package cn.iocoder.yudao.framework.ai.model.function; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | + * | |
| 21 | + * 表示模型函数调用处理程序。实现已向注册对触发函数调用的提示进行建模和调用。 | |
| 22 | + * | |
| 23 | + * https://blog.csdn.net/weixin_37546425/article/details/136402740 | |
| 24 | + * | |
| 25 | + * https://www.163.com/dy/article/ICE2S20P05119NPR.html | |
| 26 | + * | |
| 20 | 27 | * Represents a model function call handler. Implementations are registered with the |
| 21 | 28 | * Models and called on prompts that trigger the function call. |
| 22 | 29 | * | ... | ... |