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

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

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

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

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

Spring Boot

增加对文档的依赖

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>

增加一个bean配置:

@Bean
public 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:

@Bean
public 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 放logo
app.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/

ChatGPT Plugin开发setup - Java(Spring Boot) Python(fastapi)的更多相关文章

  1. Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. Java Spring boot 多商户入驻 外卖|跑腿|代驾 Uniapp版本

    技术说明: 源码下载:https://www.yuanmahy.com/8357.html 开发环境:jdk1.8,mysql5.7,node 9.4,redis6.2,npm6.9 开发工具:前端使 ...

  3. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  4. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  5. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  6. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  7. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. Java Spring Boot VS .NetCore (七) 配置文件

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  10. Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. 谈恋爱要做什么事?基于auto.js自动发早安给女朋友

    谈恋爱要做什么事?除了用心之外,每天早安晚安必然是少不了的.但是每天都发免不了会忘, 为了避免遗忘,引起不必要的尴尬,我们可以做个自动化脚本来做这件事. 1 auto.js 是什么? Auto.JS是 ...

  2. PHP文件及运行(适合PHP初学者)

    PHP文件可包含HTML.JavaScript代码和 PHP代码,换句话说PHP 代码可以嵌入HTML文档.PHP文件名以php为后缀. PHP代码以"<?php"开头,以& ...

  3. Blazor项目在VisualStudio调试时配置运行基础目录

    最近在使用 Blazor 开发管理后台时遇到了如下的问题,我这里后台整体采用了 AntDesignBlazor 组件库,在上线之后发现ReuseTabs组件在使用过程中,如果默认 / 没有指定为项目的 ...

  4. 个人数据保全计划:(2) NAS基础知识

    前言 距离去年国庆入手了NAS至今有好几个月时间了,NAS折腾起来有点麻烦,且实际作用因人而异,并没有想象中的好用,所以说好的这个系列一直没有更新~ 还有另一方面的原因,这些NAS的系统基于Linux ...

  5. MybatisX无法自动生成entity实体类

    在做项目的时候,安装MybatisX插件可以让我们不用写实体类,加快我们的开发速度,让我们更专注于业务逻辑的开发,可是最近在做项目的时候,发现MybatisX插件的MybatisX-Generator ...

  6. 谈谈Selenium中的日志

    谈谈Selenium中的日志 来源于一位同学,"老师为啥firefox执行后会有日志文件,chrome没有呢?" 比对 你打开chrome浏览器 from selenium imp ...

  7. 使用Mathematica做序列的DTFT的几个例子

    ListFourierSequenceTransform[{-2, -1, 1, 3, 3, 1, -1, -2}, \[Omega]] ParametricPlot[{Re[%], Im[%]}, ...

  8. Cesium 后处理(Post Process)

    原文地址:https://blog.csdn.net/ls870061011/article/details/123910821 作者:GIS李胜 为实现三维模型的更炫.更酷.更美观,Cesium在1 ...

  9. vue双向监听proxy

    console.log('判断页面是否有滚动条', this.hasScrollbar) const that = this that.count = 0 // 计数 that.scrollProxy ...

  10. 为什么说 ICMP 协议是网络最强辅助

    大家好,我是风筝 轻解网络系列又来了.已有高清 PDF 版本可以离线阅读了,全册 65 页,如果有需要离线版的高清 PDF 可以直接下载. 今天咱们说说 ICMP 协议.ICMP 可谓是网络世界中的最 ...