kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
docs: improve readme for new version
rodzic
eb11acd263
commit
c361da937f
Plik binarny nie jest wyświetlany.
Przed Szerokość: | Wysokość: | Rozmiar: 448 KiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 207 KiB |
30
readme.md
30
readme.md
|
@ -21,21 +21,23 @@ You can use it to start building projects powered by ChatGPT like chatbots, webs
|
||||||
|
|
||||||
## How it works
|
## How it works
|
||||||
|
|
||||||
This package requires a valid session token from OpenAI's ChatGPT webapp to access it's unofficial REST API.
|
This package requires a valid session token from ChatGPT to access it's unofficial REST API.
|
||||||
|
|
||||||
1. Go to https://chat.openai.com/chat and log in or sign up
|
To get a session token:
|
||||||
2. Open the dev tools console
|
|
||||||
3. Open `Application` > `Cookies`
|
1. Go to https://chat.openai.com/chat and log in or sign up.
|
||||||

|
2. Open dev tools.
|
||||||
|
3. Open `Application` > `Cookies`.
|
||||||
|

|
||||||
4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment.
|
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,
|
If you want to run the built-in demo, store this value as `SESSION_TOKEN` in a local `.env` file.
|
||||||
|
|
||||||
> **Note**
|
> **Note**
|
||||||
> This package will switch to using the official API once it's released.
|
> This package will switch to using the official API once it's released.
|
||||||
|
|
||||||
> **Note**
|
> **Note**
|
||||||
> Prior to v1.0.0, this package used headless Chromium 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).
|
> 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
|
||||||
|
|
||||||
|
@ -53,7 +55,7 @@ pnpm add chatgpt
|
||||||
import { ChatGPTAPI } from 'chatgpt'
|
import { ChatGPTAPI } from 'chatgpt'
|
||||||
|
|
||||||
async function example() {
|
async function example() {
|
||||||
const api = new ChatGPTAPI()
|
const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })
|
||||||
|
|
||||||
// ensure the API is properly authenticated (optional)
|
// ensure the API is properly authenticated (optional)
|
||||||
await api.ensureAuth()
|
await api.ensureAuth()
|
||||||
|
@ -68,6 +70,15 @@ async function example() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
By default, the response will be formatted as markdown. If you want to work with plaintext only, you can use:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const api = new ChatGPTAPI({
|
||||||
|
sessionToken: process.env.SESSION_TOKEN,
|
||||||
|
markdown: false
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
A full [example](./src/example.ts) is included for testing purposes:
|
A full [example](./src/example.ts) is included for testing purposes:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -98,7 +109,8 @@ If you create a cool integration, feel free to open a PR and add it to the list.
|
||||||
|
|
||||||
## Credit
|
## Credit
|
||||||
|
|
||||||
- Inspired by this [Go module](https://github.com/danielgross/whatsapp-gpt) by [Daniel Gross](https://github.com/danielgross)
|
- Huge thanks to [@RomanHotsiy](https://github.com/RomanHotsiy), [@ElijahPepe](https://github.com/ElijahPepe), [@wong2](https://github.com/wong2), 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)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@ export class ChatGPTAPI {
|
||||||
protected _apiBaseUrl: string
|
protected _apiBaseUrl: string
|
||||||
protected _backendApiBaseUrl: string
|
protected _backendApiBaseUrl: string
|
||||||
protected _userAgent: string
|
protected _userAgent: string
|
||||||
|
|
||||||
|
// stores access tokens for up to 10 seconds before needing to refresh
|
||||||
protected _accessTokenCache = new ExpiryMap<string, string>(10 * 1000)
|
protected _accessTokenCache = new ExpiryMap<string, string>(10 * 1000)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,7 +135,7 @@ export class ChatGPTAPI {
|
||||||
try {
|
try {
|
||||||
const parsedData: types.ConversationResponseEvent = JSON.parse(data)
|
const parsedData: types.ConversationResponseEvent = JSON.parse(data)
|
||||||
const message = parsedData.message
|
const message = parsedData.message
|
||||||
console.log('event', JSON.stringify(parsedData, null, 2))
|
// console.log('event', JSON.stringify(parsedData, null, 2))
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
let text = message?.content?.parts?.[0]
|
let text = message?.content?.parts?.[0]
|
||||||
|
@ -144,11 +146,11 @@ export class ChatGPTAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
response = text
|
response = text
|
||||||
|
// fullResponse = message
|
||||||
|
|
||||||
if (onProgress) {
|
if (onProgress) {
|
||||||
onProgress(text)
|
onProgress(text)
|
||||||
}
|
}
|
||||||
// fullResponse = message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Ładowanie…
Reference in New Issue