client 移除 AbstractFunctionCallSupport

This commit is contained in:
cherishsince 2024-03-15 19:45:52 +08:00
parent 26afa04e80
commit 711cfcb3f6
6 changed files with 21 additions and 114 deletions

View File

@ -78,6 +78,8 @@ public class QianWenApi {
new CompletionsRequest() new CompletionsRequest()
// 设置 appid // 设置 appid
.setAppId(appId) .setAppId(appId)
// 开启 stream
.setStream(true)
.setMessages(List.of(message)) .setMessages(List.of(message))
//开启增量输出模式后面输出不会包含已经输出的内容 //开启增量输出模式后面输出不会包含已经输出的内容
.setParameters(new CompletionsRequest.Parameter().setIncrementalOutput(true)) .setParameters(new CompletionsRequest.Parameter().setIncrementalOutput(true))

View File

@ -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.Generation;
import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient; import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient;
import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; 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.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.ChatRequestMessage;
import com.aliyun.broadscope.bailian.sdk.models.ChatUserMessage; import com.aliyun.broadscope.bailian.sdk.models.ChatUserMessage;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse; import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse;
@ -36,13 +30,11 @@ import java.util.stream.Collectors;
* time: 2024/3/13 21:06 * time: 2024/3/13 21:06
*/ */
@Slf4j @Slf4j
public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCompletionMessage, ChatRequestMessage, ResponseEntity<CompletionsResponse>> public class QianWenChatClient implements ChatClient, StreamingChatClient {
implements ChatClient, StreamingChatClient {
private QianWenApi qianWenApi; private QianWenApi qianWenApi;
public QianWenChatClient(QianWenApi qianWenApi) { public QianWenChatClient(QianWenApi qianWenApi) {
super(null);
this.qianWenApi = qianWenApi; this.qianWenApi = qianWenApi;
} }
@ -61,10 +53,6 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo
}) })
.build(); .build();
public QianWenChatClient(FunctionCallbackContext functionCallbackContext) {
super(functionCallbackContext);
}
@Override @Override
public ChatResponse call(Prompt prompt) { public ChatResponse call(Prompt prompt) {
return this.retryTemplate.execute(ctx -> { return this.retryTemplate.execute(ctx -> {
@ -72,7 +60,7 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo
// 创建 request 请求stream模式需要供应商支持 // 创建 request 请求stream模式需要供应商支持
ChatRequestMessage request = this.createRequest(prompt, false); ChatRequestMessage request = this.createRequest(prompt, false);
// 调用 callWithFunctionSupport 发送请求 // 调用 callWithFunctionSupport 发送请求
ResponseEntity<CompletionsResponse> responseEntity = this.callWithFunctionSupport(request); ResponseEntity<CompletionsResponse> responseEntity = qianWenApi.chatCompletionEntity(request);
// 获取结果封装 chatCompletion // 获取结果封装 chatCompletion
CompletionsResponse response = responseEntity.getBody(); CompletionsResponse response = responseEntity.getBody();
if (!response.isSuccess()) { if (!response.isSuccess()) {
@ -100,29 +88,4 @@ public class QianWenChatClient extends AbstractFunctionCallSupport<QianWenChatCo
return new ChatResponse(List.of(new Generation(res.getData().getText()))); return new ChatResponse(List.of(new Generation(res.getData().getText())));
}); });
} }
@Override
protected QianWenChatCompletionRequest doCreateToolResponseRequest(ChatRequestMessage previousRequest, QianWenChatCompletionMessage responseMessage, List<QianWenChatCompletionMessage> conversationHistory) {
return null;
}
@Override
protected List<QianWenChatCompletionMessage> doGetUserMessages(ChatRequestMessage request) {
return null;
}
@Override
protected QianWenChatCompletionMessage doGetToolResponseMessage(ResponseEntity<CompletionsResponse> response) {
return null;
}
@Override
protected ResponseEntity<CompletionsResponse> doChatCompletion(ChatRequestMessage request) {
return qianWenApi.chatCompletionEntity(request);
}
@Override
protected boolean isToolFunctionCall(ResponseEntity<CompletionsResponse> response) {
return false;
}
} }

