Effect CommunityEC
Effect Community16mo ago
4 replies
Wendaolee

Constructing Request Chains with Effect and TaskEither in TypeScript

Hello,guys.

I'm new to Effect and now and wrting codes to consrtuct a chain of request.In fp-ts , it always done by TaskEither and its tryCatch function.

The flow code like these:
import { Effect, flow } from "effect"
import { getEmbeddingsByFetch } from "./googleGenimi.ts"
import { createEntity } from "./zilliz.ts"

export const sampleFlow = flow(
    getEmbeddingsByFetch,
    Effect.andThen(res => {
        return res.embeddings.value
    }),
    Effect.andThen(vector => 
        createEntity('Blog_test',{
            en_name:'test',
            vector
        })
    ),
    Effect.map(res => {
        return Effect.runPromise(res)
    }),
)


The request function like this:
export const getEmbeddingsByFetch = (text:string) => {
    return Effect.tryPromise({
        try:() => fetch(`https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent?key=${GENIMI_API_KEY}`,{
            method:'POST',
            headers:{
                'Content-Type':'application/json'
            },
            body:JSON.stringify({
                "model":"models/text-embedding-004",
                "content":{
                    "parts":[
                        {
                            text
                        }
                    ]
                }
            })
        }).then(res => res.json() as Promise<GoogleSingleEmbeddingsResponse>),
        catch:(err) => err
    })
    // return await Effect.runPromiseExit(result)   
}
Was this page helpful?