cohere-ai/cohere-typescript

Cloudflare workers give error

bonadio opened this issue · 3 comments

Hi
I am trying to use cohere embedding in Cloudflare Workers, when I initialize the client, I get the error

The version I am using is 7.6.2

import { CohereClient } from "cohere-ai";
const cohere = new CohereClient({
    token: "_______"
});
const handler = {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {

        let result = {}
        try {
            result = await cohere.embed( { texts:["test 123", "New teste 123"], model:"embed-multilingual-light-v3.0", inputType:"search_document"})
    
            return Response.json(result);
        } catch (e) {
            console.log(e)
        }
        return Response.json(result);
    },
  };
  
export default handler;
✘ [ERROR] service core:user:backend: Uncaught ReferenceError: global is not defined

    at db_addchunk.js:3605:7 in node_modules/cohere-ai/core/fetcher/Fetcher.js
    at db_addchunk.js:11:50 in __require
    at db_addchunk.js:3801:21 in node_modules/cohere-ai/core/fetcher/index.js
    at db_addchunk.js:11:50 in __require
    at db_addchunk.js:6466:18 in node_modules/cohere-ai/core/index.js
    at db_addchunk.js:11:50 in __require
    at db_addchunk.js:12524:29 in node_modules/cohere-ai/Client.js
    at db_addchunk.js:11:50 in __require
    at db_addchunk.js:13146:20 in node_modules/cohere-ai/index.js
    at db_addchunk.js:11:50 in __require

In Fetcher.js if I comment this line it works

const form_data_1 = __importDefault(require("form-data"));
const qs_1 = __importDefault(require("qs"));
if (typeof window === "undefined") {
    //global.fetch = require("node-fetch");    <-- comment this line and works 
}

@bonadio thanks for reporting this issue. To get around this issue in cloudflare workers, you can try enabling the node-compat flag. See the docs here: https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-workers.

For context, Cohere's SDKs are automatically generated using Fern. For the longer term, we'll see if we can make some updates so that the SDK can run without having to enable node-compat.

Closing in favour of fern-api/fern#2555

Hey @bonadio this should be working as of 7.7.5. Let me know if you have any further issues! Thanks