本文主要是介绍【自用】chatglm-sdk-java详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
chatglm-sdk-java详解
整体项目的流程大致为:
通过工厂模式创建ChatGLMSessionFactory工厂,每个请求创建一个ChatGLMSession,通过ChatGLMSession中的方法来实现与ChatGLM的交互对接。
分为几个模块,executor,interceptor,model,session,utils,GLMApi接口,
其中session用来构建session工厂和session实例,executor是真正的chatglm调用服务接口,model部分负责封装请求和响应。
GLMApi调用了retrofit模块进行发送消息,当然也可以不使用retrofit,使用okhttp3的自带模块进行发送。
ChatGLMSessionFactory
public class IChatGLMSessionFactory implements ChatGLMSessionFactory {private final Configuration configuration;public IChatGLMSessionFactory(Configuration configuration) {this.configuration = configuration;}@Overridepublic ChatGLMSession create() {// 1. 日志配置HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();httpLoggingInterceptor.setLevel(configuration.getLevel());// 2. 开启 Http 客户端OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).addInterceptor(new GLMHTTPInterceptor(configuration)).connectTimeout(configuration.getConnectTimeout(), TimeUnit.SECONDS).writeTimeout(configuration.getWriteTimeout(),TimeUnit.SECONDS).readTimeout(configuration.getReadTimeout(),TimeUnit.SECONDS).build();configuration.setOkHttpClient(okHttpClient);// // 3. 创建 API 服务;如果想用glmapi发送则使用,缺点是不能调用okhttp的enventlistener,自己对返回值进行处理
// IGLMApi glmapi = new Retrofit.Builder()
// .baseUrl(configuration.getApiHost())
// .client(okHttpClient)
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
// .addConverterFactory(JacksonConverterFactory.create())
// .build().create(IGLMApi.class);
//
// configuration.setGlmApi(glmapi);HashMap<Model, Executor> executors = configuration.newExecutorGroup();return new IChatGLMSession(configuration, executors);}
}
Configuration
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class Configuration {// 智普Ai ChatGlM 请求地址@Getter@Setterprivate String apiHost = "https://open.bigmodel.cn/";// 智普Ai https://open.bigmodel.cn/usercenter/apikeys - apiSecretKey = {apiKey}.{apiSecret}private String apiSecretKey;public void setApiSecretKey(String apiSecretKey) {this.apiSecretKey = apiSecretKey;String[] arrStr = apiSecretKey.split("\\.");if (arrStr.length != 2) {throw new RuntimeException("invalid apiSecretKey");}this.apiKey = arrStr[0];this.apiSecret = arrStr[1];}@Getterprivate String apiKey;@Getterprivate String apiSecret;// Api 服务@Setter@Getterprivate IGLMApi glmApi;@Getter@Setterprivate OkHttpClient okHttpClient;public EventSource.Factory createRequestFactory() {return EventSources.createFactory(okHttpClient);}// OkHttp 配置信息@Setter@Getterprivate HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.HEADERS;@Setter@Getterprivate long connectTimeout = 450;@Setter@Getterprivate long writeTimeout = 450;@Setter@Getterprivate long readTimeout = 450;private HashMap<Model, Executor> executorGroup;// http keywords基础信息,用于配置factory时使用public static final String SSE_CONTENT_TYPE = "text/event-stream";public static final String DEFAULT_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";public static final String APPLICATION_JSON = "application/json";public static final String JSON_CONTENT_TYPE = APPLICATION_JSON + "; charset=utf-8";//构造执行器public HashMap<Model, Executor> newExecutorGroup() {this.executorGroup = new HashMap<>();Executor glmExecutor = new GLMExecutor(this);this.executorGroup.put(Model.GLM_3_Turbo, glmExecutor);
// this.executorGroup.put(Model.GLM_4, glmExecutor);
// this.executorGroup.put(Model.GLM_4V, glmExecutor);
// this.executorGroup.put(Model.COGVIEW_3, glmExecutor);return this.executorGroup;}}
在最后实际发送时,还需要经过拦截器,对token进行处理,对一些请求头进行处理,同样也是通过okhttp来实现
这篇关于【自用】chatglm-sdk-java详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!