百度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
相关推荐
AABBCC1033 小时前
城市天际线2/都市天际线2/Cities: Skylines ll(更新:v1.4.2f1)
百度
wan55cn@126.com8 小时前
人生如戏:换个片场,继续出演
人工智能·笔记·百度·微信
Apache Flink13 小时前
Apache Flink 2.2.0: 推动实时数据与人工智能融合,赋能AI时代的流处理
人工智能·搜索引擎·百度·flink·apache
zhaodiandiandian1 天前
人工智能与就业重构:机遇、挑战与政策应对
人工智能·百度·重构
罗光记1 天前
Solon AI 开发学习6- chat- 两种http 流式输入输出
数据库·百度·facebook·新浪微博·segmentfault
物联网软硬件开发-轨物科技2 天前
【轨物交流】杭州市科协走进轨物科技 共探“人工智能+X”创新应用
百度
文心快码BaiduComate2 天前
CCF程序员大会码力全开:AI加速营决赛入围名单揭晓,12月6日大理见!
前端·百度·程序员
陈思杰系统思考Jason3 天前
业务创新与系统思考
百度·微信·微信公众平台·新浪微博·微信开放平台
申耀的科技观察4 天前
【观察】为AI就绪筑基,为产业智能引路,联想凌拓铺就AI规模化落地通途
人工智能·百度
liugang_lawyer5 天前
防范新型金融诈骗
笔记·百度·微信·金融·微信公众平台·新浪微博