记录一下快速模板,整体很简单,如果不接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. ElementPlus 表单 resetFields 无效问题解决方法

    最近在写一个项目,一个表单递交或者使用resetFields关闭后,再打开,原来的值还存在,后查了一下网上的方法,确定是el-form-item,必须要加prop,其值要与model相同,此问题得到完 ...

  2. 打不过AI就拉拢?ChatGPT和MidJourney已成我小秘书!

    为了体验AI,晓衡这两周战斗力爆棚了! 每天大概睡了四~五个小时,而且中午也没有休息过,但精神却还很亢奋. 直到周一下午,身体才有种被掏空的感觉,晚上 10 点就睡了.可能是兴奋劲还在,早晨不到 6 ...

  3. 传输安全HTTPS

    为什么要有 HTTPS 为什么要有 HTTPS?简单的回答是:"因为 HTTP 不安全".HTTP 怎么不安全呢? 通信的消息会被窃取,无法保证机密性(保密性):由于 HTTP 是 ...

  4. Android笔记--视图显示

    视图显示 视图的宽高设置 方式一:在.xml文件中设置视图的宽和高 通过调用android:layout_width设置视图的宽 通过调用android:layout_height设置视图的高 宽和高 ...

  5. Activiti7开发(四)-我的待办

    目录 1. 查询登录用户的待办任务 2.审批 1. 查询登录用户的待办任务 private List<Task> queryMyTasks(){ String username = Sec ...

  6. 【读书笔记】格子路径计数LatticePathEnumeration 学一半的笔记

    流水账流水账这篇什么都不是 目录 方法 10.2 Lattice paths without restrictions 无限制格子路径 2维的例子,从(a,b)到(c,d),允许(0,1)和(1,0) ...

  7. CentOS8删除boot目录恢复

    系统安装完之后,boot分区最好做一个备份,因为这个分区 我们基本不会动它,所以备份一次一劳永逸,以防万一.如果我们不小心 误删除了这个目录,也不用慌,正因为这个分区,我们除了开机 其他时候基本用不到 ...

  8. 升级二进制kubernetes集群

    升级二进制kubernetes集群 背景介绍 最近由于时间不足,暂时无法对小版本更新第一时间出新的文档.若需要升级集群版本,可以参考此文档进行操作,每个节点一个一个地更新.大版本更新请各位持续关注我的 ...

  9. Python property、setter、deleter

    面向对象封装特点之一就是通过实现好的方法来访问,限制对数据的不合理访问,把对象状态私有化,仅供类的内部进行操作 下方示例,Test方法的number属性类实例的时候传递1,number是一个公开属性, ...

  10. Semantic Kernel 入门系列:🍋Connector连接器

    当我们使用Native Function的时候,除了处理一些基本的逻辑操作之外,更多的还是需要进行外部数据源和服务的对接,要么是获取相关的数据,要么是保存输出结果.这一过程在Semantic Kern ...