diff --git a/src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java b/src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java new file mode 100644 index 0000000000000000000000000000000000000000..86cd99d0c8fa293f8b31f37d926b759055343f21 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java @@ -0,0 +1,18 @@ +package cn.bugstack.chatglm.session; + +import cn.bugstack.chatglm.model.ChatCompletionRequest; +import com.fasterxml.jackson.core.JsonProcessingException; +import okhttp3.sse.EventSource; +import okhttp3.sse.EventSourceListener; + +/** + * @author 小傅哥,微信:fustack + * @description 会话服务接口 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +public interface OpenAiSession { + + EventSource completions(ChatCompletionRequest chatCompletionRequest, EventSourceListener eventSourceListener) throws JsonProcessingException; + +} diff --git a/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java b/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java new file mode 100644 index 0000000000000000000000000000000000000000..97578ad775e2ad57f7d304c5c6126886296f3a4b --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java @@ -0,0 +1,51 @@ +package cn.bugstack.chatglm.session.defaults; + +import cn.bugstack.chatglm.IOpenAiApi; +import cn.bugstack.chatglm.model.ChatCompletionRequest; +import cn.bugstack.chatglm.session.Configuration; +import cn.bugstack.chatglm.session.OpenAiSession; +import com.fasterxml.jackson.core.JsonProcessingException; +import okhttp3.MediaType; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.sse.EventSource; +import okhttp3.sse.EventSourceListener; + +/** + * @author 小傅哥,微信:fustack + * @description 会话服务 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +public class DefaultOpenAiSession implements OpenAiSession { + + /** + * OpenAi 接口 + */ + private final Configuration configuration; + /** + * 工厂事件 + */ + private final EventSource.Factory factory; + + private IOpenAiApi openAiApi; + + public DefaultOpenAiSession(Configuration configuration) { + this.configuration = configuration; + this.openAiApi = configuration.getOpenAiApi(); + this.factory = configuration.createRequestFactory(); + } + + @Override + public EventSource completions(ChatCompletionRequest chatCompletionRequest, EventSourceListener eventSourceListener) throws JsonProcessingException { + // 构建请求信息 + Request request = new Request.Builder() + .url(configuration.getApiHost().concat(IOpenAiApi.v3_completions).replace("{model}", chatCompletionRequest.getModel().getCode())) + .post(RequestBody.create(MediaType.parse("application/json"), chatCompletionRequest.toString())) + .build(); + + // 返回事件结果 + return factory.newEventSource(request, eventSourceListener); + } + +}