WebGPT: ChatGPT agent for your website

This is a simple demo that shows how to build your own ChatGPT bot on the
web. It's hosted on Autocode, a serverless Node.js development platform
specialized in shipping bots, webhooks and APIs with first-class support
for streaming web responses.
You can try out the live demo yourself at web-gpt-demo.com.

How does the bot work?
Code powering the chatbot is in functions/chat.js
.
The API exported from that file will generate a text/event-stream
response if triggered with a ?_stream
query parameter in the
url. The stream is defined by the function export using @stream {string} message
:
/**
* Respond to a user with ChatGPT via an API with streaming support
* @param {string} username A unique name to identify the current user session
* @param {string} content Content that the user has sent to ask the chat bot
* @stream {string} message Message chunk from full response
* @returns {string} response Full response from chatbot
*/
module.exports = async (username, content, context) => {
// ...
And is sent to the user via context.stream()
after triggering the
OpenAI chat completions
API and receiving the stream from it.
let completion = await lib.openai.playground['@0.1.2'].chat.completions.create({
model: `gpt-3.5-turbo`,
messages: messages,
max_tokens: 512,
temperature: 0.5,
top_p: 1,
n: 1,
presence_penalty: 0,
frequency_penalty: 0,
_stream: {
completion: (name, completion) => {
let delta = completion.choices[0].delta;
if (delta.content) {
context.stream('message', delta.content);
response += completion.choices[0].delta.content;
}
}
}
});
When we press the [Send] button below, we start a
Server-sent Event
session with the endpoint. This script powering the front-end
is available in www/script.js
.
As we receive a stream of results, we display the output in chat.

Want this bot for your Discord server instead? Check out
DiscordGPT instead. You can
follow us on Twitter at @autocode
or join us on Discord at discord.gg/autocode.