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

feat: add nodejs commonjs module support #3

Merged
merged 2 commits into from
Apr 6, 2023

Conversation

magicds
Copy link
Contributor

@magicds magicds commented Apr 6, 2023

恰好需要在nodejs中用,添加nodejs中commonjs模块的支持

@easychen
Copy link
Owner

easychen commented Apr 6, 2023

感谢 PR,能提供一个 demo 吗。我测试了下会报错,感觉是我姿势不对。

const api2d = require('./api2d-js/cjs/index.js');

async function doit()
{
    const api2d_instance = new api2d('fk18600');
    const response = await api2d_instance.completion({
        messages: [
            {
                'role':'user',
                'content':'Hello',
            }
        ],
        stream: true,
        onMessage: (message) => {
            console.log(message);
        }
    });
    console.log(response);
}

doit();

错误信息为:

in stream
TypeError: fetchEventSource is not a function
    at fetch.signal (/Users/easy/Playground/node-api2d/api2d-js/cjs/index.js:56:44)
    at new Promise (<anonymous>)
    at Api2d.completion (/Users/easy/Playground/node-api2d/api2d-js/cjs/index.js:46:20)
    at doit (/Users/easy/Playground/node-api2d/test.cjs:7:43)
    at Object.<anonymous> (/Users/easy/Playground/node-api2d/test.cjs:22:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

TypeError: fetchEventSource is not a function
    at fetch.signal (/Users/easy/Playground/node-api2d/api2d-js/cjs/index.js:56:44)
    at new Promise (<anonymous>)
    at Api2d.completion (/Users/easy/Playground/node-api2d/api2d-js/cjs/index.js:46:20)
    at doit (/Users/easy/Playground/node-api2d/test.cjs:7:43)
    at Object.<anonymous> (/Users/easy/Playground/node-api2d/test.cjs:22:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Node.js v18.14.0

@magicds
Copy link
Contributor Author

magicds commented Apr 6, 2023

我这边只用了 stream: false 的情况, stream: true, 情况下确实有问题,我调试下。

@magicds
Copy link
Contributor Author

magicds commented Apr 6, 2023

似乎目前不太好处理这个问题。

上面 fetchEventSource is not a function 是引入的问题,应为
const { fetchEventSource } = require('@microsoft/fetch-event-source');

最关键的在于 @microsoft/fetch-event-source 是为浏览器设计的,未考虑nodejs,从代码看,里面也依赖了 window document 等浏览器专属的接口。

这边有一个PR是为其提供 nodejs 支持的, Azure/fetch-event-source#28 不过还未被合并,从评论来看,代码也不够完善。

测试代码

const Api2d = require('../cjs/index.js');

const FK = 'fkey';

async function run() {
    const api = new Api2d(FK, 'https://openai.api2d.net');
    const completion = await api.completion({
        model: 'gpt-3.5-turbo',
        messages: [{ role: 'user', content: '请给我一些类似waketime的软件,需要开源的、支持自己部署的' }],
        temperature: 1,
        n: 1,
        stream: false
    });

    // console.log('completion', completion);

    const responseMessage = completion.choices[0].message;

    console.log(responseMessage);
}

async function run2() {
    const api = new Api2d(FK, 'https://openai.api2d.net');
    const completion = await api.completion({
        model: 'gpt-3.5-turbo',
        messages: [{ role: 'user', content: '请给我一些类似waketime的软件,需要开源的、支持自己部署的' }],
        temperature: 1,
        n: 1,
        stream: true,
        onMessage: (message) => {
            console.log(message);
        }
    });

    console.log(completion);
}
// run();
run2();

run1() 运行正常。

run2() 运行有诸多错误。

  • document is not defined
  • window is not defined
  • stream.getReader is not a function

@easychen
Copy link
Owner

easychen commented Apr 6, 2023

好的,那我们或许可以等等,稍后再合并这个PR …

@magicds
Copy link
Contributor Author

magicds commented Apr 6, 2023

换用 https://github.com/transitive-bullshit/chatgpt-api/blob/600b35eaec985bbbfcb6c77776dc30d4614bd085/src/fetch-sse.ts#L7 这段代码来解决这个问题。

test code

const Api2d = require('../cjs/index.js');

const FK = '你的ForwardKey';

async function run() {
    const api = new Api2d(FK, 'https://openai.api2d.net');
    const completion = await api.completion({
        model: 'gpt-3.5-turbo',
        messages: [{ role: 'user', content: '请给我一些类似waketime的软件,需要开源的、支持自己部署的' }],
        temperature: 1,
        n: 1,
        stream: false
    });
    const responseMessage = completion.choices[0].message;
    console.log(responseMessage);
}

async function run2() {
    const api = new Api2d(FK);
    const completion = await api.completion({
        model: 'gpt-3.5-turbo',
        messages: [{ role: 'user', content: '假如你是李白,写一首送给赵阳的诗,表达对他的辛勤工作的赞美,七言绝句' }],
        temperature: 1,
        n: 1,
        stream: true,
        onMessage: (message) => {
            console.log(message + '\n');
        }
    });

    console.log(completion);
}
// run();
run2();

/**
 * 山

山窗

山窗春

山窗春色

山窗春色慰

山窗春色慰风

山窗春色慰风尘

山窗春色慰风尘,

山窗春色慰风尘,赵

山窗春色慰风尘,赵郎

山窗春色慰风尘,赵郎辛

山窗春色慰风尘,赵郎辛勤

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域。

山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域。
 */

@easychen easychen merged commit c5d0ae9 into easychen:master Apr 6, 2023
@easychen
Copy link
Owner

easychen commented Apr 6, 2023

辛苦了,已经发布到 npm。0.1.12 版本。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants