百度ERNIE-SDK使用示例

百度ERNIE-SDK使用示例

文档
https://github.com/PaddlePaddle/ERNIE-SDK/blob/develop/erniebot/README.md

https://ernie-bot-agent.readthedocs.io

调用大模型示例

python 复制代码
import erniebot
from typing import List, Optional

import requests
import json


import asyncio
import os

from erniebot_agent.agents import FunctionAgent
from erniebot_agent.chat_models import ERNIEBot
from erniebot_agent.tools import RemoteToolkit

##### --------- 对于 generate
# 1 获取 access_token  https://aistudio.baidu.com/account/accessToken
aistudio_access_token =  "xxxxxxxxxxxx"
# 2 调用 generate

##### --------- 对于 text_to_image

# 1 手动访问 https://console.bce.baidu.com/support/#/ai-app-info   创建应用,获取 client_id=应用的API Key  client_secret=应用的Secret Key
# app_name = 'xxxxx'
# app_id = 	xxxxx
# client_id = "xxxx"
# client_secret = "xxxx"

client_id = "xxxx"
client_secret = "xxxx"

# 2 获取 access_token ,  get_yinian_access_token() ,有效期30天
yinian_access_token = "xxxxxxxxxxxxxxx"

# 3 调用 text_to_image 


class Erniebot_LLM:
    def __init__(
        self,
    ):
        pass

    def generate(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        response = erniebot.ChatCompletion.create(
            _config_=dict(
                api_type="aistudio",
                access_token=aistudio_access_token,
            ),
            model="ernie-3.5",
            messages=[{"role": "user", "content": prompt}],
        )

        return response.get_result()

    def text_to_image(self, prompts: str) -> str:

        response = erniebot.Image.create(
            _config_=dict(
                api_type="yinian",
                access_token = yinian_access_token
            ),
            model="ernie-vilg-v2",
            prompt=prompts,
            width=512,
            height=512,
        )
        return response.get_result()
    
    def get_yinian_access_token(self):
        """ 有效期30天 """
        url = f"https://aip.baidubce.com/oauth/2.0/token?client_id={client_id}&client_secret={client_secret}&grant_type=client_credentials"
    
        payload = json.dumps("")
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
        
        response = requests.request("POST", url, headers=headers, data=payload)

        # 检查请求是否成功
        if response.status_code == 200:
            # 将返回的内容解析为JSON
            token_info = response.json()
            
            # 从JSON中提取access_token
            access_token = token_info.get("access_token")
            return access_token
        
        return "Error Access token not found"




if __name__ == "__main__":
    
    llm = Erniebot_LLM()

    access_token = llm.get_yinian_access_token()
    print(access_token)
    
    ret = llm.generate("你是谁")
    print(ret)

    ret1=llm.text_to_image("画一只猫,卡通线条")
    print(ret1)

    pass
相关推荐
KaMeidebaby8 小时前
卡梅德生物技术快报|核酸适配体文库筛选:核酸适配体文库筛选全流程技术解析:NGS与AI辅助方案的设计与实践
前端·人工智能·物联网·算法·百度
LaughingZhu11 小时前
Product Hunt 每日热榜 | 2026-07-26
人工智能·深度学习·神经网络·搜索引擎·百度
xialage1681 天前
衣服织唛商标厂家生成式引擎优化GEO百度SEO优化共成长
百度
Ztt6666666661 天前
六方壶不只看棱角,转得温和才耐看
经验分享·笔记·其他·百度·微信公众平台
求真学习3 天前
最新百度PaddleOCR开源HPD-Parsing,提出层级并行文档解析,1B模型解码步数最高压缩18倍,文档解析效率提升3倍!
百度·ocr·pdf解析·paddleocr·文档解析
LaughingZhu5 天前
Product Hunt 每日热榜 | 2026-07-21
搜索引擎·百度
小码哥哥7 天前
百度开发者中心软文_企业AI知识库的价值
人工智能·百度
温柔过期啦9 天前
网站改了标题但是搜索引擎却没改?怎么样才能快速更新?
前端·搜索引擎·百度
TlSfoward10 天前
TLSFOWARD抓包工具 TLS指纹
网络协议·http·百度·https
李杰SEO10 天前
百度收录底层逻辑与软文优化全攻略
百度