百度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
相关推荐
摘星编程10 天前
中国MCP市场:腾讯、阿里、百度的本土化实践
百度·阿里巴巴·腾讯·中国mcp市场·本土化实践
市象11 天前
百度智能云给“数字人”发工牌
百度
白杨SEO营销13 天前
白杨SEO:百度搜索开放平台发布AI计划是什么?MCP网站红利来了?顺带说说其它
人工智能·百度
倔强的石头10613 天前
[源力觉醒 创作者计划]_文心4.5开源测评:国产大模型的技术突破与多维度能力解析
百度·开源·文心一言·文心大模型
数据知道13 天前
百度翻译详解:包括PaddleNLP、百度AI开放平台、接口逆向(包括完整代码)
人工智能·百度·语言模型·自然语言处理·机器翻译
下页、再停留16 天前
【PHP】接入百度AI开放平台人脸识别API,实现人脸对比
人工智能·百度·php
京东零售技术16 天前
京东零售在智能供应链领域的前沿探索与技术实践
人工智能·百度·零售
高hongyuan20 天前
搜索引擎高级搜索指令大全(Google、百度等浏览器通用)
搜索引擎·百度·google
趣味科技v21 天前
WAIC直击:百度慧播星发布新一代数字人技术NOVA
百度