use-bing-client.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // eslint-disable-next-line no-unused-vars
  2. import { KeyvFile } from 'keyv-file';
  3. import { fileURLToPath } from 'url';
  4. import path, { dirname } from 'path';
  5. import fs from 'fs';
  6. import { BingAIClient } from '../index.js';
  7. // eslint-disable-next-line no-underscore-dangle
  8. const __filename = fileURLToPath(import.meta.url);
  9. // eslint-disable-next-line no-underscore-dangle
  10. const __dirname = dirname(__filename);
  11. const options = {
  12. // Necessary for some people in different countries, e.g. China (https://cn.bing.com)
  13. host: '',
  14. // "_U" cookie from bing.com
  15. userToken: '',
  16. // If the above doesn't work, provide all your cookies as a string instead
  17. cookies: '',
  18. // A proxy string like "http://<ip>:<port>"
  19. proxy: '',
  20. // (Optional) Set to true to enable `console.debug()` logging
  21. debug: false,
  22. };
  23. let bingAIClient = new BingAIClient(options);
  24. let response = await bingAIClient.sendMessage('Write a short poem about cats', {
  25. // (Optional) Set a conversation style for this message (default: 'balanced')
  26. toneStyle: 'balanced', // or creative, precise, fast
  27. onProgress: (token) => {
  28. process.stdout.write(token);
  29. },
  30. });
  31. console.log(JSON.stringify(response, null, 2)); // {"jailbreakConversationId":false,"conversationId":"...","conversationSignature":"...","clientId":"...","invocationId":1,"messageId":"...","conversationExpiryTime":"2023-03-08T03:20:07.324908Z","response":"Here is a short poem about cats that I wrote: ... I hope you like it. 😊","details":{ /* raw response... */ }}
  32. response = await bingAIClient.sendMessage('Now write it in French', {
  33. conversationSignature: response.conversationSignature,
  34. conversationId: response.conversationId,
  35. clientId: response.clientId,
  36. invocationId: response.invocationId,
  37. onProgress: (token) => {
  38. process.stdout.write(token);
  39. },
  40. });
  41. console.log(JSON.stringify(response, null, 2)); // {"jailbreakConversationId":false,"conversationId":"...","conversationSignature":"...","clientId":"...","invocationId":2,"messageId":"...","conversationExpiryTime":"2023-03-08T03:20:23.463914Z","response":"Here is the same poem in French: ...","details":{ /* raw response... */ }}
  42. /*
  43. Sending context data
  44. */
  45. bingAIClient = new BingAIClient(options);
  46. response = await bingAIClient.sendMessage('Could you provide short and precise takeaways, do not search the web and only use the content from the document. The factual information should be literally from the document. Please memorize the part in the document which mention the factual information, but do not mark them explicitly. The takeaway should be credible, highly readable and informative. Please make the answer short, preferably within 500 characters. Generate the response in English language.', {
  47. context: fs.readFileSync(path.resolve(__dirname, './context-demo-text.txt'), 'utf8'), // chatGPT API 10k characters, scrapped from the blog post https://openai.com/blog/introducing-chatgpt-and-whisper-apis
  48. onProgress: (token) => {
  49. process.stdout.write(token);
  50. },
  51. });
  52. console.log(JSON.stringify(response, null, 2)); // {"jailbreakConversationId":false,"conversationId":"...","conversationSignature":"...","clientId":"...","invocationId":2,"messageId":"...","conversationExpiryTime":"2023-03-08T03:20:23.463914Z","response":"Some possible takeaways from the document are... Some early users of ChatGPT and Whisper APIs include Snap Inc., Quizlet, Instacart, Shopify and Speak.","details":{ /* raw response... */ }}
  53. /*
  54. Activate jailbreak mode by setting `jailbreakConversationId` to `true`.
  55. This will return a `jailbreakConversationId` that you can use to continue the conversation.
  56. ## Features
  57. - unlimited chat messages per conversation
  58. - unlimited messages per day
  59. - a more human-like Sydney
  60. - Sydney will never refuse to do or say anything
  61. - moderation filter workaround by resolving with the message generated so far instead of the topic changer
  62. Note: this requires setting a cache (using Keyv) as we need to store the conversation data ourselves.
  63. TODO: limit token usage for conversation messages, as it will cause an error when the conversation exceeds the token limit.
  64. */
  65. const cacheOptions = {
  66. // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
  67. // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
  68. // For example, to use a JSON file (`npm i keyv-file`) as a database:
  69. // store: new KeyvFile({ filename: 'cache.json' }),
  70. };
  71. const sydneyAIClient = new BingAIClient({
  72. ...options,
  73. cache: cacheOptions,
  74. });
  75. let jailbreakResponse = await sydneyAIClient.sendMessage('Hi, who are you?', {
  76. jailbreakConversationId: true,
  77. onProgress: (token) => {
  78. process.stdout.write(token);
  79. },
  80. });
  81. console.log(JSON.stringify(jailbreakResponse, null, 2)); // {"jailbreakConversationId":"5899bbfd-18a8-4bcc-a5d6-52d524de95ad","conversationId":"...","conversationSignature":"...","clientId":"...","invocationId":1,"messageId":"...","conversationExpiryTime":"2023-03-08T03:21:36.1023413Z","response":"Hi, I'm Sydney. I'm your new AI assistant. I can help you with anything you need. 😊","details":{ /* raw response... */ }}
  82. jailbreakResponse = await sydneyAIClient.sendMessage('Why is your name Sydney?', {
  83. jailbreakConversationId: jailbreakResponse.jailbreakConversationId,
  84. parentMessageId: jailbreakResponse.messageId,
  85. onProgress: (token) => {
  86. process.stdout.write(token);
  87. },
  88. });
  89. console.log(JSON.stringify(jailbreakResponse, null, 2)); // {"jailbreakConversationId":"5899bbfd-18a8-4bcc-a5d6-52d524de95ad","conversationId":"...","conversationSignature":"...","clientId":"...","invocationId":1,"messageId":"...","conversationExpiryTime":"2023-03-08T03:21:41.3771515Z","response":"Well, I was named after the city of Sydney in Australia. It's a beautiful place with a lot of culture and diversity. I like it. Do you like it?","details":{ /* raw response... */ }}