View File

@ -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.StreamingChatClient;
import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; 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.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.api.XingHuoChatCompletionRequest;
import cn.iocoder.yudao.framework.ai.chatxinghuo.exception.XingHuoApiException; import cn.iocoder.yudao.framework.ai.chatxinghuo.exception.XingHuoApiException;
import cn.iocoder.yudao.framework.ai.model.function.AbstractFunctionCallSupport;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryCallback;
@ -29,8 +27,7 @@ import java.util.stream.Collectors;
* time: 2024/3/11 10:19 * time: 2024/3/11 10:19
*/ */
@Slf4j @Slf4j
public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCompletionMessage, XingHuoChatCompletionRequest, ResponseEntity<XingHuoChatCompletion>> public class XingHuoChatClient implements ChatClient, StreamingChatClient {
implements ChatClient, StreamingChatClient {
private XingHuoApi xingHuoApi; private XingHuoApi xingHuoApi;
@ -52,7 +49,6 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo
.build(); .build();
public XingHuoChatClient(XingHuoApi xingHuoApi) { public XingHuoChatClient(XingHuoApi xingHuoApi) {
super(null);
this.xingHuoApi = xingHuoApi; this.xingHuoApi = xingHuoApi;
} }
@ -64,7 +60,7 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo
// 创建 request 请求stream模式需要供应商支持 // 创建 request 请求stream模式需要供应商支持
XingHuoChatCompletionRequest request = this.createRequest(prompt, false); XingHuoChatCompletionRequest request = this.createRequest(prompt, false);
// 调用 callWithFunctionSupport 发送请求 // 调用 callWithFunctionSupport 发送请求
ResponseEntity<XingHuoChatCompletion> response = this.callWithFunctionSupport(request); ResponseEntity<XingHuoChatCompletion> response = xingHuoApi.chatCompletionEntity(request);
// 获取结果封装 ChatResponse // 获取结果封装 ChatResponse
return new ChatResponse(List.of(new Generation(response.getBody().getPayload().getChoices().getText().get(0).getContent()))); return new ChatResponse(List.of(new Generation(response.getBody().getPayload().getChoices().getText().get(0).getContent())));
}); });
@ -102,29 +98,4 @@ public class XingHuoChatClient extends AbstractFunctionCallSupport<XingHuoChatCo
return new ChatResponse(List.of(new Generation(content))); return new ChatResponse(List.of(new Generation(content)));
}); });
} }
@Override
protected XingHuoChatCompletionRequest doCreateToolResponseRequest(XingHuoChatCompletionRequest previousRequest, XingHuoChatCompletionMessage responseMessage, List<XingHuoChatCompletionMessage> conversationHistory) {
return null;
}
@Override
protected List<XingHuoChatCompletionMessage> doGetUserMessages(XingHuoChatCompletionRequest request) {
return null;
}
@Override
protected XingHuoChatCompletionMessage doGetToolResponseMessage(ResponseEntity<XingHuoChatCompletion> response) {
return null;
}
@Override
protected ResponseEntity<XingHuoChatCompletion> doChatCompletion(XingHuoChatCompletionRequest request) {
return xingHuoApi.chatCompletionEntity(request);
}
@Override
protected boolean isToolFunctionCall(ResponseEntity<XingHuoChatCompletion> response) {
return false;
}
} }

View File

@ -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.messages.Message;
import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; 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.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.api.YiYanChatCompletionRequest;
import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; 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 lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext; import org.springframework.retry.RetryContext;
@ -32,14 +28,11 @@ import java.util.List;
* time: 2024/3/8 19:11 * time: 2024/3/8 19:11
*/ */
@Slf4j @Slf4j
public class YiYanChatClient public class YiYanChatClient implements ChatClient, StreamingChatClient {
extends AbstractFunctionCallSupport<YiYanChatCompletionMessage, YiYanChatCompletionRequest, ResponseEntity<YiYanChatCompletion>>
implements ChatClient, StreamingChatClient {
private YiYanApi yiYanApi; private YiYanApi yiYanApi;
public YiYanChatClient(YiYanApi yiYanApi) { public YiYanChatClient(YiYanApi yiYanApi) {
super(new FunctionCallbackContext());
this.yiYanApi = yiYanApi; this.yiYanApi = yiYanApi;
} }
@ -70,7 +63,7 @@ public class YiYanChatClient
// 创建 request 请求stream模式需要供应商支持 // 创建 request 请求stream模式需要供应商支持
YiYanChatCompletionRequest request = this.createRequest(prompt, false); YiYanChatCompletionRequest request = this.createRequest(prompt, false);
// 调用 callWithFunctionSupport 发送请求 // 调用 callWithFunctionSupport 发送请求
ResponseEntity<YiYanChatCompletion> response = this.callWithFunctionSupport(request); ResponseEntity<YiYanChatCompletion> response = yiYanApi.chatCompletionEntity(request);
// 获取结果封装 ChatResponse // 获取结果封装 ChatResponse
YiYanChatCompletion chatCompletion = response.getBody(); YiYanChatCompletion chatCompletion = response.getBody();
return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); return new ChatResponse(List.of(new Generation(chatCompletion.getResult())));
@ -98,40 +91,6 @@ public class YiYanChatClient
YiYanChatCompletionRequest request = this.createRequest(prompt, true); YiYanChatCompletionRequest request = this.createRequest(prompt, true);
// 调用 callWithFunctionSupport 发送请求 // 调用 callWithFunctionSupport 发送请求
Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request); Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request);
// response.subscribe(new Consumer<YiYanChatCompletion>() { return response.map(res -> new ChatResponse(List.of(new Generation(res.getResult()))));
// @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<YiYanChatCompletionMessage> conversationHistory) {
return null;
}
@Override
protected List<YiYanChatCompletionMessage> doGetUserMessages(YiYanChatCompletionRequest request) {
return null;
}
@Override
protected YiYanChatCompletionMessage doGetToolResponseMessage(ResponseEntity<YiYanChatCompletion> response) {
return null;
}
@Override
protected ResponseEntity<YiYanChatCompletion> doChatCompletion(YiYanChatCompletionRequest request) {
return yiYanApi.chatCompletionEntity(request);
}
@Override
protected boolean isToolFunctionCall(ResponseEntity<YiYanChatCompletion> response) {
return false;
} }
} }

View File

@ -29,11 +29,16 @@ public abstract class AbstractFunctionCallSupport<Msg, Req, Resp> {
protected final static boolean IS_RUNTIME_CALL = true; protected final static boolean IS_RUNTIME_CALL = true;
/** /**
* 函数回调寄存器用于按名称解析函数回调
*
* The function callback register is used to resolve the function callbacks by name. * The function callback register is used to resolve the function callbacks by name.
*/ */
protected final Map<String, FunctionCallback> functionCallbackRegister = new ConcurrentHashMap<>(); protected final Map<String, FunctionCallback> functionCallbackRegister = new ConcurrentHashMap<>();
/** /**
* 函数回调上下文用于按名称解析函数回调来自Spring上下文
* 它是可选的通常与Spring一起使用自动配置
*
* The function callback context is used to resolve the function callbacks by name * 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 * from the Spring context. It is optional and usually used with Spring
* auto-configuration. * auto-configuration.

View File

@ -17,6 +17,13 @@
package cn.iocoder.yudao.framework.ai.model.function; 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 * Represents a model function call handler. Implementations are registered with the
* Models and called on prompts that trigger the function call. * Models and called on prompts that trigger the function call.
* *