const Gpt4o = OpenAiCompletions.model('gpt-4o');
const AIPlan = AiPlan.fromModel(Gpt4o, {
attempts: 5,
schedule: Schedule.exponential('100 millis', 1.5),
});
class ResponseSchema extends Schema.Class<ResponseSchema>('ResponseSchema')({
content: Schema.OptionFromNullOr(Schema.String).annotations({
text: 'The image text',
}),
ocrWasSuccessful: Schema.Boolean,
}) {}
export const ocr = (data: Blob) =>
Effect.gen(function* () {
const chatService = yield* AiChat.AiChat;
const image = yield* ImagePart.fromBlob(data, 'high');
const response = chatService.structured({
input: image,
schema: ResponseSchema,
});
const plan = yield* AIPlan;
const model = yield* plan.provide(response);
console.log('model', model);
return model;
});
const program = Effect.gen(function* () {
const images = yield* pipe(
pdfBuffer,
convertPDFToJPEGBuffer,
Effect.flatMap(flow(Array.map(jpegBufferToBlob), Effect.all)),
);
const result = yield* ocr(images[0]!);
});
const i = pipe(
program,
Effect.provide(OpenAiClient.layerConfig({
apiKey: Config.redacted('OPENAI_API_KEY'),
})),
Effect.provide(NodeHttpClient.layer),
Effect.provide(DevToolsLive),
);
NodeRuntime.runMain(i);
const Gpt4o = OpenAiCompletions.model('gpt-4o');
const AIPlan = AiPlan.fromModel(Gpt4o, {
attempts: 5,
schedule: Schedule.exponential('100 millis', 1.5),
});
class ResponseSchema extends Schema.Class<ResponseSchema>('ResponseSchema')({
content: Schema.OptionFromNullOr(Schema.String).annotations({
text: 'The image text',
}),
ocrWasSuccessful: Schema.Boolean,
}) {}
export const ocr = (data: Blob) =>
Effect.gen(function* () {
const chatService = yield* AiChat.AiChat;
const image = yield* ImagePart.fromBlob(data, 'high');
const response = chatService.structured({
input: image,
schema: ResponseSchema,
});
const plan = yield* AIPlan;
const model = yield* plan.provide(response);
console.log('model', model);
return model;
});
const program = Effect.gen(function* () {
const images = yield* pipe(
pdfBuffer,
convertPDFToJPEGBuffer,
Effect.flatMap(flow(Array.map(jpegBufferToBlob), Effect.all)),
);
const result = yield* ocr(images[0]!);
});
const i = pipe(
program,
Effect.provide(OpenAiClient.layerConfig({
apiKey: Config.redacted('OPENAI_API_KEY'),
})),
Effect.provide(NodeHttpClient.layer),
Effect.provide(DevToolsLive),
);
NodeRuntime.runMain(i);