Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Embed assistants #1489

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

[feature] Embed assistants #1489

wants to merge 6 commits into from

Conversation

mishig25
Copy link
Collaborator

@mishig25 mishig25 commented Sep 25, 2024

Description

Users can now create tools and connect their tools to their assistants (read here), making for use cases such as: create an assistant that queries your websites and chats about your website.

This PR adds an ability where users can embed their assistants on their websites.

Step1: under assistants settings, there will be script tag that can be put on a website's html head section.

Screenshot 2024-09-28 at 10 50 52

Step2: HF Chat button will appear on the bottom right section of your website.

Screenshot 2024-09-28 at 10 51 11

Step3: When you click on HF Chat button, a chat window modal with minimal hf/hf-chat chrome will appear

Screenshot 2024-09-28 at 10 51 30

Testing locally

  1. follow the latest readme on how to run chat-ui locally
  2. read this comment

Reviewing

when reviewing make sure to click Hide whitespace to see the actual diff
image

Screen recording

Screen.Recording.2024-09-28.at.11.00.44.mov

it may look bit confusing in the video because I'm embedding hf-chat inside hf-chat locally for testing purposes. but you can imagine the hf-chat modal being embedded on any arbitrary website

@mishig25 mishig25 force-pushed the embed_assistant branch 2 times, most recently from 3194ba5 to 54f1570 Compare September 27, 2024 11:57

document.addEventListener('DOMContentLoaded', function() {
const button = document.createElement('button');
button.className = 'fixed bottom-5 right-5 z-50 px-1.5 py-1 bg-blue-500 text-white rounded cursor-pointer hover:bg-blue-600 transition-colors flex items-center focus:outline-none';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is it going to work with tailwindcss?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will convert it back to regular css once we agree on the design

@mishig25 mishig25 force-pushed the embed_assistant branch 7 times, most recently from ed116a5 to 8749982 Compare September 28, 2024 03:25
@mishig25 mishig25 changed the title embed assistants [feature] Embed assistants Sep 28, 2024
@mishig25 mishig25 marked this pull request as ready for review September 28, 2024 03:37
Comment on lines +206 to +213
<!-- TODO: remove -->
{#if !$page.data.embeddedAssistantId}
<script
src="http://localhost:5173/chat/api/assistant/{$page.data.assistants.at(-1)
._id}/embed-snippet"
defer
></script>
{/if}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make testing the embedded assistants easier, I'm embedding hf assistant inside the chat-ui. These lines would be removed before merging to main.

you can change {$page.data.assistants.at(-1)._id} this part that grabs the last assistant id to any specific local assistant id if you want

@nsarrazin
Copy link
Collaborator

this is really cool! will do a deeper review but loving this already 🚀

Copy link
Collaborator

@nsarrazin nsarrazin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works great ! found one bug and the rest are more open ended comments, let me know what you think!

@@ -194,6 +199,26 @@
</div>
</div>

<div>
<h2 class="b-2 inline font-semibold">Embed this assistant</h2>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just my opinion but I think this is kind of an advanced feature and it's very visible in the UI of assistants atm

Maybe a button that copies the code to clipboard with an explainer text would be enough and take less space ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it can grow the usage of HuggingChat I think it's fine if it's stays well visible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes same

Comment on lines +4 to +5
const script = `(function() {
function resizeIframeToContentSize(iframe) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, i'm curious if there's a way to do this that's more maintainable ? it works great but i can imagine it would be very difficult to modify in the future

i know there's a render() method from svelte you can use to get the HTML for a component, we use something similar in thumbnails (

const renderedComponent = (ChatThumbnail as unknown as SvelteComponent).render({
name: assistant.name,
description: assistant.description,
createdByName: assistant.createdByName,
avatar,
});
const reactLike = html(
"<style>" + renderedComponent.css.code + "</style>" + renderedComponent.html
);
const svg = await satori(reactLike, {
) not sure how it handles javascript and idk if it's worth it but it could let you build this feature in a svelte component

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or simpler, make the JS snippet completely static (a static file) that gets the assistant's id from a property in a <a> link inside the embedding webpage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<script src="https://link/to/static.js"></script>

<a huggingchat-assistant="66017fca" href="https://hf.co/chat/assistant/66017fca">Chat with {Assistant}</a>

Comment on lines +1 to +2
export async function GET({ params }) {
const { id } = params;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nit: should the route be called embed-snippet.js ?

const iframe = document.createElement('iframe');
iframe.className = 'w-full';
iframe.style.height = '400px'; // Set an initial height
iframe.src = \`http://localhost:5173/chat/?embeddedAssistantId=${id}\`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will have to be configurable using the base path I guess in order to work on self-hosted versions

@nsarrazin
Copy link
Collaborator

nsarrazin commented Sep 30, 2024

just thinking out loud, would it be interesting to have the script try to find an existing button in the dom?

for example if there's already a button with id assistant-show on the page we can hook the modal display to it.

This would let people integrate the button where they want on the page with their own styling, with fallback on the absolutely positioned button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants