Skip to content

Commit

Permalink
adding netlify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vjnvisakh-jtc committed Mar 18, 2024
1 parent 9c82486 commit 314b630
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
49 changes: 49 additions & 0 deletions netlify/functions/gemini.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { QuoteResponse } from './types';
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require("@google/generative-ai");

const MODEL_NAME = 'gemini-1.0-pro';
const API_KEY = process.env.REACT_APP_GEMINI_API_KEY;

export const getQuoteFromGemini = async (): Promise<QuoteResponse> => {
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });

const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
};

const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];

const parts = [
{text: "Give me a motivational quote in the form of json\n {\"quote\":\"<quote>\",\"author\":\"<author>\"}"},
];

const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});

const response = result.response;
return JSON.parse(response.text()) as QuoteResponse;
}
16 changes: 16 additions & 0 deletions netlify/functions/ninja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { QuoteResponse } from './types';

const NINJA_API_KEY = process.env.REACT_APP_NINJA_API_KEY as string;
const NINJA_URL = process.env.REACT_APP_NINJA_URL as string;

export const getQuoteFromNinja = async () => {
const response = await fetch(NINJA_URL,
{
method: 'GET',
headers: { 'X-Api-Key': NINJA_API_KEY }
}
);

const data = await response.json();
return data[0] as QuoteResponse;
}
15 changes: 15 additions & 0 deletions netlify/functions/quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Context } from '@netlify/functions';
import { getQuoteFromNinja } from './ninja';
import { getQuoteFromGemini } from './gemini';

export const quote = async (req: Request, context: Context) => {
let data;

if (Math.random() < 0.5) {
data = await getQuoteFromNinja();
} else {
data = await getQuoteFromGemini();
}

return new Response(data);
}
4 changes: 4 additions & 0 deletions netlify/functions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type QuoteResponse = {
quote: string;
author: string;
}
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@google/generative-ai": "^0.2.1",
"@netlify/functions": "^2.6.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down

0 comments on commit 314b630

Please sign in to comment.