ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Solon AI 开发学习15 - chat - 拦截器

Solon AI 开发学习15 - 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、应用示例

记录日志

import lombok.extern.slf4j.Slf4j;
import org.noear.solon.ai.chat.ChatRequest;
import org.noear.solon.ai.chat.ChatResponse;
import org.noear.solon.ai.chat.interceptor.*;
import org.reactivestreams.Publisher;import java.io.IOException;@Slf4j
public class ChatLogInterceptor implements ChatInterceptor {@Overridepublic ChatResponse interceptCall(ChatRequest req, CallChain chain) throws IOException {log.warn("ChatInterceptor-interceptCall: " + req.getConfig().getModel());return chain.doIntercept(req);}@Overridepublic Publisher<ChatResponse> interceptStream(ChatRequest req, StreamChain chain) {log.warn("ChatInterceptor-interceptStream: " + req.getConfig().getModel());return chain.doIntercept(req);}@Overridepublic 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 添加拦截器。
返回列表