前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Erniebot搭建学习&绘图网站

基于Erniebot搭建学习&绘图网站

原创
作者头像
用户4080233
发布2024-06-17 00:19:42
870
发布2024-06-17 00:19:42

1 效果

1.1 学习网站

1.2 绘图网站

2 方案实现

2.1 文心一言SDK安装及使⽤

erniebot-0.5.3-py3-none-any.whl

https://pypi.org/project/erniebot/#files

代码语言:javascript
复制
安装erniebot 需要python3.8 以上,实际运⾏时需要openssl 1.1 以上
代码语言:python
代码运行次数:0
复制
import erniebot 
# 定义使⽤的令牌为aistudio平台的,这是访问⽂⼼⼀⾔API的必要凭证 
erniebot.api_type = 'aistudio' 
# 引⼊⾃⼰的令牌,该令牌⽤于验证API请求的身份 
# 注意:此处的令牌是⼀个示例,实际使⽤时需要替换成⾃⼰的令牌 
erniebot.access_token = "从https://aistudio.baidu.com/index/accessToken获取" 
# 定义使⽤的模型是ernie-bot,这是文心一言的⼀个模型 
# 查看⽀持的模型 
# List supported models 
models = erniebot.Model.list() 
[ 
    ('ernie-3.5', '⽂⼼⼤模型(ernie-3.5)'), 
    ('ernie-turbo', '⽂⼼⼤模型(ernie-turbo)'), 
    ('ernie-4.0', '⽂⼼⼤模型(ernie-4.0)'), 
    ('ernie-longtext', '⽂⼼⼤模型(ernie-longtext)'), 
    ('ernie-text-embedding', '⽂⼼百中语义模型'), 
    ('ernie-vilg-v2', '⽂⼼⼀格模型')
]
print(models)
# ernie-bot ⽂⼼⼀⾔旗舰版
# ernie-bot-turbo ⽂⼼⼀⾔轻量版
# ernie-text-embedding ⽂⼼百中语义模型
# ernie-vilg-v2 ⽂⼼⼀格模型
model = 'ernie-3.5'
# 定义prompt
message_content = "The task scenario is: I need you to refine the knowledge points
I provide into four small modules to help me learn. " \
 "The best way to refine is to follow a good learning path, and
you need to stand from the perspective of a teacher to help me learn the knowledge
well. " \
 f"-对每个模块进⾏介绍,让读者能够直观的知道该模块的学习内容 我提供的知识点为:{question} " \
 "-示例json⽂件如下,参考它的格式:[{\"模块主题\": \"\", \"本模块内容简介
\": \"\"},] " \
 "- Strictly follow the format I provided " \
 "- 每个模块的介绍在30个中⽂汉字左右。 " \
 "- The output is just pure JSON format, with no other descriptions."
# 将⽂本和其他参数封装成消息,便于传给⽂⼼
messages = [{
     'role': 'user', # ⽤户的⻆⾊
     'top_p': '0.001', # ⽂⼼⼀⾔⽣成⽂本时的参数,控制⽣成的多样性
     'content': message_content # 传输的⽂本内容
 }]
# 调用文心一言的ChatCompletion.create⽅法,将封装好的消息和其他参数传递给文心,以获取精炼后的知
识点模块
response = erniebot.ChatCompletion.create(
    model=model, # 使⽤的模型
    messages=messages, # 传输的消息
)
# 将返回的精炼后的知识点模块的⽂本内容赋值给answer变量
answer = response.result

注:文心一言SDK详细使用参考

Erniebot SDK & Langchain Qianfan使⽤

绘图使⽤AI作画-基础版API,基于⽂⼼ERNIE-ViLG⼤模型

api参考文档:https://ai.baidu.com/ai-doc/NLP/Ml9i5amtk

如果调⽤api, 需要调发送请求和查询结果两个api

sdk参考文档:https://ernie-bot-agent.readthedocs.io/zh-cn/latest/sdk/guides/image

如果⽤sdk, 会提示无权限。因为vilg模型需要有⼀格那边的鉴权才能⽤,其他LLM用aistudio这边BU提供即可,可以免费用用。

代码语言:javascript
复制
# 发送请求 
url = "https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token=****" 
payload = json.dumps({ 
    "text": query, 
     "resolution": "1024*1024", 
     "style": "写实⻛格" 
}) 
headers = { 
     'Content-Type': 'application/json', 
     'Accept': 'application/json' 
} 
response = requests.request("POST", url, headers=headers, data=payload) 
data = response.json() 
taskId = data["data"]["taskId"] 

# 查询结果 
url = "https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImg?access_token=****" 
payload = json.dumps({
    "taskId": taskId 
})
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 200: 
    data = response.json()["data"]
    return data.get('img', None)
return None

2.2 工程框架

Django(⼀个⾼级的Python Web框架)

启动命令

代码语言:javascript
复制
python manage.py runserver host:port

其它框架可以是streamlit 或者gradio。

aistudio 应用通常使⽤的是streamlit 或者gradio https://www.gradio.app/docs/interface,示例如下:

代码语言:javascript
复制
streamlit:基于⽂⼼⼀⾔API的⽂档知识问答系统_AI应⽤-⻜桨AI Studio星河社区
gradio: Gradio应⽤介绍与零基础部署实践 - ⻜桨AI Studio星河社区 (https://aistudio.baidu.com/projectdetail/6536645)

几个月前上网看到的一言系列产品,附上来

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 效果
    • 1.1 学习网站
      • 1.2 绘图网站
      • 2 方案实现
        • 2.1 文心一言SDK安装及使⽤
          • 2.2 工程框架
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档