-
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.
- Loading branch information
1 parent
9c82486
commit 314b630
Showing
6 changed files
with
112 additions
and
0 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 } 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; | ||
} |
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 } 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; | ||
} |
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,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); | ||
} |
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,4 @@ | ||
export type QuoteResponse = { | ||
quote: string; | ||
author: string; | ||
} |
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