欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 百度ERNIE-SDK使用示例

百度ERNIE-SDK使用示例

2024/10/25 0:32:15 来源:https://blog.csdn.net/WMX843230304WMX/article/details/141615034  浏览:    关键词:百度ERNIE-SDK使用示例

百度ERNIE-SDK使用示例

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

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

调用大模型示例

import erniebot
from typing import List, Optionalimport requests
import jsonimport asyncio
import osfrom 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,):passdef 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:# 将返回的内容解析为JSONtoken_info = response.json()# 从JSON中提取access_tokenaccess_token = token_info.get("access_token")return access_tokenreturn "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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com