百度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
相关推荐
小小鱼儿小小林36 分钟前
百度世界 2025 核心看点:文心 5.0、萝卜快跑、惠博星数字人、伐谋智能体齐亮相!
百度·数字人·千帆·百度世界
DisonTangor41 分钟前
【百度拥抱开源】介绍ERNIE-4.5-VL-28B-A3B-Thinking:多模态AI的重大突破
人工智能·百度·语言模型·开源·aigc
道可云2 天前
制造强国新图景实践——解析《实施意见》对制造业的影响
百度·制造
Ma0407132 天前
【论文阅读16】-LLM-TSFD:一种基于大型语言模型的工业时间序列人机回路故障诊断方法
百度
YG亲测源码屋2 天前
新手做网站如何被百度快速收录教程
百度·百度收录·快速收录
罗光记3 天前
Quantinuum 发布新型量子计算机“Helios“
数据库·经验分享·其他·百度·twitter
oil欧哟3 天前
文心 5.0 来了,百度大模型的破局之战
前端·人工智能·百度·prompt
YG亲测源码屋3 天前
怎么让自己的网址被百度收录(网站如何被百度收录进去)
java·百度·dubbo
YG亲测源码屋3 天前
[SEO]什么是百度快速收录?如何优先获得快速收录权益?
百度·dubbo
GitCode官方3 天前
GitCode 同步发布百度 ERNIE-4.5-VL-28B-A3B-Thinking 多模态大模型
百度·gitcode