2023-02-15 12:29:56 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
const { KeyvFile } = require('keyv-file');
|
|
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const askBing = async ({
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
jailbreak,
|
|
|
|
|
jailbreakConversationId,
|
2023-04-04 12:53:41 -04:00
|
|
|
context,
|
|
|
|
|
systemMessage,
|
2023-03-31 03:22:57 +08:00
|
|
|
conversationSignature,
|
|
|
|
|
clientId,
|
|
|
|
|
invocationId,
|
|
|
|
|
toneStyle,
|
2023-04-10 00:41:34 +08:00
|
|
|
token,
|
2023-03-31 03:22:57 +08:00
|
|
|
onProgress
|
|
|
|
|
}) => {
|
2023-05-16 14:30:24 -04:00
|
|
|
const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api');
|
2023-03-31 03:22:57 +08:00
|
|
|
const store = {
|
|
|
|
|
store: new KeyvFile({ filename: './data/cache.json' })
|
|
|
|
|
};
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-03-10 12:55:45 -05:00
|
|
|
const bingAIClient = new BingAIClient({
|
|
|
|
|
// "_U" cookie from bing.com
|
2023-05-18 11:09:31 -07:00
|
|
|
userToken:
|
|
|
|
|
process.env.BINGAI_TOKEN == 'user_provided' ? token : process.env.BINGAI_TOKEN ?? null,
|
2023-03-10 12:55:45 -05:00
|
|
|
// If the above doesn't work, provide all your cookies as a string instead
|
|
|
|
|
// cookies: '',
|
2023-02-15 12:29:56 -05:00
|
|
|
debug: false,
|
2023-03-31 03:22:57 +08:00
|
|
|
cache: store,
|
2023-04-08 00:14:44 +08:00
|
|
|
host: process.env.BINGAI_HOST || null,
|
2023-03-25 09:40:36 -04:00
|
|
|
proxy: process.env.PROXY || null
|
2023-03-10 12:55:45 -05:00
|
|
|
});
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-04-05 02:29:11 +08:00
|
|
|
let options = {};
|
|
|
|
|
|
|
|
|
|
if (jailbreakConversationId == 'false') {
|
|
|
|
|
jailbreakConversationId = false;
|
|
|
|
|
}
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-04-05 02:29:11 +08:00
|
|
|
if (jailbreak)
|
|
|
|
|
options = {
|
|
|
|
|
jailbreakConversationId: jailbreakConversationId || jailbreak,
|
|
|
|
|
context,
|
|
|
|
|
systemMessage,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
toneStyle,
|
|
|
|
|
onProgress
|
|
|
|
|
};
|
|
|
|
|
else {
|
|
|
|
|
options = {
|
|
|
|
|
conversationId,
|
|
|
|
|
context,
|
|
|
|
|
systemMessage,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
toneStyle,
|
|
|
|
|
onProgress
|
|
|
|
|
};
|
2023-03-31 04:22:16 +08:00
|
|
|
|
2023-04-05 02:29:11 +08:00
|
|
|
// don't give those parameters for new conversation
|
|
|
|
|
// for new conversation, conversationSignature always is null
|
|
|
|
|
if (conversationSignature) {
|
|
|
|
|
options.conversationSignature = conversationSignature;
|
|
|
|
|
options.clientId = clientId;
|
|
|
|
|
options.invocationId = invocationId;
|
|
|
|
|
}
|
2023-03-25 09:40:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('bing options', options);
|
2023-03-15 00:47:17 +08:00
|
|
|
|
|
|
|
|
const res = await bingAIClient.sendMessage(text, options);
|
2023-02-15 12:29:56 -05:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
2023-04-04 21:00:04 -04:00
|
|
|
// for reference:
|
|
|
|
|
// https://github.com/waylaidwanderer/node-chatgpt-api/blob/main/demos/use-bing-client.js
|
2023-02-15 12:29:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { askBing };
|