python整合文言一心聊天代码

更新时间:2024/6/2 18:25:21点击: 技术文章



ERNIE-Bot 4.0是百度最新发布的自研?语?模型,实现了基础模型的全面升级,在理解、生成、逻辑和记忆能力上都有着显著提升,支持5K输入+2K输出。本文介绍了千帆相关API及应用。


import requests
import json

API_KEY = "0QUv2R****vr88hC"
SECRET_KEY = "lPOCLl****vk64Y1"

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({
        "temperature": 0.95,
        "top_p": 0.8,
        "penalty_score": 1,
        "disable_search": False,
        "enable_citation": False
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :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}
    return str(requests.post(url, params=params).json().get("access_token"))

if __name__ == '__main__':
    main()