├── wrangler.toml ├── package.json ├── src └── index.js ├── LICENSE ├── README.md └── .gitignore /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "google-generativelanguage-proxy" 2 | main = "src/index.js" 3 | compatibility_date = "2023-12-22" 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "google-generativelanguage-proxy", 3 | "version": "0.0.0", 4 | "devDependencies": { 5 | "jest": "^29.7.0", 6 | "wrangler": "3.80.4" 7 | }, 8 | "private": true, 9 | "scripts": { 10 | "start": "wrangler dev", 11 | "deploy": "wrangler deploy" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | addEventListener('fetch', event => { 2 | event.respondWith(handleRequest(event.request)) 3 | }) 4 | 5 | async function handleRequest(request) { 6 | const originalUrl = new URL(request.url) 7 | const targetURL = new URL(originalUrl.pathname + originalUrl.search, 'https://generativelanguage.googleapis.com') 8 | 9 | let init = { 10 | method: request.method, 11 | headers: request.headers, 12 | body: request.body, 13 | redirect: 'manual' 14 | } 15 | 16 | // Forward the request to the target URL and return the response directly. 17 | return fetch(targetURL, init) 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 ShinChven 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Generative Language API CloudFlare Proxy 2 | 3 | ## Introduction 4 | 5 | This repository provides a CloudFlare Worker setup to proxy the Google Generative Language API. It's especially useful for users who encounter difficulties accessing the API directly. 6 | 7 | ## Prerequisites 8 | 9 | - A CloudFlare account 10 | - Basic familiarity with terminal/command-line operations 11 | 12 | ## Setup Instructions 13 | 14 | 1. **Create a CloudFlare Account:** Sign up for an account at [CloudFlare's website](https://www.cloudflare.com/). 15 | 16 | 2. **Clone the Repository:** Download or clone this repository to your local machine. 17 | 18 | ``` 19 | git clone [repository-link] 20 | ``` 21 | 22 | 3. **Install CloudFlare CLI:** Open your terminal and run the following command to install the CloudFlare Wrangler CLI. 23 | 24 | ``` 25 | npm i -g wrangler 26 | ``` 27 | 28 | 4. **Configure `wrangler.toml`:** Navigate to the cloned repository's directory and update the `wrangler.toml` file with your desired worker name. 29 | 30 | ``` 31 | cd [repository-name] 32 | vim wrangler.toml 33 | ``` 34 | 35 | 5. **Deploy the Worker:** Deploy your CloudFlare worker using the following command. 36 | 37 | ``` 38 | npm run deploy 39 | ``` 40 | 41 | This command will automatically compile and upload your worker to CloudFlare. 42 | 43 | 6. **Set a Domain Trigger:** Go to your CloudFlare dashboard and set a domain in the `Triggers` settings of your worker. This step is crucial for the worker to respond to API requests. 44 | 45 | ## Usage 46 | 47 | ```bash 48 | #!/bin/bash 49 | 50 | API_KEY="you api key" 51 | 52 | PROXY_URL="https://proxy.yourdomain.com" 53 | 54 | curl \ 55 | -X POST ${PROXY_URL}'/v1beta/models/gemini-pro:generateContent?key='${API_KEY} \ 56 | -H 'Content-Type: application/json' \ 57 | -d @<(echo '{ 58 | "contents": [ 59 | { 60 | "parts": [ 61 | { text: "What is Cyberpunk 2077?" } 62 | ] 63 | } 64 | ], 65 | "generationConfig": { 66 | "temperature": 0.9, 67 | "topK": 1, 68 | "topP": 1, 69 | "maxOutputTokens": 2048, 70 | "stopSequences": [] 71 | }, 72 | "safetySettings": [ 73 | { 74 | "category": "HARM_CATEGORY_HARASSMENT", 75 | "threshold": "BLOCK_MEDIUM_AND_ABOVE" 76 | }, 77 | { 78 | "category": "HARM_CATEGORY_HATE_SPEECH", 79 | "threshold": "BLOCK_MEDIUM_AND_ABOVE" 80 | }, 81 | { 82 | "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", 83 | "threshold": "BLOCK_MEDIUM_AND_ABOVE" 84 | }, 85 | { 86 | "category": "HARM_CATEGORY_DANGEROUS_CONTENT", 87 | "threshold": "BLOCK_MEDIUM_AND_ABOVE" 88 | } 89 | ] 90 | }') 91 | ``` 92 | 93 | ## Contributing 94 | 95 | Your contributions to improve this proxy are welcome. Please feel free to fork the repository, make your changes, and submit a pull request. 96 | 97 | ## License 98 | 99 | [LICENSE](./LICENSE) -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | 3 | logs 4 | _.log 5 | npm-debug.log_ 6 | yarn-debug.log* 7 | yarn-error.log* 8 | lerna-debug.log* 9 | .pnpm-debug.log* 10 | 11 | # Diagnostic reports (https://nodejs.org/api/report.html) 12 | 13 | report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json 14 | 15 | # Runtime data 16 | 17 | pids 18 | _.pid 19 | _.seed 20 | \*.pid.lock 21 | 22 | # Directory for instrumented libs generated by jscoverage/JSCover 23 | 24 | lib-cov 25 | 26 | # Coverage directory used by tools like istanbul 27 | 28 | coverage 29 | \*.lcov 30 | 31 | # nyc test coverage 32 | 33 | .nyc_output 34 | 35 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 36 | 37 | .grunt 38 | 39 | # Bower dependency directory (https://bower.io/) 40 | 41 | bower_components 42 | 43 | # node-waf configuration 44 | 45 | .lock-wscript 46 | 47 | # Compiled binary addons (https://nodejs.org/api/addons.html) 48 | 49 | build/Release 50 | 51 | # Dependency directories 52 | 53 | node_modules/ 54 | jspm_packages/ 55 | 56 | # Snowpack dependency directory (https://snowpack.dev/) 57 | 58 | web_modules/ 59 | 60 | # TypeScript cache 61 | 62 | \*.tsbuildinfo 63 | 64 | # Optional npm cache directory 65 | 66 | .npm 67 | package-lock.json 68 | 69 | # Optional eslint cache 70 | 71 | .eslintcache 72 | 73 | # Optional stylelint cache 74 | 75 | .stylelintcache 76 | 77 | # Microbundle cache 78 | 79 | .rpt2_cache/ 80 | .rts2_cache_cjs/ 81 | .rts2_cache_es/ 82 | .rts2_cache_umd/ 83 | 84 | # Optional REPL history 85 | 86 | .node_repl_history 87 | 88 | # Output of 'npm pack' 89 | 90 | \*.tgz 91 | 92 | # Yarn Integrity file 93 | 94 | .yarn-integrity 95 | 96 | # dotenv environment variable files 97 | 98 | .env 99 | .env.development.local 100 | .env.test.local 101 | .env.production.local 102 | .env.local 103 | 104 | # parcel-bundler cache (https://parceljs.org/) 105 | 106 | .cache 107 | .parcel-cache 108 | 109 | # Next.js build output 110 | 111 | .next 112 | out 113 | 114 | # Nuxt.js build / generate output 115 | 116 | .nuxt 117 | dist 118 | 119 | # Gatsby files 120 | 121 | .cache/ 122 | 123 | # Comment in the public line in if your project uses Gatsby and not Next.js 124 | 125 | # https://nextjs.org/blog/next-9-1#public-directory-support 126 | 127 | # public 128 | 129 | # vuepress build output 130 | 131 | .vuepress/dist 132 | 133 | # vuepress v2.x temp and cache directory 134 | 135 | .temp 136 | .cache 137 | 138 | # Docusaurus cache and generated files 139 | 140 | .docusaurus 141 | 142 | # Serverless directories 143 | 144 | .serverless/ 145 | 146 | # FuseBox cache 147 | 148 | .fusebox/ 149 | 150 | # DynamoDB Local files 151 | 152 | .dynamodb/ 153 | 154 | # TernJS port file 155 | 156 | .tern-port 157 | 158 | # Stores VSCode versions used for testing VSCode extensions 159 | 160 | .vscode-test 161 | 162 | # yarn v2 163 | 164 | .yarn/cache 165 | .yarn/unplugged 166 | .yarn/build-state.yml 167 | .yarn/install-state.gz 168 | .pnp.\* 169 | 170 | # wrangler project 171 | 172 | .dev.vars 173 | .wrangler/ 174 | --------------------------------------------------------------------------------