Skip to content

Commit

Permalink
feat: support bearer token (#745)
Browse files Browse the repository at this point in the history
* feat: add support for qianfan console api calls (#732)

* doc: 增加 go 自定义错误码文档 (#733)

* support custom retry codes

* add custom retry code example

* release go v0.0.11 (#734)

* fix: api v2 & bearer token

* fix: api v2

* fix: lint and update version

* Update evaluation_manager.py (#736)

* Update evaluation_manager.py

* Update consts.py

* Afs datasource (#737)

* 添加 AFSDatasource

* Debug

* feat: 添加数据集 V2 API (#739)

* 添加数据集 V2 API

* 追加注释

* fix text2img model list (#741)

* Fix unclosed aiohttp session whlie exception raised (#743)

* Fix unclosed aiohttp session whlie exception raised

* format and lint

* Update http_client.py

---------

Co-authored-by: NuODaniel <[email protected]>

* fix: merge

---------

Co-authored-by: Azure99 <[email protected]>
Co-authored-by: Liu Jun <[email protected]>
Co-authored-by: AlexT <[email protected]>
Co-authored-by: Dobiichi-Origami <[email protected]>
Co-authored-by: Guocheng <[email protected]>
  • Loading branch information
6 people authored Aug 17, 2024
1 parent b8ea363 commit edad164
Show file tree
Hide file tree
Showing 49 changed files with 2,085 additions and 192 deletions.
34 changes: 31 additions & 3 deletions docs/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,42 @@ async for r in resp:
- `json_body`:请求体
- `retry_config`:请求使用的重试信息

##### V2 版本
#### V2 版本

千帆平台推出了 V2 版本的推理 API,SDK 也支持对 V2 版本的 API 进行调用,只需要创建对象时传入 `version="2"` 即可,其余使用方法与上述一致,差异点主要在于字段名称,具体字段名请参考 API 文档
千帆平台推出了 V2 版本的推理 API,SDK 也支持对 V2 版本的 API 进行调用:

##### V2 鉴权

API v2 采用Bearer Token的鉴权方式:可以通过access_key 和 secret_key 获取。因此可以选择以下两种方式设置鉴权信息:
```python
import os
# 安全认证
os.environ['QIANFAN_ACCESS_KEY'] = 'your_access_key'
os.environ['QIANFAN_SECRET_KEY'] = 'your_secret_key'
# 或 bearer token
os.environ['QIANFAN_BEARER_TOKEN'] = 'your_bearer_token'
```

我们可以运行以下接口获取BEARER_TOKEN(可用于需要临时鉴权,或进行应用分发的场景):

```python
import os
os.environ['QIANFAN_ACCESS_KEY'] = 'your_access_key'
os.environ['QIANFAN_SECRET_KEY'] = 'your_secret_key'

resp = IAM.create_bearer_token(100)
print(resp.body)
token = resp.body["token"]
```

##### V2 示例:

只需要创建对象时传入 `version="2"` 即可,其余使用方法与上述一致,差异点主要在于字段名称,具体字段名请参考 API 文档

```python
# 在创建时传入 version 以使用 V2 版本
# model 字段为可选,默认为 ernie-speed-8k,也可以指定其他模型,后续调用均会使用该模型
chat = qianfan.ChatCompletion(version="2", model="ernie-speed-8k")
chat = qianfan.ChatCompletion(version="2", app_id='app-xxx', model="ernie-speed-8k")

# 调用方式与 V1 版本一致,具体字段名参考 API 文档
resp = chat.do(
Expand Down
18 changes: 18 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,21 @@ chat := qianfan.NewChatCompletion( // Completion 与 Embedding 可以用同样
WithLLMRetryBackoffFactor(1), // 指数回避因子
)
```

同时,由于只有部分错误可以通过重试解决,SDK 只会对部分错误码进行重试,可以通过如下方式自定义修改

```go
qianfan.GetConfig().RetryErrCodes = []int{
// 以下是 SDK 默认重试的错误码
qianfan.ServiceUnavailableErrCode, // 2
qianfan.ServerHighLoadErrCode, // 336100
qianfan.QPSLimitReachedErrCode, // 18
qianfan.RPMLimitReachedErrCode, // 336501
qianfan.TPMLimitReachedErrCode, // 336502
qianfan.AppNotExistErrCode, // 15
// 以下为非内置错误码,仅为示例如何增加自定义错误码
qianfan.UnknownErrorErrCode,
// 也可以直接提供 int 类型的错误码
336000,
}
```
2 changes: 1 addition & 1 deletion go/qianfan/model_endpoint_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func getModelEndpointRetriever() *modelEndpointRetriever {
"chat": ChatModelEndpoint,
"completions": CompletionModelEndpoint,
"embeddings": EmbeddingEndpoint,
"text2image": make(map[string]string),
"text2image": Text2ImageEndpoint,
"image2text": make(map[string]string),
}
for modelType, endpointMap := range initMap {
Expand Down
2 changes: 1 addition & 1 deletion go/qianfan/text2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *Text2Image) ModelList() []string {
models := getModelEndpointRetriever().GetModelList(context.TODO(), "text2image")
list := make([]string, len(models))
i := 0
for k := range Text2ImageEndpoint {
for k := range models {
list[i] = k
i++
}
Expand Down
2 changes: 1 addition & 1 deletion go/qianfan/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
package qianfan

// SDK 版本
const Version = "v0.0.10"
const Version = "v0.0.11"
const versionIndicator = "qianfan_go_sdk_" + Version
6 changes: 3 additions & 3 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.9</version>
<version>0.1.0</version>
</dependency>
```

Expand All @@ -21,13 +21,13 @@
对于Kotlin DSL,在build.gradle.kts的dependencies中添加依赖。

```kotlin
implementation("com.baidubce:qianfan:0.0.9")
implementation("com.baidubce:qianfan:0.1.0")
```

对于Groovy DSL,在build.gradle的dependencies中添加依赖。

```groovy
implementation 'com.baidubce:qianfan:0.0.9'
implementation 'com.baidubce:qianfan:0.1.0'
```

> 我们提供了一些 [示例](./examples),可以帮助快速了解 SDK 的使用方法并完成常见功能。
Expand Down
2 changes: 1 addition & 1 deletion java/example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.9</version>
<version>0.1.0</version>
</dependency>
</dependencies>
</project>
64 changes: 64 additions & 0 deletions java/example/src/main/java/com/baidubce/ConsoleExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024 Baidu, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.baidubce;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.util.CollUtils;

import java.util.Map;

/**
* 本示例实现了Console管控API调用流程
* API文档可见 <a href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ely8ai160">API列表</a>
*/
public class ConsoleExample {
public static void main(String[] args) {
describePresetServices();
describeTPMResource();
}

private static void describePresetServices() {
// 获取预置服务列表 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Glygmrg7v
Map<String, Object> response = new Qianfan().console()
// 对应文档中请求地址的后缀
.route("/v2/service")
// 对应文档中Query参数的Action
.action("DescribePresetServices")
// 如果不传入任何Response类,则默认返回Map<String, Object>
.execute()
// 可以传入class或者TypeRef来指定反序列化后返回的Response类
// .execute(DescribePresetServicesResponse.class)
.getResult();
System.out.println(response);
}

private static void describeTPMResource() {
// 查询TPM配额信息详情 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/ultmls9l9
Map<String, Object> response = new Qianfan().console().route("/v2/charge").action("DescribeTPMResource")
// 需要传入参数的场景,可以自行封装请求类,或者使用Map.of()来构建请求Body
// Java 8可以使用SDK提供的CollUtils.mapOf()来替代Map.of()
.body(CollUtils.mapOf(
"model", "ernie-4.0-8k",
"paymentTiming", "Postpaid"
))
.execute()
.getResult();
System.out.println(response);
}
}


113 changes: 113 additions & 0 deletions java/example/src/main/java/com/baidubce/SystemMemoryExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2024 Baidu, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.baidubce;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.core.builder.MessageBuilder;
import com.baidubce.qianfan.model.chat.Message;
import com.baidubce.qianfan.util.CollUtils;

import java.util.List;
import java.util.Map;

/**
* 本示例实现了简易的系统记忆管理接口及推理接口的全流程调用
* 系统记忆Console接口文档可见 <a href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Mlwg321zw">创建系统记忆</a>
*/
public class SystemMemoryExample {
// 在模型服务-应用接入中创建应用,即可获得应用的AppID、API Key和Secret Key
private static final String APP_ID = "替换为实际的AppId";
private static final String APP_API_KEY = "替换为实际的ApiKey";
private static final String APP_SECRET_KEY = "替换为实际的SecretKey";

public static void main(String[] args) throws InterruptedException {
// 注意,在生产环境中,应当手动创建一个系统记忆并维护记忆内容,然后在推理中重复使用该系统记忆
String systemMemoryId = createSystemMemory(APP_ID, "度小茶饮品店智能客服系统记忆");
System.out.println("系统记忆ID:" + systemMemoryId);

Boolean result = modifySystemMemory(systemMemoryId, CollUtils.listOf(
new MessageBuilder()
.add("user", "你的幸运数字是什么?")
.add("system", "我的幸运数字是42。")
.build(),
new MessageBuilder()
.add("user", "能推荐一款适合夏天饮用的饮品吗?")
.add("system", "当然可以,我们推荐冰镇柠檬绿茶,清新爽口,非常适合夏日消暑。")
.build()
));
System.out.println("修改系统记忆结果:" + result);

Thread.sleep(5000);

Map<String, Object> memories = describeSystemMemory(systemMemoryId);
System.out.println("记忆列表:" + memories);

String system = "你是度小茶饮品店的智能客服。";
String response = chat(systemMemoryId, system, "你的幸运数字是什么");
System.out.println("推理结果:" + response);
String response2 = chat(systemMemoryId, system, "推荐一个适合夏天的饮料");
System.out.println("推理结果2:" + response2);
}

private static String createSystemMemory(String appId, String description) {
return new Qianfan().console()
.route("/v2/memory")
.action("CreateSystemMemory")
.body(CollUtils.mapOf(
"appId", appId,
"description", description
))
.execute(String.class)
.getResult();
}

private static Boolean modifySystemMemory(String systemMemoryId, List<List<Message>> memories) {
return new Qianfan().console()
.route("/v2/memory")
.action("ModifySystemMemory")
.body(CollUtils.mapOf(
"systemMemoryId", systemMemoryId,
"memories", memories
))
.execute(Boolean.class)
.getResult();
}

private static Map<String, Object> describeSystemMemory(String systemMemoryId) {
return new Qianfan().console()
.route("/v2/memory")
.action("DescribeSystemMemory")
.body(CollUtils.mapOf(
"systemMemoryId", systemMemoryId
))
.execute()
.getResult();
}

private static String chat(String systemMemoryId, String system, String query) {
// 使用系统记忆时,鉴权需要使用OAuth方式,同时需要传入与系统记忆相同应用的Api Key和Secret Key
return new Qianfan(Auth.TYPE_OAUTH, APP_API_KEY, APP_SECRET_KEY).chatCompletion()
.model("ERNIE-3.5-8K")
.system(system)
.enableSystemMemory(true)
.systemMemoryId(systemMemoryId)
.addUserMessage(query)
.execute()
.getResult();
}
}
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.9</version>
<version>0.1.0</version>
<packaging>jar</packaging>

<name>qianfan</name>
Expand Down
17 changes: 17 additions & 0 deletions java/src/main/java/com/baidubce/qianfan/Qianfan.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.baidubce.qianfan.model.completion.CompletionRequest;
import com.baidubce.qianfan.model.completion.CompletionResponse;
import com.baidubce.qianfan.model.console.ConsoleRequest;
import com.baidubce.qianfan.model.console.ConsoleResponse;
import com.baidubce.qianfan.model.embedding.EmbeddingRequest;
import com.baidubce.qianfan.model.embedding.EmbeddingResponse;
import com.baidubce.qianfan.model.image.Image2TextRequest;
Expand All @@ -37,6 +39,9 @@
import com.baidubce.qianfan.model.rerank.RerankRequest;
import com.baidubce.qianfan.model.rerank.RerankResponse;

import java.lang.reflect.Type;


public class Qianfan {
private final QianfanClient client;

Expand Down Expand Up @@ -138,11 +143,23 @@ public StreamIterator<PluginResponse> pluginStream(PluginRequest request) {
return requestStream(request, PluginResponse.class);
}

public ConsoleBuilder console() {
return new ConsoleBuilder(this);
}

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);
}
}
Loading

0 comments on commit edad164

Please sign in to comment.