登录百度智能云:百度智能云
文心一言4.0使用
开通付费:
 
 创建应用:
 
 自行创建应用名称:
 
 对话测试:
 
API测试
ERNIE-Bot-4 API:ERNIE-Bot-4
打开链接查看自己的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_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()
 
其中,[应用API Key]和[应用Secret Key]分别复制client_id和client_secret,注意是完整字符,不带*。
在PyCharm中即可看到文本回复:
 {
“id”:“as-xzha01438e”,
“object”:“chat.completion”,
“created”:1698112809,
“result”:“我的大模型版本是文心一言,英文名是ERNIE Bot,是基于百度文心大模型技术推出的生成式对话产品。百度文心大模型是百度自主研发的产业级知识增强大模型,既包含基础通用的大模型,也包含面向重点任务领域和行业的大模型,以及丰富的工具与平台,支撑企业与开发者进行高效便捷的应用开发。相比Chat GPT,文心一言更适用于中文语境。”,
“is_truncated”:false,
“need_clear_history”:false,
“usage”:{“prompt_tokens”:10,“completion_tokens”:140,“total_tokens”:150}
}



















