-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Alemayhu <alexander@alemayhu.com>
- Loading branch information
Showing
5 changed files
with
77 additions
and
93 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 |
---|---|---|
@@ -1,20 +1,35 @@ | ||
import { HarmBlockThreshold, HarmCategory } from '@google-cloud/vertexai'; | ||
import { | ||
HarmBlockThreshold, | ||
HarmCategory, | ||
SafetySetting, | ||
} from '@google-cloud/vertexai'; | ||
|
||
export const SAFETY_SETTINGS = [ | ||
export const SAFETY_SETTINGS: SafetySetting[] = [ | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, | ||
category: HarmCategory.HARM_CATEGORY_HARASSMENT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HARASSMENT, | ||
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
]; | ||
|
||
export const VERTEX_AI_CONFIG = { | ||
project: 'notion-to-anki', | ||
location: 'europe-west3', | ||
model: 'gemini-1.5-pro-002', | ||
generationConfig: { | ||
maxOutputTokens: 8192, | ||
temperature: 1, | ||
topP: 0.95, | ||
}, | ||
}; |
26 changes: 26 additions & 0 deletions
26
src/infrastracture/adapters/fileConversion/contentGenerationUtils.ts
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,26 @@ | ||
import { GenerateContentRequest } from '@google-cloud/vertexai'; | ||
import { setupVertexAI } from './vertexAIUtils'; | ||
|
||
export async function generateContent(req: GenerateContentRequest): Promise<string> { | ||
const generativeModel = setupVertexAI(); | ||
let content = ''; | ||
|
||
try { | ||
const streamingResp = await generativeModel.generateContentStream(req); | ||
for await (const item of streamingResp.stream) { | ||
if ( | ||
item.candidates && | ||
item.candidates[0].content && | ||
item.candidates[0].content.parts | ||
) { | ||
content += item.candidates[0].content.parts | ||
.map((part) => part.text) | ||
.join(''); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error generating content stream:', error); | ||
} | ||
|
||
return content; | ||
} | ||
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
15 changes: 15 additions & 0 deletions
15
src/infrastracture/adapters/fileConversion/vertexAIUtils.ts
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 { GenerativeModel, VertexAI } from '@google-cloud/vertexai'; | ||
import { SAFETY_SETTINGS, VERTEX_AI_CONFIG } from './constants'; | ||
|
||
export function setupVertexAI(): GenerativeModel { | ||
const vertexAI = new VertexAI({ | ||
project: VERTEX_AI_CONFIG.project, | ||
location: VERTEX_AI_CONFIG.location, | ||
}); | ||
|
||
return vertexAI.getGenerativeModel({ | ||
model: VERTEX_AI_CONFIG.model, | ||
generationConfig: VERTEX_AI_CONFIG.generationConfig, | ||
safetySettings: SAFETY_SETTINGS, | ||
}); | ||
} |