3
千帆大模型之调用API实战
大模型开发/技术交流
- 社区上线
- 开箱评测
- API
2023.10.051532看过
天呢,太厉害了,作为一个前端学生,开发出来了一个AI程序!!!!
千帆大模型平台的控制台界面还是挺友好的,直接把开发流程给列出来了。
因为是调用API,所以按照常理,需要创建一个应用:
可以看到,可以调用N个模型的接口,嘿嘿,我还是选择了百度自研的ERNIE-Bot-turbo:
在左侧菜单导航中,找到应用接入菜单,并进入
然后创建
创建的很快,眨眼间就创建成功了:
点进去详情界面,就可以看到该应用的详细信息,API Key,Secret Key都是一会要用到的,可以先记录下来。
调用API
官方文档提供了基础写法
在Python3中可以使用requests可以比较友好的操作api请求:
#!/usr/bin/python3import requestsimport json# 一、获取access_tokenurl = "https://aip.baidubce.com/oauth/2.0/token"APIKey = "lWoUs2oSxE5WK8OVzfaNx5vg"SecretKey = "SRpXtKFx7QgatWoGraXc3LU5lFcxospQ"params = {"grant_type" : "client_credentials","client_id" : APIKey,"client_secret" : SecretKey,}response = requests.post(url, timeout=10, params=params)jsondata = json.loads(response.text)print(jsondata)
打印:
这里比较重要,需要我们记住的字段是 access_token,之后需要调用其他API的时候,要用到。expires_in是有效时间,这个时间过去了,获取到的token就无效了,按照常理是需要使用refresh_token来重新刷新token的,youtube等是这样处理的。
{"refresh_token": "25.140b781be704a90c5110e04ecf8eacec.315360000.2011879778.282335-40538962","expires_in": 2592000,"session_key": "9mzdXUxNC8qtzMr2ePJC/oHHPZjOt3YUCQVg8ACGx7H+x92ixYMBaYVFQwBPF0R+7PMFCOVHR5GD7Y194ZOfwtPmvsCz3A==","access_token": "24.f8e7510486923e9ff4d3efa0148d61cf.2592000.1699111778.282335-40538962","scope": "public brain_all_scope ai_custom_yiyan_com ai_custom_yiyan_com_eb_instant wenxinworkshop_mgr ai_custom_yiyan_com_bloomz7b1 ai_custom_yiyan_com_emb_text ai_custom_yiyan_com_llama2_7b ai_custom_yiyan_com_llama2_13b ai_custom_yiyan_com_llama2_70b ai_custom_yiyan_com_chatglm2_6b_32k ai_custom_yiyan_com_aquilachat_7b ai_custom_yiyan_com_emb_bge_large_zh ai_custom_yiyan_com_emb_bge_large_en ai_custom_yiyan_com_qianfan_chinese_llama_2_7b ai_custom_qianfan_bloomz_7b_compressed wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx","session_secret": "22e3c10387939e298dd9c85c570cfad1"}
如果不熟悉python操作json的话,这样就可以取到access_token的val:
access_token = jsondata["access_token"]print("##########################")print(access_token)
打印:
{"refresh_token": "25.c7ea683b6f7e23bcbef19d4fca5e81c0.315360000.2011879949.282335-40538962","expires_in": 2592000,"session_key": "9mzdWulL3t7BkomWPdts5gK5RxPBmWk+sDynRwI8aq+b39Q/IOiz9gJ/D89uwsa2JKBmNpIjM/e/Ubgaa74TKBUnJgwBtQ==","access_token": "24.a53ab06ff6df690595563ccc9a15bbe9.2592000.1699111949.282335-40538962","scope": "public brain_all_scope ai_custom_yiyan_com ai_custom_yiyan_com_eb_instant wenxinworkshop_mgr ai_custom_yiyan_com_bloomz7b1 ai_custom_yiyan_com_emb_text ai_custom_yiyan_com_llama2_7b ai_custom_yiyan_com_llama2_13b ai_custom_yiyan_com_llama2_70b ai_custom_yiyan_com_chatglm2_6b_32k ai_custom_yiyan_com_aquilachat_7b ai_custom_yiyan_com_emb_bge_large_zh ai_custom_yiyan_com_emb_bge_large_en ai_custom_yiyan_com_qianfan_chinese_llama_2_7b ai_custom_qianfan_bloomz_7b_compressed wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx","session_secret": "1e5d2d7ae7ea52fb4420da9172e3a602"}##########################24.a53ab06ff6df690595563ccc9a15bbe9.2592000.1699111949.282335-40538962
结合上面获取到的json中的token,就可以继续下一步了:
定义了一个msg为发送的消息,然后再收到一个消息来接收
# 二、发送chat消息msg="你好,请问你是谁?"recv_msg = ""_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant"# 1、发送params = { "access_token" : access_token}body = {"messages": [{"role": "user","content": msg}]}headers = { "Content-Type": "application/json" }json_data = json.dumps(body, indent=4)response = requests.post(_url, timeout=10, params=params, data=json_data ,headers=headers)# 转成JSONjsondata = json.loads(response.text)print(jsondata)recv_msg = jsondata["result"]# 2、接受&处理 消息print("收到了: ",recv_msg)
打印:
{"refresh_token": "25.719912c92cebc40cf1d21f26ff1fb24a.315360000.2011880136.282335-40538962","expires_in": 2592000,"session_key": "9mzdDZeT+GD1MZn9cujgg17HF0MmnB+wqlegHd03WxVl+4TJqqKEr8BcCx4bNvfJOSxLmHGWQ1DDEk0psF4KtWl6z0AB5g==","access_token": "24.6adb689e43276d8392615996ac6981fa.2592000.1699112136.282335-40538962","scope": "public brain_all_scope ai_custom_yiyan_com ai_custom_yiyan_com_eb_instant wenxinworkshop_mgr ai_custom_yiyan_com_bloomz7b1 ai_custom_yiyan_com_emb_text ai_custom_yiyan_com_llama2_7b ai_custom_yiyan_com_llama2_13b ai_custom_yiyan_com_llama2_70b ai_custom_yiyan_com_chatglm2_6b_32k ai_custom_yiyan_com_aquilachat_7b ai_custom_yiyan_com_emb_bge_large_zh ai_custom_yiyan_com_emb_bge_large_en ai_custom_yiyan_com_qianfan_chinese_llama_2_7b ai_custom_qianfan_bloomz_7b_compressed wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx","session_secret": "bd82b164771dbd38166088b9b41c4769"}##########################24.6adb689e43276d8392615996ac6981fa.2592000.1699112136.282335-40538962{'id': 'as-kvynzjpcgx', 'object': 'chat.completion', 'created': 1696520137, 'result': '你好,我是百度研发的知识增强大语言模型,我能够与人对话互动,回答问题,协助创作,高效便捷地帮助人们获取信息、知识和灵感。', 'is_truncated': False, 'need_clear_history': False, 'usage': {'prompt_tokens': 7, 'completion_tokens': 53, 'total_tokens': 60}}收到了: 你好,我是百度研发的知识增强大语言模型,我能够与人对话互动,回答问题,协助创作,高效便捷地帮助人们获取信息、知识和灵感。
天呢,太厉害了,作为一个前端学生,开发出来了一个AI程序!!!!
全部代码放这了~~~,可以一键运行
#!/usr/bin/python3import requestsimport json# 一、获取access_tokenurl = "https://aip.baidubce.com/oauth/2.0/token"APIKey = "lWoUs2oSxE5WK8OVzfaNx5vg"SecretKey = "SRpXtKFx7QgatWoGraXc3LU5lFcxospQ"params = {"grant_type" : "client_credentials","client_id" : APIKey,"client_secret" : SecretKey,}response = requests.post(url, timeout=10, params=params)jsondata = json.loads(response.text)formatted_json_str = json.dumps(jsondata, indent=4) # 使用indent参数来设置缩进空格数print(formatted_json_str)access_token = jsondata["access_token"]print("##########################")print(access_token)# 二、发送chat消息msg="你好,请问你是谁?"recv_msg = ""_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant"# 1、发送消息# 封装query消息params = { "access_token" : access_token}# 封装body参数body = {"messages": [{"role": "user","content": msg}]}headers = { "Content-Type": "application/json" }json_data = json.dumps(body, indent=4) # indent 参数可选,用于格式化输出response = requests.post(_url, timeout=10, params=params, data=json_data ,headers=headers)if response.status_code == 200:# 转成JSONjsondata = json.loads(response.text)print(jsondata)recv_msg = jsondata["result"]else:print("请求失败")# 2、接受&处理 消息print("收到了: ",recv_msg)
评论