pull/3/head
Travis Fischer 2022-12-02 18:53:33 -06:00
rodzic 82a5232333
commit eb1f3aa2b1
2 zmienionych plików z 42 dodań i 50 usunięć

Wyświetl plik

@ -36,7 +36,7 @@
#### Defined in
chatgpt-api.ts:20
[chatgpt-api.ts:20](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L20)
## Methods
@ -50,7 +50,7 @@ chatgpt-api.ts:20
#### Defined in
chatgpt-api.ts:175
[chatgpt-api.ts:175](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L175)
___
@ -64,7 +64,7 @@ ___
#### Defined in
chatgpt-api.ts:88
[chatgpt-api.ts:88](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L88)
___
@ -78,7 +78,7 @@ ___
#### Defined in
chatgpt-api.ts:93
[chatgpt-api.ts:93](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L93)
___
@ -92,7 +92,7 @@ ___
#### Defined in
chatgpt-api.ts:113
[chatgpt-api.ts:113](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L113)
___
@ -106,7 +106,7 @@ ___
#### Defined in
chatgpt-api.ts:103
[chatgpt-api.ts:103](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L103)
___
@ -127,7 +127,7 @@ ___
#### Defined in
chatgpt-api.ts:48
[chatgpt-api.ts:48](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L48)
___
@ -147,4 +147,4 @@ ___
#### Defined in
chatgpt-api.ts:151
[chatgpt-api.ts:151](https://github.com/transitive-bullshit/chatgpt-api/blob/82a5232/src/chatgpt-api.ts#L151)

Wyświetl plik

@ -2,13 +2,14 @@ chatgpt / [Exports](modules.md)
# ChatGPT API <!-- omit in toc -->
> Node.js wrapper around [ChatGPT](https://openai.com/blog/chatgpt/). Uses headless Chrome as a temporary solution until the official API is released.
> Node.js TS wrapper around [ChatGPT](https://openai.com/blog/chatgpt/). Uses headless Chrome until the official API is released.
[![NPM](https://img.shields.io/npm/v/chatgpt.svg)](https://www.npmjs.com/package/chatgpt) [![Build Status](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml/badge.svg)](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml) [![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/transitive-bullshit/chatgpt-api/blob/main/license) [![Prettier Code Formatting](https://img.shields.io/badge/code_style-prettier-brightgreen.svg)](https://prettier.io)
- [Intro](#intro)
- [Auth](#auth)
- [Usage](#usage)
- [Example](#example)
- [Docs](#docs)
- [Todo](#todo)
- [Related](#related)
@ -18,56 +19,34 @@ chatgpt / [Exports](modules.md)
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'
async function example() {
const api = new ChatGPTAPI()
const api = new ChatGPTAPI()
// open chromium and wait until the user has logged in
await api.init({ auth: 'blocking' })
// open chromium and wait until the user has logged in
await api.init({ auth: 'blocking' })
// send a message and wait for a complete response, then parse it as markdown
const response = await api.sendMessage(
'Write a python version of bubble sort. Do not include example usage.'
)
/* // response
Here is an implementation of bubble sort in Python:
\`\`\`python
def bubble_sort(lst):
# Set the initial flag to True to start the loop
swapped = True
# Keep looping until there are no more swaps
while swapped:
# Set the flag to False initially
swapped = False
# Loop through the list
for i in range(len(lst) - 1):
# If the current element is greater than the next element,
# swap them and set the flag to True
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
swapped = True
# Return the sorted list
return lst
\`\`\`
*/
// send a message and wait for a complete response, then parse it as markdown
const response = await api.sendMessage(
'Write a python version of bubble sort. Do not include example usage.'
)
console.log(response)
}
```
Here's the same response rendered as markdown:
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
def bubble_sort(lst):
@ -91,20 +70,33 @@ def bubble_sort(lst):
return lst
```
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.
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:
```ts
const api = new ChatGPTAPI({ markdown: false })
```
## Example
A full example is included for testing purposes:
```
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)
## License