提交 5d1f4fef 编写于 作者: 小傅哥's avatar 小傅哥

feat:会话接口和实现

上级 280b6693
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;
}
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册