一、手把手教你如何注册文心一言插件服务
插件是提高生产力的关键,它们可以大大提高我们的工作效率和创造力。在实现文心一言的插件时,我们需要采取一些步骤。
- 首先,我们需要明确插件的作用和功能。这可以帮助我们更好地了解插件的需求和目的,从而更好地实现它们。
- 其次,我们需要选择合适的开发工具和平台。这可以让我们更好地利用现有的技术和资源,同时提高开发效率和质量。
- 最后,我们需要进行测试和调试。这可以确保我们的插件能够正常工作并满足我们的需求。在实现文心一言的插件时,我也遇到了许多困难和挑战。但是,通过不断地尝试和实践,我成功地实现了许多插件,并且能够有效地使用它们来提高我的工作效率和创造力。
那么怎么才能实现文心一言的插件呢?我亲身经历走了一遍,替大家踩了许多坑,分享给大家。
二、实操
1.打开插件页面
- 打开 yiyan.baidu.com/
- 选择插件开发
- 进入到 我的插件页面yiyan.baidu.com/pluginSubmi... 可以看到管理界面如下:
- 提交插件信息
此处需要服务发布的链接。
2.插件服务编写
插件目前大致结构如下:
- 目录结构
bash
-a---- 2023/11/26 4:12 793 ai-plugin.json
-a---- 2023/11/26 15:30 5274 example.yaml
-a---- 2023/11/15 0:41 495 msg_content.yaml
-a---- 2023/11/26 15:26 1605 openapi.yaml
-a---- 2023/11/15 0:41 1475 register.py
-a---- 2023/11/15 0:41 28 requirements.txt
- 文件含义
bash
- ai-plugin.json #插件主描述文件
- openapi.yaml #插件API服务的标准描述文件
- register.py #插件注册服务,可以启动到本地
- requirements.txt #启动插件注册服务所依赖的库,要求python >= 3.7
- openapi.yaml #插件API服务的标准描述文件
- register.py 服务启动
python
#!/usr/env python3
# -*- coding: UTF-8 -*-
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, Response, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from flask import Flask, request, send_file, make_response
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "https://yiyan.baidu.com"}})
@app.route("/ai-plugin.json")
async def pluginManifest():
host = request.host_url
with open("ai-plugin.json", encoding="utf-8") as f:
text = f.read().replace("PLUGIN_HOST", host)
return text, 200, {"Content-Type": "application/json"}
@app.route("/openapi.yaml")
async def openapiSpec():
host = request.host_url
with open("openapi.yaml", encoding="utf-8") as f:
text = f.read().replace("PLUGIN_HOST", host)
return text, 200, {"Content-Type": "text/yaml"}
@app.route("/example.yaml")
async def exampleSpec():
host = request.host_url
with open("example.yaml", encoding="utf-8") as f:
text = f.read().replace("PLUGIN_HOST", host)
return text, 200, {"Content-Type": "text/yaml"}
@app.route("/msg_content.yaml")
async def msgContentSpec():
host = request.host_url
with open("msg_content.yaml", encoding="utf-8") as f:
text = f.read().replace("PLUGIN_HOST", host)
return text, 200, {"Content-Type": "text/yaml"}
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=8022)
markdown
- **特别注意的是要跨域**
- 此外就是发布服务,各类yaml文件发布出去
- ai-plugin.json
python
{
"schema_version":"v1",
"name_for_human":"词云解析_1126",
"name_for_model":"wordCloud_1126",
"description_for_human":"支持用户上传txt文件,生成词云图",
"description_for_model":"Support users to upload txt files and generate wordcloud images",
"auth":{
"type":"service_http",
"authorization_type":"Basic"
},
"api":{
"type":"openapi",
"url":"http://127.0.0.1:8022/openapi.yaml"
},
"logo_url":"https://wordcloud.bj.bcebos.com/logo/wordcloud.png",
"contact_email":"yiyan-plugin@baidu.com",
"legal_info_url":"https://example.com/legal",
"examples": {
"url": "http://127.0.0.1:8022/example.yaml"
},
"msg_content": {
"url": "http://127.0.0.1:8022/msg_content.yaml"
}
}
要注意的是,logo必须互联网可访问。
- msg-content.yaml
yaml
analyze:
success:
# errCode = 0 是默认成功,无需单独定义
# 有时使用sse返回,执行过程中有很多过程,都应该定义为成功
- errCode: statistics
actionName: 进行词频统计
actionContent: 词频统计完成
- errCode: generate
actionName: 正在生成词云图
actionContent: 词云图生成完成
- errCode: output
actionName: 正在输出词云图
actionContent: 词云图输出完成
这是进度过程
- examlpe.yaml
yaml
version: 0.0.1
examples:
- context: # 对话历史
- role: user
content: "帮我生成词云图,Down, down, down. There was nothing else to do, so Alice soon began
talking again. 'Dinah'll miss me very much to-night, I should think!'
(Dinah was the cat.) 'I hope they'll remember her saucer of milk at
tea-time. Dinah my dear! I wish you were down here with me! There are no
mice in the air, I'm afraid, but you might catch a bat, and that's very
like a mouse, you know. But do cats eat bats, I wonder?' And here Alice
began to get rather sleepy, and went on saying to herself, in a dreamy
sort of way, 'Do cats eat bats? Do cats eat bats?' and sometimes, 'Do
bats eat cats?' for, you see, as she couldn't answer either question,
it didn't much matter which way she put it. She felt that she was dozing
off, and had just begun to dream that she was walking hand in hand with
Dinah, and saying to her very earnestly, 'Now, Dinah, tell me the truth
did you ever eat a bat?' when suddenly, thump! thump! down she came upon
a heap of sticks and dry leaves, and the fall was over."
- role: bot
# 触发插件
plugin:
# 应当触发的接口operationId
operationId: analyze
# 思考过程,对触发有帮助
thoughts: 这是一个生成词云图的需求
# 请求参数填写 如果是在parameter中的参数,放在第一层级即可
requestArguments: # 请求插件的参数
query: "Down, down, down. There was nothing else to do, so Alice soon began
talking again. 'Dinah'll miss me very much to-night, I should think!'
(Dinah was the cat.) 'I hope they'll remember her saucer of milk at
tea-time. Dinah my dear! I wish you were down here with me! There are no
mice in the air, I'm afraid, but you might catch a bat, and that's very
like a mouse, you know. But do cats eat bats, I wonder?' And here Alice
began to get rather sleepy, and went on saying to herself, in a dreamy
sort of way, 'Do cats eat bats? Do cats eat bats?' and sometimes, 'Do
bats eat cats?' for, you see, as she couldn't answer either question,
it didn't much matter which way she put it. She felt that she was dozing
off, and had just begun to dream that she was walking hand in hand with
Dinah, and saying to her very earnestly, 'Now, Dinah, tell me the truth
did you ever eat a bat?' when suddenly, thump! thump! down she came upon
a heap of sticks and dry leaves, and the fall was over."
- context: # 对话历史
- role: user
content: "生成一幅词云图,文字是:飞桨 (PaddlePaddle)以百度多年的深度学习技术研究和业务应用为基础,集深度学习核心框架、基础模型库、端到端开发套件、工具组件和服务平台于一体,2016 年正式开源,是全面开源开放、技术领先、功能完备的产业级深度学习平台。飞桨源于产业实践,始终致力于与产业深入融合。目前飞桨已广泛应用于工业、农业、服务业等,服务 535万开发者,创造67万个模型,服务20万企事业单位,产学研用紧密协同培养超过200万AI人才,与合作伙伴一起帮助越来越多的行业完成 AI 赋能。"
- role: bot
# 触发插件
plugin:
# 应当触发的接口operationId
operationId: analyze
# 思考过程,对触发有帮助
thoughts: 这是一个生成词云图的需求
# 请求参数填写 如果是在parameter中的参数,放在第一层级即可
requestArguments: # 请求插件的参数
query: "飞桨以百度多年的深度学习技术研究和业务应用为基础,集深度学习核心框架、基础模型库、端到端开发套件、工具组件和服务平台于一体,2016 年正式开源,是全面开源开放、技术领先、功能完备的产业级深度学习平台。飞桨源于产业实践,始终致力于与产业深入融合。目前飞桨已广泛应用于工业、农业、服务业等,服务 535万开发者,创造67万个模型,服务20万企事业单位,产学研用紧密协同培养超过200万AI人才,与合作伙伴一起帮助越来越多的行业完成 AI 赋能。"
- context: # 对话历史
- role: user
content: 如何生成一个词云图
- role: bot
# 触发插件
plugin:
# 无需触发
thoughts: 我不需要使用以上工具
- context: # 对话历史
- role: user
content: 词云图是什么
- role: bot
# 触发插件
plugin:
# 无需触发
thoughts: 用我不需要使用以上工具
- context: # 对话历史
- role: user
content: 生成一个词云图,文件地址为 https://www.erniebotplugins.cn:8081/analyze/word/cloud
- role: bot
# 触发插件
plugin:
# 应当触发的接口operationId
operationId: analyze
# 思考过程,对触发有帮助
thoughts: 这是一个生成词云图的需求,用户指定了文件的地址,只需提取文件地址即可
# 请求参数填写 如果是在parameter中的参数,放在第一层级即可
requestArguments: # 请求插件的参数
fileUrl: https://www.erniebotplugins.cn:8081/analyze/word/cloud
- openapi.yaml
yaml
openapi: 3.0.1
info:
title: word cloud API
version: 1.0.0
description:
servers:
- url: https://www.erniebotplugins.cn:8081
paths:
/analyze/word/cloud:
post:
operationId: analyze
description:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AnalyzeRequestSchema'
responses:
'200':
description: Successful SSE response
content:
text/event-stream:
schema:
$ref: '#/components/schemas/AnalyzeResponseSchema'
components:
schemas:
AnalyzeRequestSchema:
type: object
properties:
query:
type: string
description: 文字输入。如果请求中只有文字输入,fileUrl不填。
fileUrl:
type: string
description: 文字路径地址。如果请求中有地址,只填写fileUrl参数,切勿填写历史记录中的生成结果
AnalyzeResponseSchema:
type: object
properties:
errCode:
type: integer
description: 插件响应的业务错误码
errMsg:
type: string
description: 插件响应的业务错误信息
status:
type: string
description: 当前请求的状态信息
result:
type: string
description: 词云生成结果
actionName:
type: string
description: 执行动作名称
actionContent:
type: string
description: 执行动作内容
该文件非常重要,特别要注意的是servers,必须是外网网址,而不是本地地址。此外涉及到图片等访问,必须是https的域名才行,这样才可以跨域访问。
3.鉴权
此处鉴权token为星河社区首页token,直接拷贝进去即可。
至此,插件发布完毕