Source code for chat_protocol
from typing import Optional, Protocol, Any, TypedDict, Union
from mdvtools.mdvproject import MDVProject
[docs]
class AskQuestionResult(TypedDict):
[docs]
view_name: Optional[str]
[docs]
class HandleError(Protocol):
[docs]
def __call__(self, error: Union[str, Exception], *, extra_metadata: Optional[dict] = None) -> None:
...
[docs]
class ChatRequest(TypedDict):
[docs]
handle_error: HandleError
[docs]
class ProjectChatProtocol(Protocol):
def __init__(self, project: MDVProject): ...
[docs]
def log(self, msg: str, *args: Any) -> None: ...
[docs]
def get_suggested_questions(self) -> list[str]: ...
[docs]
def ask_question(
self,
chat_request: ChatRequest,
) -> AskQuestionResult: ...
[docs]
init_error: Optional[bool]
[docs]
error_message: Optional[str]
[docs]
chat_enabled = True # pending server extension config mechanism
try:
from mdvtools.llm.langchain_mdv import ProjectChat as _ProjectChat # type: ignore
# raise Exception("test error in import langchain_mdv")
except Exception as e:
class _ProjectChat(ProjectChatProtocol):
def __init__(self, project: MDVProject):
self.project = project
self.init_error = True
self.welcome = (
"There was a problem initializing the chatbot:\n\n"
f"{msg}"
)
self.error_message = msg
def log(self, msg, *args):
print("[dummy bot]: " + (msg % args if args else msg))
def get_suggested_questions(self):
return []
def ask_question(self, chat_request: ChatRequest):
handle_error = chat_request["handle_error"]
msg = self.welcome
handle_error(f"## Sorry, I can't help you right now...\n\n{msg}")
return AskQuestionResult(
code=None,
view_name=None,
error=True,
message=f"Sorry, I can't help you right now\n\n{msg}"
)
# Tell the type checker we’re exposing a ProjectChat conforming to the protocol
[docs]
ProjectChat: type[ProjectChatProtocol] = _ProjectChat