ChatGPT Plugin开发setup - Java(Spring Boot) Python(fastapi)_全球热点评

记录一下快速模板,整体很简单,如果不接auth,只需要以下:

提供一个/.well-known/ai-plugin.json接口,返回openAI所需要的格式提供openAPI规范的文档CORS设置

其他的和普通的web开发类似.

本地开发就直接使用localhost即可,前几天官方localhost无法联通,最近应该修复了.


(资料图片仅供参考)

要让GPT更好理解接口内容,接口需要写详细的文档,在文档内写清楚各个参数作用和可选值以及示例.

Spring Boot增加对文档的依赖
    org.springdoc    springdoc-openapi-starter-webmvc-ui    2.1.0

增加一个bean配置:

@Beanpublic OpenAPI openAPI() {    return new OpenAPI()            .info(new Info().title("html fetcher")                            .description("get content from url")                            .version("1.0"));}

文档的地址为/v3/api-docs

增加ai-plugin.json接口
@GetMapping(value = "/.well-known/ai-plugin.json", produces = "application/json")public String aiPlugin() {    return """            {                    "schema_version": "v1",                    "name_for_human": "html fetcher Plugin",                    "name_for_model": "html_fetcher",                    "description_for_human": "Plugin for getting content from url",                    "description_for_model": "Plugin for getting content from url",                    "auth": {                        "type": "none"                    },                    "api": {                        "type": "openapi",                        "url": "http://localhost:8080/v3/api-docs",                        "is_user_authenticated": false                    },                    "logo_url": "http://localhost:8080/logo.png",                    "contact_email": "support@example.com",                    "legal_info_url": "http://www.example.com/legal"                }    """;}

logo直接放到\resources\static中内容根据自己插件修改,本地开发直接写localhost,部署写对应的网站地址.

CORS设置

测试的时候允许可以写*, 后续上线更改为openai.com:

@Beanpublic CorsFilter corsFilter() {    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();    CorsConfiguration config = new CorsConfiguration();    config.addAllowedOrigin("*");    config.addAllowedHeader("*");    config.addAllowedMethod("*");    source.registerCorsConfiguration("/**", config);    return new CorsFilter(source);}

要写自定义文档可以用spring-doc的相关注解.比如写在接口上用@Operation, 字段上使用@Schema注解.@Schema内可以用allowableValuesexample等来约束openai的查询.

fastapi

fastapi自带openAPI集成,只需要把json dump成yaml即可, setup比较简单, 这里直接全部放一起了:

app = FastAPI()app.add_middleware(    CORSMiddleware,    allow_origins=["*"],    allow_credentials=True,    allow_methods=["*"],    allow_headers=["*"],)# static 文件夹用来做静态资源host 放logoapp.mount("/static", StaticFiles(directory="static"), name="static")# json -> yaml@app.get("/openapi.yaml")async def get_openapi():    return Response(content=yaml.dump(app.openapi()), media_type="application/yaml")@app.get("/.well-known/ai-plugin.json")async def openai_api_plugin():    return {        "schema_version": "v1",        "name_for_human": "",        "name_for_model": "",        "description_for_human": "",        "description_for_model": "",        "auth": {            "type": "none"        },        "api": {            "type": "openapi",            "url": "http://localhost:8000/openapi.yaml",            "is_user_authenticated": False        },        "logo_url": "http://localhost:8000/static/logo.png",        "contact_email": "support@example.com",        "legal_info_url": "http://www.example.com/legal"    }

自定义文档内容对于接口的可以用summary,response_description参数, query参数可以用Annotationd, 一个例子:

@app.get("/api/query_profit_data", summary="query profit data by company code, year and quarter", response_description="""return profit data in format {"key":{"0":"value"}}, panda"s dataframe""")async def query_profit_data(code: Annotated[str, Query(description="the company code", example="sh.600000")],                            year: Annotated[int, Query(description="year to get profit", example=2023)],                            quarter: Annotated[                                int, Query(description="quarter to get profit. allow values:1,2,3,4", example=1)]):
参考资料

chatgpt plugin: https://openai.com/blog/chatgpt-plugins

spring doc: https://springdoc.org/v2/

fastapi: https://fastapi.tiangolo.com/

关键词:

    为你推荐

    不取消亚洲行了?白宫:拜登将按计划出席G7广岛峰会

    中新网5月15日电综合外媒报道,白宫当地时间14日表示,美国总统拜登将于5月18日至21日赴日出席在广岛举行的

    来源:中国青年网 23-05-15

    虚拟电厂热捧背后的冷思考|天天观焦点

    虚拟电厂的目标是通过金融手段和电力现货配合,做好电力经济的保供减碳和风险管理。目前,国内虚拟电厂主要

    来源:华夏能源网 23-05-15

    焦点消息!刘光裕:商代有篇籍

    据《史记·三代世表》,从商汤至殷纣王,凡二十九王[1]。《竹书纪年》记商代二十九王,四百九十六年;[2]商

    来源:古籍 23-05-15

    每日讯息!新城市(300778)盘中异动 股价振幅达7.94% 上涨6.84%(05-15)

    摘要:2023年05月15日新城市(300778)股价大幅拉升6 84%,该股报24 35元 股,振幅7 94%。2023年05月15日

    来源:自选股智能写手 23-05-15

    全球要闻:三河李某,在香河被抓!

    乐在北三县(ID:lzbsx88)“靖安2023”专项行动以来,香河县公安局聚焦难点,突出重点,集中力量深入打击

    来源:乐在北三县 23-05-15
    返回顶部