Skip to content

Commit

Permalink
更改包命名,提取 QianfanV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobiichi-Origami committed Nov 25, 2024
1 parent 2eaef05 commit 49ba26d
Show file tree
Hide file tree
Showing 28 changed files with 156 additions and 75 deletions.
36 changes: 5 additions & 31 deletions java/src/main/java/com/baidubce/qianfan/Qianfan.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

import com.baidubce.qianfan.core.StreamIterator;
import com.baidubce.qianfan.core.builder.*;
import com.baidubce.qianfan.model.BaseRequest;
import com.baidubce.qianfan.model.BaseResponse;
import com.baidubce.qianfan.model.RateLimitConfig;
import com.baidubce.qianfan.model.RetryConfig;
import com.baidubce.qianfan.model.chat.ChatRequest;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.baidubce.qianfan.model.chat.V2.request.V2Request;
import com.baidubce.qianfan.model.chat.V2.response.V2Response;
import com.baidubce.qianfan.model.chat.V2.response.V2StreamResponse;
import com.baidubce.qianfan.model.completion.CompletionRequest;
import com.baidubce.qianfan.model.completion.CompletionResponse;
import com.baidubce.qianfan.model.console.ConsoleRequest;
Expand All @@ -45,8 +40,7 @@
import java.lang.reflect.Type;


