代码拉取完成,页面将自动刷新
import requests
import json
with open("./config.json", "r", encoding="utf-8") as f:
config = json.load(f)
API_KEY = config['API_KEY']
SECRET_KEY = config['SECRET_KEY']
context=[{"role": "user", "content": "你是专业的翻译助手"},{"role": "assistant", "content": "好的,请开始提问"}]
def ErnieBot(prompt, model="Ernie"):
prompt=prompt[0:11000]
if model=="Ernie":
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=" + get_access_token()
prompt=prompt[0:11000]
elif model=="ChatGLM":
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatglm2_6b_32k?access_token=" + get_access_token()
prompt=prompt[0:2000]
context.append({"role": "user", "content": prompt})
payload = json.dumps({
"messages": context,
"temperature": 0.1
})
headers = {'Content-Type': 'application/json'}
response = requests.request("POST", url, headers=headers, data=payload)
result = json.loads(response.text)['result']
context.append({"role": "assistant", "content": result})
return result
def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。