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 810b5f4
Show file tree
Hide file tree
Showing 3 changed files with 25 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';

Check failure on line 1 in src/infrastracture/adapters/fileConversion/convertImageToHTML.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Missing file extension for "./vertexAIUtils"

Check failure on line 1 in src/infrastracture/adapters/fileConversion/convertImageToHTML.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Cannot find module './vertexAIUtils' or its corresponding type declarations.

/**
* 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';

Check failure on line 4 in src/infrastracture/adapters/fileConversion/convertPDFToHTML.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Missing file extension for "./vertexAIUtils"

Check failure on line 4 in src/infrastracture/adapters/fileConversion/convertPDFToHTML.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Cannot find module './vertexAIUtils' or its corresponding type declarations.

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

0 comments on commit 810b5f4

Please sign in to comment.