Skip to content

Commit

Permalink
Add chat endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vforvalerio87 committed Oct 21, 2023
1 parent 7452c88 commit 1fd6965
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ void (async () => {
res.json({ success: true })
})

app.post("/chat", async (req, res) => {
app.post("/chat", urlencoded({ "extended": true }), async (req, res) => {
try {
const output = await chat(req.body.prompt, redisClient)
res.json({
response: output
})
const output = await chat(req.body.text, redisClient)
res.send(output)
} catch(err) {
console.log("Error in /chat", err)
res.status(500).send(err)
Expand Down
34 changes: 26 additions & 8 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
width: 100%;
}

input[type="submit"]:disabled {
background-color: #888;
}

button:hover {
background-color: #444;
}
Expand Down Expand Up @@ -83,7 +87,8 @@ <h2>Teach something to the AI</h2>
hx-push-url="false"
hx-swap="none"
hx-on::before-send="handleBeforeSend(event)"
hx-on::after-request="handleAfterRequest(event)"
hx-on::after-request="handleAfterAdd(event)"
hx-on::after-settle="alert('I learned something new!')"
>
<textarea
name="text"
Expand All @@ -101,19 +106,23 @@ <h2>Teach something to the AI</h2>
<h2>Talk to the AI about your data</h2>
<form
hx-boost="true"
action="/createEmbedding"
action="/chat"
method="post"
hx-push-url="false"
hx-swap="none"
hx-target="#response"
hx-swap="innerHTML"
hx-on::before-send="handleBeforeSend(event)"
hx-on::after-request="handleAfterRequest(event)"
hx-on::after-request="handleAfterChat(event)"
>
<textarea
id="talk2dataText"
name="text"
maxlength="4000"
></textarea>
<br/>
<button onclick="talk2data()" id="talk2dataButton"> Chat </button>
<input
type="submit"
value="Chat"
/>
</form>

<p id="response"></p>
Expand All @@ -127,14 +136,23 @@ <h2>Talk to the AI about your data</h2>
button.disabled = true
}

function handleAfterRequest(event) {
function handleAfterAdd(event) {
const textArea = event.target.querySelector("textarea")
const button = event.target.querySelector("input[type='submit']")

textArea.disabled = false
button.disabled = false
textArea.value = ""
alert("I learned something new!")
}

function handleAfterChat(event) {
const textArea = event.target.querySelector("textarea")
const button = event.target.querySelector("input[type='submit']")

textArea.disabled = false
button.disabled = false
textArea.value = ""
alert('I learned something new!')
}

function talk2data() {
Expand Down

0 comments on commit 1fd6965

Please sign in to comment.