Skip to content

Commit

Permalink
feature 1.1.6 ChatCompletion接口支持logprobs参数设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Grt1228 authored and guorutao committed Dec 25, 2023
1 parent a105435 commit 0e0caf3
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
- 支持全部OpenAI的Api

# 📑 更新日志
- [x] 1.1.6 ChatCompletion接口支持logprobs参数设置
- [x] 1.1.5 Bug Fix
- [x] 1.1.3 支持Assistant、Run、Thread、Message Api。测试案例[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_3/OpenAiClientTest.java)
- [x] 1.1.2-beta0 支持附加图片的ChatCompletion、指定返回数据格式、Tool Call、Dall-e-3生成图片、FineTuneJob、文本转语音TTS。官方文档示例:[chatgpt-java.unfbx.com](https://chatgpt-java.unfbx.com/docs/category/-%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD) 。测试案例[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_2/OpenAiClientTest.java)
Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ WebSocket Reference:[OpenAIWebSocketEventSourceListener](https://github.com/Gr
- Supports all OpenAI APIs.

# 📑 Update Log
- [x] 1.1.6 ChatCompletion api support logprobs param.
- [x] 1.1.5 Bug Fix
- [x] 1.1.3 Upgrade to support Assistant、Run、Thread、Message Api. eg: [OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_3/OpenAiClientTest.java)
- [x] 1.1.2-beta0 Upgrade to support chat completion with picture GPT-4V、return JSON model、Tool Call、Dall-e-3、Fine Tune Job、TTS.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
<name>chatgpt-java</name>
<description>OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK .</description>
<url>https://chatgpt-java.unfbx.com</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public class BaseChatCompletion implements Serializable {
* @since 1.1.2
*/
private Integer seed;
/**
* @since 1.1.6
*/
private Boolean logprobs;
/**
* @since 1.1.6
*/
@JsonProperty("top_logprobs")
private Integer topLogprobs;


/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/chat/ChatChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class ChatChoice implements Serializable {
private Message message;
@JsonProperty("finish_reason")
private String finishReason;

private LogprobContent logprobs;
}
28 changes: 28 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/chat/Logprob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.unfbx.chatgpt.entity.chat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
* 描述:
*
* @author https:www.unfbx.com
* @since 2023-12-25
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Logprob implements Serializable {
private String token;

private BigDecimal logprob;

private List<Integer> bytes;

@JsonProperty("top_logprobs")
private List<Logprob> topLogprobs;
}
20 changes: 20 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/chat/LogprobContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.unfbx.chatgpt.entity.chat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* 描述:
*
* @author https:www.unfbx.com
* @since 2023-12-25
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class LogprobContent implements Serializable {

private List<Logprob> content;
}
141 changes: 141 additions & 0 deletions src/test/java/com/unfbx/chatgpt/v1_1_6/OpenAiClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.unfbx.chatgpt.v1_1_6;


import com.unfbx.chatgpt.FirstKeyStrategy;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.OpenAiStreamClient;
import com.unfbx.chatgpt.entity.chat.*;
import com.unfbx.chatgpt.function.KeyRandomStrategy;
import com.unfbx.chatgpt.interceptor.DynamicKeyOpenAiAuthInterceptor;
import com.unfbx.chatgpt.interceptor.OpenAILogger;
import com.unfbx.chatgpt.interceptor.OpenAiResponseInterceptor;
import com.unfbx.chatgpt.sse.ConsoleEventSourceListener;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* 描述: 测试类
*
* @author https:www.unfbx.com
* 2023-12-25
*/
@Slf4j
public class OpenAiClientTest {

private OpenAiClient client;
private OpenAiStreamClient streamClient;


@Before
public void before() {
//可以为null
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
// .proxy(proxy)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(new OpenAiResponseInterceptor())
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
client = OpenAiClient.builder()
//支持多key传入,请求时候随机选择
.apiKey(Arrays.asList("sk-*************************************"))
//自定义key的获取策略:默认KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
.apiHost("https://*************/")
.build();

streamClient = OpenAiStreamClient.builder()
//支持多key传入,请求时候随机选择
.apiKey(Arrays.asList("sk-*************************************"))
//自定义key的获取策略:默认KeyRandomStrategy
.keyStrategy(new KeyRandomStrategy())
.authInterceptor(new DynamicKeyOpenAiAuthInterceptor())
.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 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 chat() {
//聊天模型:gpt-3.5
Message message = Message.builder().role(Message.Role.USER).content("你好啊我的伙伴!").build();
ChatCompletion chatCompletion = ChatCompletion
.builder()
.messages(Collections.singletonList(message))
.model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
.logprobs(true)
.topLogprobs(2)
.build();
ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> {
System.out.println(e.getMessage());
});
}


@Test
public void chatCompletions() {
ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();
Message message = Message.builder().role(Message.Role.USER).content("random one word!").build();
ChatCompletion chatCompletion = ChatCompletion
.builder()
.model(ChatCompletion.Model.GPT_4_1106_PREVIEW.getName())
.temperature(0.2)
.maxTokens(2048)
.messages(Collections.singletonList(message))
.stream(true)
.logprobs(true)
.topLogprobs(2)
.build();
streamClient.streamChatCompletion(chatCompletion, eventSourceListener);
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

0 comments on commit 0e0caf3

Please sign in to comment.