diff --git a/cloudflare-workers-example/.assets/example.png b/cloudflare-workers-example/.assets/example.png new file mode 100644 index 00000000..95422315 Binary files /dev/null and b/cloudflare-workers-example/.assets/example.png differ diff --git a/cloudflare-workers-example/.gitignore b/cloudflare-workers-example/.gitignore new file mode 100644 index 00000000..42387803 --- /dev/null +++ b/cloudflare-workers-example/.gitignore @@ -0,0 +1,171 @@ +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# wrangler project + +.dev.vars diff --git a/cloudflare-workers-example/README.md b/cloudflare-workers-example/README.md new file mode 100644 index 00000000..85b97581 --- /dev/null +++ b/cloudflare-workers-example/README.md @@ -0,0 +1,28 @@ +# Example ChatGPT Plugin for Cloudflare Workers + +This is an example plugin showing how to build [ChatGPT plugins](https://platform.openai.com/docs/plugins/introduction) using [Cloudflare Workers](https://workers.dev). Using this example, you can deploy a plugin to Cloudflare Workers in just a few minutes. + +The sample plugin allows ChatGPT users to search for repositories using GitHub's search API. The plugin is implemented in TypeScript and uses the [OpenAPI](https://www.openapis.org/) specification to define the plugin's API. + +![Example conversation in ChatGPT showing the plugin in use](./.assets/example.png) + +## Get started + +0. Sign up for [Cloudflare Workers](https://workers.dev). The free tier is more than enough for most use cases. +1. Install [wrangler](https://developers.cloudflare.com/workers/cli-wrangler/install-update), the Cloudflare Workers CLI +2. Clone this project and install dependencies with `npm install` +3. Run `wrangler login` to login to your Cloudflare account in wrangler +4. Run `wrangler publish` to publish the plugin to Cloudflare Workers + +## Usage + + +## Usage + +1. You can configure the `.well-known/ai-plugin.json` route in `index.ts`. +2. Update the OpenAPI schema in `openapi.ts`. +3. You can set up any new routes and the associated OpenAPI schema by defining new routes. See `search.ts` for an example. + +## Deploying to OpenAI's API + +Follow the instructions [in the ChatGPT documentation](https://platform.openai.com/docs/plugins/introduction/plugin-flow) to deploy your plugin and begin using it in ChatGPT. diff --git a/cloudflare-workers-example/package.json b/cloudflare-workers-example/package.json new file mode 100644 index 00000000..8b837e7c --- /dev/null +++ b/cloudflare-workers-example/package.json @@ -0,0 +1,16 @@ +{ + "name": "cloudflare-workers-chatgpt-plugin-example", + "version": "0.0.1", + "devDependencies": { + "@cloudflare/workers-types": "^4.20230404.0", + "wrangler": "^2.15.1" + }, + "private": true, + "scripts": { + "start": "wrangler dev", + "deploy": "wrangler publish" + }, + "dependencies": { + "@cloudflare/itty-router-openapi": "^0.1.2" + } +} diff --git a/cloudflare-workers-example/src/index.ts b/cloudflare-workers-example/src/index.ts new file mode 100644 index 00000000..54448940 --- /dev/null +++ b/cloudflare-workers-example/src/index.ts @@ -0,0 +1,31 @@ +import {OpenAPIRouter} from "@cloudflare/itty-router-openapi"; +import {GetSearch} from "./search"; + +export const router = OpenAPIRouter({ + schema: { + info: { + title: 'GitHub Repositories Search API', + description: 'A plugin that allows the user to search for GitHub repositories using ChatGPT', + version: 'v0.0.1', + }, + }, + docs_url: '/', + aiPlugin: { + name_for_human: 'GitHub Repositories Search', + name_for_model: 'github_repositories_search', + description_for_human: "GitHub Repositories Search plugin for ChatGPT.", + description_for_model: "GitHub Repositories Search plugin for ChatGPT. You can search for GitHub repositories using this plugin.", + contact_email: 'support@example.com', + legal_info_url: 'http://www.example.com/legal', + logo_url: 'https://workers.cloudflare.com/resources/logo/logo.svg', + }, +}) + +router.get('/search', GetSearch) + +// 404 for everything else +router.all('*', () => new Response('Not Found.', {status: 404})) + +export default { + fetch: router.handle +} diff --git a/cloudflare-workers-example/src/search.ts b/cloudflare-workers-example/src/search.ts new file mode 100644 index 00000000..bb211224 --- /dev/null +++ b/cloudflare-workers-example/src/search.ts @@ -0,0 +1,57 @@ +import {ApiException, OpenAPIRoute, Query, ValidationError} from "@cloudflare/itty-router-openapi"; + +export class GetSearch extends OpenAPIRoute { + static schema = { + tags: ['Search'], + summary: 'Search repositories by a query parameter', + parameters: { + q: Query(String, { + description: 'The query to search for', + default: 'cloudflare workers' + }), + }, + responses: { + '200': { + schema: { + repos: [ + { + name: 'itty-router-openapi', + description: 'OpenAPI 3 schema generator and validator for Cloudflare Workers', + stars: '80', + url: 'https://github.com/cloudflare/itty-router-openapi', + } + ] + }, + }, + }, + } + + async handle(request: Request, env, ctx, data: Record) { + const url = `https://api.github.com/search/repositories?q=${data.q}` + + const resp = await fetch(url, { + headers: { + 'Accept': 'application/vnd.github.v3+json', + 'User-Agent': 'RepoAI - Cloudflare Workers ChatGPT Plugin Example' + } + }) + + if (!resp.ok) { + return new Response(await resp.text(), {status: 400}) + } + + const json = await resp.json() + + // @ts-ignore + const repos = json.items.map((item: any) => ({ + name: item.name, + description: item.description, + stars: item.stargazers_count, + url: item.html_url + })) + + return { + repos: repos + } + } +} diff --git a/cloudflare-workers-example/wrangler.toml b/cloudflare-workers-example/wrangler.toml new file mode 100644 index 00000000..d65c181a --- /dev/null +++ b/cloudflare-workers-example/wrangler.toml @@ -0,0 +1,3 @@ +name = "cloudflare-workers-chatgpt-plugin-example" +main = "src/index.ts" +compatibility_date = "2023-04-07"