From 59dca7a63455b89ee837e93d5f9c6613058d1487 Mon Sep 17 00:00:00 2001 From: Wei Ouyang Date: Tue, 5 Mar 2024 01:27:21 +0100 Subject: [PATCH] Fix books --- bioimageio_chatbot/chatbot.py | 9 +++-- .../chatbot_extensions/__init__.py | 17 +++++---- .../chatbot_extensions/docs_extension.py | 33 ++++++++++++----- bioimageio_chatbot/static/index.html | 37 +++++++++++++++---- pyproject.toml | 2 +- 5 files changed, 70 insertions(+), 28 deletions(-) diff --git a/bioimageio_chatbot/chatbot.py b/bioimageio_chatbot/chatbot.py index f468c2a..cc6c0b3 100644 --- a/bioimageio_chatbot/chatbot.py +++ b/bioimageio_chatbot/chatbot.py @@ -198,17 +198,20 @@ class ThoughtsSchema(BaseModel): ] # remove item with 'book' in all_extensions melman_extensions = [ - ext for ext in all_extensions if "book" not in ext["name"].lower() + ext for ext in all_extensions if ext["id"] != "books" ] kowalski_extensions = [ - ext for ext in all_extensions if ext["name"] in ["SearchWeb", "CodeInterpreter"] + ext for ext in all_extensions if ext["id"] == "web" ] # only keep the item with 'book' in all_extensions king_julien_extensions = [ - ext for ext in all_extensions if "book" in ext["name"].lower() + ext for ext in all_extensions if "books" == ext["id"] + ] + [ + ext for ext in all_extensions if ext["id"] == "web" ] + return [ {"name": "Melman", "agent": melman, "extensions": melman_extensions}, {"name": "Kowalski", "agent": kowalski, "extensions": kowalski_extensions}, diff --git a/bioimageio_chatbot/chatbot_extensions/__init__.py b/bioimageio_chatbot/chatbot_extensions/__init__.py index d4a29c8..873e0f9 100644 --- a/bioimageio_chatbot/chatbot_extensions/__init__.py +++ b/bioimageio_chatbot/chatbot_extensions/__init__.py @@ -11,13 +11,16 @@ def get_builtin_extensions(): for module in pkgutil.walk_packages(__path__, __name__ + '.'): if module.name.endswith('_extension'): ext_module = module.module_finder.find_module(module.name).load_module(module.name) - ext = ext_module.get_extension() - if not isinstance(ext, ChatbotExtension): - print(f"Failed to load chatbot extension: {module.name}.") - continue - if ext.id in [e.id for e in extensions]: - raise ValueError(f"Extension name {ext.id} already exists.") - extensions.append(ext) + exts = ext_module.get_extension() + if isinstance(exts, ChatbotExtension): + exts = [exts] + for ext in exts: + if not isinstance(ext, ChatbotExtension): + print(f"Failed to load chatbot extension: {module.name}.") + continue + if ext.id in [e.id for e in extensions]: + raise ValueError(f"Extension name {ext.id} already exists.") + extensions.append(ext) return extensions diff --git a/bioimageio_chatbot/chatbot_extensions/docs_extension.py b/bioimageio_chatbot/chatbot_extensions/docs_extension.py index 1c6e101..b9d740b 100644 --- a/bioimageio_chatbot/chatbot_extensions/docs_extension.py +++ b/bioimageio_chatbot/chatbot_extensions/docs_extension.py @@ -139,15 +139,28 @@ def get_extension(): "BIOIMAGEIO_KNOWLEDGE_BASE_PATH", "./bioimageio-knowledge-base" ) docs_store_dict = load_knowledge_base(knowledge_base_path) - tools = {} + + docs_tools = {} + books_tools = {} for col in collections: - tools["search_" + col["id"]] = create_tool(docs_store_dict, col) - - sinfo = ChatbotExtension( - id="docs", - name="Search BioImage Knowledge Base", - description="Search information in the documents of the bioimage.io knowledge base. Provide a list of keywords to search information in the documents. Returns a list of relevant documents.", - tools=tools, - ) + if "book" in col["id"]: + books_tools["search_" + col["id"]] = create_tool(docs_store_dict, col) + else: + docs_tools["search_" + col["id"]] = create_tool(docs_store_dict, col) + + if docs_tools: + sinfo1 = ChatbotExtension( + id="docs", + name="Search BioImage Docs", + description="Search information in the documents of the bioimage.io knowledge base. Provide a list of keywords to search information in the documents. Returns a list of relevant documents.", + tools=docs_tools, + ) + if books_tools: + sinfo2 = ChatbotExtension( + id="books", + name="Search BioImage Books", + description="Search information in BioImage books. Provide a list of keywords to search information in the books. Returns a list of relevant documents.", + tools=books_tools, + ) - return sinfo + return sinfo1, sinfo2 diff --git a/bioimageio_chatbot/static/index.html b/bioimageio_chatbot/static/index.html index e4c39b2..116c15d 100644 --- a/bioimageio_chatbot/static/index.html +++ b/bioimageio_chatbot/static/index.html @@ -666,13 +666,36 @@

Welcome to BioImage.IO Chatbot

try { const server_url = urlParams.get('server_url'); - // Call the login function to get the token - token = await hyphaWebsocketClient.login({ - "server_url": server_url || "https://ai.imjoy.io", - "login_callback": login_callback, - }); - // If the login is successful, proceed to initialize the service - svc = await initializeService(); + // try to load token from localstorage before trying to get it, after getting it save it to localstorage with a expiry time (3h), when using a saved token, always check the expiry time + try{ + token = localStorage.getItem('token'); + if(token){ + const tokenExpiry = localStorage.getItem('tokenExpiry'); + if(tokenExpiry && new Date(tokenExpiry) > new Date()){ + console.log("Using saved token:", token) + } + else{ + // Call the login function to get the token + token = await hyphaWebsocketClient.login({ + "server_url": server_url || "https://ai.imjoy.io", + "login_callback": login_callback, + }); + localStorage.setItem('token', token); + localStorage.setItem('tokenExpiry', new Date(Date.now() + 3*60*60*1000).toISOString()); + } + } + // If the login is successful, proceed to initialize the service + svc = await initializeService(); + } + catch(e){ + console.error("Failed to login:", e) + token = null; + localStorage.removeItem('token'); + localStorage.removeItem('tokenExpiry'); + alert("Failed to login, please reload the page and try again, error:" + e) + throw e; + } + // Show the app interface showAppInterface(); } catch (e) { diff --git a/pyproject.toml b/pyproject.toml index a4ad233..96235ca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools", "wheel"] [project] name = "bioimageio-chatbot" -version = "0.1.72" +version = "0.1.73" readme = "README.md" description = "Your Personal Assistant in BioImage Analysis." dependencies = [