-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding netlify functions * changes for default export * json * cors * cors2 --------- Co-authored-by: Visakh Vijayan <visakh.vijayan@jalantechnologies.com>
- Loading branch information
1 parent
9c82486
commit 5a96cfa
Showing
9 changed files
with
149 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { QuoteResponse, QuoteSource } 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()), type: QuoteSource.GEMINI } as QuoteResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { QuoteResponse, QuoteSource } 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], type: QuoteSource.NINJA } as QuoteResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Context } from '@netlify/functions'; | ||
import { getQuoteFromNinja } from './ninja'; | ||
import { getQuoteFromGemini } from './gemini'; | ||
|
||
const quote = async (req: Request, context: Context) => { | ||
let data; | ||
|
||
if (req.method === 'OPTIONS') { | ||
const res = new Response(); | ||
|
||
res.headers.set("Access-Control-Allow-Origin", "*"); | ||
res.headers.append("Access-Control-Allow-Headers", "*"); | ||
res.headers.append("Access-Control-Allow-Methods", "*"); | ||
|
||
return res; | ||
} | ||
|
||
if (Math.random() < 0.5) { | ||
data = await getQuoteFromNinja(); | ||
} else { | ||
data = await getQuoteFromGemini(); | ||
} | ||
|
||
return Response.json({ data }); | ||
} | ||
|
||
export default quote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export enum QuoteSource { | ||
NINJA = 'NINJA', | ||
GEMINI = 'GEMINI' | ||
} | ||
|
||
export type QuoteResponse = { | ||
quote: string; | ||
author: string; | ||
type: QuoteSource; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { QuoteResponse } from "../types/common"; | ||
|
||
const NETLIFY_QUOTE_URL = process.env.REACT_APP_NETLIFY_QUOTE_URL as string; | ||
|
||
export const getQuoteFromNetlify = async () => { | ||
const response = await fetch(NETLIFY_QUOTE_URL); | ||
const data = await response.json(); | ||
return data.data as QuoteResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
export enum QuoteSource { | ||
NINJA = 'NINJA', | ||
GEMINI = 'GEMINI' | ||
} | ||
|
||
export type QuoteResponse = { | ||
quote: string; | ||
author: string; | ||
type: QuoteSource; | ||
} |