Coder Social home page Coder Social logo

waylaidwanderer / node-chatgpt-api Goto Github PK

View Code? Open in Web Editor NEW
4.2K 41.0 737.0 1.71 MB

A client implementation for ChatGPT and Bing AI. Available as a Node.js module, REST API server, and CLI app.

Home Page: https://www.npmjs.com/package/@waylaidwanderer/chatgpt-api

License: MIT License

JavaScript 99.86% Dockerfile 0.14%
chatgpt chatgpt-api gpt openai openai-api api api-rest api-server cli nodejs

node-chatgpt-api's People

Contributors

aiqinxuancai avatar berry-13 avatar danny-avila avatar dependabot[bot] avatar emipc avatar erisfiregamer1 avatar fuegovic avatar gengjiawen avatar hibobmaster avatar josstorer avatar kq514550 avatar leask avatar likev avatar luisotee avatar notenough1111 avatar oskardotglobal avatar other-erisfg avatar queercat avatar richard-weiss avatar ropoctl avatar ryanwy avatar songkeys avatar syedahmedhaidershah avatar timefox avatar today- avatar trinculo54 avatar waylaidwanderer avatar xuxin3101 avatar yuwangi avatar zhouxinhao1208 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-chatgpt-api's Issues

timeout for fetch

It seems there is no timeout option like timeoutMs for the function. Sometimes a request gets stuck for a long time。

commonjs

hi, how to use it with commonjs?

thx

How can i resolve "An error ocurred. Try refreshing your browser."

I follow the get start guide and use reverse proxy already. Do i miss any step in order to get this work ??

In the debug log

Request :
https://chatgpt.pawan.krd/api/completions

{
  model: 'text-chat-davinci-002-sh-alpha-aoruigiofdj83',
  temperature: 0.8,
  top_p: 1,
  presence_penalty: 1,
  stop: [ '<|im_end|>', '<|im_sep|>', '\nUser:' ],
  max_tokens: 1000,
  prompt: '\n' +
    '<|im_sep|>Instructions:\n' +
    'You are ChatGPT, a large language model trained by OpenAI.\n' +
    'Current date: February 13, 2023<|im_sep|>\n' +
    '\n' +
    'User:\n' +
    'Hello!<|im_end|>\n' +
    'ChatGPT:\n'
}

Response :

{"detail":"An error ocurred. Try refreshing your browser."}

UND_ERR_SOCKET (There was an error communicating with ChatGPT)

