Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cloudflare Workers example #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cloudflare-workers-example/.assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions cloudflare-workers-example/.gitignore
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions cloudflare-workers-example/README.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions cloudflare-workers-example/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
31 changes: 31 additions & 0 deletions cloudflare-workers-example/src/index.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
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
}
57 changes: 57 additions & 0 deletions cloudflare-workers-example/src/search.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>) {
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
}
}
}
3 changes: 3 additions & 0 deletions cloudflare-workers-example/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "cloudflare-workers-chatgpt-plugin-example"
main = "src/index.ts"
compatibility_date = "2023-04-07"