Merge branch 'main' into main

pull/19/head
Travis Fischer 2022-12-06 02:50:48 -06:00 zatwierdzone przez GitHub
commit 82ee928c01
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
7 zmienionych plików z 75 dodań i 67 usunięć

Wyświetl plik

@ -36,7 +36,7 @@ Creates a new client wrapper around the unofficial ChatGPT REST API.
#### Defined in #### Defined in
[chatgpt-api.ts:31](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/chatgpt-api.ts#L31) [chatgpt-api.ts:31](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/chatgpt-api.ts#L31)
## Methods ## Methods
@ -50,7 +50,7 @@ Creates a new client wrapper around the unofficial ChatGPT REST API.
#### Defined in #### Defined in
[chatgpt-api.ts:74](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/chatgpt-api.ts#L74) [chatgpt-api.ts:74](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/chatgpt-api.ts#L74)
___ ___
@ -64,7 +64,7 @@ ___
#### Defined in #### Defined in
[chatgpt-api.ts:65](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/chatgpt-api.ts#L65) [chatgpt-api.ts:65](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/chatgpt-api.ts#L65)
___ ___
@ -78,7 +78,7 @@ ___
#### Defined in #### Defined in
[chatgpt-api.ts:165](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/chatgpt-api.ts#L165) [chatgpt-api.ts:165](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/chatgpt-api.ts#L165)
___ ___
@ -95,7 +95,7 @@ the response.
| :------ | :------ | :------ | | :------ | :------ | :------ |
| `message` | `string` | The plaintext message to send. | | `message` | `string` | The plaintext message to send. |
| `opts` | `Object` | - | | `opts` | `Object` | - |
| `opts.converstationId?` | `string` | - | | `opts.conversationId?` | `string` | Optional ID of the previous message in a conversation |
| `opts.onProgress?` | (`partialResponse`: `string`) => `void` | - | | `opts.onProgress?` | (`partialResponse`: `string`) => `void` | - |
#### Returns #### Returns
@ -104,4 +104,4 @@ the response.
#### Defined in #### Defined in
[chatgpt-api.ts:86](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/chatgpt-api.ts#L86) [chatgpt-api.ts:86](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/chatgpt-api.ts#L86)

Wyświetl plik

@ -43,7 +43,7 @@
#### Defined in #### Defined in
[types.ts:104](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L104) [types.ts:109](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L109)
___ ___
@ -53,7 +53,7 @@ ___
#### Defined in #### Defined in
[types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L1) [types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L1)
___ ___
@ -75,7 +75,7 @@ https://chat.openapi.com/backend-api/conversation
#### Defined in #### Defined in
[types.ts:129](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L129) [types.ts:134](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L134)
___ ___
@ -93,7 +93,7 @@ ___
#### Defined in #### Defined in
[types.ts:246](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L246) [types.ts:251](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L251)
___ ___
@ -118,7 +118,7 @@ ___
#### Defined in #### Defined in
[types.ts:252](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L252) [types.ts:257](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L257)
___ ___
@ -135,7 +135,7 @@ ___
#### Defined in #### Defined in
[types.ts:265](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L265) [types.ts:270](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L270)
___ ___
@ -157,7 +157,7 @@ https://chat.openapi.com/backend-api/conversation/message_feedback
#### Defined in #### Defined in
[types.ts:188](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L188) [types.ts:193](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L193)
___ ___
@ -167,7 +167,7 @@ ___
#### Defined in #### Defined in
[types.ts:244](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L244) [types.ts:249](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L249)
___ ___
@ -187,7 +187,7 @@ ___
#### Defined in #### Defined in
[types.ts:217](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L217) [types.ts:222](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L222)
___ ___
@ -197,7 +197,7 @@ ___
#### Defined in #### Defined in
[types.ts:215](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L215) [types.ts:220](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L220)
___ ___
@ -207,7 +207,7 @@ ___
#### Defined in #### Defined in
[types.ts:270](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L270) [types.ts:275](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L275)
___ ___
@ -225,7 +225,7 @@ ___
#### Defined in #### Defined in
[types.ts:72](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L72) [types.ts:77](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L77)
___ ___
@ -243,7 +243,7 @@ https://chat.openapi.com/backend-api/models
#### Defined in #### Defined in
[types.ts:65](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L65) [types.ts:70](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L70)
___ ___
@ -262,7 +262,7 @@ https://chat.openapi.com/backend-api/moderations
#### Defined in #### Defined in
[types.ts:92](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L92) [types.ts:97](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L97)
___ ___
@ -282,7 +282,7 @@ https://chat.openapi.com/backend-api/moderations
#### Defined in #### Defined in
[types.ts:109](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L109) [types.ts:114](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L114)
___ ___
@ -300,7 +300,7 @@ ___
#### Defined in #### Defined in
[types.ts:156](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L156) [types.ts:161](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L161)
___ ___
@ -317,7 +317,7 @@ ___
#### Defined in #### Defined in
[types.ts:173](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L173) [types.ts:178](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L178)
___ ___
@ -327,7 +327,7 @@ ___
#### Defined in #### Defined in
[types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L3) [types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L3)
___ ___
@ -342,12 +342,13 @@ https://chat.openapi.com/api/auth/session
| Name | Type | Description | | Name | Type | Description |
| :------ | :------ | :------ | | :------ | :------ | :------ |
| `accessToken` | `string` | The access token | | `accessToken` | `string` | The access token |
| `error?` | `string` \| ``null`` | If there was an error associated with this request |
| `expires` | `string` | ISO date of the expiration date of the access token | | `expires` | `string` | ISO date of the expiration date of the access token |
| `user` | [`User`](modules.md#user) | Object of the current user | | `user` | [`User`](modules.md#user) | Authenticated user |
#### Defined in #### Defined in
[types.ts:8](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L8) [types.ts:8](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L8)
___ ___
@ -369,7 +370,7 @@ ___
#### Defined in #### Defined in
[types.ts:25](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/types.ts#L25) [types.ts:30](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/types.ts#L30)
## Functions ## Functions
@ -389,4 +390,4 @@ ___
#### Defined in #### Defined in
[utils.ts:4](https://github.com/transitive-bullshit/chatgpt-api/blob/c9cef79/src/utils.ts#L4) [utils.ts:4](https://github.com/transitive-bullshit/chatgpt-api/blob/80d77a3/src/utils.ts#L4)

Wyświetl plik

@ -11,10 +11,10 @@ chatgpt / [Exports](modules.md)
[![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) [![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) - [Intro](#intro)
- [How it works](#how-it-works)
- [Install](#install) - [Install](#install)
- [Usage](#usage) - [Usage](#usage)
- [Docs](#docs) - [Docs](#docs)
- [How it works](#how-it-works)
- [Examples](#examples) - [Examples](#examples)
- [Credit](#credit) - [Credit](#credit)
- [License](#license) - [License](#license)
@ -25,34 +25,10 @@ This package is a Node.js wrapper around [ChatGPT](https://openai.com/blog/chatg
You can use it to start building projects powered by ChatGPT like chatbots, websites, etc... You can use it to start building projects powered by ChatGPT like chatbots, websites, etc...
## How it works
This package requires a valid session token from ChatGPT to access it's unofficial REST API.
To get a session token:
1. Go to https://chat.openai.com/chat and log in or sign up.
2. Open dev tools.
3. Open `Application` > `Cookies`.
![ChatGPT cookies](./media/session-token.png)
4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment.
If you want to run the built-in demo, store this value as `SESSION_TOKEN` in a local `.env` file.
> **Note**
> This package will switch to using the official API once it's released.
> **Note**
> Prior to v1.0.0, this package used a headless browser via [Playwright](https://playwright.dev/) to automate the web UI. Here are the [docs for the initial browser version](https://github.com/transitive-bullshit/chatgpt-api/tree/v0.4.2).
## Install ## Install
```bash ```bash
npm install --save chatgpt npm install chatgpt
# or
yarn add chatgpt
# or
pnpm add chatgpt
``` ```
## Usage ## Usage
@ -61,9 +37,10 @@ pnpm add chatgpt
import { ChatGPTAPI } from 'chatgpt' import { ChatGPTAPI } from 'chatgpt'
async function example() { async function example() {
// sessionToken is required; see below for details
const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN }) const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })
// ensure the API is properly authenticated (optional) // ensure the API is properly authenticated
await api.ensureAuth() await api.ensureAuth()
// send a message and wait for the response // send a message and wait for the response
@ -85,41 +62,65 @@ const api = new ChatGPTAPI({
}) })
``` ```
A full [example](./src/example.ts) is included for testing purposes: A full [demo](./src/demo.ts) is included for testing purposes:
```bash ```bash
# 1. clone repo # 1. clone repo
# 2. install node deps # 2. install node deps
# 3. set `SESSION_TOKEN` in .env # 3. set `SESSION_TOKEN` in .env
# 4. run: # 4. run:
npx tsx src/example.ts npx tsx src/demo.ts
``` ```
## Docs ## Docs
See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on methods and parameters. See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on methods and parameters.
## How it works
**This package requires a valid session token from ChatGPT to access it's unofficial REST API.**
To get a session token:
1. Go to https://chat.openai.com/chat and log in or sign up.
2. Open dev tools.
3. Open `Application` > `Cookies`.
![ChatGPT cookies](./media/session-token.png)
4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment.
If you want to run the built-in demo, store this value as `SESSION_TOKEN` in a local `.env` file.
> **Note**
> This package will switch to using the official API once it's released.
> **Note**
> Prior to v1.0.0, this package used a headless browser via [Playwright](https://playwright.dev/) to automate the web UI. Here are the [docs for the initial browser version](https://github.com/transitive-bullshit/chatgpt-api/tree/v0.4.2).
## Examples ## Examples
All of these awesome projects use the `chatgpt` package. 🤯 All of these awesome projects are built using the `chatgpt` package. 🤯
- [Twitter Bot](https://github.com/transitive-bullshit/chatgpt-twitter-bot) powered by ChatGPT ✨ - [Twitter Bot](https://github.com/transitive-bullshit/chatgpt-twitter-bot) powered by ChatGPT ✨
- Mention [@ChatGPTBot](https://twitter.com/ChatGPTBot) on Twitter with your prompt to try it out - Mention [@ChatGPTBot](https://twitter.com/ChatGPTBot) on Twitter with your prompt to try it out
- [Chrome Extension](https://github.com/gragland/chatgpt-everywhere) ([demo](https://twitter.com/gabe_ragland/status/1599466486422470656)) - [Chrome Extension](https://github.com/gragland/chatgpt-everywhere) ([demo](https://twitter.com/gabe_ragland/status/1599466486422470656))
- [VSCode Extension](https://github.com/mpociot/chatgpt-vscode) ([demo](https://twitter.com/marcelpociot/status/1599180144551526400)) - [VSCode Extension](https://github.com/mpociot/chatgpt-vscode) ([demo](https://twitter.com/marcelpociot/status/1599180144551526400))
- [Go Telegram Bot](https://github.com/m1guelpf/chatgpt-telegram) - [Go Telegram Bot](https://github.com/m1guelpf/chatgpt-telegram)
- [Github ProBot](https://github.com/oceanlvr/ChatGPTBot) - [GitHub ProBot](https://github.com/oceanlvr/ChatGPTBot)
- [Discord Bot](https://github.com/onury5506/Discord-ChatGPT-Bot)
- [WeChat Bot](https://github.com/AutumnWhj/ChatGPT-wechat-bot)
- [Lovelines.xyz](https://lovelines.xyz) - [Lovelines.xyz](https://lovelines.xyz)
- [EXM smart contracts](https://github.com/decentldotland/molecule)
If you create a cool integration, feel free to open a PR and add it to the list. If you create a cool integration, feel free to open a PR and add it to the list.
## Credit ## Credit
- Huge thanks to [@RomanHotsiy](https://github.com/RomanHotsiy), [@ElijahPepe](https://github.com/ElijahPepe), [@wong2](https://github.com/wong2), and all the other contributors 💪 - Huge thanks to [@RomanHotsiy](https://github.com/RomanHotsiy), [@ElijahPepe](https://github.com/ElijahPepe), and all the other contributors 💪
- The original browser version was inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross) - The original browser version was inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
- The original REST version was inspired by [chat-gpt-google-extension](https://github.com/wong2/chat-gpt-google-extension) by [@wong2](https://github.com/wong2)
## License ## License
MIT © [Travis Fischer](https://transitivebullsh.it) 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> If you found this project interesting, please consider supporting my open source work by [sponsoring me](https://github.com/sponsors/transitive-bullshit) or <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>

Wyświetl plik

@ -1,6 +1,6 @@
{ {
"name": "chatgpt", "name": "chatgpt",
"version": "1.2.0", "version": "1.2.2",
"description": "Node.js client for the unofficial ChatGPT API.", "description": "Node.js client for the unofficial ChatGPT API.",
"author": "Travis Fischer <travis@transitivebullsh.it>", "author": "Travis Fischer <travis@transitivebullsh.it>",
"repository": "transitive-bullshit/chatgpt-api", "repository": "transitive-bullshit/chatgpt-api",
@ -12,7 +12,8 @@
".": { ".": {
"import": "./build/index.mjs", "import": "./build/index.mjs",
"require": "./build/index.js", "require": "./build/index.js",
"types": "./build/index.d.ts" "types": "./build/index.d.ts",
"default": "./build/index.mjs"
} }
}, },
"files": [ "files": [

Wyświetl plik

@ -105,6 +105,7 @@ All of these awesome projects are built using the `chatgpt` package. 🤯
- [Go Telegram Bot](https://github.com/m1guelpf/chatgpt-telegram) - [Go Telegram Bot](https://github.com/m1guelpf/chatgpt-telegram)
- [GitHub ProBot](https://github.com/oceanlvr/ChatGPTBot) - [GitHub ProBot](https://github.com/oceanlvr/ChatGPTBot)
- [Discord Bot](https://github.com/onury5506/Discord-ChatGPT-Bot) - [Discord Bot](https://github.com/onury5506/Discord-ChatGPT-Bot)
- [WeChat Bot](https://github.com/AutumnWhj/ChatGPT-wechat-bot)
- [Lovelines.xyz](https://lovelines.xyz) - [Lovelines.xyz](https://lovelines.xyz)
- [EXM smart contracts](https://github.com/decentldotland/molecule) - [EXM smart contracts](https://github.com/decentldotland/molecule)
@ -112,8 +113,9 @@ If you create a cool integration, feel free to open a PR and add it to the list.
## Credit ## Credit
- Huge thanks to [@RomanHotsiy](https://github.com/RomanHotsiy), [@ElijahPepe](https://github.com/ElijahPepe), [@wong2](https://github.com/wong2), and all the other contributors 💪 - Huge thanks to [@RomanHotsiy](https://github.com/RomanHotsiy), [@ElijahPepe](https://github.com/ElijahPepe), and all the other contributors 💪
- The original browser version was inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross) - The original browser version was inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
- The original REST version was inspired by [chat-gpt-google-extension](https://github.com/wong2/chat-gpt-google-extension) by [@wong2](https://github.com/wong2)
## License ## License

Wyświetl plik

@ -8,6 +8,8 @@ dotenv.config()
const isCI = !!process.env.CI const isCI = !!process.env.CI
test('ChatGPTAPI invalid session token', async (t) => { test('ChatGPTAPI invalid session token', async (t) => {
t.timeout(30 * 1000) // 30 seconds
t.throws(() => new ChatGPTAPI({ sessionToken: null }), { t.throws(() => new ChatGPTAPI({ sessionToken: null }), {
message: 'ChatGPT invalid session token' message: 'ChatGPT invalid session token'
}) })
@ -51,6 +53,7 @@ test('ChatGPTAPI valid session token', async (t) => {
if (!isCI) { if (!isCI) {
test('ChatGPTAPI expired session token', async (t) => { test('ChatGPTAPI expired session token', async (t) => {
t.timeout(30 * 1000) // 30 seconds
const expiredSessionToken = process.env.TEST_EXPIRED_SESSION_TOKEN const expiredSessionToken = process.env.TEST_EXPIRED_SESSION_TOKEN
await t.throwsAsync( await t.throwsAsync(

Wyświetl plik

@ -86,11 +86,11 @@ export class ChatGPTAPI {
async sendMessage( async sendMessage(
message: string, message: string,
opts: { opts: {
converstationId?: string conversationId?: string
onProgress?: (partialResponse: string) => void onProgress?: (partialResponse: string) => void
} = {} } = {}
): Promise<string> { ): Promise<string> {
const { converstationId = uuidv4(), onProgress } = opts const { conversationId = uuidv4(), onProgress } = opts
const accessToken = await this.refreshAccessToken() const accessToken = await this.refreshAccessToken()
@ -107,7 +107,7 @@ export class ChatGPTAPI {
} }
], ],
model: 'text-davinci-002-render', model: 'text-davinci-002-render',
parent_message_id: converstationId parent_message_id: conversationId
} }
const url = `${this._backendApiBaseUrl}/conversation` const url = `${this._backendApiBaseUrl}/conversation`