This package is a Node.js TypeScript wrapper around [ChatGPT](https://openai.com/blog/chatgpt) by [OpenAI](https://openai.com).
You can use it to start experimenting with ChatGPT by integrating it into websites, chatbots, etc...
## Auth
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 build experiments with until OpenAPI's official API for ChatGPT is released.
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.
The first time you run `ChatGPTAPI`.init, Chromium will be opened in non-headless mode so you can log in manually. After the first time, Chromium is launched with a persistent context, so you shouldn't need to keep re-logging in.
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.
## Usage
```ts
import{ChatGPTAPI}from'chatgpt'
asyncfunctionexample(){
constapi=newChatGPTAPI()
constapi=newChatGPTAPI()
// open chromium and wait until the user has logged in
awaitapi.init({auth:'blocking'})
// open chromium and wait until the user has logged in
awaitapi.init({auth:'blocking'})
// send a message and wait for a complete response, then parse it as markdown
constresponse=awaitapi.sendMessage(
'Write a python version of bubble sort. Do not include example usage.'
)
// send a message and wait for a complete response, then parse it as markdown
constresponse=awaitapi.sendMessage(
'Write a python version of bubble sort. Do not include example usage.'
)
console.log(response)
}
```
/* // response
Here is an implementation of bubble sort in Python:
Which outputs a similar reponse to this (as a markdown string, including the _```python_ code block prefix):
\`\`\`python
```python
def bubble_sort(lst):
# Set the initial flag to True to start the loop
swapped = True
...
...
@@ -61,50 +68,35 @@ def bubble_sort(lst):
# Return the sorted list
return lst
\`\`\`
*/
```
Here's the same response rendered as markdown:
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:
Here is an implementation of bubble sort in Python:
```python
defbubble_sort(lst):
# Set the initial flag to True to start the loop
swapped=True
```ts
const api = new ChatGPTAPI({ markdown: false })
```
# Keep looping until there are no more swaps
whileswapped:
# Set the flag to False initially
swapped=False
## Example
# Loop through the list
foriinrange(len(lst)-1):
# If the current element is greater than the next element,
# swap them and set the flag to True
iflst[i]>lst[i+1]:
lst[i],lst[i+1]=lst[i+1],lst[i]
swapped=True
A full example is included for testing purposes:
# Return the sorted list
returnlst
```
Note that 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 quality to be excellent in my testing, but if you'd rather output plaintext, just pass `{ markdown: false }` to the `ChatGPTAPI` constructor.
npx tsx src/example.ts
```
## Docs
See the [auto-generated docs](./docs/modules.md).
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
- Inspired by the[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)