Added chat name suggestion function
This commit is contained in:
parent
c1983d23d9
commit
eddfd99636
|
@ -30,22 +30,10 @@
|
||||||
breaks: true,
|
breaks: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const send = async () => {
|
const sendRequest = async (messages: Message[]): Promise<Response> => {
|
||||||
// Compose the input message
|
|
||||||
const inputMessage: Message = { role: "user", content: input.value };
|
|
||||||
addMessage(chatId, inputMessage);
|
|
||||||
|
|
||||||
// Clear the input value
|
|
||||||
input.value = "";
|
|
||||||
|
|
||||||
// Resize back to auto
|
|
||||||
input.style.height = "auto";
|
|
||||||
|
|
||||||
// Show updating bar
|
|
||||||
updating = true;
|
|
||||||
|
|
||||||
// Send API request
|
// Send API request
|
||||||
/*
|
/*
|
||||||
|
// Not working yet: a way to get the response as a stream
|
||||||
await fetchEventSource("https://api.openai.com/v1/chat/completions", {
|
await fetchEventSource("https://api.openai.com/v1/chat/completions", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -71,6 +59,8 @@
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
// Show updating bar
|
||||||
|
updating = true;
|
||||||
|
|
||||||
const response: Response = await (
|
const response: Response = await (
|
||||||
await fetch("https://api.openai.com/v1/chat/completions", {
|
await fetch("https://api.openai.com/v1/chat/completions", {
|
||||||
|
@ -82,7 +72,7 @@
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: "gpt-3.5-turbo",
|
model: "gpt-3.5-turbo",
|
||||||
// Submit only the role and content of the messages, provide the previous messages as well for context
|
// Submit only the role and content of the messages, provide the previous messages as well for context
|
||||||
messages: chat.messages.map((message): Message => {
|
messages: messages.map((message): Message => {
|
||||||
const { role, content } = message;
|
const { role, content } = message;
|
||||||
return { role, content };
|
return { role, content };
|
||||||
}),
|
}),
|
||||||
|
@ -96,11 +86,25 @@
|
||||||
})
|
})
|
||||||
).json();
|
).json();
|
||||||
|
|
||||||
console.log(response);
|
|
||||||
|
|
||||||
// Hide updating bar
|
// Hide updating bar
|
||||||
updating = false;
|
updating = false;
|
||||||
|
|
||||||
|
return response;
|
||||||
|
};
|
||||||
|
|
||||||
|
const submitForm = async (): Promise<void> => {
|
||||||
|
// Compose the input message
|
||||||
|
const inputMessage: Message = { role: "user", content: input.value };
|
||||||
|
addMessage(chatId, inputMessage);
|
||||||
|
|
||||||
|
// Clear the input value
|
||||||
|
input.value = "";
|
||||||
|
|
||||||
|
// Resize back to single line height
|
||||||
|
input.style.height = "auto";
|
||||||
|
|
||||||
|
const response = await sendRequest(chat.messages);
|
||||||
|
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
addMessage(chatId, {
|
addMessage(chatId, {
|
||||||
role: "system",
|
role: "system",
|
||||||
|
@ -113,6 +117,29 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const suggestName = async (): Promise<void> => {
|
||||||
|
const suggestMessage: Message = {
|
||||||
|
role: "user",
|
||||||
|
content: "Can you give me a 5 word summary of this conversation's topic?",
|
||||||
|
};
|
||||||
|
addMessage(chatId, suggestMessage);
|
||||||
|
|
||||||
|
const response = await sendRequest(chat.messages);
|
||||||
|
|
||||||
|
if (response.error) {
|
||||||
|
addMessage(chatId, {
|
||||||
|
role: "system",
|
||||||
|
content: `Error: ${response.error.message}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
response.choices.map((choice) => {
|
||||||
|
choice.message.usage = response.usage;
|
||||||
|
addMessage(chatId, choice.message);
|
||||||
|
chat.name = choice.message.content;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav class="level is-mobile chat-header">
|
<nav class="level is-mobile chat-header">
|
||||||
|
@ -123,6 +150,7 @@
|
||||||
<a
|
<a
|
||||||
href={"#"}
|
href={"#"}
|
||||||
class="greyscale ml-2 is-hidden editbutton"
|
class="greyscale ml-2 is-hidden editbutton"
|
||||||
|
title="Rename chat"
|
||||||
on:click|preventDefault={() => {
|
on:click|preventDefault={() => {
|
||||||
let newChatName = prompt("Enter a new name for this chat", chat.name);
|
let newChatName = prompt("Enter a new name for this chat", chat.name);
|
||||||
if (newChatName) {
|
if (newChatName) {
|
||||||
|
@ -133,6 +161,14 @@
|
||||||
>
|
>
|
||||||
✏️
|
✏️
|
||||||
</a>
|
</a>
|
||||||
|
<a
|
||||||
|
href={"#"}
|
||||||
|
class="greyscale ml-2 is-hidden editbutton"
|
||||||
|
title="Suggest a chat name"
|
||||||
|
on:click|preventDefault={suggestName}
|
||||||
|
>
|
||||||
|
💡
|
||||||
|
</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -211,7 +247,7 @@
|
||||||
<progress class="progress is-small is-dark" max="100" />
|
<progress class="progress is-small is-dark" max="100" />
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<form class="field has-addons has-addons-right" on:submit|preventDefault={send}>
|
<form class="field has-addons has-addons-right" on:submit|preventDefault={submitForm}>
|
||||||
<p class="control is-expanded">
|
<p class="control is-expanded">
|
||||||
<textarea
|
<textarea
|
||||||
class="input is-info is-medium is-focused chat-input"
|
class="input is-info is-medium is-focused chat-input"
|
||||||
|
@ -220,7 +256,7 @@
|
||||||
on:keydown={(e) => {
|
on:keydown={(e) => {
|
||||||
// Only send if Enter is pressed, not Shift+Enter
|
// Only send if Enter is pressed, not Shift+Enter
|
||||||
if (e.key === "Enter" && !e.shiftKey) {
|
if (e.key === "Enter" && !e.shiftKey) {
|
||||||
send();
|
submitForm();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Reference in New Issue