Chatgpt-Java是OpenAI官方Api的Java SDK,可以快速接入项目使用。支持OpenAI官方全部接口。
目前收获将2500+star🌟。
<dependency>
<groupId>com.unfbxgroupId>
<artifactId>chatgpt-javaartifactId>
<version>1.1.2-beta0version>
dependency>
目前支持的功能:
Dall-e-3 | FineTuneJob | TTS | ChatCompletionWithPicture |
---|---|---|---|
AI生成图片 | 微调job | 文本转语音 | 附加图片的ChatCompletion |
TikToken | Chat | Completions | Images | Speech To Text | 余额查询 |
---|---|---|---|---|---|
Token计算 | GPT-3.5、4.0对话模型 | GPT-3.0对话 | 图片模型 | 语音转文字,语音翻译 | 余额查询 |
Embeddings | Files | Moderations | Fine-tune | Models |
---|---|---|---|---|
嵌入 | 自定义训练模型 | 文本审核,敏感词鉴别 | 微调 | 模型检索相关 |
OpenAi在上周更新了新的版本,发布了很多新的功能,包括GPT-4V、附加图片的ChatCompletion、指定返回数据格式、Tool Call、Dall-e-3生成图片、FineTuneJob、文本转语音TTS等等功能。
本周Chatgpt-Java同步更新,支持最新的Api。
所以的新版本Api更新基于原有的OpenAiClient和OpenAiStreamClient,所以构建客户端的当时是没有变化的。
创建Client如下:
@Slf4j
public class OpenAiClientTest {
private OpenAiClient client;
private OpenAiStreamClient streamClient;
@Before
public void before() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(new OpenAiResponseInterceptor())
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
client = OpenAiClient.builder()
.apiKey(Arrays.asList("*********************"))
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
.apiHost("https://*******/")
.build();
streamClient = OpenAiStreamClient.builder()
//支持多key传入,请求时候随机选择
.apiKey(Arrays.asList("*********************"))
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
.apiHost("https://*******/")
.build();
}
}
/**
* 聊天模型支持图片流式示例
*/
@Test
public void pictureChat() {
Content textContent = Content.builder().text("What’s in this image?").type(Content.Type.TEXT.getName()).build();
ImageUrl imageUrl = ImageUrl.builder().url("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg").build();
Content imageContent = Content.builder().imageUrl(imageUrl).type(Content.Type.IMAGE_URL.getName()).build();
List<Content> contentList = new ArrayList<>();
contentList.add(textContent);
contentList.add(imageContent);
MessagePicture message = MessagePicture.builder().role(Message.Role.USER).content(contentList).build();
//#####请求参数使用ChatCompletionWithPicture类
ChatCompletionWithPicture chatCompletion = ChatCompletionWithPicture
.builder()
.messages(Collections.singletonList(message))
.model(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName())
.build();
ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}
/**
* 聊天模型支持图片流式示例
*/
@Test
public void pictureChatV2() {
Content textContent = Content.builder().text("What’s in this image?").type(Content.Type.TEXT.getName()).build();
ImageUrl imageUrl = ImageUrl.builder().url("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg").build();
Content imageContent = Content.builder().imageUrl(imageUrl).type(Content.Type.IMAGE_URL.getName()).build();
List<Content> contentList = new ArrayList<>();
contentList.add(textContent);
contentList.add(imageContent);
MessagePicture message = MessagePicture.builder().role(Message.Role.USER).content(contentList).build();
ChatCompletionWithPicture chatCompletion = ChatCompletionWithPicture
.builder()
.messages(Collections.singletonList(message))
.model(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName())
.build();
ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}
附加图片的chatCompletion暂时不支持以下简易接口请求
最新版的OpenAi接口支持执行数据返回格式。以下仅举例阻塞输出的方案,流式输出一样的使用方法不再举例。
指定数据格式目前支持:
参考源码:com/unfbx/chatgpt/entity/chat/ResponseFormat.java
/**
* 自定义返回数据格式
*/
@Test
public void diyReturnDataModelChat() {
Message message = Message.builder().role(Message.Role.USER).content("随机输出10个单词,使用json输出").build();
ChatCompletion chatCompletion = ChatCompletion
.builder()
.messages(Collections.singletonList(message))
.responseFormat(ResponseFormat.builder().type(ResponseFormat.Type.JSON_OBJECT.getName()).build())
.model(ChatCompletion.Model.GPT_4_1106_PREVIEW.getName())
.build();
ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}
{
"words": [
"aberration",
"nostalgia",
"quintessential",
"harmony",
"serendipity",
"benevolent",
"ephemeral",
"labyrinth",
"zenith",
"vivacious"
]
}
最新版的OpenAi接口支持Dall-e-3模型生成图片,功能更加强大。
/**
* 新版图片生成模型使用示例
*/
@Test
public void generateImageByDall_e_3() {
Image image = Image.builder()
.responseFormat(com.unfbx.chatgpt.entity.images.ResponseFormat.URL.getName())
.model(Image.Model.DALL_E_3.getName())
.prompt("一个咖啡杯,上面印刷Unfbx四个字母。")
.n(1)
.quality(Image.Quality.HD.getName())
.size(SizeEnum.size_1024_1792.getName())
.style(Image.Style.NATURAL.getName())
.build();
ImageResponse imageResponse = client.genImages(image);
// ImageResponse imageResponse = client.genImages("一个咖啡杯,上面印刷Unfbx四个字母。");
System.out.println(imageResponse.getData().get(0).getUrl());
}
OpenAi支持自定义训练专属模型,通过FineTuneJob进行模型微调训练。
FineTuneJob支持的模型有:
更多详细参考源码:com/unfbx/chatgpt/entity/fineTune/job/FineTuneJob.java
创建文件格式为json1
下面是一个示例文件:fine_tune_test_file.json1
文件在github源码根目录,文件内容;
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java好用么?"}, {"role": "assistant", "content": "还行吧。"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java最新版本是什么?"}, {"role": "assistant", "content": "1.1.2版本"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java支持流式输出么?"}, {"role": "assistant", "content": "最新版支持流式输出"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java的作者是谁?"}, {"role": "assistant", "content": "作者是Unfbx,个人网站:https://www.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,微信公众号是什么?"}, {"role": "assistant", "content": "程序员的黑洞。"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,个人博客地址是什么?"}, {"role": "assistant", "content": "https://www.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java官方地址是什么?"}, {"role": "assistant", "content": "https://chatgpt-java.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java的github地址是什么?"}, {"role": "assistant", "content": "https://github.com/Grt1228/chatgpt-java"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java的简单介绍。"}, {"role": "assistant", "content": "OpenAI官方Api的Java SDK,可以快速接入项目使用。目前支持OpenAI官方全部接口,**同时支持Tokens计算。参考文档:[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)**"}]}
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "OpenAi的官方Java Sdk 是什么?"}, {"role": "assistant", "content": "chagpt-java,github地址:https://github.com/Grt1228/chatgpt-java"}]}
/**
* fineTuneJob使用示例
*/
@Test
public void uploadFile() {
UploadFileResponse uploadFileResponse = client.uploadFile(new java.io.File("fine_tune_test_file.json1"));
//返回文件的id:file-6KaBdtVlaassk9Y2P5ZjTqIC
System.out.println(uploadFileResponse.getId());
}
@Test
public void fineTuneJob() {
//file-KaNQn5V9YHlLqVQzo8CUMdIr是文件上传返回的文件id
FineTuneJobResponse fineTuneJobResponse = client.fineTuneJob("file-KaNQn5V9YHlLqVQzo8CUMdIr");
//返回job id = ftjob-5WQr0bZ7grvjnY3Or2sqiixl
System.out.println(fineTuneJobResponse.toString());
}
需要主义只有FineTuneJob执行完成fineTunedModel属性才会有值,job失败或者未执行完成此属性为null。
结合第4.0章节可以看到job的执行信息。
@Test
public void retrieveFineTuneJob() {
//传入job id
FineTuneJobResponse fineTuneJobResponse = client.retrieveFineTuneJob("ftjob-5WQr0bZ7grvjnY3Or2sqiixl");
System.out.println(fineTuneJobResponse);
}
支持分页查询
@Test
public void retrieveFineTuneJob() {
// FineTuneJobListResponse jobListResponse = client.fineTuneJobs("ftjob-cG7zIraBhAkq5Ybs7311lH7t", 5);
FineTuneJobListResponse<FineTuneJobResponse> jobListResponse = client.fineTuneJobs(null, 20);
System.out.println(jobListResponse);
}
支持分页查询,支持分页
@Test
public void fineTuneJobEvents() {
FineTuneJobListResponse<FineTuneJobEvent> listResponse = client.fineTuneJobEvents("ftjob-5WQr0bZ7grvjnY3Or2sqiixl", null, 20);
// FineTuneJobListResponse listResponse = client.fineTuneJobEvents("ftjob-5WQr0bZ7grvjnY3Or2sqiixl", "ftevent-WwB8lpWxhjgUJX9DYdb47zJe", 20);
listResponse.getData().forEach(e -> System.out.println(e.getMessage()));
}
输出信息,输出信息会返回创建的模型id,这个就是后续使用的模型id。
The job has successfully completed
New fine-tuned model created: ft:gpt-3.5-turbo-1106:personal::8K5KwJTU
Step 91/100: training loss=0.45
Step 81/100: training loss=0.00
Step 71/100: training loss=0.00
Step 61/100: training loss=0.94
Step 51/100: training loss=0.19
Step 41/100: training loss=0.06
Step 31/100: training loss=0.95
Step 21/100: training loss=1.99
Step 11/100: training loss=2.50
Step 1/100: training loss=5.42
Fine-tuning job started
Files validated, moving job to queued state
Validating training file: file-KaNQn5V9YHlLqVQzo8CUMdIr
Created fine-tuning job: ftjob-5WQr0bZ7grvjnY3Or2sqiixl
注意model参数为自定义的模型id。此id会在fineTuneJobEvents完成后返回。
此id的获取有几种方式:
@Test
public void fineTuneJobModelChat() {
Message message1 = Message.builder().role(Message.Role.SYSTEM).content("OnBot是一个聊天机器人。").build();
Message message2 = Message.builder().role(Message.Role.USER).content("OnBot请问:Chatgpt-java的作者是谁?").build();
List<Message> messages = new ArrayList<>(2);
messages.add(message1);
messages.add(message2);
ChatCompletion chatCompletion = ChatCompletion
.builder()
.messages(messages)
.model("ft:gpt-3.5-turbo-1106:personal::8K5KwJTU")
.build();
ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> {
System.out.println(e.getMessage());
});
}
输出信息
作者是Unfbx,个人网站:https://www.unfbx.com
OpenAi最新接口支持TTS,支持高清语音,支持六种人声。
/**
* tts使用示例
*/
@Test
public void textToSpeed() {
TextToSpeech textToSpeech = TextToSpeech.builder()
.model(TextToSpeech.Model.TTS_1_HD.getName())
.input("OpenAI官方Api的Java SDK,可以快速接入项目使用。目前支持OpenAI官方全部接口,同时支持Tokens计算。官方github地址:https://github.com/Grt1228/chatgpt-java。欢迎star。")
.voice(TtsVoice.NOVA.getName())
.responseFormat(TtsFormat.MP3.getName())
.build();
File file = new File("C:\\Users\\***\\Desktop\\test.mp3");
client.textToSpeech(textToSpeech, new Callback<ResponseBody>() {
@SneakyThrows
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
InputStream inputStream = response.body().byteStream();
//创建文件
if (!file.exists()) {
if (!file.getParentFile().exists())
file.getParentFile().mkdir();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
log.error("createNewFile IOException");
}
}
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[8192];
int len;
while ((len = inputStream.read(data, 0, 8192)) != -1) {
os.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
访问项目开源地址:https://github.com/Grt1228/chatgpt-java
点一点免费的star
获取更多SDK功能