百度文心一言4.0——使用及API测试

登录百度智能云:百度智能云

文心一言4.0使用

开通付费:

创建应用:

自行创建应用名称:

对话测试:

API测试

ERNIE-Bot-4 API:ERNIE-Bot-4

打开链接查看自己的API Key,Secret Key。

可参考:API在线调试介绍

找到示例代码即可:

python 复制代码
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}

}

相关推荐
伊织code12 小时前
PyTorch API 2
pytorch·api·cpu·cuda·微分·autograd
ruster19 小时前
RustMailer `/unified-search` API: 高速邮件搜索与 CRM 集成
api
一个专注api接口开发的小白2 天前
手把手教程:使用 Postman 测试与调试淘宝商品详情 API
前端·数据挖掘·api
xcLeigh3 天前
文心一言4.5开源模型实战:ERNIE-4.5-0.3B轻量化部署与效能突破
人工智能·开源·大模型·文心一言·ernie·轻量化部署
伊织code3 天前
PyTorch API 6
pytorch·api·ddp
电商API大数据接口开发Cris3 天前
Java Spring Boot 集成淘宝 SDK:实现稳定可靠的商品信息查询服务
前端·数据挖掘·api
伊织code4 天前
PyTorch API 7
pytorch·api·张量·稀疏
蓝倾4 天前
如何将淘宝获取店铺所有商品API接口应用到自己店铺?实战讲解详细步骤
api·fastapi
一个专注api接口开发的小白5 天前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
前端·数据挖掘·api
电商api24677428106 天前
用拼多多 API 实现拼多多店铺商品搜索权重提升
api