{"id":14959674,"url":"https://github.com/jddev273/streamed-chatgpt-api","last_synced_at":"2025-05-02T12:31:56.746Z","repository":{"id":146346702,"uuid":"617878712","full_name":"jddev273/streamed-chatgpt-api","owner":"jddev273","description":"A node module for streaming API responses from the ChatGPT APi.","archived":false,"fork":false,"pushed_at":"2024-02-17T01:17:26.000Z","size":90,"stargazers_count":31,"open_issues_count":7,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T02:06:47.822Z","etag":null,"topics":["chat","chatgpt","chatgptapi","openai","streaming"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jddev273.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-23T09:55:57.000Z","updated_at":"2025-01-13T20:17:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"e425fa89-2336-4754-bf84-21fd2d8c4a57","html_url":"https://github.com/jddev273/streamed-chatgpt-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jddev273%2Fstreamed-chatgpt-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jddev273%2Fstreamed-chatgpt-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jddev273%2Fstreamed-chatgpt-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jddev273%2Fstreamed-chatgpt-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jddev273","download_url":"https://codeload.github.com/jddev273/streamed-chatgpt-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252038222,"owners_count":21684655,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chat","chatgpt","chatgptapi","openai","streaming"],"created_at":"2024-09-24T13:20:34.718Z","updated_at":"2025-05-02T12:31:55.379Z","avatar_url":"https://github.com/jddev273.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Streamed ChatGPT API With Node\n\nA Node.js module for streaming ChatGPT API responses using the OpenAI API. Streamed ChatGPT API allows users to fetch AI-generated responses in real-time. This module was created due to issues with other modules that were causing freezing, which provided a poor user experience. Various timeouts have been implemented to automatically retry and throw errors in case of disconnections.\n\n## Introduction\n\nChatGPT is an advanced AI language model developed by OpenAI. This module enables you to interact with the ChatGPT API, allowing you to send messages and receive AI-generated responses in real-time. The OpenAI API provides access to various models, including the gpt-3.5-turbo model, which is used by default in this module.\n\n## Usage Example\n\nA simple node web app showing usage of the module with streamed chat can be found here: [Streamed ChatGPT API Usage Example](https://github.com/jddev273/simple-chatgpt-chat-streaming-demo)\n\n## Installation\n\nInstall using npm:\n\n```\nnpm install streamed-chatgpt-api\n```\n\n## Usage\n\nTo use the module, first import it:\n\n```js\nconst { fetchStreamedChat, fetchStreamedChatContent } = require('streamed-chatgpt-api');\n```\n\nThen call the `fetchStreamedChat` function with your options and a callback function to process the streamed response.  This is the simplest example, you can pass in an OpenAI API key and a single string as a prompt:\n\n```js\nconst apiKey = 'your_api_key_here';\n\nfetchStreamedChat({\n    apiKey,\n    messageInput: 'Hello, how are you?',\n}, (responseChunk) =\u003e {\n    // get the actual content from the JSON\n    const content = JSON.parse(responseChunk).choices[0].delta.content;\n    if (content) {\n        process.stdout.write(content);\n    }\n});\n```\n\nYou can also pass in an an array as shown in the OpenAI API documentation, where you can define the System prompt, and on going conversation.  Here's a simple example:\n\n```js\nconst apiKey = 'your_api_key_here';\n\nconst messages = [{ role: 'system', content: '' }, { role: 'user', content: 'capital of canada' }];\n\nfetchStreamedChat({\n    apiKey,\n    messageInput: messages,\n    fetchTimeout: 10000,\n    }, (responseChunk) =\u003e {\n    // get the actual content from the JSON\n    const content = JSON.parse(responseChunk).choices[0].delta.content;\n    if (content) {\n        process.stdout.write(content);\n    }\n});\n```\n\n### Using fetchStreamedChatContent\n\nThe `fetchStreamedChatContent` function is a higher-level function that simplifies the process of fetching the generated content by not requiring you to deal with individual chunks. It takes the same options as `fetchStreamedChat` but also accepts three optional callback functions, `onResponse`, `onFinish`, and `onError`.\n\n```js\nconst apiKey = 'your_api_key_here';\n\nfetchStreamedChatContent({\n    apiKey,\n    messageInput: 'Hello, how are you?',\n}, (content) =\u003e {\n    // onResponse\n    process.stdout.write(content);\n}, () =\u003e {\n    // onFinish\n    console.log('Chat completed');\n}, (error) =\u003e {\n    // onError\n    console.error('Error:', error);\n});\n\n```\n\nYou can specify the same options as specified in OpenAI's chat completion reference.\n\nHere is a table of the parameters below.  Only the API key and messageInput (prompt) are required.  By default the gpt-3.5-turbo is used.\n\n### Options\n\nThe following options are available for the `fetchStreamedChat` and `fetchStreamedChatContent` functions:\n\n| Option | Type | Default | Description |\n| ------ | ---- | ------- | ----------- |\n| apiKey | string | - | Your OpenAI API key. |\n| messageInput | string or array of objects | - | The input message or messages to generate a chat response for. |\n| apiUrl | string | \"https://api.openai.com/v1/chat/completions\" | The OpenAI API URL to use. |\n| model | string | \"gpt-3.5-turbo\" | The OpenAI model to use. |\n| temperature | number | - | The softmax temperature to use. |\n| topP | number | - | The top_p value to use. |\n| n | number | - | The number of responses to generate. |\n| stop | string or array of strings | - | The sequence or sequences to stop generation at. |\n| maxTokens | number | - | The maximum number of tokens to generate. |\n| presencePenalty | number | - | The presence penalty value to use. |\n| frequencyPenalty | number | - | The frequency penalty value to use. |\n| logitBias | object | - | The logit bias object to use. |\n| user | string | - | The user ID to use for the chat session. |\n| retryCount | number | 3 | The number of times to retry if the chat response fetch fails. |\n| fetchTimeout | number | 20000 | The timeout value for the fetch request. |\n| readTimeout | number | 10000 | The timeout value for reading the response stream. |\n| retryInterval | number | 2000 | The interval between retries. |\n| totalTime | number | 300000 | The total time to allow for the chat response fetch. |\n\n## License\n\nMIT\n\n## Author\n\nCreated by Johann Dowa\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjddev273%2Fstreamed-chatgpt-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjddev273%2Fstreamed-chatgpt-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjddev273%2Fstreamed-chatgpt-api/lists"}