public class Qianfan {
private final QianfanClient client;
public class Qianfan extends QianfanBase {

public Qianfan() {
this.client = new QianfanClient();
Expand All @@ -70,6 +64,10 @@ public Qianfan setRateLimitConfig(RateLimitConfig rateLimitConfig) {
return this;
}

public QianfanV2 v2() {
return new QianfanV2(this.client.covertToV2());
}

public ChatBuilder chatCompletion() {
return new ChatBuilder(this);
}
Expand All @@ -83,19 +81,6 @@ public StreamIterator<ChatResponse> chatCompletionStream(ChatRequest request) {
return requestStream(request, ChatResponse.class);
}

public ChatV2Builder chatCompletionV2() {
return new ChatV2Builder(this);
}

public V2Response chatCompletionV2(V2Request request) {
return request(request, V2Response.class);
}

public StreamIterator<V2StreamResponse> chatCompletionV2Stream(V2Request request) {
request.setStream(true);
return requestStream(request, V2StreamResponse.class);
}

public CompletionBuilder completion() {
return new CompletionBuilder(this);
}
Expand Down Expand Up @@ -167,15 +152,4 @@ public <T> ConsoleResponse<T> console(ConsoleRequest request, Type type) {
return consoleRequest(request, type);
}

public <T extends BaseResponse<T>, U extends BaseRequest<U>> T request(BaseRequest<U> request, Class<T> responseClass) {
return client.request(request, responseClass);
}

public <T extends BaseResponse<T>, U extends BaseRequest<U>> StreamIterator<T> requestStream(BaseRequest<U> request, Class<T> responseClass) {
return client.requestStream(request, responseClass);
}

public <T> ConsoleResponse<T> consoleRequest(ConsoleRequest request, Type type) {
return client.consoleRequest(request, type);
}
}
31 changes: 31 additions & 0 deletions java/src/main/java/com/baidubce/qianfan/QianfanBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baidubce.qianfan;

import com.baidubce.qianfan.core.StreamIterator;
import com.baidubce.qianfan.model.BaseRequest;
import com.baidubce.qianfan.model.BaseResponse;
import com.baidubce.qianfan.model.console.ConsoleRequest;
import com.baidubce.qianfan.model.console.ConsoleResponse;

import java.lang.reflect.Type;

abstract public class QianfanBase {
protected QianfanClient client;

protected QianfanBase() {}

public <T extends BaseResponse<T>, U extends BaseRequest<U>> T request(BaseRequest<U> request, Class<T> responseClass) {
return client.request(request, responseClass);
}

public <T extends BaseResponse<T>, U extends BaseRequest<U>> StreamIterator<T> requestStream(BaseRequest<U> request, Class<T> responseClass) {
return client.requestStream(request, responseClass);
}

public <T> ConsoleResponse<T> consoleRequest(ConsoleRequest request, Type type) {
return client.consoleRequest(request, type);
}

public void setClient(QianfanClient client) {
this.client = client;
}
}
10 changes: 8 additions & 2 deletions java/src/main/java/com/baidubce/qianfan/QianfanClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class QianfanClient {
private static final String REQUEST_SOURCE_PREFIX = "qianfan_java_sdk_v";
private static final String REQUEST_SOURCE = REQUEST_SOURCE_PREFIX + SDK_VERSION;

private final IAuth auth;
private final ModelEndpointRetriever endpointRetriever;
private IAuth auth;
private ModelEndpointRetriever endpointRetriever;
private RetryConfig retryConfig;
private RateLimiter rateLimiter;

Expand Down Expand Up @@ -81,6 +81,12 @@ public void setRateLimitConfig(RateLimitConfig rateLimitConfig) {
this.rateLimiter = new RateLimiter(rateLimitConfig);
}

public QianfanClient covertToV2() {
this.auth = auth.convertToV2();
this.endpointRetriever = new ModelEndpointRetriever(auth);
return this;
}

@SuppressWarnings("unchecked")
public <T extends BaseResponse<T>, U extends BaseRequest<U>> T request(BaseRequest<U> request, Class<T> responseClass) {
return request(
Expand Down
36 changes: 36 additions & 0 deletions java/src/main/java/com/baidubce/qianfan/QianfanV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baidubce.qianfan;

import com.baidubce.qianfan.core.StreamIterator;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.core.builder.ChatV2Builder;
import com.baidubce.qianfan.model.chat.v2.request.V2Request;
import com.baidubce.qianfan.model.chat.v2.response.V2Response;
import com.baidubce.qianfan.model.chat.v2.response.V2StreamResponse;

public class QianfanV2 extends QianfanBase {

public QianfanV2() {
this.client = new QianfanClient();
}

public QianfanV2(String accessKey, String secretKey) {
this.client = new QianfanClient(Auth.TYPE_V2, accessKey, secretKey);
}

public QianfanV2(QianfanClient client) {
this.client = client;
}

public ChatV2Builder chatCompletion() {
return new ChatV2Builder(this);
}

public V2Response chatCompletion(V2Request request) {
return request(request, V2Response.class);
}

public StreamIterator<V2StreamResponse> chatCompletionStream(V2Request request) {
request.setStream(true);
return requestStream(request, V2StreamResponse.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IAuth create() {
String secretKey = QianfanConfig.getQianfanSecretKey();
String version = QianfanConfig.getQianfanInferVersion();
if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey)) {
if (version.equals("V2")) {
if (TYPE_V2.equals(version)) {
return create(TYPE_V2, accessKey, secretKey);
}
return create(TYPE_IAM, accessKey, secretKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public String authType() {
return Auth.TYPE_IAM;
}

@Override
public IAuth convertToV2() {
return new QianfanV2Auth(accessKey, secretKey);
}

@Override
public HttpRequest signRequest(HttpRequest request) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
public interface IAuth {
HttpRequest signRequest(HttpRequest request);
String authType();
IAuth convertToV2();
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public String authType() {
return Auth.TYPE_OAUTH;
}

@Override
public IAuth convertToV2() {
throw new AuthException("Can't convert OAuth to V2Auth");
}

@Override
public HttpRequest signRequest(HttpRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.baidubce.qianfan.core.auth;

import com.baidubce.qianfan.core.QianfanConfig;
import com.baidubce.qianfan.model.chat.V2.other.BearTokenResponse;
import com.baidubce.qianfan.model.chat.v2.other.BearerTokenResponse;
import com.baidubce.qianfan.model.exception.QianfanException;
import com.baidubce.qianfan.model.exception.RequestException;
import com.baidubce.qianfan.util.Json;
Expand Down Expand Up @@ -31,6 +31,11 @@ public String authType() {
return Auth.TYPE_V2;
}

@Override
public IAuth convertToV2() {
return this;
}

@Override
public HttpRequest signRequest(HttpRequest request) {
return request
Expand All @@ -49,11 +54,11 @@ public void applyNewBearToken() {
try {
HttpResponse<String> response = iamRequest.executeString();

BearTokenResponse bearTokenResponse = Json.deserialize(
response.getStringBody(), BearTokenResponse.class
BearerTokenResponse bearerTokenResponse = Json.deserialize(
response.getStringBody(), BearerTokenResponse.class
);
this.token = String.format("Bearer %s", bearTokenResponse.getToken());
this.expiredTime = Instant.parse(bearTokenResponse.getExpireTime());
this.token = String.format("Bearer %s", bearerTokenResponse.getToken());
this.expiredTime = Instant.parse(bearerTokenResponse.getExpireTime());
} catch (QianfanException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.baidubce.qianfan.core.builder;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.QianfanBase;
import com.baidubce.qianfan.QianfanV2;
import com.baidubce.qianfan.model.exception.ValidationException;

import java.util.Map;

abstract class BaseBuilder<T extends BaseBuilder<T>> {
private Qianfan qianfan;
private QianfanBase qianfan;

private String model;

Expand All @@ -35,7 +37,7 @@ abstract class BaseBuilder<T extends BaseBuilder<T>> {
protected BaseBuilder() {
}

protected BaseBuilder(Qianfan qianfan) {
protected BaseBuilder(QianfanBase qianfan) {
this.qianfan = qianfan;
}

Expand Down Expand Up @@ -77,11 +79,28 @@ public T userId(String userId) {

protected Qianfan getQianfan() {
if (qianfan == null) {
throw new ValidationException("Qianfan client is not set. " +
throw new ValidationException("QianfanBase client is not set. " +
"please create builder from Qianfan client, " +
"or use build() instead of execute() to get Request and send it by yourself.");
}
return qianfan;

if (!(qianfan instanceof Qianfan)) {
throw new ValidationException("QianfanBase is not the instance of Qianfan");
}
return (Qianfan) qianfan;
}

protected QianfanV2 getQianfanV2() {
if (qianfan == null) {
throw new ValidationException("QianfanBase client is not set. " +
"please create builder from Qianfan client, " +
"or use build() instead of execute() to get Request and send it by yourself.");
}

if (!(qianfan instanceof QianfanV2)) {
throw new ValidationException("QianfanBase is not the instance of Qianfan");
}
return (QianfanV2) qianfan;
}

protected String getModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.baidubce.qianfan.core.builder;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.V2.*;
import com.baidubce.qianfan.model.chat.V2.request.V2Request;
import com.baidubce.qianfan.model.chat.V2.request.Tool;
import com.baidubce.qianfan.model.chat.V2.request.ToolResult;
import com.baidubce.qianfan.model.chat.V2.response.V2Response;
import com.baidubce.qianfan.QianfanV2;
import com.baidubce.qianfan.model.chat.v2.*;
import com.baidubce.qianfan.model.chat.v2.request.V2Request;
import com.baidubce.qianfan.model.chat.v2.request.Tool;
import com.baidubce.qianfan.model.chat.v2.request.ToolResult;
import com.baidubce.qianfan.model.chat.v2.response.V2Response;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ChatV2Builder() {
super();
}

public ChatV2Builder(Qianfan qianfan) {
public ChatV2Builder(QianfanV2 qianfan) {
super(qianfan);
}

Expand Down Expand Up @@ -186,6 +186,6 @@ public V2Request build() {
}

public V2Response execute() {
return super.getQianfan().chatCompletionV2(build());
return super.getQianfanV2().chatCompletion(build());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.baidubce.qianfan.core.builder;

import com.baidubce.qianfan.model.chat.V2.Message;
import com.baidubce.qianfan.model.chat.V2.ToolCall;
import com.baidubce.qianfan.model.chat.V2.request.ToolResult;
import com.baidubce.qianfan.model.chat.v2.Message;
import com.baidubce.qianfan.model.chat.v2.ToolCall;
import com.baidubce.qianfan.model.chat.v2.request.ToolResult;
import com.baidubce.qianfan.model.constant.V2.ChatRole;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baidubce.qianfan.model.chat.V2;
package com.baidubce.qianfan.model.chat.v2;

import com.google.gson.annotations.SerializedName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.baidubce.qianfan.model.chat.V2;
package com.baidubce.qianfan.model.chat.v2;

import com.baidubce.qianfan.model.chat.V2.response.Function;
import com.baidubce.qianfan.model.chat.v2.response.Function;

public class ToolCall {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.baidubce.qianfan.model.chat.V2.other;
package com.baidubce.qianfan.model.chat.v2.other;

import com.google.gson.annotations.SerializedName;

public class BearTokenResponse {
public class BearerTokenResponse {
@SerializedName("userId")
private String userId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baidubce.qianfan.model.chat.V2.request;
package com.baidubce.qianfan.model.chat.v2.request;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baidubce.qianfan.model.chat.V2.request;
package com.baidubce.qianfan.model.chat.v2.request;

public class Tool {
public final String type = "function";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baidubce.qianfan.model.chat.V2.request;
package com.baidubce.qianfan.model.chat.v2.request;

public class ToolChoice {
public final String type = "function";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baidubce.qianfan.model.chat.V2.request;
package com.baidubce.qianfan.model.chat.v2.request;

public class ToolResult {
private String toolName;
Expand Down
Loading

0 comments on commit 49ba26d

Please sign in to comment.