Skip to content

Commit ad68fc3

Browse files
committed
1:客户端核心类添加基础doc文档注释 2:优化异常抛出文案 3:格式化日期类移除到客户端核心类外,避免用户误用,对用户更友好
1 parent 3eecfb0 commit ad68fc3

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/main/java/com/plexpt/chatgpt/ChatGPT.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
4141
import retrofit2.converter.jackson.JacksonConverterFactory;
4242

43+
import static com.plexpt.chatgpt.util.FormatDateUtil.formatDate;
44+
4345

4446
/**
4547
* open ai 客户端
@@ -80,7 +82,7 @@ public class ChatGPT {
8082

8183

8284
/**
83-
* 初始化
85+
* 初始化:与服务端建立连接,成功后可直接与服务端进行对话
8486
*/
8587
public ChatGPT init() {
8688
OkHttpClient.Builder client = new OkHttpClient.Builder();
@@ -109,7 +111,7 @@ public ChatGPT init() {
109111
log.error(baseResponse.getError().getMessage());
110112
throw new ChatException(baseResponse.getError().getMessage());
111113
}
112-
throw new ChatException("error");
114+
throw new ChatException("ChatGPT init error!");
113115
}
114116
return response;
115117
});
@@ -139,8 +141,8 @@ public ChatGPT init() {
139141
/**
140142
* 最新版的GPT-3.5 chat completion 更加贴近官方网站的问答模型
141143
*
142-
* @param chatCompletion 问答参数
143-
* @return 答案
144+
* @param chatCompletion 问答参数,即咨询的内容
145+
* @return 服务端的问答响应
144146
*/
145147
public ChatCompletionResponse chatCompletion(ChatCompletion chatCompletion) {
146148
Single<ChatCompletionResponse> chatCompletionResponse =
@@ -149,17 +151,20 @@ public ChatCompletionResponse chatCompletion(ChatCompletion chatCompletion) {
149151
}
150152

151153
/**
152-
* 简易版
154+
* 支持多个问答参数来与服务端进行对话
153155
*
154-
* @param messages 问答参数
156+
* @param messages 问答参数,即咨询的内容
157+
* @return 服务端的问答响应
155158
*/
156159
public ChatCompletionResponse chatCompletion(List<Message> messages) {
157160
ChatCompletion chatCompletion = ChatCompletion.builder().messages(messages).build();
158161
return this.chatCompletion(chatCompletion);
159162
}
160163

161164
/**
162-
* 直接问
165+
* 与服务端进行对话
166+
* @param message 问答参数,即咨询的内容
167+
* @return 服务端的问答响应
163168
*/
164169
public String chat(String message) {
165170
ChatCompletion chatCompletion = ChatCompletion.builder()
@@ -172,7 +177,7 @@ public String chat(String message) {
172177
/**
173178
* 余额查询
174179
*
175-
* @return
180+
* @return 余额总金额及明细
176181
*/
177182
public CreditGrantsResponse creditGrants() {
178183
Single<CreditGrantsResponse> creditGrants = this.apiClient.creditGrants();
@@ -183,7 +188,7 @@ public CreditGrantsResponse creditGrants() {
183188
/**
184189
* 余额查询
185190
*
186-
* @return
191+
* @return 余额总金额
187192
*/
188193
public BigDecimal balance() {
189194
Single<SubscriptionData> subscription = apiClient.subscription();
@@ -200,9 +205,9 @@ public BigDecimal balance() {
200205
}
201206

202207
/**
203-
* 余额查询
208+
* 新建连接进行余额查询
204209
*
205-
* @return
210+
* @return 余额总金额
206211
*/
207212
public static BigDecimal balance(String key) {
208213
ChatGPT chatGPT = ChatGPT.builder()
@@ -212,9 +217,4 @@ public static BigDecimal balance(String key) {
212217

213218
return chatGPT.balance();
214219
}
215-
216-
public static String formatDate(Date date) {
217-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
218-
return sdf.format(date);
219-
}
220220
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.plexpt.chatgpt.util;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
import java.util.Locale;
6+
7+
public class FormatDateUtil {
8+
/**
9+
* 使用SimpleDateFormat格式化日期为yyyy-MM-dd,每次新建对象避免线程安全问题
10+
* @param date 日期
11+
* @return yyyy-MM-dd格式的日期字符串对象
12+
*/
13+
public static String formatDate(Date date) {
14+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
15+
return sdf.format(date);
16+
}
17+
}

0 commit comments

Comments
 (0)