kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add docs; tweak ts
rodzic
8ef2c77a10
commit
3e372b55b1
|
@ -0,0 +1 @@
|
||||||
|
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
|
|
@ -0,0 +1,150 @@
|
||||||
|
[chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTAPI
|
||||||
|
|
||||||
|
# Class: ChatGPTAPI
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
|
||||||
|
### Constructors
|
||||||
|
|
||||||
|
- [constructor](ChatGPTAPI.md#constructor)
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
|
||||||
|
- [close](ChatGPTAPI.md#close)
|
||||||
|
- [getIsSignedIn](ChatGPTAPI.md#getissignedin)
|
||||||
|
- [getLastMessage](ChatGPTAPI.md#getlastmessage)
|
||||||
|
- [getMessages](ChatGPTAPI.md#getmessages)
|
||||||
|
- [getPrompts](ChatGPTAPI.md#getprompts)
|
||||||
|
- [init](ChatGPTAPI.md#init)
|
||||||
|
- [sendMessage](ChatGPTAPI.md#sendmessage)
|
||||||
|
|
||||||
|
## Constructors
|
||||||
|
|
||||||
|
### constructor
|
||||||
|
|
||||||
|
• **new ChatGPTAPI**(`opts?`)
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
| Name | Type | Description |
|
||||||
|
| :------ | :------ | :------ |
|
||||||
|
| `opts` | `Object` | - |
|
||||||
|
| `opts.chatUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/'` * |
|
||||||
|
| `opts.headless?` | `boolean` | **`Default Value`** `false` * |
|
||||||
|
| `opts.markdown?` | `boolean` | **`Default Value`** `true` * |
|
||||||
|
| `opts.userDataDir?` | `string` | **`Default Value`** `'/tmp/chatgpt'` * |
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:20
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### close
|
||||||
|
|
||||||
|
▸ **close**(): `Promise`<`void`\>
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`void`\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:175
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### getIsSignedIn
|
||||||
|
|
||||||
|
▸ **getIsSignedIn**(): `Promise`<`boolean`\>
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`boolean`\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:88
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### getLastMessage
|
||||||
|
|
||||||
|
▸ **getLastMessage**(): `Promise`<`string`\>
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`string`\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:93
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### getMessages
|
||||||
|
|
||||||
|
▸ **getMessages**(): `Promise`<`string`[]\>
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`string`[]\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:113
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### getPrompts
|
||||||
|
|
||||||
|
▸ **getPrompts**(): `Promise`<`string`[]\>
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`string`[]\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:103
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### init
|
||||||
|
|
||||||
|
▸ **init**(`opts?`): `Promise`<`Page`\>
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
| Name | Type |
|
||||||
|
| :------ | :------ |
|
||||||
|
| `opts` | `Object` |
|
||||||
|
| `opts.auth?` | ``"blocking"`` \| ``"eager"`` |
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`Page`\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:48
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
### sendMessage
|
||||||
|
|
||||||
|
▸ **sendMessage**(`message`): `Promise`<`string`\>
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
| Name | Type |
|
||||||
|
| :------ | :------ |
|
||||||
|
| `message` | `string` |
|
||||||
|
|
||||||
|
#### Returns
|
||||||
|
|
||||||
|
`Promise`<`string`\>
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
chatgpt-api.ts:151
|
|
@ -0,0 +1,9 @@
|
||||||
|
[chatgpt](readme.md) / Exports
|
||||||
|
|
||||||
|
# chatgpt
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
- [ChatGPTAPI](classes/ChatGPTAPI.md)
|
|
@ -0,0 +1,113 @@
|
||||||
|
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.
|
||||||
|
|
||||||
|
[](https://www.npmjs.com/package/chatgpt) [](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml) [](https://github.com/transitive-bullshit/chatgpt-api/blob/main/license) [](https://prettier.io)
|
||||||
|
|
||||||
|
- [Intro](#intro)
|
||||||
|
- [Auth](#auth)
|
||||||
|
- [Usage](#usage)
|
||||||
|
- [Docs](#docs)
|
||||||
|
- [Todo](#todo)
|
||||||
|
- [Related](#related)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Intro
|
||||||
|
|
||||||
|
This package is a Node.js TypeScript wrapper around [ChatGPT](https://openai.com/blog/chatgpt) by [OpenAI](https://openai.com).
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```ts
|
||||||
|
async function example() {
|
||||||
|
const api = new ChatGPTAPI()
|
||||||
|
|
||||||
|
// 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
|
||||||
|
\`\`\`
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's the same response rendered as markdown:
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Docs
|
||||||
|
|
||||||
|
See the [auto-generated docs](./docs/modules.md).
|
||||||
|
|
||||||
|
## Todo
|
||||||
|
|
||||||
|
- [ ] Add message and conversation IDs
|
||||||
|
- [ ] Add support for streaming responses
|
||||||
|
|
||||||
|
## Related
|
||||||
|
|
||||||
|
- Inspired by the [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT © [Travis Fischer](https://transitivebullsh.it)
|
||||||
|
|
||||||
|
Support my open source work by <a href="https://twitter.com/transitive_bs">following me on twitter <img src="https://storage.googleapis.com/saasify-assets/twitter-logo.svg" alt="twitter" height="24px" align="center"></a>
|
90
readme.md
90
readme.md
|
@ -1,10 +1,12 @@
|
||||||
# ChatGPT API <!-- omit in toc -->
|
# ChatGPT API <!-- omit in toc -->
|
||||||
|
|
||||||
> Node.js wrapper around ChatGPT. Uses headless Chrome as a temporary solution until the official API is released.
|
> Node.js wrapper around [ChatGPT](https://openai.com/blog/chatgpt/). Uses headless Chrome as a temporary solution until the official API is released.
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/chatgpt) [](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml) [](https://github.com/transitive-bullshit/chatgpt-api/blob/main/license) [](https://prettier.io)
|
[](https://www.npmjs.com/package/chatgpt) [](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml) [](https://github.com/transitive-bullshit/chatgpt-api/blob/main/license) [](https://prettier.io)
|
||||||
|
|
||||||
- [Intro](#intro)
|
- [Intro](#intro)
|
||||||
|
- [Auth](#auth)
|
||||||
|
- [Usage](#usage)
|
||||||
- [Docs](#docs)
|
- [Docs](#docs)
|
||||||
- [Todo](#todo)
|
- [Todo](#todo)
|
||||||
- [Related](#related)
|
- [Related](#related)
|
||||||
|
@ -12,16 +14,98 @@
|
||||||
|
|
||||||
## Intro
|
## Intro
|
||||||
|
|
||||||
TODO
|
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 access programatically.
|
||||||
|
|
||||||
|
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
|
||||||
|
async function example() {
|
||||||
|
const api = new ChatGPTAPI()
|
||||||
|
|
||||||
|
// 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.'
|
||||||
|
)
|
||||||
|
console.log(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// which outputs:
|
||||||
|
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
\`\`\`
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's the same response rendered as markdown:
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
## Docs
|
## 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
|
## Todo
|
||||||
|
|
||||||
- [ ] Add message and conversation IDs
|
- [ ] Add message and conversation IDs
|
||||||
- [ ] Add support for streaming responses
|
- [ ] Add support for streaming responses
|
||||||
|
- [ ] Add basic unit tests
|
||||||
|
|
||||||
## Related
|
## Related
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,9 @@ export class ChatGPTAPI {
|
||||||
this._markdown = !!markdown
|
this._markdown = !!markdown
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init(opts: { auth?: 'blocking' | 'eager' } = {}) {
|
||||||
|
const { auth = 'eager' } = opts
|
||||||
|
|
||||||
this._browser = await chromium.launchPersistentContext(this._userDataDir, {
|
this._browser = await chromium.launchPersistentContext(this._userDataDir, {
|
||||||
headless: this._headless
|
headless: this._headless
|
||||||
})
|
})
|
||||||
|
@ -68,6 +70,18 @@ export class ChatGPTAPI {
|
||||||
}
|
}
|
||||||
} while (true)
|
} while (true)
|
||||||
|
|
||||||
|
if (auth === 'blocking') {
|
||||||
|
do {
|
||||||
|
const isSignedIn = await this.getIsSignedIn()
|
||||||
|
if (isSignedIn) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Please sign in to ChatGPT')
|
||||||
|
await delay(1000)
|
||||||
|
} while (true)
|
||||||
|
}
|
||||||
|
|
||||||
return this._page
|
return this._page
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue