Skip to content

Commit

Permalink
refactor: reduce vertex duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Alemayhu <alexander@alemayhu.com>
  • Loading branch information
aalemayhu committed Dec 27, 2024
1 parent 1e3f2a0 commit b6b70b0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
25 changes: 20 additions & 5 deletions src/infrastracture/adapters/fileConversion/constants.ts
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,
},
};
19 changes: 2 additions & 17 deletions src/infrastracture/adapters/fileConversion/convertImageToHTML.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VertexAI } from '@google-cloud/vertexai';
import { SAFETY_SETTINGS } from './constants';
import { setupVertexAI } from './vertexAIUtils';

/**
* Google VertexAI is returning Markdown:
Expand All @@ -16,21 +15,7 @@ export function removeFirstAndLastLine(content: string): string {
export const convertImageToHTML = async (
imageData: string
): Promise<string> => {
const vertexAI = new VertexAI({
project: 'notion-to-anki',
location: 'europe-west3',
});
const model = 'gemini-1.5-pro-002';

const generativeModel = vertexAI.preview.getGenerativeModel({
model: model,
generationConfig: {
maxOutputTokens: 8192,
temperature: 1,
topP: 0.95,
},
safetySettings: SAFETY_SETTINGS,
});
const generativeModel = setupVertexAI();

const text1 = {
text: `Convert the text in this image to the following format for (every question is their own ul):
Expand Down
20 changes: 3 additions & 17 deletions src/infrastracture/adapters/fileConversion/convertPDFToHTML.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import path from 'path';
import fs from 'fs';

import { GenerateContentRequest, VertexAI } from '@google-cloud/vertexai';
import { SAFETY_SETTINGS } from './constants';
import { GenerateContentRequest } from '@google-cloud/vertexai';
import { setupVertexAI } from './vertexAIUtils';

export const convertPDFToHTML = async (pdf: string): Promise<string> => {
const vertexAI = new VertexAI({
project: 'notion-to-anki',
location: 'europe-west3',
});
const model = 'gemini-1.5-pro-002';
const generativeModel = vertexAI.preview.getGenerativeModel({
model: model,
generationConfig: {
maxOutputTokens: 8192,
temperature: 1,
topP: 0.95,
},
safetySettings: SAFETY_SETTINGS,
});
const generativeModel = setupVertexAI();

const document1 = {
inlineData: {
Expand Down
15 changes: 15 additions & 0 deletions src/infrastracture/adapters/fileConversion/vertexAIUtils.ts
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,
});
}

0 comments on commit b6b70b0

Please sign in to comment.