一、准备工作
进入百度智能云千帆大模型平台,点击应用接入-创建应用;按提默认完成创建
二、开始使用
单轮调用
进入API列表 - ModelBuilder以第一个ERNIE-4.0-8K为例,选择“HTTP请求调用”,把第一步创建应用的 应用API Key、应用Secret Key替换到代码中,即可进行单轮对话。
import requests
import jsondef get_access_token():"""使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key"""url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"payload = json.dumps("")headers = {'Content-Type': 'application/json','Accept': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)return response.json().get("access_token")def main():url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + get_access_token()payload = json.dumps({"messages": [{"role": "user","content": "介绍一下北京"}]})headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)if __name__ == '__main__':main()
多轮调用
每次请求调用5轮之前的会话,并且把response保存在会话记录参与到下一轮的调用。
import timeimport pandas as pd
import json
import requestsAPI_KEY="API_KEY"
SECRET_KEY="SECRET_KEY"menu=['query1','query2','query3','query4']#上下文初始化
context=[]context_prefix="文本的内容是:"
context_suffix="。文本中内容区分开便于后期做embedding"
#initial_question="你好,能帮我描述系统菜单吗?"all_responses=[]def get_access_token():
"""
使用AK,SK生成鉴权签名(AccessToken)
:return:access_token,或是None(如果错误)
"""url="https://aip.baidubce.com/oauth/2.0/token"params={"grant_type":"client_credentials","client_id":API_KEY,"client_secret":SECRET_KEY}returnstr(requests.post(url,params=params).json().get("access_token"))#Functiontoaddanewmessagetothecontextandmaintainthelatest5rounds
def update_context(new_assistant_response):#添加用户的新提问#context.append({"role":"user","content":new_user_input})#添加模型的新回答context.append({"role":"assistant","content":new_assistant_response})#保持上下文的最新五轮对话(即10条消息)
if len(context)>9:context.pop(0)context.pop(0)def get_desc_by_api(context):url="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro? access_token="+get_access_token()payload=json.dumps({"messages":context
})headers={'Content-Type':'application/json'
}response=requests.request("POST",url,headers=headers,data=payload)returnresponsedefinitialize_context():context.extend([
{
"role":"user",
"content":"query1"
},{
"role":"assistant",
"content":"response1"
},{
"role":"user",
"content":"query2"
},{
"role":"assistant",
"content":"response2"
}
])
#context=context[0]initialize_context()
print("context",context)
#模拟多轮对话
for i in range(len(menu)):current_menu_item=menu[i]#生成用户提问new_user_input=context_prefix+current_menu_item+context_suffixcontext.append({"role":"user","content":new_user_input})#发送请求api_response=get_desc_by_api(context)print("status_code:",api_response.text)ifapi_response.status_code==200:new_assistant_response=api_response.json().get("result")#更新上下文update_context(new_assistant_response)print(f"对话轮次{i+1},当前上下文:{context}")response_entry={"menu_item":current_menu_item,"response":new_assistant_response}all_responses.append(response_entry)#将所有回答存储到一个JSON文件中
with open('responses.json','w',encoding='utf-8') as f:json.dump(all_responses,f,ensure_ascii=False,indent=4)