use-browser-client.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // import { ChatGPTBrowserClient } from '@waylaidwanderer/chatgpt-api';
  2. import { ChatGPTBrowserClient } from '../index.js';
  3. const clientOptions = {
  4. // (Optional) Support for a reverse proxy for the completions endpoint (private API server).
  5. // Warning: This will expose your access token to a third party. Consider the risks before using this.
  6. reverseProxyUrl: 'https://bypass.churchless.tech/api/conversation',
  7. // Access token from https://chat.openai.com/api/auth/session
  8. accessToken: '',
  9. // Cookies from chat.openai.com (likely not required if using reverse proxy server).
  10. cookies: '',
  11. // (Optional) Set to true to enable `console.debug()` logging
  12. // debug: true,
  13. };
  14. const chatGptClient = new ChatGPTBrowserClient(clientOptions);
  15. const response = await chatGptClient.sendMessage('Hello!');
  16. console.log(response); // { response: 'Hi! How can I help you today?', conversationId: '...', messageId: '...' }
  17. const response2 = await chatGptClient.sendMessage('Write a poem about cats.', { conversationId: response.conversationId, parentMessageId: response.messageId });
  18. console.log(response2.response); // Cats are the best pets in the world.
  19. const response3 = await chatGptClient.sendMessage('Now write it in French.', {
  20. conversationId: response2.conversationId,
  21. parentMessageId: response2.messageId,
  22. // If you want streamed responses, you can set the `onProgress` callback to receive the response as it's generated.
  23. // You will receive one token at a time, so you will need to concatenate them yourself.
  24. onProgress: token => process.stdout.write(token),
  25. });
  26. console.log();
  27. console.log(response3.response); // Les chats sont les meilleurs animaux de compagnie du monde.
  28. // (Optional) Lets you delete the conversation when you're done with it.
  29. await chatGptClient.deleteConversation(response3.conversationId);