Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CHATBOT_LANGUAGES = {
"en": "en-US",
"de": "de-DE"
}
CHATBOT_ASSISTANT_NAME = "Assistant"
```
In addition, the chatbot endpoint needs to be added to the `config/urls.py`

Expand Down Expand Up @@ -263,4 +264,4 @@ Acknowledgement

We would like to thank the Federal Government and the Heads of Government of the Länder, as well as the
Joint Science Conference (GWK), for their funding and support within the framework of the NFDI4ING consortium.
Funded by the German Research Foundation (DFG) - project number 442146713."
Funded by the German Research Foundation (DFG) - project number 442146713.
64 changes: 43 additions & 21 deletions rdmo_chatbot/chatbot/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

class BaseAdapter:

async def call_copilot(self, name, args={}, default=None):
return_value = default
if cl.context.session.client_type == "copilot":
return_value = await cl.CopilotFunction(name=name, args=args).acall()
return return_value
async def call_copilot(self, name: str, default=None, **kwargs):
if cl.context.session.client_type != "copilot":
return default

result = await cl.CopilotFunction(name=name, args=kwargs).acall()

return default if result is None else result

async def on_chat_start(self):
pass
Expand Down Expand Up @@ -67,19 +69,11 @@ async def on_chat_start(self):

# check if we have a history, yet
if store.has_history(user.identifier, project_id):
content = getattr(config, f"CONTINUATION_{lang_code.upper()}", "")
await cl.Message(content=content).send()
await self.send_continuation(lang_code)
await self.send_history(user, project_id)
else:
# if the history is empty, display the confirmation message
content = getattr(config, f"CONFIRMATION_{lang_code.upper()}", "")
message = cl.AskActionMessage(content=content, actions=[
cl.Action(name="confirmation", icon="check", label="", payload={"value": "confirmation"}),
cl.Action(name="leave", icon="x", label="", payload={"value": "leave"}),
])
response = await message.send()
await message.remove()

if response and response.get("payload").get("value") == "confirmation":
if await self.send_confirmation(lang_code):
content = getattr(config, f"START_{lang_code.upper()}", "").strip()
store.set_history(user.identifier, project_id, [
AIMessage(content=content)
Expand All @@ -93,7 +87,8 @@ async def on_user_message(self, message):
user = cl.user_session.get("user")

# get the full project from the copilot
project = await self.call_copilot("getProject", default={})
project = await self.call_copilot("getProject")
project = project if isinstance(project, dict) else {}
project_id = project.get("id")

# get the history from the store
Expand Down Expand Up @@ -153,7 +148,7 @@ async def on_system_message(self, message):
store.reset_history(user.identifier, project_id)

async def on_transfer(self, action):
await self.call_copilot("handleTransfer", args=action.payload)
await self.call_copilot("handleTransfer", **action.payload)

async def on_contact(self, action):
# get user and project_id from the session
Expand All @@ -163,10 +158,37 @@ async def on_contact(self, action):
# get the history from the store
history = store.get_history(user.identifier, project_id)

await self.call_copilot("openContactModal", args={
"history": messages_to_dicts(history)
})
await self.call_copilot("openContactModal", history=messages_to_dicts(history))

async def send_continuation(self, lang_code):
content = getattr(config, f"CONTINUATION_{lang_code.upper()}", "")
await cl.Message(content=content).send()

async def send_history(self, user, project_id):
history = store.get_history(user.identifier, project_id)

for message in history:
if isinstance(message, HumanMessage):
message_author = user.display_name or "You"
message_type = "user_message"
elif isinstance(message, AIMessage):
message_author = config.ASSISTANT_NAME
message_type = "assistant_message"
else:
continue

await cl.Message(content=message.content, author=message_author, type=message_type).send()

async def send_confirmation(self, lang_code):
content = getattr(config, f"CONFIRMATION_{lang_code.upper()}", "")
message = cl.AskActionMessage(content=content, actions=[
cl.Action(name="confirmation", icon="check", label="", payload={"value": "confirmation"}),
cl.Action(name="leave", icon="x", label="", payload={"value": "leave"}),
])
response = await message.send()
await message.remove()

return response and response.get("payload").get("value") == "confirmation"

class OpenAILangChainAdapter(LangChainAdapter):

Expand Down
4 changes: 2 additions & 2 deletions rdmo_chatbot/plugin/utils.py
Copy link
Member

Choose a reason for hiding this comment

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

All but this change should be a different PR 🥴.

Copy link
Member Author

Choose a reason for hiding this comment

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

IKR 😜

Copy link
Member Author

Choose a reason for hiding this comment

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

now Ive minimized the changes and only made 2 commits

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import UTC, datetime, timedelta
from datetime import datetime, timedelta, timezone

from django import template
from django.conf import settings
Expand All @@ -15,7 +15,7 @@ def get_chatbot_token(user):
"identifier": user.username,
"display_name": get_full_name(user),
"metadata": {},
"exp": datetime.now(UTC) + timedelta(minutes=60 * 24),
"exp": datetime.now(timezone.utc) + timedelta(minutes=60 * 24),
}

return jwt.encode(token_data, settings.CHATBOT_AUTH_SECRET, algorithm="HS256")