-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
144 lines (118 loc) · 4.71 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { useUser } from '@auth0/nextjs-auth0'
import Layout from '../components/layout'
import { useEffect, useState } from 'react'
import { OpenAIService } from '../client-services/open-ai-service'
import ReactMarkdown from 'react-markdown';
import { IDialog, IMessage } from '../lib/models/dialog';
const Home = () => {
const { user, isLoading } = useUser()
const [dialogs, setDialogs] = useState<IDialog[]>([]);
const temperature = 0;
useEffect(() => {
const fetchDialogs = async () => {
if (!user) return;
const dialogs = await fetch('/api/dialogs').then(r => r.json()) as IDialog[];
if (dialogs.find(d => d.messages.length === 0)) {
setDialogs(dialogs);
} else {
const newDialog: IDialog = { messages: [], temperature: 0, model: 'gpt-3.5-turbo', owner: user.email };
const allDialogs = [newDialog, ...dialogs];
setDialogs(allDialogs);
}
};
fetchDialogs();
}, [user]);
async function handleKeyDown(event: React.KeyboardEvent<HTMLTextAreaElement>, dialog: IDialog) {
if (event.key === 'Enter' && (event.shiftKey || event.ctrlKey)) {
event.preventDefault();
const postDialog = async (newDialog: IDialog) => {
const response = await fetch('/api/dialogs', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newDialog),
});
return response.json();
};
const putDialog = async (oldDialog: IDialog) => {
await fetch('/api/dialogs', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(oldDialog),
});
};
const newMessage: IMessage = { text: event.currentTarget.value, sender: 'user' };
dialog.messages.push(newMessage);
if (dialog._id) {
await putDialog(dialog);
} else {
const newDialog = await postDialog(dialog) as IDialog;
dialog._id = newDialog._id;
}
const botMessage: IMessage = { text: '', sender: 'assistant' };
dialog.messages.push(botMessage);
await putDialog(dialog);
const gptMessages = dialog.messages.map(m => {
return { role: m.sender, content: m.text };
});
const openAIService = new OpenAIService();
const textareaElement = event.target as HTMLTextAreaElement;
textareaElement.value = '';
const server = true;
if (server) {
openAIService.getCompletionStreamFromServer(gptMessages, (s: string) => {
botMessage.text += s;
setDialogs([...dialogs]);
putDialog(dialog);
});
} else {
openAIService.getCompletionStream(gptMessages, temperature, (s: string) => {
botMessage.text += s;
setDialogs([...dialogs]);
});
}
}
}
return (
<Layout user={user} loading={isLoading}>
<h1>BetterGPT</h1>
{isLoading && <p>Loading login info...</p>}
{!isLoading && !user && (
<p>
Please <a href="/api/auth/login">log in</a>
</p>
)}
{user && (
<>
<h2>Dialogs:</h2>
<div className="accordion" id="accordionExample">
{dialogs.map((dialog, i) => (
<div className="accordion-item" key={i}>
<h2 className="accordion-header" id={`heading${i}`}>
<button className="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target={`#collapse${i}`} aria-expanded="true" aria-controls={`collapse${i}`}>
{dialog.messages.length > 0 ? `${dialog.messages[0].sender}: ${dialog.messages[0].text}` : 'New Dialog'}
</button>
</h2>
<div id={`collapse${i}`} className="accordion-collapse collapse" aria-labelledby={`heading${i}`} data-bs-parent="#accordionExample">
<div className="accordion-body">
<ul className='list-group'>
{dialog.messages.map((message, j) => (
<li className='list-group-item' key={j}><div className='row'><div className='col-2 text-uppercase fw-bold'>{message.sender}:</div><div className='col-10'><ReactMarkdown>{message.text}</ReactMarkdown></div></div></li>
))}
</ul>
<textarea key={`textarea${i}`} placeholder="Press Ctrl+Enter or Shift+Enter to send." id="question" name="question" rows={3} cols={50} onKeyDown={e => handleKeyDown(e, dialog)} className='form-control mt-1' />
</div>
</div>
</div>
))}
</div>
</>
)}
</Layout>
)
}
// fast/cached SSR page
export default Home