Skip to content

Commit

Permalink
Merge pull request #137 from bioimage-io/support-at-assistant
Browse files Browse the repository at this point in the history
Support talk to a different assistant
  • Loading branch information
oeway authored Jun 5, 2024
2 parents 78f8288 + 7f998bf commit 52b9901
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
26 changes: 24 additions & 2 deletions bioimageio_chatbot/chatbot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import os
import json
import re
import datetime
import secrets
import aiofiles
Expand Down Expand Up @@ -321,7 +322,7 @@ async def report(user_report, context=None):
print(f"User report saved to {filename}")

async def talk_to_assistant(
assistant_name, session_id, user_message: QuestionWithHistory, status_callback, user
assistant_name, session_id, user_message: QuestionWithHistory, status_callback, user, cross_assistant=False
):
user = user or {}
if quota_manager.check_quota(user.get("email")) <= 0:
Expand Down Expand Up @@ -362,6 +363,8 @@ async def stream_callback(message):
print(
f"\nUser: {user_message.question}\nAssistant({assistant_name}): {response.text}\nRemaining quota: {response.remaining_quota}\n"
)
if cross_assistant:
response.text = f"`{assistant_name}`: {response.text}"

if session_id:
user_message.chat_history.append(
Expand Down Expand Up @@ -400,14 +403,33 @@ async def chat(
context.get("user")
), "You don't have permission to use the chatbot, please sign up and wait for approval"

text = text.strip()

assistant_names = [a["name"].lower() for a in assistants]

# Check if the text starts with @ followed by a name
match = re.match(r'@(\w+)', text, flags=re.IGNORECASE)
if match:
# If it does, extract the name and set it as the assistant_name
assistant_name = match.group(1).lower()
# Check if the assistant_name is in the list of assistant_names
if assistant_name not in assistant_names:
raise ValueError(f"Assistant '{assistant_name}' not found. Available assistants are {assistant_names}")
# Remove the @name part from the text
text = re.sub(r'@(\w+)', '', text, 1).strip()
assistant_name = assistants[assistant_names.index(assistant_name)]['name']
cross_assistant = True
else:
cross_assistant = False

m = QuestionWithHistory(
question=text,
chat_history=chat_history,
user_profile=UserProfile.model_validate(user_profile),
chatbot_extensions=extensions,
context=context,
)
return await talk_to_assistant(assistant_name, session_id, m, status_callback, context.get("user"))
return await talk_to_assistant(assistant_name, session_id, m, status_callback, context.get("user"), cross_assistant)

async def ping(context=None):
if login_required and context and context.get("user"):
Expand Down
59 changes: 50 additions & 9 deletions bioimageio_chatbot/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,6 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>
<button class="btn" type="button" id="tooltip-button" data-bs-toggle="tooltip" data-bs-placement="left" data-bs-html="true" data-bs-delay='{"show":"100", "hide":"100"}' title="">
<i class="fas fa-question-circle"></i>
</button>
<script>
document.getElementById('tooltip-button').setAttribute('title',
'<b>Melman:</b> An assistant specializing in searching through bioimage analysis tools, including the BioImage Model Zoo, the BioImage Archive, biii.eu, and others, to provide comprehensive support in bioimaging.<br>' +
'<b>Nina:</b> Your tutor for learning about bioimage analysis and AI. Nina is the perfect guide, offering detailed explanations and support throughout your learning journey.<br>' +
'<b>Bridget:</b> Offers tools to analyze your images directly or helps you construct a Python pipeline for advanced image processing tasks.<br>' +
'<b>Skyler:</b> Facilitates the integration of bioimaging tools and workflows, making it easier to apply advanced analysis techniques in your research.'
);
</script>
<div id="assistant-buttons" class="dropdown-menu dropdown-menu-right"
aria-labelledby="assistant-menu-button">
</div>
Expand All @@ -429,6 +421,15 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>
</label>
<input type="file" id="fileUpload" style="visibility: hidden; position: absolute;"
onchange="fileSelected()" -->
<!-- Replace the existing label and input with the following: -->
<div class="dropdown">
<button class="btn" style="padding: 0px;" type="button" aria-haspopup="true" aria-expanded="false" id="at-assistant-button" data-toggle="dropdown" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-html="true" data-bs-delay='{"show":"100", "hide":"100"}' title="">
<i class="fas fa-at" style="font-size: 1.2rem;" id="atIcon"></i>
</button>
<div id="at-buttons" class="dropdown-menu dropdown-menu-left"
aria-labelledby="at-assistant-button">
</div>
</div>
<textarea class="form-control message" id="textMessageArea" rows="1"
style="flex-grow: 1; border: 1px solid #ced4da; border-radius: .25rem; height: 36px; padding: .375rem .75rem;"></textarea>
</div>
Expand Down Expand Up @@ -538,6 +539,12 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>
<script src="https://www.unpkg.com/[email protected]/dist/js/bootstrap.min.js"></script>
<script src="/chat/worker-manager.js"></script>
<script>
document.getElementById('tooltip-button').setAttribute('title',
'<b>Melman:</b> An assistant specializing in searching through bioimage analysis tools, including the BioImage Model Zoo, the BioImage Archive, biii.eu, and others, to provide comprehensive support in bioimaging.<br>' +
'<b>Nina:</b> Your tutor for learning about bioimage analysis and AI. Nina is the perfect guide, offering detailed explanations and support throughout your learning journey.<br>' +
'<b>Bridget:</b> Offers tools to analyze your images directly or helps you construct a Python pipeline for advanced image processing tasks.<br>' +
'<b>Skyler:</b> Facilitates the integration of bioimaging tools and workflows, making it easier to apply advanced analysis techniques in your research.'
);
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl, {
Expand Down Expand Up @@ -1493,6 +1500,41 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>
const href = '?' + urlParams.toString();
return `<li><a class="dropdown-item" href="${href}" data-assistant="${a.name}">${a.alias}(${a.name})</a></li>`
}).join("\n")

