Skip to content

Commit

Permalink
feature:1.0.11 支持tokens、增加新的余额查询、BUG修复
Browse files Browse the repository at this point in the history
  • Loading branch information
guorutao committed Apr 9, 2023
1 parent 0cb60db commit 622cbbf
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/unfbx/chatgpt/OpenAiApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.unfbx.chatgpt;

import com.unfbx.chatgpt.entity.billing.BillingUsage;
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
import com.unfbx.chatgpt.entity.billing.Subscription;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.common.OpenAiResponse;
Expand Down Expand Up @@ -31,6 +33,7 @@
import okhttp3.ResponseBody;
import retrofit2.http.*;

import java.time.LocalDate;
import java.util.Map;

/**
Expand Down Expand Up @@ -297,9 +300,30 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,

/**
* 余额查询
* 官方禁止访问此接口
*
* @return 余额结果
*/
@GET("dashboard/billing/credit_grants")
@Deprecated
Single<CreditGrantsResponse> creditGrants();

/**
* 账户信息查询:里面包含总金额(美元)等信息
*
* @return
*/
@GET("v1/dashboard/billing/subscription")
Single<Subscription> subscription();

/**
* 账户调用接口消耗金额信息查询
* totalUsage = 账户总使用金额(美分)
*
* @return
* @param starDate
* @param endDate
*/
@GET("v1/dashboard/billing/usage")
Single<BillingUsage> billingUsage(@Query("start_date") LocalDate starDate, @Query("end_date") LocalDate endDate);
}
28 changes: 27 additions & 1 deletion src/main/java/com/unfbx/chatgpt/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import cn.hutool.core.util.StrUtil;

import com.unfbx.chatgpt.constant.OpenAIConst;
import com.unfbx.chatgpt.entity.billing.BillingUsage;
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
import com.unfbx.chatgpt.entity.billing.Subscription;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.chat.Message;
Expand Down Expand Up @@ -45,6 +47,7 @@
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -699,7 +702,8 @@ private void checkSpeechFileSize(java.io.File file) {
/**
* ## 官方已经禁止使用此api
* OpenAi账户余额查询
*
* @see #subscription()
* @see #billingUsage(LocalDate, LocalDate)
* @return
*/
@Deprecated
Expand All @@ -708,6 +712,28 @@ public CreditGrantsResponse creditGrants() {
return creditGrants.blockingGet();
}

/**
* 账户信息查询:里面包含总金额等信息
*
* @return
*/
public Subscription subscription() {
Single<Subscription> subscription = this.openAiApi.subscription();
return subscription.blockingGet();
}

/**
* 账户调用接口消耗金额信息查询
* 最多查询100天
* @param starDate 开始时间
* @param endDate 结束时间
* @return
*/
public BillingUsage billingUsage(@NotNull LocalDate starDate, @NotNull LocalDate endDate) {
Single<BillingUsage> billingUsage = this.openAiApi.billingUsage(starDate, endDate);
return billingUsage.blockingGet();
}


public static final class Builder {
/**
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/unfbx/chatgpt/OpenAiStreamClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unfbx.chatgpt.constant.OpenAIConst;
import com.unfbx.chatgpt.entity.billing.BillingUsage;
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
import com.unfbx.chatgpt.entity.billing.Subscription;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.Message;
import com.unfbx.chatgpt.entity.common.OpenAiResponse;
Expand All @@ -18,6 +20,7 @@
import com.unfbx.chatgpt.function.KeyStrategyFunction;
import com.unfbx.chatgpt.interceptor.HeaderAuthorizationInterceptor;
import com.unfbx.chatgpt.sse.ConsoleEventSourceListener;
import io.reactivex.Single;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,7 +29,11 @@
import okhttp3.sse.EventSourceListener;
import okhttp3.sse.EventSources;
import org.jetbrains.annotations.NotNull;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -62,6 +69,9 @@ public class OpenAiStreamClient {
@Getter
private KeyStrategyFunction<List<String>, String> keyStrategy;

@Getter
private OpenAiApi openAiApi;

/**
* 构造实例对象
*
Expand Down Expand Up @@ -93,6 +103,13 @@ private OpenAiStreamClient(Builder builder) {
.build();
}
okHttpClient = builder.okHttpClient;

this.openAiApi = new Retrofit.Builder()
.baseUrl(apiHost)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.build().create(OpenAiApi.class);
}

/**
Expand Down Expand Up @@ -251,6 +268,28 @@ public CreditGrantsResponse creditGrants() {
return completionResponse;
}

/**
* 账户信息查询:里面包含总金额等信息
*
* @return
*/
public Subscription subscription() {
Single<Subscription> subscription = this.openAiApi.subscription();
return subscription.blockingGet();
}

/**
* 账户调用接口消耗金额信息查询
* 最多查询100天
* @param starDate 开始时间
* @param endDate 结束时间
* @return
*/
public BillingUsage billingUsage(@NotNull LocalDate starDate, @NotNull LocalDate endDate) {
Single<BillingUsage> billingUsage = this.openAiApi.billingUsage(starDate, endDate);
return billingUsage.blockingGet();
}

/**
* 构造
*
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/BillingUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.unfbx.chatgpt.entity.billing;

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

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

/**
* 描述:金额消耗信息
*
* @author https:www.unfbx.com
* @since 2023-04-08
*/
@Data
public class BillingUsage {

@JsonProperty("object")
private String object;
/**
* 账号金额消耗明细
*/
@JsonProperty("daily_costs")
private List<DailyCost> dailyCosts;
/**
* 总使用金额:美分
*/
@JsonProperty("total_usage")
private BigDecimal totalUsage;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

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

/**
Expand All @@ -12,7 +13,7 @@
* @since 2023-03-18
*/
@Data
public class CreditGrantsResponse {
public class CreditGrantsResponse implements Serializable {
private String object;
/**
* 总金额:美元
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/DailyCost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.unfbx.chatgpt.entity.billing;

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

import java.util.List;

/**
* 描述:金额消耗列表
*
* @author https:www.unfbx.com
* @since 2023-04-08
*/
@Data
public class DailyCost {
/**
* 时间戳
*/
@JsonProperty("timestamp")
private long timestamp;
/**
* 模型消耗金额详情
*/
@JsonProperty("line_items")
private List<LineItem> lineItems;
}
23 changes: 23 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/LineItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.unfbx.chatgpt.entity.billing;

import lombok.Data;

import java.math.BigDecimal;

/**
* 描述:金额消耗列表
*
* @author https:www.unfbx.com
* @since 2023-04-08
*/
@Data
public class LineItem {
/**
* 模型名称
*/
private String name;
/**
* 消耗金额
*/
private BigDecimal cost;
}
15 changes: 15 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/Plan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.unfbx.chatgpt.entity.billing;

import lombok.Data;

/**
* 描述:
*
* @author https:www.unfbx.com
* @since 2023-04-08
*/
@Data
public class Plan {
private String title;
private String id;
}
52 changes: 52 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/Subscription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.unfbx.chatgpt.entity.billing;

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

/**
* 描述:账户信息
*
* @author https:www.unfbx.com
* @since 2023-04-08
*/
@Data
public class Subscription {
@JsonProperty("object")
private String object;
@JsonProperty("has_payment_method")
private boolean hasPaymentMethod;
@JsonProperty("canceled")
private boolean canceled;
@JsonProperty("canceled_at")
private Object canceledAt;
@JsonProperty("delinquent")
private Object delinquent;
@JsonProperty("access_until")
private long accessUntil;
@JsonProperty("soft_limit")
private long softLimit;
@JsonProperty("hard_limit")
private long hardLimit;
@JsonProperty("system_hard_limit")
private long systemHardLimit;
@JsonProperty("soft_limit_usd")
private double softLimitUsd;
@JsonProperty("hard_limit_usd")
private double hardLimitUsd;
@JsonProperty("system_hard_limit_usd")
private double systemHardLimitUsd;
@JsonProperty("plan")
private Plan plan;
@JsonProperty("account_name")
private String accountName;
@JsonProperty("po_number")
private Object poNumber;
@JsonProperty("billing_email")
private Object billingEmail;
@JsonProperty("tax_ids")
private Object taxIds;
@JsonProperty("billing_address")
private Object billingAddress;
@JsonProperty("business_address")
private Object businessAddress;
}

0 comments on commit 622cbbf

Please sign in to comment.