Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get parsed value directly from openai #124

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/utils/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,16 @@ export function encodeImage(image: Buffer) {
return Buffer.from(image).toString("base64");
}

export function parseOpenAiResponse(response: string) {
try {
const data = JSON.parse(response);
return data;
} catch (e) {
console.error("Failed to parse OpenAI response", e);
return null;
}
}

export async function parseVitalsFromImage(image: Buffer) {
const compressedImage = await compressImage(image);
const b64Image = encodeImage(compressedImage);
const imageUrl = `data:image/jpeg;base64,${b64Image}`;

const completions = await openai.chat.completions.create({
model: "gpt-4-vision-preview",
model: "gpt-4-turbo",
max_tokens: 4096,
temperature: 0.4,
response_format: { type: "json_object" },
messages: [
{
role: "system",
Expand Down Expand Up @@ -69,5 +60,6 @@ export async function parseVitalsFromImage(image: Buffer) {
return null;
}

return parseOpenAiResponse(response);
console.log(`[OCR] : ${response}`);
return JSON.parse(response);
}
Loading