Skip to content

Commit

Permalink
Merge pull request #1 from rahulsuresh-git/feature/v1
Browse files Browse the repository at this point in the history
v1
  • Loading branch information
rahulsuresh-git authored Sep 6, 2024
2 parents 855d295 + 62f165c commit 13bcff7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: Deploy

on:
push:
branches:
- main
pull_request:
repository_dispatch:

jobs:
Expand All @@ -16,3 +15,4 @@ jobs:
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
107 changes: 106 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,106 @@
[TODO]
![Logo](/static/cover.png)

# Cloudflare Worker + Google Gemini Starter Kit

Harness Google Gemini AI's power with a no-cost scalable backend API, and all the benefits of Cloudflare's AI Gateway! Comes with instant setup, DDOS protection, built-in rate limiting, and efficient caching—all in just one click.

This starter kit helps you deploy a backend endpoint for Google Gemini AI through Cloudflare's serverless infrastructure.

## Features

✅ Serverless (No downtime!) \
✅ Free-of-cost (No Credit Card!) \
✅ DDOS Protection \
✅ Rate Limiting \
✅ Caching \
✅ Secure

## Why do we need this?

The rise of Generative AI tools has created a demand for quick and easy access to Large Language Models (LLMs) for developers and students building applications. However, existing solutions often present several challenges. These include credit card requirements for API sign-ups, significant effort needed to host a backend proxy endpoint, backends (like Node.js, Python) requiring additional hosting solutions, and the need for manual implementation of rate-limiting and security mechanisms to prevent abuse.

This starter kit addresses these common problems by leveraging Cloudflare Workers. It is a cloud-based backend server that offers auto-scaling capabilities, and is backed by Cloudflare's leading security features. Best of all, it's completely free to use.

## Deployment

- Create a Cloudflare account. If you don't already have one, sign up for a Cloudflare account at https://www.cloudflare.com

- Get a Google Gemini API key

- Visit the Google AI Studio at https://aistudio.google.com/app/apikey
- Accept the consent form
- Click "Create API key"
- Select "Create API key in new project"
- Copy and securely store the generated API key for later use.

- Deploy to Cloudflare by clicking the button below.

[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/rahulsuresh-git/cloudflare-worker-gemini-starter-kit)

- After successfully deploying your worker, update its environment variables:

- Navigate to the Cloudflare Dashboard: dash.cloudflare.com
- Go to Workers & Pages
- Select the `cloudflare-worker-gemini-starter-kit` worker
- Click on Settings > Variables

- Update the following variables:

```
GOOGLE_GEMINI_API_KEY: Enter your Google Gemini API key
CLOUDFLARE_ACCOUNT_ID: Find this on the "Workers & Pages" page (right side)
CLOUDFLARE_AI_GATEWAY_NAME:
- Go to AI > AI Gateway in the Cloudflare Dashboard
- Create a new AI Gateway with any name you choose
- Update this variable with the chosen name
```

- Deploy your worker again to apply the changes.

- Test your API by hitting `curl 'http://<your-worker-url>/?prompt=What%20is%20life?' `

## Run Locally

Clone the project

```bash
git clone https://github.com/rahulsuresh-git/cloudflare-worker-gemini-starter-kit
```

Go to the project directory

```bash
cd cloudflare-worker-gemini-starter-kit
```

Install dependencies

```bash
npm install
```

Start the server

```bash
npm run dev
```

<!-- ## FAQ
#### Question 1
Answer 1
#### Question 2
Answer 2 -->

## Authors

- [@rahulsuresh-git](https://www.github.com/rahulsuresh-git)

## License

[MIT](https://choosealicense.com/licenses/mit/)
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { GoogleGenerativeAI } from '@google/generative-ai';

export default {
async fetch(request, env, ctx): Promise<Response> {
const { searchParams } = new URL(request.url);
const prompt = searchParams.get('prompt');

if (!prompt) {
return Response.json(
{ success: false, error: 'Prompt missing! Add a prompt to the URL using ?prompt=', response: null },
{ status: 400 }
);
}

const genAI = new GoogleGenerativeAI(env.GOOGLE_GEMINI_API_KEY);

try {
Expand All @@ -11,7 +21,6 @@ export default {
baseUrl: `https://gateway.ai.cloudflare.com/v1/${env.CLOUDFLARE_ACCOUNT_ID}/${env.CLOUDFLARE_AI_GATEWAY_NAME}/google-ai-studio`,
}
);
const prompt = 'Who are you?';
const result = await model.generateContent(prompt);
return Response.json({ success: true, response: result.response.text() }, { status: 200 });
} catch (error) {
Expand Down
Binary file added static/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 13bcff7

Please sign in to comment.