-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
b8ea363
commit edad164
Showing
49 changed files
with
2,085 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
java/example/src/main/java/com/baidubce/ConsoleExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
java/example/src/main/java/com/baidubce/SystemMemoryExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.