Effect CommunityEC
Effect Community•9mo ago•
1 reply
Niklas

Issue with Layer Dependency in AiChat Usage

Great AI library! 😃 I've been keen to try it out since the conference and finally I got a chance today.
I've started to play around with AiChat with the objective to transform a OCR service away from langchain but I'm stuck on a Layer dependency of AiChat.AiChat. Shouldn't that be included in the OpenAiClient.layerConfig?

Below is the code:

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 i: Effect.Effect<void, AiError | Error | ConfigError, AiChat.AiChat>
Anyone who knows how to solve it?
Was this page helpful?