package com.master.chat.api.openai;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.master.chat.api.openai.constant.OpenAIConst;
import com.master.chat.api.openai.entity.Tts.TextToSpeech;
import com.master.chat.api.openai.entity.assistant.*;
import com.master.chat.api.openai.entity.assistant.message.MessageFileResponse;
import com.master.chat.api.openai.entity.assistant.message.MessageResponse;
import com.master.chat.api.openai.entity.assistant.message.ModifyMessage;
import com.master.chat.api.openai.entity.assistant.run.*;
import com.master.chat.api.openai.entity.assistant.thread.ModifyThread;
import com.master.chat.api.openai.entity.assistant.thread.Thread;
import com.master.chat.api.openai.entity.assistant.thread.ThreadMessage;
import com.master.chat.api.openai.entity.assistant.thread.ThreadResponse;
import com.master.chat.api.openai.entity.billing.BillingUsage;
import com.master.chat.api.openai.entity.billing.CreditGrantsResponse;
import com.master.chat.api.openai.entity.billing.Subscription;
import com.master.chat.api.openai.entity.chat.*;
import com.master.chat.api.openai.entity.common.DeleteResponse;
import com.master.chat.api.openai.entity.common.OpenAiResponse;
import com.master.chat.api.openai.entity.common.PageRequest;
import com.master.chat.api.openai.entity.completions.Completion;
import com.master.chat.api.openai.entity.completions.CompletionResponse;
import com.master.chat.api.openai.entity.edits.Edit;
import com.master.chat.api.openai.entity.edits.EditResponse;
import com.master.chat.api.openai.entity.embeddings.Embedding;
import com.master.chat.api.openai.entity.embeddings.EmbeddingResponse;
import com.master.chat.api.openai.entity.engines.Engine;
import com.master.chat.api.openai.entity.files.File;
import com.master.chat.api.openai.entity.files.UploadFileResponse;
import com.master.chat.api.openai.entity.fineTune.Event;
import com.master.chat.api.openai.entity.fineTune.FineTune;
import com.master.chat.api.openai.entity.fineTune.FineTuneDeleteResponse;
import com.master.chat.api.openai.entity.fineTune.FineTuneResponse;
import com.master.chat.api.openai.entity.fineTune.job.FineTuneJob;
import com.master.chat.api.openai.entity.fineTune.job.FineTuneJobEvent;
import com.master.chat.api.openai.entity.fineTune.job.FineTuneJobListResponse;
import com.master.chat.api.openai.entity.fineTune.job.FineTuneJobResponse;
import com.master.chat.api.openai.entity.images.Image;
import com.master.chat.api.openai.entity.images.*;
import com.master.chat.api.openai.entity.models.Model;
import com.master.chat.api.openai.entity.models.ModelResponse;
import com.master.chat.api.openai.entity.moderations.Moderation;
import com.master.chat.api.openai.entity.moderations.ModerationResponse;
import com.master.chat.api.openai.entity.whisper.Transcriptions;
import com.master.chat.api.openai.entity.whisper.Translations;
import com.master.chat.api.openai.entity.whisper.WhisperResponse;
import com.master.chat.api.openai.exception.OpenAIException;
import com.master.chat.api.openai.exception.CommonError;
import com.master.chat.api.openai.function.KeyRandomStrategy;
import com.master.chat.api.openai.function.KeyStrategyFunction;
import com.master.chat.api.openai.interceptor.DefaultOpenAiAuthInterceptor;
import com.master.chat.api.openai.interceptor.DynamicKeyOpenAiAuthInterceptor;
import com.master.chat.api.openai.interceptor.OpenAiAuthInterceptor;
import com.master.chat.api.openai.plugin.PluginAbstract;
import com.master.chat.api.openai.plugin.PluginParam;
import io.reactivex.Single;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 描述: open ai 客户端
*
* @author https:www.unfbx.com
* @since 2023-02-11
*/
@Slf4j
public class OpenAiClient {
/**
* keys
*/
@Getter
@NotNull
private List<String> apiKey;
/**
* 自定义api host使用builder的方式构造client
*/
@Getter
private String apiHost;
@Getter
private OpenAiApi openAiApi;
/**
* 自定义的okHttpClient
* 如果不自定义 ,就是用sdk默认的OkHttpClient实例
*/
@Getter
private OkHttpClient okHttpClient;
/**
* api key的获取策略
*/
@Getter
private KeyStrategyFunction<List<String>, String> keyStrategy;
/**
* 自定义鉴权处理拦截器<br/>
* 可以不设置,默认实现:DefaultOpenAiAuthInterceptor <br/>
* 如需自定义实现参考:DealKeyWithOpenAiAuthInterceptor
*
* @see DynamicKeyOpenAiAuthInterceptor
* @see DefaultOpenAiAuthInterceptor
*/
@Getter
private OpenAiAuthInterceptor authInterceptor;
/**
* 默认的分页参数
*/
@Getter
private PageRequest pageRequest = PageRequest.builder().build();
public OpenAiClient() {
}
/**
* 构造器
*
* @return OpenAiClient.Builder
*/
public static Builder builder() {
return new Builder();
}
/**
* 构造
*
* @param builder
*/
private OpenAiClient(Builder builder) {
if (CollectionUtil.isEmpty(builder.apiKey)) {
throw new OpenAIException(CommonError.API_KEYS_NOT_NUL);
}
apiKey = builder.apiKey;
if (StrUtil.isBlank(builder.apiHost)) {
builder.apiHost = OpenAIConst.OPENAI_HOST;
}
apiHost = builder.apiHost;
if (Objects.isNull(builder.keyStrategy)) {
builder.keyStrategy = new KeyRandomStrategy();
}
keyStrategy = builder.keyStrategy;
if (Objects.isNull(builder.authInterceptor)) {
builder.authInterceptor = new DefaultOpenAiAuthInterceptor();
}
authInterceptor = builder.authInterceptor;
authInterceptor.setApiKey(this.apiKey);
authInterceptor.setKeyStrategy(this.keyStrategy);
if (Objects.isNull(builder.okHttpClient)) {
builder.okHttpClient = this.okHttpClient();
} else {
//自定义的okhttpClient 需要增加api keys
builder.okHttpClient = builder.okHttpClient
.newBuilder()
.addInterceptor(authInterceptor)
.build();
}
okHttpClient = builder.okHttpClient;
this.openAiApi = new Retrofit.Builder()
.baseUrl(apiHost)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.build().create(OpenAiApi.class);
}
/**
* 创建默认OkHttpClient
*
* @return
*/
private OkHttpClient okHttpClient() {
if (Objects.isNull(this.authInterceptor)) {
this.authInterceptor = new DefaultOpenAiAuthInterceptor();
}
this.authInterceptor.setApiKey(this.apiKey);
this.authInterceptor.setKeyStrategy(this.keyStrategy);
return new OkHttpClient
.Builder()
.addInterceptor(this.authInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).build();
}
/**
* openAi模型列表
*
* @return Model list
*/
public List<Model> models() {
Single<ModelResponse> models = this.openAiApi.models();
return models.blockingGet().getData();
}
/**
* openAi模型详细信息
*
* @param id 模型主键
* @return Model 模型类
*/
pub
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本项目是基于Java和Vue开发的科研项目优化与分析系统,包含1104个文件,其中包括558个Java源代码文件、132个Vue组件文件、112个SVG图像文件、94个JavaScript脚本文件、47个TypeScript文件、41个XML配置文件、26个JPG图像文件、13个PNG图像文件、10个Markdown文档文件和10个SCSS样式表文件。该系统旨在为科研人员提供科研项目优化与分析的全面支持,通过数据可视化、统计分析等功能,帮助用户更有效地管理和分析科研项目数据。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java和Vue的科研项目优化与分析系统设计源码 (1102个子文件)
package.bat 156B
build.bat 121B
run-web.bat 114B
.eslintrc.cjs 58B
start.cmd 90B
nginx.conf 875B
tailwind.css 431B
.env.development 267B
Dockerfile 1006B
.dockerignore 69B
.editorconfig 514B
.editorconfig 198B
.env 231B
.eslintignore 298B
.eslintignore 26B
chat.gif 874KB
chat.gif 874KB
imageLoading.gif 225KB
401.gif 160KB
loading.gif 75KB
.gitattributes 306B
.gitignore 373B
.gitignore 350B
.gitignore 284B
ie.html 23KB
index.html 5KB
index.html 2KB
favicon.ico 4KB
favicon.ico 4KB
OpenAiClient.java 50KB
OpenAiApi.java 25KB
SseServiceImpl.java 22KB
GptServiceImpl.java 21KB
OpenAiStreamClient.java 18KB
ResourceServiceImpl.java 14KB
SysUserServiceImpl.java 13KB
SysUserController.java 11KB
CommonController.java 11KB
TikTokensUtil.java 10KB
SSEListener.java 10KB
SysJobController.java 10KB
InitBean.java 10KB
AliyunSMSUtil.java 10KB
RedisUtils.java 9KB
TencentCOSUtil.java 9KB
UserServiceImpl.java 9KB
ChatMessageServiceImpl.java 9KB
SparkSseListener.java 9KB
SysConfigServiceImpl.java 9KB
LoginController.java 9KB
RoleController.java 8KB
FileUploadUtils.java 8KB
WenXinClient.java 8KB
ZhiPuApi.java 8KB
SysJobServiceImpl.java 7KB
AliyunOSSUtil.java 7KB
AppApiController.java 7KB
LoginController.java 7KB
RequestAspect.java 7KB
RoleServiceImpl.java 7KB
DictController.java 7KB
GlobalExceptionHandler.java 6KB
AsyncFactory.java 6KB
DictServiceImpl.java 6KB
DeptServiceImpl.java 6KB
LoginLogServiceImpl.java 6KB
ChatServiceImpl.java 6KB
BaseConfigServiceImpl.java 6KB
JwtTokenUtils.java 6KB
JobInvokeUtil.java 6KB
BaseChatCompletion.java 6KB
Server.java 5KB
ZhiPuClient.java 5KB
QianWenSseListener.java 5KB
DictTypeServiceImpl.java 5KB
ChatController.java 5KB
UserController.java 5KB
SysLogAspect.java 5KB
SparkClient.java 5KB
OpenkeyServiceImpl.java 5KB
SysConfigController.java 5KB
NoticeServiceImpl.java 5KB
TencentSMSUtil.java 5KB
CacheController.java 5KB
ResourceController.java 5KB
SparkBaseListener.java 5KB
SysUserRoleServiceImpl.java 5KB
PluginListener.java 5KB
JwtAuthenticationFilter.java 4KB
DeptController.java 4KB
AssistantServiceImpl.java 4KB
ScheduleUtils.java 4KB
NoticeController.java 4KB
CaptchaConfig.java 4KB
DruidConfig.java 4KB
PostController.java 4KB
PostServiceImpl.java 4KB
AssistantTypeServiceImpl.java 4KB
UserDetail.java 4KB
WebsocketServer.java 4KB
共 1102 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
xyq2024
- 粉丝: 2363
- 资源: 5440
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功