Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/klich3/firebase-gpt-chat-completion-stream
Example of ChatGPT Stream implementation in Firebase with functions. For this method you have to use `fetch`.
https://github.com/klich3/firebase-gpt-chat-completion-stream
chatgpt chatgpt-api firebase firebase-function firebase-functions gpt openai openai-api
Last synced: about 1 month ago
JSON representation
Example of ChatGPT Stream implementation in Firebase with functions. For this method you have to use `fetch`.
- Host: GitHub
- URL: https://github.com/klich3/firebase-gpt-chat-completion-stream
- Owner: klich3
- License: mit
- Created: 2023-06-01T09:51:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-18T12:05:47.000Z (10 months ago)
- Last Synced: 2024-04-14T11:27:08.027Z (9 months ago)
- Topics: chatgpt, chatgpt-api, firebase, firebase-function, firebase-functions, gpt, openai, openai-api
- Language: JavaScript
- Homepage:
- Size: 91.8 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# Firebase Cloud Functions for GPT Chat Completion
![firebase+gpt](images/firebase_and_gpt.jpg?raw=true)
Example of ChatGPT ***Stream*** implementation in Firebase with functions. For this method you have to use `fetch`.
***Issue:***: This method seems to be slower than the other Stream method because it does not stream.## SETUP
In `functions` folder create file `.env` with your OpenAI Api key.
Sample:
```text
OPEN_AI_KEY="sk-"
```## SETUP WITH CUSTOM FUNCTIONS FOLDER
To use functions in a custom folder different from the default, suppose the destination folder is `functions` inside we create a new folder named `gpt-chat-completion-stream` and copy files from this project that are inside the `functions` folder. Then edit the `firebase.json` file from the root folder of your project.
```json
{
...
"firestore": {
...
},
...
"functions": [
{
"source": "functions/gpt-chat-completion-stream",
"codebase": "gpt-chat-completion-stream",
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"],
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"runtime": "nodejs16"
}
]
...
}
```## Deploy
Run `firebase deploy --only functions:gpt-chat-completion-stream`
## Sample usage in REACT
```javascript
const [responseChunk, setResponseChunk] = useState("");
let responseStream = "";const callPrompt = (e:any) => {
fetch("https://-.cloudfunctions.net/gptPromptStream", {
method: "POST",
body: JSON.stringify({
prompt: [{role: "user", content: "Hello world"}],
}),
}).then(async (response) => {
if(!response && !response.body) return;const decoder = new TextDecoder("utf-8");
const reader = response?.body?.getReader();while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}const text = decoder.decode(value);
responseStream += text;
setResponseChunk(responseStream);
}
})
.catch((err) => {
console.error(`Error occurred while attempting Fetch request: ${err}`);
});
};
...//JSX part:
<>
Call prompt
Response
{responseChunk}
>
```---
# BEWARE
⚠️
This method may no longer be functional as the LangChain package is updated daily.
If you feel like collaborating you can create Pull Request...