python调用文心千帆大模型

一、登录智能云控制台

https://console.bce.baidu.com/

二、创建AI应用

已登录状态下,进入控制台创建应用 https://console.bce.baidu.com/ai/#/ai/easydlLiteNlp/app/list
应用创建后获取AppID、API Key、Secret Key
在这里插入图片描述

三、API调用

import requests
import json


def 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?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()

猜你喜欢

转载自blog.csdn.net/qq236237606/article/details/131897049