ChatGPT Plugin开发setup - Java(Spring Boot) Python(fastapi)

这篇具有很好参考价值的文章主要介绍了ChatGPT Plugin开发setup - Java(Spring Boot) Python(fastapi)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

记录一下快速模板,整体很简单,如果不接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/文章来源地址https://www.toymoban.com/news/detail-445349.html

到了这里,关于ChatGPT Plugin开发setup - Java(Spring Boot) Python(fastapi)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解决方法

    出现这个报错通常是因为 Maven 无法找到相应的 Spring Boot Maven 插件。要解决这个问题,可以尝试以下几种方法: 检查  pom.xml  文件中是否正确配置了 Spring Boot Maven 插件。确保以下内容存在,并且版本号是有效的: 确保  groupId 、 artifactId  和  version  的值与你使用的 Spring

    2024年03月27日
    浏览(46)
  • 解决Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的问题

    在一个风和日丽的下午,我跟着B站一个Up主敲一个SpringBoot+VUE项目,敲着代码听着歌,在使用Maven下载相关包时,突然就报错啦!(自己的已经解决了,当时没截图,只能用一下别人的图了): 代码如下: 那问题发生了能怎么办呢?百度呗!毕竟面向百度编程时一个好习惯!

    2024年02月16日
    浏览(35)
  • Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解决办法

     pom.xml文件中出现这样的依赖报错 解决办法如下: 1、找到这个 spring-boot-starter-parent 依赖 2、将其版本复制到 spring-boot-maven-plugin 下面      

    2024年02月15日
    浏览(32)
  • 已解决org.springframework.boot:spring-boot-maven-plugin:

    已解决org.springframework.boot:spring-boot-maven-plugin: org.springframework.boot:spring-boot-maven-plugin: org.springframework.boot:spring-boot-maven-plugin 是 Spring Boot 提供的一个 Maven 插件,它用于将 Spring Boot 应用打包成可执行的 JAR 文件或者 WAR 文件。 下滑查看解决方法 该插件提供了许多功能,如将依赖

    2024年02月08日
    浏览(34)
  • Java Spring Boot 开发框架

    Spring Boot是一种基于Java编程语言的开发框架,它的目标是简化Java应用程序的开发过程。Spring Boot提供了一种快速、易于使用的方式来创建独立的、生产级别的Java应用程序。本文将介绍Spring Boot的特性、优势以及如何使用它来开发高效、可靠的应用程序。 Spring Boot是由Pivotal团队

    2024年02月08日
    浏览(40)
  • 【bug日记】报错“Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found”(通过google终于解决)

    Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found 看了网上十几篇的回答,大部分都是通过File-Invalidate Caches和增加version版本号解决的, 而我尝试了以上两种方法都无法解决!!! 最后通过google搜索相关解决方案,在评论中看到了一个解决方法 按照该方法后,成功解决

    2024年01月24日
    浏览(36)
  • 找不到插件 ‘org.springframework.boot:spring-boot-maven-plugin:‘问题

    出现pom文件找不到插件 ‘org.springframework.boot:spring-boot-maven-plugin:‘问题, 可能是因为版本没有绑定好,去一级父类依赖找对应的插件版本,在pom文件中加上。 把父类的version加到pom中   出现这个说明已经绑定好了,删除版本也可以    

    2024年02月07日
    浏览(34)
  • [springboot] spring-boot-maven-plugin指定版本问题

    1. pom.xml文件报如下错误: (需要指定版本) Plugin \\\'org.springframework.boot:spring-boot-maven-plugin:\\\' not found 解决方法: 打开Maven本地仓库。查看/org/springframework/boot路径下的spring-boot-maven-plugin文件夹底下,是否存在与springboot的版本相对应版本号,如果存在,添加version标签为自己的sp

    2024年02月13日
    浏览(49)
  • spring-boot-maven-plugin报红的解决办法

    目录 一、遇到问题 二、出现这个问题的原因 三、解决办法   在springboot创建的时候,会遇到在pom.xml文件里面的spring-boot-maven-plugin这个依赖爆红没有导进去  因为在maven的阿里云仓库里面没有找到这个依赖 1. 在pom.xml文件找到父工程的版本号,然后复制过去让报红的那个依赖去

    2024年02月16日
    浏览(23)
  • bug1-找不到插件 ‘org.springframework.boot:spring-boot-maven-plugin:‘

    项目无缘无故出现此问题,在该位置加上版本号后解决。 步骤1:点击groudid进入spring-boot-maven-plugin-2.4.1.pom 步骤2:在spring-boot-maven-plugin-2.4.1.pom文件中找到报错的spring-boot-maven-plugin 步骤3:可以找到对应的版本号,将此版本号,添加到报错的位置。 步骤4:刷新maven,解决此问题。

    2024年02月16日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包