Created
December 1, 2023 17:15
-
-
Save youtube-jocoding/6202226985e704262e5b1ea8f31f712a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Chat UI Screen</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==" crossorigin="anonymous" referrerpolicy="no-referrer" /> | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
font-family: Arial, sans-serif; | |
font-size: 14px; | |
} | |
.chat-container { | |
max-width: 500px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
.chat-box { | |
background-color: #f2f2f2; | |
padding: 10px; | |
border-radius: 10px; | |
margin-bottom: 20px; | |
overflow-y: scroll; | |
height: 300px; | |
} | |
.chat-message { | |
background-color: #fff; | |
padding: 10px; | |
border-radius: 10px; | |
margin-bottom: 10px; | |
} | |
.chat-message p { | |
margin: 0; | |
padding: 0; | |
} | |
.chat-input { | |
display: flex; | |
margin-top: 20px; | |
} | |
.chat-input input { | |
flex: 1; | |
padding: 10px; | |
border: none; | |
border-radius: 5px; | |
margin-right: 10px; | |
} | |
.chat-input button { | |
background-color: #4CAF50; | |
color: #fff; | |
border: none; | |
padding: 10px; | |
border-radius: 5px; | |
cursor: pointer; | |
} | |
.chat-input button:hover { | |
background-color: #3e8e41; | |
} | |
.assistant { | |
color: blue; | |
} | |
.intro-container { | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
align-items: center; | |
} | |
.intro-container img { | |
width: 50%; | |
min-width: 300px; | |
} | |
#loader { | |
font-size: 25px; | |
text-align: center; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="intro" class="intro-container"> | |
<h1>운세를 알려드립니다.</h1> | |
<img src="doge.png" alt="chatdoge"> | |
<label for="date">생년월일</label> | |
<input id="date" type="date"> | |
<label for="hour">태어난 시간</label> | |
<select id="hour" name="hour"> | |
<option value="">모름</option> | |
<option value="00">00</option> | |
<option value="01">01</option> | |
<option value="02">02</option> | |
<option value="03">03</option> | |
<option value="04">04</option> | |
<option value="05">05</option> | |
<option value="06">06</option> | |
<option value="07">07</option> | |
<option value="08">08</option> | |
<option value="09">09</option> | |
<option value="10">10</option> | |
<option value="11">11</option> | |
<option value="12">12</option> | |
<option value="13">13</option> | |
<option value="14">14</option> | |
<option value="15">15</option> | |
<option value="16">16</option> | |
<option value="17">17</option> | |
<option value="18">18</option> | |
<option value="19">19</option> | |
<option value="20">20</option> | |
<option value="21">21</option> | |
<option value="22">22</option> | |
<option value="23">23</option> | |
</select> | |
<button onclick="start()">오늘의 운세보기</button> | |
</div> | |
<div id="chat" class="chat-container" style="display: none;"> | |
<div class="chat-box"> | |
<div class="chat-message"> | |
<p class="assistant">운세에 대해서 물어봐 주세요!</p> | |
</div> | |
</div> | |
<div id="loader" class="loader" style="display: none;"> | |
<i class="fa fa-spinner fa-spin"></i> | |
</div> | |
<div class="chat-input"> | |
<input type="text" placeholder="Type your message here..."> | |
<button id="btn" onclick="spinner()">Send</button> | |
</div> | |
</div> | |
<script> | |
const chatBox = document.querySelector('.chat-box'); | |
let userMessages = []; | |
let assistantMessages = []; | |
let threadId = '' | |
let myDateTime = '' | |
function spinner() { | |
document.getElementById('loader').style.display = "block"; | |
} | |
function start() { | |
const date = document.getElementById('date').value; | |
const hour = document.getElementById('hour').value; | |
if (date === '') { | |
alert('생년월일을 입력해주세요.'); | |
return; | |
} | |
myDateTime = date + hour; | |
document.getElementById("intro").style.display = "none"; | |
document.getElementById("chat").style.display = "block"; | |
} | |
const sendMessage = async () => { | |
const chatInput = document.querySelector('.chat-input input'); | |
const chatMessage = document.createElement('div'); | |
chatMessage.classList.add('chat-message'); | |
chatMessage.innerHTML = ` | |
<p>${chatInput.value}</p> | |
`; | |
chatBox.appendChild(chatMessage); | |
//userMessage 메세지 추가 | |
userMessage = chatInput.value; | |
chatInput.value = ''; | |
const response = await fetch('http://localhost:3000/fortuneTell', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
myDateTime: myDateTime, | |
userMessage: userMessage, | |
threadId: threadId | |
}) | |
}); | |
const data = await response.json(); | |
document.getElementById('loader').style.display = "none"; | |
//assistantMessage 메세지 추가 | |
assistantMessages.push(data.assistant); | |
threadId = data.threadId; | |
const astrologerMessage = document.createElement('div'); | |
astrologerMessage.classList.add('chat-message'); | |
astrologerMessage.innerHTML = ` | |
<p class='assistant'>${data.assistant}</p> | |
`; | |
chatBox.appendChild(astrologerMessage); | |
}; | |
document.querySelector('.chat-input button').addEventListener('click', sendMessage); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const apiKey = "sk-" | |
const OpenAI = require('openai'); | |
const express = require('express') | |
var cors = require('cors') | |
const app = express() | |
const openai = new OpenAI({ | |
apiKey: apiKey, // defaults to process.env["OPENAI_API_KEY"] | |
}); | |
app.use(cors()); | |
//POST 요청 받을 수 있게 만듬 | |
app.use(express.json()) // for parsing application/json | |
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded | |
// POST method route | |
app.post('/fortuneTell', async function (req, res) { | |
let { myDateTime, userMessage, threadId } = req.body | |
let todayDateTime = new Date().toLocaleString('ko-KR', { timeZone: 'Asia/Seoul' }); | |
const assistantId = "asst_ifKwqJal24vz6RZFmiKcStkx" | |
if (threadId == '') { | |
const emptyThread = await openai.beta.threads.create(); | |
threadId = emptyThread.id; | |
await openai.beta.threads.messages.create( | |
threadId, | |
{role: "user", content: `저의 생년월일과 태어난 시간은 ${myDateTime}입니다. 오늘은 ${todayDateTime}입니다.`} | |
); | |
} | |
await openai.beta.threads.messages.create( | |
threadId, | |
{ role: "user", content: userMessage } | |
); | |
let run = await openai.beta.threads.runs.create( | |
threadId, | |
{ assistant_id: assistantId } | |
); | |
while (run.status != "completed") { | |
run = await openai.beta.threads.runs.retrieve( | |
threadId, | |
run.id | |
); | |
//time.sleep(0.5) | |
await new Promise((resolve) => setTimeout(resolve, 500)); | |
} | |
const threadMessages = await openai.beta.threads.messages.list(threadId); | |
assistantLastMsg = threadMessages.data[0].content[0].text.value | |
res.json({"assistant": assistantLastMsg, "threadId": threadId}); | |
}); | |
app.listen(3000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment