From 064179b03a9604c4c8c941973f5f7274e5b251f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=82=85=E5=93=A5?= <184172133@qq.com> Date: Tue, 10 Oct 2023 22:04:58 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E4=BC=9A=E8=AF=9D=E5=B7=A5?= =?UTF-8?q?=E5=8E=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatglm/session/OpenAiSessionFactory.java | 13 ++++ .../defaults/DefaultOpenAiSessionFactory.java | 61 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/main/java/cn/bugstack/chatglm/session/OpenAiSessionFactory.java create mode 100644 src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSessionFactory.java diff --git a/src/main/java/cn/bugstack/chatglm/session/OpenAiSessionFactory.java b/src/main/java/cn/bugstack/chatglm/session/OpenAiSessionFactory.java new file mode 100644 index 0000000..f60a4b7 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/session/OpenAiSessionFactory.java @@ -0,0 +1,13 @@ +package cn.bugstack.chatglm.session; + +/** + * @author 小傅哥,微信:fustack + * @description 工厂接口 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +public interface OpenAiSessionFactory { + + OpenAiSession openSession(); + +} diff --git a/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSessionFactory.java b/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSessionFactory.java new file mode 100644 index 0000000..b72dd97 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSessionFactory.java @@ -0,0 +1,61 @@ +package cn.bugstack.chatglm.session.defaults; + +import cn.bugstack.chatglm.IOpenAiApi; +import cn.bugstack.chatglm.interceptor.OpenAiHTTPInterceptor; +import cn.bugstack.chatglm.session.Configuration; +import cn.bugstack.chatglm.session.OpenAiSession; +import cn.bugstack.chatglm.session.OpenAiSessionFactory; +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import retrofit2.Retrofit; +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; +import retrofit2.converter.jackson.JacksonConverterFactory; + +import java.util.concurrent.TimeUnit; + +/** + * @author 小傅哥,微信:fustack + * @description 会话工厂 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +public class DefaultOpenAiSessionFactory implements OpenAiSessionFactory { + + private final Configuration configuration; + + public DefaultOpenAiSessionFactory(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public OpenAiSession openSession() { + // 1. 日志配置 + HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); + httpLoggingInterceptor.setLevel(configuration.getLevel()); + + // 2. 开启 Http 客户端 + OkHttpClient okHttpClient = new OkHttpClient + .Builder() + .addInterceptor(httpLoggingInterceptor) + .addInterceptor(new OpenAiHTTPInterceptor(configuration)) + .connectTimeout(configuration.getConnectTimeout(), TimeUnit.SECONDS) + .writeTimeout(configuration.getWriteTimeout(), TimeUnit.SECONDS) + .readTimeout(configuration.getReadTimeout(), TimeUnit.SECONDS) + .build(); + + configuration.setOkHttpClient(okHttpClient); + + // 3. 创建 API 服务 + IOpenAiApi openAiApi = new Retrofit.Builder() + .baseUrl(configuration.getApiHost()) + .client(okHttpClient) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) + .addConverterFactory(JacksonConverterFactory.create()) + .build().create(IOpenAiApi.class); + + configuration.setOpenAiApi(openAiApi); + + return new DefaultOpenAiSession(configuration); + } + +} -- GitLab