diff --git a/app/chatgpt.js b/app/chatgpt.js
index 48f7e7165c..c7eb1a926f 100644
--- a/app/chatgpt.js
+++ b/app/chatgpt.js
@@ -1,11 +1,23 @@
require('dotenv').config();
+const Keyv = require('keyv');
+const messageStore = new Keyv(process.env.MONGODB_URI, { namespace: 'chatgpt' });
-const ask = async (question) => {
+const ask = async (question, progressCallback, convo) => {
const { ChatGPTAPI } = await import('chatgpt');
- const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_KEY });
- const res = await api.sendMessage(question, {
- onProgress: (partialRes) => console.log(partialRes.text)
- });
+ const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_KEY, messageStore });
+ let options = {
+ onProgress: (partialRes) => {
+ if (partialRes.text.length > 0) {
+ progressCallback(partialRes);
+ }
+ }
+ };
+
+ if (!!convo.parentMessageId && !!convo.conversationId) {
+ options = { ...options, ...convo };
+ }
+
+ const res = await api.sendMessage(question, options);
return res;
};
diff --git a/index.html b/index.html
deleted file mode 100644
index 71e55d189c..0000000000
--- a/index.html
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
+ return (
+
+ {messages.map((message, i) => (
+
+ ))}
+
+
+ );
+}
diff --git a/src/components/TextChat.jsx b/src/components/TextChat.jsx
index b81aa2d5fb..d8acc9558f 100644
--- a/src/components/TextChat.jsx
+++ b/src/components/TextChat.jsx
@@ -1,18 +1,33 @@
import React, { useState } from 'react';
import { SSE } from '../../app/sse';
-const handleSubmit = (payload) => {
+const handleSubmit = (text, messageHandler, convo, convoHandler) => {
+ let payload = { text };
+ if (convo.conversationId && convo.parentMessageId) {
+ payload = {
+ ...payload,
+ conversationId: convo.conversationId,
+ parentMessageId: convo.parentMessageId
+ };
+ }
+
const events = new SSE('http://localhost:3050/ask', {
- payload: JSON.stringify({ text: payload }),
+ payload: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' }
});
- console.log('we in handleSubmit');
+
events.onopen = function () {
console.log('connection is opened');
};
events.onmessage = function (e) {
- console.log(e);
+ const data = JSON.parse(e.data);
+ if (!!data.message) {
+ messageHandler(data.text.replace(/^\n/, ''));
+ } else {
+ console.log(data);
+ convoHandler(data);
+ }
};
events.onerror = function (e) {
@@ -23,8 +38,13 @@ const handleSubmit = (payload) => {
events.stream();
};
-export default function TextChat() {
+export default function TextChat({ messages, setMessages, conversation = null }) {
const [text, setText] = useState('');
+ const [convo, setConvo] = useState({ conversationId: null, parentMessageId: null });
+
+ if (!!conversation) {
+ setConvo(conversation);
+ }
const handleKeyPress = (e) => {
if (e.key === 'Enter' && e.shiftKey) {
@@ -32,8 +52,21 @@ export default function TextChat() {
}
if (e.key === 'Enter' && !e.shiftKey) {
- console.log('Submit Enter');
- handleSubmit(text);
+ const payload = text.trim();
+ const currentMsg = { sender: 'user', text: payload, current: true };
+ setMessages([...messages, currentMsg]);
+ setText('');
+ const messageHandler = (data) => {
+ setMessages([...messages, currentMsg, { sender: 'GPT', text: data }]);
+ };
+ const convoHandler = (data) => {
+ if (convo.conversationId === null && convo.parentMessageId === null) {
+ const { conversationId, parentMessageId } = data;
+ setConvo({ conversationId, parentMessageId: data.id });
+ }
+ };
+ console.log('User Input:', payload);
+ handleSubmit(payload, messageHandler, convo, convoHandler);
}
};
@@ -41,6 +74,7 @@ export default function TextChat() {
<>