This package is a Node.js TypeScript wrapper around [ChatGPT](https://openai.com/blog/chatgpt) by [OpenAI](https://openai.com).
This package is a Node.js wrapper around [ChatGPT](https://openai.com/blog/chatgpt) by [OpenAI](https://openai.com). TS batteries included. ✨
You can use it to start experimenting with ChatGPT by integrating it into websites, chatbots, etc...
You can use it to start building projects powered by ChatGPT like chatbots, websites, etc...
## Auth
## How it works
It uses headless Chromium via [Playwright](https://playwright.dev) under the hood, so **you still need to have access to ChatGPT**, but it makes it much easier to access programatically.
We use headless Chromium via [Playwright](https://playwright.dev) to automate the webapp, so **you still need to have access to ChatGPT**. It just makes building API-like integrations much easier.
Chromium is opened in non-headless mode by default, which is important because the first time you run `ChatGPTAPI`.init, you'll need to log in manually. Chromium is launched with a persistent context, so you shouldn't need to keep re-logging in after the first time.
Chromium will be opened in non-headless mode by default, which is important because the first time you run `ChatGPTAPI.init()`, you'll need to log in manually. We launch Chromium with a persistent context, however, so you shouldn't need to keep re-logging in after the first time. When you log in the first time, _make sure that you also dismiss the welcome modal_.
> **Note**
> We'll replace headless chrome with the official API once it's released.
## Install
```bash
npm install--save chatgpt
# or
yarn add chatgpt
# or
pnpm add chatgpt
```
## Usage
## Usage
...
@@ -35,10 +47,10 @@ import { ChatGPTAPI } from 'chatgpt'
...
@@ -35,10 +47,10 @@ import { ChatGPTAPI } from 'chatgpt'
asyncfunctionexample(){
asyncfunctionexample(){
constapi=newChatGPTAPI()
constapi=newChatGPTAPI()
// open chromium and wait until the user has logged in
// open chromium and wait until you've logged in
awaitapi.init({auth:'blocking'})
awaitapi.init({auth:'blocking'})
// send a message and wait for a complete response, then parse it as markdown
// send a message and wait for the response
constresponse=awaitapi.sendMessage(
constresponse=awaitapi.sendMessage(
'Write a python version of bubble sort. Do not include example usage.'
'Write a python version of bubble sort. Do not include example usage.'
)
)
...
@@ -70,17 +82,18 @@ def bubble_sort(lst):
...
@@ -70,17 +82,18 @@ def bubble_sort(lst):
return lst
return lst
```
```
The default functionality is to parse ChatGPT responses as markdown using [html-to-md](https://github.com/stonehank/html-to-md). I've found the markdown parsing to work really well during my testing, but if you'd rather output plaintext, you can use:
By default, ChatGPT responses are parsed as markdown using [html-to-md](https://github.com/stonehank/html-to-md). I've found that this works really well during my testing, but if you'd rather output plaintext, you can use:
```ts
```ts
const api = new ChatGPTAPI({ markdown: false })
const api = new ChatGPTAPI({ markdown: false })
```
```
## Example
A full [example](./src/example.ts) is included for testing purposes:
A full example is included for testing purposes:
```bash
# clone repo
```
# install node deps
# then run
npx tsx src/example.ts
npx tsx src/example.ts
```
```
...
@@ -88,15 +101,10 @@ npx tsx src/example.ts
...
@@ -88,15 +101,10 @@ npx tsx src/example.ts
See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on methods parameters.
See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on methods parameters.
## Todo
- [ ] Add message and conversation IDs
- [ ] Add support for streaming responses
- [ ] Add basic unit tests
## Related
## Related
- Inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
- Inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
@@ -22,7 +22,7 @@ You can use it to start building projects powered by ChatGPT like chatbots, webs
...
@@ -22,7 +22,7 @@ You can use it to start building projects powered by ChatGPT like chatbots, webs
We use headless Chromium via [Playwright](https://playwright.dev) to automate the webapp, so **you still need to have access to ChatGPT**. It just makes building API-like integrations much easier.
We use headless Chromium via [Playwright](https://playwright.dev) to automate the webapp, so **you still need to have access to ChatGPT**. It just makes building API-like integrations much easier.
Chromium will be opened in non-headless mode by default, which is important because the first time you run `ChatGPTAPI.init()`, you'll need to log in manually. We launch Chromium with a persistent context, however, so you shouldn't need to keep re-logging in after the first time.
Chromium will be opened in non-headless mode by default, which is important because the first time you run `ChatGPTAPI.init()`, you'll need to log in manually. We launch Chromium with a persistent context, however, so you shouldn't need to keep re-logging in after the first time. When you log in the first time, _make sure that you also dismiss the welcome modal_.
> **Note**
> **Note**
> We'll replace headless chrome with the official API once it's released.
> We'll replace headless chrome with the official API once it's released.