Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Robot Assistance</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="chat-container">
      <div id="chat-header">
        <img src="https://i.imgur.com/TnUa864.png" alt="ChatGPT Profile Picture" />
        <h3>Robot-assitance</h3>
      </div>
      <div id="chat-history">
        <ul id="message-list"></ul>
      </div>
      <div id="chat-input">
        <form>
          <input
            type="text"
            id="user-message"
            placeholder="Type your message here..."
          />
          <button type="submit">Send</button>
        </form>
      </div>
      <script src="script.js"></script>
    </div>
  </body>
</html>


              
            
!

CSS

              
                $primary-color: #4caf50;
$secondary-color: #f5f5f5;
$border-color: #ccc;
$box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);


#chat-container {
  border: 1px solid $border-color;
  border-radius: 5px;
  box-shadow: $box-shadow;
  width: 400px;
  height: 500px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

#chat-header {
  background-color: $secondary-color;
  padding: 10px;
  border-radius: 5px 5px 0 0;
}

#chat-header h3 {
  margin: 0;
}

#chat-history {
  flex-grow: 1;
  padding: 10px;
  overflow-y: scroll;
}

#message-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.user-message {
  background-color: $primary-color;
  color: #fff;
  border-radius: 5px;
  padding: 10px;
  margin: 10px 0;
  align-self: flex-end;
}

.bot-message {
  background-color: $secondary-color;
  color: #000;
  border-radius: 5px;
  padding: 10px;
  margin: 10px 0;
  align-self: flex-start;
}

#chat-input {
  background-color: $secondary-color;
  padding: 10px;
  border-radius: 0 0 5px 5px;
}

#chat-input input[type="text"] {
  border: none;
  border-radius: 5px;
  padding: 10px;
  width: 80%;
  &::placeholder {
    color: #ccc;
  }
}

#chat-input button {
  border: none;
  border-radius: 5px;
  background-color: $primary-color;
  color: #fff;
  padding: 10px;
  margin-left: 10px;
  width: 20%;
  cursor: pointer;
  &:hover {
    background-color: darken($primary-color, 10%);
  }
}
#chat-header {
  display: flex;
  align-items: center;
}

#chat-header img {
  border-radius: 50%;
  width: 40px;
  height: 40px;
  margin-right: 10px;
}
              
            
!

JS

              
                async function getChatResponse(message) {
  const prompt = `${message}\n`;
  const maxTokens = 50;
  const apiKey = "YOUR_API_KEY_HERE";
  const apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";

  try {
    const response = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${apiKey}`,
      },
      body: JSON.stringify({
        prompt,
        max_tokens: maxTokens,
      }),
    });

    if (!response.ok) {
      throw new Error(`Request failed with status ${response.status}`);
    }

    const data = await response.json();

    if (!data.choices || data.choices.length === 0) {
      throw new Error("Response data is empty");
    }

    const completions = data.choices[0].text;
    const chatResponse = completions.substring(
      completions.indexOf(prompt) + prompt.length,
      completions.lastIndexOf("\n")
    );
    return chatResponse;
  } catch (error) {
    console.error(error);
    return "I'm sorry, I'm having trouble processing your message right now. Please try again later.";
  }
}

const form = document.querySelector("form");
const messageList = document.querySelector("#message-list");

form.addEventListener("submit", (event) => {
  event.preventDefault();

  const input = document.querySelector("#user-message");
  const message = input.value.trim();

  // Add user message to chat window
  const userMessageElement = document.createElement("li");
  userMessageElement.classList.add("user-message");
  userMessageElement.textContent = message;
  messageList.appendChild(userMessageElement);

  // Get response from ChatGPT
  getChatResponse(message)
    .then((response) => {
      // Add ChatGPT response to chat window
      const chatResponseElement = document.createElement("li");
      chatResponseElement.classList.add("chat-response");
      chatResponseElement.textContent = response;
      messageList.appendChild(chatResponseElement);

      // Scroll to bottom of chat window
      messageList.scrollTop = messageList.scrollHeight;

      // Clear input field
      input.value = "";
    })
    .catch((error) => {
      console.error(error);
      alert(
        "I'm sorry, I'm having trouble processing your message right now. Please try again later."
      );
    });
});

              
            
!
999px

Console