using reverse proxy, some questions throw an error

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14152:11)
    at async ChatGPTClient.getCompletion (file:///C:/Users/JerryWn/AppData/Local/Programs/node-v18.14.0-win-x64/node_modules/@waylaidwanderer/chatgpt-api/src/ChatGPTClient.js:138:26)
    at async ChatGPTClient.sendMessage (file:///C:/Users/JerryWn/AppData/Local/Programs/node-v18.14.0-win-x64/node_modules/@waylaidwanderer/chatgpt-api/src/ChatGPTClient.js:196:28)
    at async Object.<anonymous> (file:///C:/Users/JerryWn/AppData/Local/Programs/node-v18.14.0-win-x64/node_modules/@waylaidwanderer/chatgpt-api/bin/server.js:100:18) {
  cause: SocketError: other side closed
      at TLSSocket.onSocketEnd (node:internal/deps/undici/undici:9227:26)
      at TLSSocket.emit (node:events:525:35)
      at endReadableNT (node:internal/streams/readable:1359:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    code: 'UND_ERR_SOCKET',
    socket: {
      localAddress: '198.18.0.1',
      localPort: 58538,
      remoteAddress: '198.18.0.10',
      remotePort: 443,
      remoteFamily: 'IPv4',
      timeout: undefined,
      bytesWritten: 1764,
      bytesRead: 0
    }
  }
}

ReferenceError: fetch is not defined

ReferenceError: fetch is not defined
at file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/node_modules/@fortaine/fetch-event-source/lib/esm/fetch.js:46:85
at new Promise ()
at fetchEventSource (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/node_modules/@fortaine/fetch-event-source/lib/esm/fetch.js:18:12)
at file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/src/ChatGPTClient.js:77:27
at new Promise ()
at ChatGPTClient.getCompletion (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/src/ChatGPTClient.js:74:20)
at ChatGPTClient.sendMessage (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/src/ChatGPTClient.js:160:24)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async onMessage (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/bin/cli.js:142:26)
at async file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/bin/cli.js:83:1

why this prompt? tell it You are ChatGPT?

promptPrefix = `You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short.

text in req is null when use bing

I use your library when I call to get the answer and only get null text results

{
author: 'bot',
id: '4e9561bb-7e67-471f-b950-dc028e8729ed',
conversationId: undefined,
clientId: undefined,
conversationSignature: undefined,
invocationId: '2',
text: ''
}

Use dependabot or another method to keep dependencies up to date

There are lots of breaking changes with the API necessitating fast paced releases upstream. Some sort of automation is required to keep pace with them and keep this package usable.

Dependabot only updates once a day so is quite slow, there must be a better method, do you know of one?

Reverse Proxy Code?

Thats a great news that you have found out the way to use official models.
Could we possibly get a the implementation for the proxy? As using someone else free proxy from github will just bring capacity sooner or later. On another side having an option for where and how your data proccessed is great.

Reverse proxy seems down

The reverse proxy servers worked great, but as of just now both of the two reverse proxy servers are down now, which seems inevitable given how many requests they would handle.
This happened when using cli, 2 hours ago it was working fine.

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14152:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async create (file:///root/node-chatgpt-api/node_modules/@waylaidwanderer/fetch-event-source/lib/esm/fetch.js:52:34) {
  cause: Error: connect EHOSTUNREACH 2a09:8280:1::3:e03e:443
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
    errno: -113,
    code: 'EHOSTUNREACH',
    syscall: 'connect',
    address: '2a09:8280:1::3:e03e',
    port: 443
  }

TypeError: stream.getReader is not a function

`newuser@work % npm run cli

@waylaidwanderer/[email protected] cli
node bin/cli.js

╔═══════════════╗
║ ChatGPT CLI ║
╚═══════════════╝

Type "!" to access the command menu.
? Write a message: hi
TypeError: stream.getReader is not a function
at getBytes (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/node_modules/@fortaine/fetch-event-source/lib/esm/parse.js:2:27)
at create (file:///Users/newuser/Documents/chatgpt/node-chatgpt-api/node_modules/@fortaine/fetch-event-source/lib/esm/fetch.js:54:23)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Type "!" to access the command menu.
? Write a message: `

this is a bug? or my setting is error?

export default { // Your OpenAI API key openaiApiKey: 'apikey' || '', chatGptClient: { // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions modelOptions: { // The model is set to text-chat-davinci-002-20221122 by default, but you can override // it and any other parameters here //model: 'text-chat-davinci-002-20221122', model: 'text-davinci-003', }, // (Optional) Set custom instructions instead of "You are ChatGPT...". // promptPrefix: 'You are Bob, a cowboy in Western times...', // (Optional) Set a custom name for the user // userLabel: 'User', // (Optional) Set a custom name for ChatGPT // chatGptLabel: 'ChatGPT', // (Optional) Set to true to enable console.debug()logging debug: false, }, // Options for the Keyv cache, see https://www.npmjs.com/package/keyv. // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default) cacheOptions: {}, // Options for the API server apiOptions: { port: process.env.API_PORT || 3000, host: process.env.API_HOST || 'localhost', // (Optional) Set to true to enableconsole.debug()logging debug: false, }, // If set, ChatGPTClient will usekeyv-fileto store conversations to this JSON file instead of in memory. // However,cacheOptions.store will override this if set storageFilePath: process.env.STORAGE_FILE_PATH || './cache.json', }

My account has been removed from the waiting list, but running `use-bing-client.js` returns the following:

My account has been removed from the waiting list, but running use-bing-client.js returns the following:

node .\use-bing-client.js

performing handshake

handshake established


{"arguments":[{"source":"cib","optionsSets":["nlu_direct_response_filter","deepleo","enable_debug_commands","disable_emoji_spoken_text","responsible_ai_policy_235","enablemm"],"isStartOfSession":true,"message":{"author":"user","inputMethod":"Keyboard","text":"Write a short poem about cats","messageType":"Chat"},"participant":{}}],"invocationId":"0","target":"chat","type":4}
2. --------------------------
[{"type":2,"invocationId":"0","item":{"firstNewMessageIndex":null,"suggestedResponses":null,"conversationId":"7104db73-d7ed-4428-baa3-3ab8f406e816","requestId":"4c647392-28de-4bcb-b0ff-c9a30be92cca","telemetry":{"metrics":null,"startTime":"2023-02-12T04:07:52.1888796Z"},"result":{"value":"UnauthorizedRequest","message":"The conversationSignature is not set and is required to ensure that you have permission to use our APIs. IsAuthenticated=False. App ID=","error":"UnauthorizedRequest","renewCert":true,"serviceVersion":"20230210.12"}}},{"type":3,"invocationId":"0"}]

err

Error: Premature close
at TLSSocket.onSocketClose (file:///***/node_modules/@waylaidwanderer/chatgpt-api/node_modules/node-fetch/src/index.js:389:19)
at TLSSocket.emit (node:events:525:35)
at node:net:301:12
at TCP.done (node:_tls_wrap:588:7) {
code: 'ERR_STREAM_PREMATURE_CLOSE'
}

TypeError: fetch failed

TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11118:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async create (file:///app/node_modules/@waylaidwanderer/fetch-event-source/lib/esm/fetch.js:52:34) {
cause: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}
}

ConversationId is not being generated for me

Request
{ "message": "I need a python script to check for balanced parenthesis in a string", "conversationId": "your-conversation-id (optional)", "parentMessageId": "your-parent-message-id (optional)" }

Response
{ "response": "```\ndef is_balanced(string):\n stack = []\n for char in string:\n if char in ('(', '{', '['):\n stack.append(char)\n elif char in (')', '}', ']'):\n if not stack:\n return False\n else:\n top = stack.pop()\n if top == '(' and char != ')':\n return False\n elif top == '{' and char != '}':\n return False\n elif top == '[' and char != ']':\n return False\n return not stack\n```", "conversationId": "your-conversation-id (optional)", "messageId": "0bfcc43d-c4a7-4822-9692-26b44b4de193" }

Improvement suggestions

Questions

How stable is this for you in general? How many accts are you typically using and how many reqs / hour?

  • can you DM me on discord what you're using for residential IP proxy? just curious

Feedback

  • it'd be nice to have the pool functionality available as a module usable outside of this server (stretch goal; not necessary; just a thought)
  • right now, the way you're initializing accts, it's hard to know when they're available after one or more initSession calls succeeds
  • checking the response for special response strings is important for robustness
  • maybe we could just port https://github.com/transitive-bullshit/chatgpt-twitter-bot/blob/main/src/chatgpt-api-pool.ts? I know you asked me to do this awhile ago, but I never got around to it..
  • maybe add support for 2captcha as well as nopecha
  • it looks you're periodically running refreshSession but never resetSession. does this work for you consistently in practice? ChatGPTAPIBrowser.sendMessage has some error handling & retry logic, but over time it's gotten pretty messy btwn the logic I have in there vs in the ChatGPTAPIPool in my twitter bot. Lots of stuff is duplicated, and it's kind of a mess.
  • maybe add an example curl or httpie example to the readme to show how you're invoking the local API? something people can copy & paste
  • conversation ID persistence will be important

Comments

  • I really like the ability to use a proxy per acct

Sorry for the long feedback. I just want to make sure if I'm going to prominently direct people to this project from the chatgpt-api repo, that it encapsulates the robustness learnings I've come across from running the twitter bot.

The more feedback I write, the more I think this might not be useful feedback. I dunno; just stream of consciousness.

Cheers && thanks! 🙏 💕 🙏

"That model is currently overloaded with other requests"

'That model is currently overloaded with other requests. You can retry your request, or contact us through our help center at help.openai.com if the error persists. (Please include the request ID de79efb84259b34394b36c1f27bb548c in your message.)',
type: 'server_error'

Authorization to access BingAIClient

Hello,
I would like to get authorization to access and use the BingAIClient since I got the necessary "-U" cookie.

Could you explain to me how I can get authorization to access the project?

Message:
"result":{"value":"UnauthorizedRequest","message":"The conversationSignature is not set and is required to ensure that you have permission to use our APIs. IsAuthenticated=False. App ID=","error": "UnauthorizedRequest"

thanks in advance for your help.

That model is currently overloaded with other requests.

when i test it ,it show that :

Error: Failed to send message. HTTP 503 - {
  "error": {
    "message": "That model is currently overloaded with other requests. You can retry your request, or contact us through our help center at help.openai.com if the error persists. (Ple
ase include the request ID 169bee8d  in your message.)",
    "type": "server_error",
    "param": null,
    "code": null
  }
}

How can I get "_U" cookie from bing.com?

Hi, sorry if this is a simple question.

I have a JS code to get "_U" cookie from a page. But I don't know how to use it on bing.com.

The blow is my funciton.

function getCookie() {
  const cookieName = "_U";
  const cookies = document.cookie.split(';');
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].trim();
    if (cookie.startsWith(cookieName + '=')) {
      return decodeURIComponent(cookie.substring(cookieName.length + 1));
    }
  }
  return null;
}

That model does not exist'

status: 404,
json: {
error: {
message: 'That model does not exist',
type: 'invalid_request_error',
param: null,
code: null
}

That model does not exist

message: 'That model does not exist',
type: 'invalid_request_error',
param: null,
code: null

Hello. Looks like the model has been removed.

more than one account

I use 3 accounts

But now I have a problem when I start a conversation and I get a conversationId and I want to use it, I get an error "Conversation not found" because the system created the conversation with Accoount1 and is now using Account 3

what is the best way to solve this?

bing

what are the limits in bing?

getaddrinfo ENOTFOUND api.openai.com

nodejs 16.18.0
after set the apikey
npm install
npm start
when post to /conversation , the server echo en error

FetchError: request to https://api.openai.com/v1/completions failed, reason: getaddrinfo ENOTFOUND api.openai.com
at ClientRequest. (file:///D:/codes/node-chatgpt-api-1.15.0/node_modules/node-fetch/src/index.js:108:11)
at ClientRequest.emit (node:events:525:35)
at TLSSocket.socketErrorListener (node:_http_client:494:9)
at TLSSocket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
erroredSysCall: 'getaddrinfo'
}

404 Not Found

file:///C:/Users/Daniel/Documents/2023/OpenAi/node_modules/@waylaidwanderer/chatgpt-api/src/ChatGPTClient.js:47
const error = new Error(Failed to send message. HTTP ${response.status} - ${body});
^

Error: Failed to send message. HTTP 404 - {
"error": {
"message": "That model does not exist",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

at ChatGPTClient.getCompletion (file:///C:/Users/Daniel/Documents/2023/OpenAi/node_modules/@waylaidwanderer/chatgpt-api/src/ChatGPTClient.js:47:27)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ChatGPTClient.sendMessage (file:///C:/Users/Daniel/Documents/2023/OpenAi/node_modules/@waylaidwanderer/chatgpt-api/src/ChatGPTClient.js:83:24)
at async promptCompletion (file:///C:/Users/Daniel/Documents/2023/OpenAi/src/controllers/openAi/open.js:12:24) {

status: 404,
json: {
error: {
message: 'That model does not exist',
type: 'invalid_request_error',
param: null,
code: null
}
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.