• 文心一言插件开发全流程,ERNIE-Bot-SDK可以调用文心一言的能力


    前言

    在这里插入图片描述

    2023年8月31日,文心一言 率先向全社会全面开放。
    随之而来的便是向开发者开放 插件 生态。

    在这里插入图片描述

    在这里插入图片描述

    插件

    插件是什么

    如果说文心一言是一个智能中枢大脑,插件就是文心一言的耳、目、手。插件将“文心一言”AI能力与外部应用相结合,既能丰富大模型的能力和应用场景,也能利用大模型的生成能力完成此前无法实现的任务,比如发邮件等。

    工作原理

    • 插件注册:开发者将插件的 manifest 文件注册到一言插件库中,校验通过后一言即可使用插件处理用户 query 。
    • 插件触发:解析调度模块将使用生成的API,来调用插件服务。插件服务完成处理后,返回 json 数据由一言汇总结果进行返回。
    • 插件解析:一言插件系统的触发调度模块,将识别用户 query,并将根据 manifest 文件中的插件API接口和参数的自然语言描述来选择使用哪个插件,以及生成调用插件的 API。

    例如用户在平台上选择天气插件,输入:“今天北京的天气怎么样?”。 模型首先会根据用户意图调用天气插件,并且解析query中时间(今天)和地点(北京)信息,然后以 json 结构输入开发者提供的天气API接口中,获得接口返回的天气信息,经过大模型进行语言润色后,生成面向用户的回答。

    申请开发权限

    申请开发权限:https://yiyan.baidu.com/developer

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    等待申请通过的邮件就可以开始了

    开始

    第一步:安装python

    python下载链接:https://www.python.org/downloads/

    问题1:如果在cmd中输入python会打开应用商店,就在用户变量的 PATH 中把WindowsApps移到python下面
    在这里插入图片描述
    问题2:如果cmd中报“pip指令找不到”,那就要在用户变量的 PATH 中添加C:\Users\25218\AppData\Local\Programs\Python\Python311\Scripts

    第二步:搭建项目

    单词本的项目:https://pan.baidu.com/s/1K7wjPMPMprxtosnF5D-3tQ?pwd=q7xa

    接入一个插件核心流程如下所示:

    • 构思插件 manifest 描述文件(ai-plugin.json,必选)
    • 定义插件服务描述文件(openapi.yaml,必选)
    • 开发自己的plugin-server(openapi服务,必选)

    在这里插入图片描述

    manifest 描述文件:ai-plugin.json

    {
        "schema_version":"v1",(插件的版本号,用于开发者标记和使用)
        "name_for_human":"天气预报_dwh",(此字段将面向用户查看,平台内全局唯一标识,后缀数字建议长且随机,更不容易重名冲突)
        "name_for_model":"weather_prediction_dwh",(模型将用于定位插件的名称,建议是有语义信息的英文字符串)
        "description_for_human":"个性化查询不同地区的天气",(面向用户介绍插件,建议介绍插件的主要能力,相关限制等。不超过100个字符,前端可完整显示前40 个字符,超出的字符将在用户 hover 时展示。)
        "description_for_model":"帮助用户查询不同地区的天气",(面向模型的自然语言描述,请描述插件的核心能力、使用场景等,将用于模型参考解析是否触发插件,建议不超过200个字符。)
        "auth": (用户鉴权相关字段)
        {
            "type": "none"    
        },
        "api":API规范) 
        {
            "type": "openapi",
            "url": "http://127.0.0.1:8081/.well-known/openapi.yaml"
        },
        "logo_url": "http://127.0.0.1:8081/logo.png",(用于获取插件标识的URL"contact_email": "support@example.com",(安全/审核、支持和停用的电子邮件联系方式)
        "legal_info_url": "http://www.example.com/legal"(用户查看插件信息的重定向URL}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    插件服务描述文件:openapi.yaml

    openapi: 3.0.1
    info:
      title: 天气预报
      description: 个性化查询不同地区的天气。
      version: "v1"
    servers:
      - url: http://127.0.0.1:8081
    paths:
      /get_weather:
        post:
          operationId: getWeather
          summary: 展示天气
          requestBody:
            required: true
            content:
              application/json:
                schema:
                  $ref: "#/components/schemas/getWeather"
          responses:
            "200":
              description: 天气展示完成
              content:
                application/json:
                  schema:
                    $ref: "#/components/schemas/weatherResponse"
    components:
      schemas:
        getWeather:
          type: object
          required: [city]
          properties:
            city:
              type: string
              description: 城市
        weatherResponse:
          type: object
          required: [weather]
          properties:
            weather:
              type: object
              description: 天气内容
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    开发自己的plugin-server

    Python Web框架里比较有名当属Django,Flask相对于Django而言是轻量级的Web框架。

    #!/usr/env python3
    # -*- coding: UTF-8 -*-
    
    from flask import Flask, request, send_file, make_response
    from flask_cors import CORS
    import json
    import random
    import requests
    
    #读取key
    file = open('.env')
    KEY=file.readline()[4:]
    print(KEY)
    file.readline()
    
    app = Flask(__name__)
    CORS(app, resources={r"/*": {"origins": "https://yiyan.baidu.com"}})
    
    def make_json_response(data, status_code=200):
        response = make_response(json.dumps(data), status_code)
        response.headers["Content-Type"] = "application/json"
        return response
    
    @app.route("/logo.png")
    async def plugin_logo():
        """
            注册用的:返回插件的logo,要求48 x 48大小的png文件.
            注意:API路由是固定的,事先约定的。
        """
        return send_file('logo.png', mimetype='image/png')
    
    
    @app.route("/.well-known/ai-plugin.json")
    async def plugin_manifest():
        """
            注册用的:返回插件的描述文件,描述了插件是什么等信息。
            注意:API路由是固定的,事先约定的。
        """
        host = request.host_url
        with open(".well-known/ai-plugin.json", encoding="utf-8") as f:
            text = f.read().replace("PLUGIN_HOST", host)
            return text, 200, {"Content-Type": "application/json"}
    
    
    @app.route("/.well-known/openapi.yaml")
    async def openapi_spec():
        """
            注册用的:返回插件所依赖的插件服务的API接口描述,参照openapi规范编写。
            注意:API路由是固定的,事先约定的。
        """
        with open(".well-known/openapi.yaml", encoding="utf-8") as f:
            text = f.read()
            return text, 200, {"Content-Type": "text/yaml"}
    
    @app.route("/get_weather", methods=["POST"])
    async def get_weather():
        """
            查询天气
        """
        city = request.get_json().get('city')
        r=requests.get("第三方接口?key="+KEY+"&city="+city)
        return make_json_response(json.loads(r.text))
    
    @app.route('/')
    def index():
        return 'welcome to my webpage!'
    
    if __name__ == '__main__':
        app.run(debug=True, host='127.0.0.1', port=8081)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    第三步:上传插件

    在这里插入图片描述
    可以通过输入http:127.0.0.1:8081或者项目压缩包(rar、zip)提交。

    在这里插入图片描述

    SDK

    SDK文档:https://github.com/PaddlePaddle/ERNIE-Bot-SDK
    这个功能需要进行认证鉴权,付费服务,支持的模型平台有以下三个。
    在这里插入图片描述

    例子:
    在这里插入图片描述


    相关链接

    百度文心一言插件文档:https://yiyan.baidu.com/developer
    百度文心一言SDK:https://github.com/PaddlePaddle/ERNIE-Bot-SDK
    高德天气查询文档:https://lbs.amap.com/api/webservice/guide/api/weatherinfo/#t1
    高德开放平台:https://lbs.amap.com/
    单词本demo:https://pan.baidu.com/share/init?surl=K7wjPMPMprxtosnF5D-3tQ&pwd=q7xa

  • 相关阅读:
    Clickhouse表引擎—集成系列引擎
    什么是粘包和半包问题
    基于Echarts实现可视化数据大屏机械设备监测大数据统计平台HTML页面
    【面试:并发篇19:多线程:Park&Unpark】
    设计模式-13-职责链(责任链)模式
    Unity基于Text的快捷键修改组件
    SpringBoot 10 登录功能和登录拦截器
    mysql的存储过程
    redis(10)事务和锁机制
    VMware Workstation Pro详解
  • 原文地址:https://blog.csdn.net/weixin_46318413/article/details/132711209