Skip to content

Commit

Permalink
Fix books
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Mar 5, 2024
1 parent 2026062 commit 59dca7a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
9 changes: 6 additions & 3 deletions bioimageio_chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
17 changes: 10 additions & 7 deletions bioimageio_chatbot/chatbot_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 23 additions & 10 deletions bioimageio_chatbot/chatbot_extensions/docs_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 30 additions & 7 deletions bioimageio_chatbot/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,36 @@ <h3 class="text-center">Welcome to BioImage.IO Chatbot</h3>

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) {
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.1.72"
version = "0.1.73"
readme = "README.md"
description = "Your Personal Assistant in BioImage Analysis."
dependencies = [
Expand Down

0 comments on commit 59dca7a

Please sign in to comment.