-
Notifications
You must be signed in to change notification settings - Fork 10
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
Move gemini rating logic to server #14
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
VITE_GEMINI_GENAI_KEY= | ||
VITE_GEMINI_RATING_ENDPOINT= | ||
VITE_GEMMA_MODEL_PATH= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { HarmBlockThreshold, HarmCategory } from '@google/generative-ai'; | ||
|
||
export const SAFETY_SETTINGS_GEMINI = [ | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HARASSMENT, | ||
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, | ||
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, | ||
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH, | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEMINI_GENAI_KEY= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# This file specifies files that are *not* uploaded to Google Cloud | ||
# using gcloud. It follows the same syntax as .gitignore, with the addition of | ||
# "#!include" directives (which insert the entries of the given .gitignore-style | ||
# file at that point). | ||
# | ||
# For more information, run: | ||
# $ gcloud topic gcloudignore | ||
# | ||
.gcloudignore | ||
# If you would like to upload your .git directory, .gitignore file or files | ||
# from your .gitignore file, remove the corresponding line | ||
# below: | ||
.git | ||
.gitignore | ||
|
||
node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# A Cloud function providing Gemini-powered ratings based on user reviews | ||
|
||
## Develop | ||
|
||
``` | ||
npm run dev | ||
``` | ||
|
||
## Deploy | ||
|
||
Make sure you're logged in and that you use the right Cloud project: | ||
|
||
``` | ||
gcloud auth login | ||
gcloud config list | ||
``` | ||
|
||
Deploy your function: | ||
|
||
``` | ||
gcloud functions deploy product-reviews-gemini-get-rating --runtime nodejs20 --trigger-http | ||
``` | ||
|
||
## Use in the product-reviews frontend app | ||
|
||
Set VITE_GEMINI_RATING_ENDPOINT in ../.env (at the root directory) to the function's URL. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import functions from '@google-cloud/functions-framework'; | ||
import { GoogleGenerativeAI } from '@google/generative-ai'; | ||
import { SAFETY_SETTINGS_GEMINI } from './.config-ai.js'; | ||
|
||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
functions.http('product-reviews-gemini-get-rating', async (req, res) => { | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
if (req.method === 'OPTIONS') { | ||
console.log('is OPTIONS') | ||
// Send response to OPTIONS requests | ||
|
||
res.set('Access-Control-Allow-Methods', 'GET'); | ||
res.set('Access-Control-Allow-Headers', 'Content-Type'); | ||
res.set('Access-Control-Max-Age', '3600'); | ||
return res.status(204).send(''); | ||
} | ||
|
||
if (!req.body.review) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drive by comment: would it make sense to implement a few best practices here around making sure that calls only come from your own website? (e.g. checking referrer...). I know that this can easily be spoofed, but might be a good indicator for people looking at this that they should think about how to secure their API endpoints. This might also be complete overkill here... |
||
return res.sendStatus(400); | ||
} | ||
const review = req.body.review; | ||
let model; | ||
let rating; | ||
try { | ||
model = setupGemini(); | ||
} catch (e) { | ||
console.error('Error loading Gemini', e); | ||
return res.sendStatus(500); | ||
} | ||
try { | ||
rating = await getRating(model, review); | ||
} catch (e) { | ||
console.error('Error querying Gemini API (server)', e); | ||
return res.sendStatus(500); | ||
} | ||
|
||
return res.send(rating.toString()); | ||
}); | ||
|
||
function setupGemini() { | ||
const geminiGenAi = new GoogleGenerativeAI( | ||
process.env.GEMINI_GENAI_KEY | ||
); | ||
// https://ai.google.dev/tutorials/web_quickstart?_gl=1*mnb5md*_up*MQ..*_ga*MTI3OTI5NDU0MC4xNzA5NzMyOTI0*_ga_P1DBVKWT6V*MTcwOTczMjkyMy4xLjAuMTcwOTczMzAwOS4wLjAuMA..#use-safety-settings | ||
const model = geminiGenAi.getGenerativeModel({ | ||
model: 'gemini-1.0-pro-latest', | ||
safetySettings: SAFETY_SETTINGS_GEMINI, | ||
}); | ||
return model; | ||
} | ||
|
||
async function getRating(model, review) { | ||
const result = await model.generateContent( | ||
`Here is a product review: "${review}". Based on this review, assess how many stars (between 1 and 5) the review corresponds to. Return an integer. Don't give me a function, just give me a number between 1 and 5.` | ||
); | ||
const rating = parseInt(await result.response.text()); | ||
return rating; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.