const atAssistantButton = document.getElementById("at-buttons");
atAssistantButton.innerHTML = Object.values(allAssistants).map((a) => {
return `<li><a class="dropdown-item" data-assistant="${a.name}">${a.alias}(${a.name})</a></li>`
}).join("\n");

// Get all the dropdown items
const dropdownItems = document.querySelectorAll('.dropdown-item');

// Add a 'click' event listener to each dropdown item
dropdownItems.forEach(item => {
item.addEventListener('click', function(event) {
// Prevent the default action
event.preventDefault();
// Get the selected assistant's name
const assistantName = this.getAttribute('data-assistant');

// Get the textarea
const textarea = document.getElementById('textMessageArea');

// Get the current content of the textarea
const currentContent = textarea.value;

// Check if the current content starts with an @ symbol followed by any word
const match = currentContent.match(/^@(\w+)/);

// If it does, replace the existing @xxx with the selected assistant's name
// If it doesn't, prepend the selected assistant's name to the current content
const newContent = match ? currentContent.replace(match[0], '@' + assistantName) : '@' + assistantName + ' ' + currentContent;

// Set the new content to the textarea
textarea.value = newContent;

});
});
}
else {
$('#assistant-menu-button').hide();
Expand Down Expand Up @@ -1620,7 +1662,6 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>
var textarea = document.getElementById('textMessageArea');
textarea.addEventListener('input', autoResize, false);
autoResize.call(textarea);

var renderer = new marked.Renderer();
marked.setOptions({
gfm: true,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools", "wheel"]

[project]
name = "bioimageio-chatbot"
version = "0.2.2"
version = "0.2.3"
readme = "README.md"
description = "Your Personal Assistant in Computational BioImaging."
dependencies = [
Expand Down

0 comments on commit 52b9901

Please sign in to comment.