Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Code, as one single file, a chrome extension that saves the inputs and outputs of…

  1. User

    Code, as one single file, a chrome extension that saves the inputs and outputs of chatting with an LLM inside of a HuggingFace Space.

  2. Assistant

    Certainly! Below is the code for a simple Chrome extension that captures the inputs and outputs while chatting with an LLM inside a Hugging Face Space. The extension saves the conversation data to the browser's local storage. Since Chrome extensions require at least a manifest.json file and a script file, we'll combine everything into a single file for the sake of this example.

    You can create a single file (e.g., extension.zip) containing the following structure:

    - manifest.json
    - content.js
    

    But for the purpose of providing everything in one place, here is the content of both files:


    manifest.json

    {
      "manifest_version": 3,
      "name": "Hugging Face Chat Logger",
      "version": "1.0",
      "description": "Saves inputs and outputs of chatting with an LLM inside a Hugging Face Space.",
      "permissions": ["storage"],
      "content_scripts": [
        {
          "matches": ["https://huggingface.co/spaces/*"],
          "js": ["content.js"]
        }
      ]
    }
    

    content.js

    // Function to save the conversation to local storage
    function saveConversation(inputText, outputText) {
      chrome.storage.local.get({ conversations: [] }, function(result) {
        const conversations = result.conversations;
        conversations.push({ input: inputText, output: outputText, timestamp: new Date() });
        chrome.storage.local.set({ conversations: conversations });
      });
    }
    
    // Observer to monitor changes in the chat interface
    const observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {
        const addedNodes = mutation.addedNodes;
        addedNodes.forEach(function(node) {
          if (node.nodeType === Node.ELEMENT_NODE) {
            // Detect user input messages
            if (node.matches('.message.user')) {
              const inputText = node.innerText;
              // Wait for the assistant's reply
              const chatContainer = node.parentNode;
              const assistantReply = chatContainer.querySelector('.message.assistant');
              if (assistantReply) {
                const outputText = assistantReply.innerText;
                saveConversation(inputText, outputText);
              }
            }
          }
        });
      });
    });
    
    // Start observing the chat container
    const chatContainer = document.querySelector('.chat-container');
    if (chatContainer) {
      observer.observe(chatContainer, { childList: true, subtree: true });
    }
    

    Instructions:

    1. Create the Extension Files:

      • Create a new folder on your computer (e.g., hf-chat-logger).
      • Inside this folder, create two files: manifest.json and content.js.
      • Copy the content provided above into the respective files.
    2. Load the Extension in Chrome:

      • Open Google Chrome and go to chrome://extensions/.
      • Enable "Developer mode" by toggling the switch in the top right corner.
      • Click on "Load unpacked" and select the folder you created (hf-chat-logger).
    3. Using the Extension:

      • Navigate to a Hugging Face Space where you can chat with an LLM (e.g., a space that uses Gradio for chat interfaces).
      • Start a conversation with the LLM.
      • The extension will automatically save your inputs and the LLM's outputs to Chrome's local storage.
    4. Accessing the Saved Conversations:

      • Open the Chrome Developer Console (Cmd+Option+J on Mac or Ctrl+Shift+J on Windows/Linux).

      • Run the following command to retrieve the conversations:

        chrome.storage.local.get('conversations', function(result) {
          console.log(result.conversations);
        });
        
      • The conversations will be logged in the console.

    Notes:

    • Permissions: The extension requires the storage permission to save data to Chrome's local storage.
    • Matching URLs: The content script runs on any URL that matches https://huggingface.co/spaces/*. You can adjust the matches property in manifest.json if needed.
    • Selectors: The selectors in content.js (e.g., .chat-container, .message.user, .message.assistant) are based on the assumed structure of the Hugging Face chat interface. You may need to adjust these selectors to match the actual HTML structure of the chat interface you are using.

    Customization:

    • Saving to a Hugging Face Space: If you have control over the Hugging Face Space and want to send the conversation data back to it, you would need an API endpoint on your server to receive the data. You can modify the saveConversation function to send a POST request instead of saving to local storage.

      function saveConversation(inputText, outputText) {
        fetch('https://your-hf-space/api/save_conversation', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            input: inputText,
            output: outputText,
            timestamp: new Date()
          })
        })
        .then(response => response.json())
        .then(data => console.log('Conversation saved:', data))
        .catch((error) => {
          console.error('Error:', error);
        });
      }
      

      Ensure that your Hugging Face Space is set up to handle the /api/save_conversation endpoint appropriately.

    Disclaimer:

    • This is a basic example to get you started. Depending on the specific Hugging Face Space and how the chat interface is implemented, you may need to adjust the code accordingly.
    • Always make sure to comply with privacy policies and terms of service when recording conversations.

    I hope this helps you create the Chrome extension you need. Let me know if you have any questions or need further assistance!