百度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
相关推荐
OJAC近屿智能12 小时前
ChatGPT再升级!
大数据·人工智能·百度·chatgpt·近屿智能
不爱吃饭爱吃菜2 天前
uniapp微信小程序-长按按钮百度语音识别回显文字
前端·javascript·vue.js·百度·微信小程序·uni-app·语音识别
资深の小白2 天前
一个基于 Spring Boot 的实现,用于代理百度 AI 的 OCR 接口
人工智能·spring boot·百度
陈思杰系统思考Jason4 天前
系统平衡与企业挑战
百度·微信·微信公众平台·新浪微博·微信开放平台
空中湖4 天前
中国人工智能智能体研究报告
搜索引擎·百度
程序员的世界你不懂5 天前
数据处理专题(十三)
经验分享·百度
白杨SEO营销10 天前
白杨SEO:如何查看百度、抖音、微信、微博、小红书、知乎、B站、视频号、快手等7天内最热门话题及流量关键词有哪些?使用方法和免费工具推荐以及注意事项【干货】
百度
学渣6765615 天前
多模态人工智能研究:视觉语言模型的过去、现在与未来
搜索引擎·计算机视觉·百度·多模态
KuaFuAI17 天前
百度「心响」:通用超级智能体,重新定义AI任务执行新范式
人工智能·百度·agent·mcp
百度Geek说19 天前
千亿级打点PV的成本治理实践
后端·百度