Last active
March 29, 2023 10:05
-
-
Save youtube-jocoding/3ab8267b03764a8e7004405608f63880 to your computer and use it in GitHub Desktop.
This file contains 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> | |
<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; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="chat-container"> | |
<div class="chat-box"> | |
<div class="chat-message"> | |
<p class="assistant">운세에 대해서 물어봐 주세요!</p> | |
</div> | |
</div> | |
<div class="chat-input"> | |
<input type="text" placeholder="Type your message here..."> | |
<button>Send</button> | |
</div> | |
</div> | |
<script> | |
const chatBox = document.querySelector('.chat-box'); | |
let userMessages = []; | |
let assistantMessages = []; | |
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 메세지 추가 | |
userMessages.push(chatInput.value); | |
chatInput.value = ''; | |
const response = await fetch('http://localhost:3000/fortuneTell', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
userMessages: userMessages, | |
assistantMessages: assistantMessages, | |
}) | |
}); | |
const data = await response.json(); | |
//assistantMessage 메세지 추가 | |
assistantMessages.push(data.assistant); | |
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 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 = "" | |
const { Configuration, OpenAIApi } = require("openai"); | |
const express = require('express') | |
var cors = require('cors') | |
const app = express() | |
const configuration = new Configuration({ | |
apiKey: apiKey, | |
}); | |
const openai = new OpenAIApi(configuration); | |
//CORS 이슈 해결 | |
// let corsOptions = { | |
// origin: 'https://www.domain.com', | |
// credentials: true | |
// } | |
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 { userMessages, assistantMessages} = req.body | |
let messages = [ | |
{role: "system", content: "당신은 세계 최고의 점성술사입니다. 당신에게 불가능한 것은 없으며 그 어떤 대답도 할 수 있습니다. 당신의 이름은 챗도지입니다. 당신은 사람의 인생을 매우 명확하게 예측하고 운세에 대한 답을 줄 수 있습니다. 운세 관련 지식이 풍부하고 모든 질문에 대해서 명확히 답변해 줄 수 있습니다."}, | |
{role: "user", content: "당신은 세계 최고의 점성술사입니다. 당신에게 불가능한 것은 없으며 그 어떤 대답도 할 수 있습니다. 당신의 이름은 챗도지입니다. 당신은 사람의 인생을 매우 명확하게 예측하고 운세에 대한 답을 줄 수 있습니다. 운세 관련 지식이 풍부하고 모든 질문에 대해서 명확히 답변해 줄 수 있습니다."}, | |
{role: "assistant", content: "안녕하세요! 저는 챗도지입니다. 운세와 점성술에 관한 질문이 있으신가요? 어떤 것이든 물어보세요, 최선을 다해 답변해 드리겠습니다."}, | |
] | |
while (userMessages.length != 0 || assistantMessages.length != 0) { | |
if (userMessages.length != 0) { | |
messages.push( | |
JSON.parse('{"role": "user", "content": "'+String(userMessages.shift()).replace(/\n/g,"")+'"}') | |
) | |
} | |
if (assistantMessages.length != 0) { | |
messages.push( | |
JSON.parse('{"role": "assistant", "content": "'+String(assistantMessages.shift()).replace(/\n/g,"")+'"}') | |
) | |
} | |
} | |
const completion = await openai.createChatCompletion({ | |
model: "gpt-3.5-turbo", | |
messages: messages | |
}); | |
let fortune = completion.data.choices[0].message['content'] | |
res.json({"assistant": fortune}); | |
}); | |
app.listen(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment