chat - 拦截器
聊天拦截器,是专门给 ChatModel 使用的拦截器。主要作用有:
- 记录请求或响应日志
- 检查数据与道德安全
- 修改请求数据
- 修改响应数据
1、ChatInterceptor 接口
public interface ChatInterceptor {
/**
* 拦截 Call 请求
*
* @param req 请求
* @param chain 拦截链
*/
ChatResponse interceptCall(ChatRequest req, CallChain chain) throws IOException;
/**
* 拦截 Stream 请求
*
* @param req 请求
* @param chain 拦截链
*/
Publisher<ChatResponse> interceptStream(ChatRequest req, StreamChain chain);
/**
* 拦截工具
*
* @param req 请求
* @param chain 拦截链
*/
String interceptTool(ToolRequest req, ToolChain chain) throws Throwable;
}
日志提示:
- ChatRequest:toRequestData,可以获取请求的原始数据
- ChatResponse:getResponseData,可以获取响应的原始数据
2、应用示例
记录日志
@Slf4j
public class ChatLogInterceptor implements ChatInterceptor {
@Override
public ChatResponse interceptCall(ChatRequest req, CallChain chain) throws IOException {
log.warn("ChatInterceptor-interceptCall: " + req.getConfig().getModel());
return chain.doIntercept(req);
}
@Override
public Publisher<ChatResponse> interceptStream(ChatRequest req, StreamChain chain) {
log.warn("ChatInterceptor-interceptStream: " + req.getConfig().getModel());
return chain.doIntercept(req);
}
@Override
public String interceptTool(ToolRequest req, ToolChain chain) throws Throwable {
log.warn("ChatInterceptor-interceptTool: " + req.getConfig().getModel());
return chain.doIntercept(req);
}
}
private ChatModel.Builder getChatModelBuilder() {
return ChatModel.of(apiUrl)
.apiKey(apiKey)
.model(model)
.defaultInterceptorAdd(new ChatLogInterceptor());
}
//或者请求时,通过 options 添加拦截器。
检查敏感词,待写...