From 924424c7541a6a5209e8c41935792cd45885f1f4 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 27 Mar 2023 02:08:21 +0700 Subject: [PATCH] Throw actionable exceptions when content types or chat not configured --- src/interface/emacs/khoj.el | 10 ++++++++-- src/khoj/routers/api.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index 8bfaf782..604bdc0a 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -655,8 +655,14 @@ Render results in BUFFER-NAME using QUERY, CONTENT-TYPE." (encoded-query (url-hexify-string query)) (query-url (format "%s/api/chat?q=%s" khoj-server-url encoded-query))) (with-temp-buffer - (url-insert-file-contents query-url) - (json-parse-buffer :object-type 'alist)))) + (condition-case ex + (progn + (url-insert-file-contents query-url) + (json-parse-buffer :object-type 'alist)) + ('file-error (cond ((string-match "Internal server error" (nth 2 ex)) + (message "Chat processor not configured. Configure OpenAI API key and restart it. Exception: [%s]" ex)) + (t (message "Chat exception: [%s]" ex)))))))) + (defun khoj--render-chat-message (message sender &optional receive-date) "Render chat messages as `org-mode' list item. diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 312c1ffc..8ee6de13 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -33,6 +33,12 @@ def get_default_config_data(): @api.get("/config/types", response_model=List[str]) def get_config_types(): """Get configured content types""" + if state.config is None or state.config.content_type is None: + raise HTTPException( + status_code=500, + detail="Content types not configured. Configure at least one content type on server and restart it.", + ) + configured_content_types = state.config.content_type.dict(exclude_none=True) return [ search_type.value @@ -190,6 +196,15 @@ def update(t: Optional[SearchType] = None, force: Optional[bool] = False): @api.get("/chat") def chat(q: Optional[str] = None): + if ( + state.processor_config is None + or state.processor_config.conversation is None + or state.processor_config.conversation.openai_api_key is None + ): + raise HTTPException( + status_code=500, detail="Chat processor not configured. Configure OpenAI API key on server and restart it." + ) + # Initialize Variables api_key = state.processor_config.conversation.openai_api_key model = state.processor_config.conversation.model