feat: add nodejs commonjs module support#3
Merged
easychen merged 2 commits intoeasychen:masterfrom Apr 6, 2023
Merged
Conversation
Owner
|
感谢 PR,能提供一个 demo 吗。我测试了下会报错,感觉是我姿势不对。 错误信息为: |
Contributor
Author
|
我这边只用了 |
Contributor
Author
|
似乎目前不太好处理这个问题。 上面 fetchEventSource is not a function 是引入的问题,应为 最关键的在于 @microsoft/fetch-event-source 是为浏览器设计的,未考虑nodejs,从代码看,里面也依赖了 这边有一个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();
|
Owner
|
好的,那我们或许可以等等,稍后再合并这个PR … |
Contributor
Author
|
换用 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();
/**
* 山
山窗
山窗春
山窗春色
山窗春色慰
山窗春色慰风
山窗春色慰风尘
山窗春色慰风尘,
山窗春色慰风尘,赵
山窗春色慰风尘,赵郎
山窗春色慰风尘,赵郎辛
山窗春色慰风尘,赵郎辛勤
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域。
山窗春色慰风尘,赵郎辛勤已不因。仰望繁星思前路,顶礼荣光追疆域。
*/ |
Owner
|
辛苦了,已经发布到 npm。0.1.12 版本。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
恰好需要在nodejs中用,添加nodejs中commonjs模块的支持