khoj-ai / khoj

Your AI second brain. Self-hostable. Get answers from the internet or your docs. Use any online or local LLM (e.g gpt, claude, gemini, llama, qwen, mistral). Build custom agents, personalized automations.
https://khoj.dev
GNU Affero General Public License v3.0
12.85k stars 657 forks source link

AttributeError: 'NoneType' object has no attribute 'org' #365

Closed leszekhanusz closed 1 year ago

leszekhanusz commented 1 year ago

Using khoj-assistant version 0.10.0 After trying to enable the Llama2 model, then trying to ask a question in the chat, I have the following error:

...
[14:48:06] INFO     📩 Saved sentence-transformers/clip-ViT-B-32 model to disk                                                                                        helpers.py:85
           WARNING  🚨 No Content configuration available.                                                                                                         configure.py:165
           INFO     📬 Search models, Conversation processor updated via API                                                                                             api.py:600
[14:48:13] INFO     127.0.0.1:60178 - "GET /chat HTTP/1.1" 200                                                                                                      h11_impl.py:431
           INFO     127.0.0.1:60178 - "GET /assets/khoj.css HTTP/1.1" 304                                                                                           h11_impl.py:431
           INFO     127.0.0.1:60178 - "GET /assets/icons/khoj-logo-sideways.svg HTTP/1.1" 304                                                                       h11_impl.py:431
           INFO     127.0.0.1:60178 - "GET /api/chat/history?client=web HTTP/1.1" 200                                                                               h11_impl.py:431
[14:48:43] INFO     127.0.0.1:54996 - "GET /api/chat?q=How%20to%20add%20an%20item%20to%20a%20list%20in%20Python%3F&n=5&client=web&stream=true HTTP/1.1" 500         h11_impl.py:431
           ERROR    Exception in ASGI application                                                                                                                   h11_impl.py:369

                    ╭───────────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────────╮                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py:366 in run_asgi                           │                
                    │                                                                                                                                             │                
                    │   363 │   # ASGI exception wrapper                                                                                                          │                
                    │   364 │   async def run_asgi(self, app):                                                                                                    │                
                    │   365 │   │   try:                                                                                                                          │                
                    │ ❱ 366 │   │   │   result = await app(self.scope, self.receive, self.send)                                                                   │                
                    │   367 │   │   except BaseException as exc:                                                                                                  │                
                    │   368 │   │   │   msg = "Exception in ASGI application\n"                                                                                   │                
                    │   369 │   │   │   self.logger.error(msg, exc_info=exc)                                                                                      │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py:75 in __call__                           │                
                    │                                                                                                                                             │                
                    │   72 │   │   │   │   │   port = 0                                                                                                           │                
                    │   73 │   │   │   │   │   scope["client"] = (host, port)  # type: ignore[index]                                                              │                
                    │   74 │   │                                                                                                                                  │                
                    │ ❱ 75 │   │   return await self.app(scope, receive, send)                                                                                    │                
                    │   76                                                                                                                                        │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/fastapi/applications.py:269 in __call__                                      │                
                    │                                                                                                                                             │                
                    │   266 │   async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:                                                     │                
                    │   267 │   │   if self.root_path:                                                                                                            │                
                    │   268 │   │   │   scope["root_path"] = self.root_path                                                                                       │                
                    │ ❱ 269 │   │   await super().__call__(scope, receive, send)                                                                                  │                
                    │   270 │                                                                                                                                     │                
                    │   271 │   def add_api_route(                                                                                                                │                
                    │   272 │   │   self,                                                                                                                         │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/applications.py:124 in __call__                                    │                
                    │                                                                                                                                             │                
                    │   121 │                                                                                                                                     │                
                    │   122 │   async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:                                                     │                
                    │   123 │   │   scope["app"] = self                                                                                                           │                
                    │ ❱ 124 │   │   await self.middleware_stack(scope, receive, send)                                                                             │                
                    │   125 │                                                                                                                                     │                
                    │   126 │   # The following usages are now discouraged in favour of configuration                                                             │                
                    │   127 │   # during Starlette.__init__(...)                                                                                                  │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/middleware/errors.py:184 in __call__                               │                
                    │                                                                                                                                             │                
                    │   181 │   │   │   # We always continue to raise the exception.                                                                              │                
                    │   182 │   │   │   # This allows servers to log the error, or allows test clients                                                            │                
                    │   183 │   │   │   # to optionally raise the error within the test case.                                                                     │                
                    │ ❱ 184 │   │   │   raise exc                                                                                                                 │                
                    │   185 │                                                                                                                                     │                
                    │   186 │   def format_line(                                                                                                                  │                
                    │   187 │   │   self, index: int, line: str, frame_lineno: int, frame_index: int                                                              │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/middleware/errors.py:162 in __call__                               │                
                    │                                                                                                                                             │                
                    │   159 │   │   │   await send(message)                                                                                                       │                
                    │   160 │   │                                                                                                                                 │                
                    │   161 │   │   try:                                                                                                                          │                
                    │ ❱ 162 │   │   │   await self.app(scope, receive, _send)                                                                                     │                
                    │   163 │   │   except Exception as exc:                                                                                                      │                
                    │   164 │   │   │   request = Request(scope)                                                                                                  │                
                    │   165 │   │   │   if self.debug:                                                                                                            │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/exceptions.py:93 in __call__                                       │                
                    │                                                                                                                                             │                
                    │    90 │   │   │   │   handler = self._lookup_exception_handler(exc)                                                                         │                
                    │    91 │   │   │                                                                                                                             │                
                    │    92 │   │   │   if handler is None:                                                                                                       │                
                    │ ❱  93 │   │   │   │   raise exc                                                                                                             │                
                    │    94 │   │   │                                                                                                                             │                
                    │    95 │   │   │   if response_started:                                                                                                      │                
                    │    96 │   │   │   │   msg = "Caught handled exception, but response already started."                                                       │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/exceptions.py:82 in __call__                                       │                
                    │                                                                                                                                             │                
                    │    79 │   │   │   await send(message)                                                                                                       │                
                    │    80 │   │                                                                                                                                 │                
                    │    81 │   │   try:                                                                                                                          │                
                    │ ❱  82 │   │   │   await self.app(scope, receive, sender)                                                                                    │                
                    │    83 │   │   except Exception as exc:                                                                                                      │                
                    │    84 │   │   │   handler = None                                                                                                            │                
                    │    85                                                                                                                                       │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py:21 in __call__                          │                
                    │                                                                                                                                             │                
                    │   18 │   │   │   │   │   await self.app(scope, receive, send)                                                                               │                
                    │   19 │   │   │   │   except Exception as e:                                                                                                 │                
                    │   20 │   │   │   │   │   dependency_exception = e                                                                                           │                
                    │ ❱ 21 │   │   │   │   │   raise e                                                                                                            │                
                    │   22 │   │   │   if dependency_exception:                                                                                                   │                
                    │   23 │   │   │   │   # This exception was possibly handled by the dependency but it should                                                  │                
                    │   24 │   │   │   │   # still bubble up so that the ServerErrorMiddleware can return a 500                                                   │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py:18 in __call__                          │                
                    │                                                                                                                                             │                
                    │   15 │   │   │   async with AsyncExitStack() as stack:                                                                                      │                
                    │   16 │   │   │   │   scope[self.context_name] = stack                                                                                       │                
                    │   17 │   │   │   │   try:                                                                                                                   │                
                    │ ❱ 18 │   │   │   │   │   await self.app(scope, receive, send)                                                                               │                
                    │   19 │   │   │   │   except Exception as e:                                                                                                 │                
                    │   20 │   │   │   │   │   dependency_exception = e                                                                                           │                
                    │   21 │   │   │   │   │   raise e                                                                                                            │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/routing.py:670 in __call__                                         │                
                    │                                                                                                                                             │                
                    │   667 │   │   │   match, child_scope = route.matches(scope)                                                                                 │                
                    │   668 │   │   │   if match == Match.FULL:                                                                                                   │                
                    │   669 │   │   │   │   scope.update(child_scope)                                                                                             │                
                    │ ❱ 670 │   │   │   │   await route.handle(scope, receive, send)                                                                              │                
                    │   671 │   │   │   │   return                                                                                                                │                
                    │   672 │   │   │   elif match == Match.PARTIAL and partial is None:                                                                          │                
                    │   673 │   │   │   │   partial = route                                                                                                       │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/routing.py:266 in handle                                           │                
                    │                                                                                                                                             │                
                    │   263 │   │   │   │   )                                                                                                                     │                
                    │   264 │   │   │   await response(scope, receive, send)                                                                                      │                
                    │   265 │   │   else:                                                                                                                         │                
                    │ ❱ 266 │   │   │   await self.app(scope, receive, send)                                                                                      │                
                    │   267 │                                                                                                                                     │                
                    │   268 │   def __eq__(self, other: typing.Any) -> bool:                                                                                      │                
                    │   269 │   │   return (                                                                                                                      │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/starlette/routing.py:65 in app                                               │                
                    │                                                                                                                                             │                
                    │    62 │   async def app(scope: Scope, receive: Receive, send: Send) -> None:                                                                │                
                    │    63 │   │   request = Request(scope, receive=receive, send=send)                                                                          │                
                    │    64 │   │   if is_coroutine:                                                                                                              │                
                    │ ❱  65 │   │   │   response = await func(request)                                                                                            │                
                    │    66 │   │   else:                                                                                                                         │                
                    │    67 │   │   │   response = await run_in_threadpool(func, request)                                                                         │                
                    │    68 │   │   await response(scope, receive, send)                                                                                          │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/fastapi/routing.py:227 in app                                                │                
                    │                                                                                                                                             │                
                    │    224 │   │   if errors:                                                                                                                   │                
                    │    225 │   │   │   raise RequestValidationError(errors, body=body)                                                                          │                
                    │    226 │   │   else:                                                                                                                        │                
                    │ ❱  227 │   │   │   raw_response = await run_endpoint_function(                                                                              │                
                    │    228 │   │   │   │   dependant=dependant, values=values, is_coroutine=is_coroutine                                                        │                
                    │    229 │   │   │   )                                                                                                                        │                
                    │    230                                                                                                                                      │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/fastapi/routing.py:160 in run_endpoint_function                              │                
                    │                                                                                                                                             │                
                    │    157 │   assert dependant.call is not None, "dependant.call must be a function"                                                           │                
                    │    158 │                                                                                                                                    │                
                    │    159 │   if is_coroutine:                                                                                                                 │                
                    │ ❱  160 │   │   return await dependant.call(**values)                                                                                        │                
                    │    161 │   else:                                                                                                                            │                
                    │    162 │   │   return await run_in_threadpool(dependant.call, **values)                                                                     │                
                    │    163                                                                                                                                      │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/khoj/routers/api.py:655 in chat                                              │                
                    │                                                                                                                                             │                
                    │   652 │   host: Optional[str] = Header(None),                                                                                               │                
                    │   653 ) -> Response:                                                                                                                        │                
                    │   654 │   perform_chat_checks()                                                                                                             │                
                    │ ❱ 655 │   compiled_references, inferred_queries = await                                                                                     │                
                    │       extract_references_and_questions(request, q, (n or 5))                                                                                │                
                    │   656 │                                                                                                                                     │                
                    │   657 │   # Get the (streamed) chat response from the LLM of choice.                                                                        │                
                    │   658 │   llm_response = generate_chat_response(                                                                                            │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/khoj/routers/api.py:729 in extract_references_and_questions                  │                
                    │                                                                                                                                             │                
                    │   726 │   │   │   for query in inferred_queries:                                                                                            │                
                    │   727 │   │   │   │   n_items = min(n, 3) if                                                                                                │                
                    │       state.processor_config.conversation.enable_offline_chat else n                                                                        │                
                    │   728 │   │   │   │   result_list.extend(                                                                                                   │                
                    │ ❱ 729 │   │   │   │   │   await search(query, request=request, n=n_items, r=True,                                                           │                
                    │       score_threshold=-5.0, dedupe=False)                                                                                                   │                
                    │   730 │   │   │   │   )                                                                                                                     │                
                    │   731 │   │   │   compiled_references = [item.additional["compiled"] for item in result_list]                                               │                
                    │   732                                                                                                                                       │                
                    │                                                                                                                                             │                
                    │ /mnt/tera/miniconda3/envs/textgen/lib/python3.10/site-packages/khoj/routers/api.py:402 in search                                            │                
                    │                                                                                                                                             │                
                    │   399 │   │   │   │   )                                                                                                                     │                
                    │   400 │                                                                                                                                     │                
                    │   401 │   with concurrent.futures.ThreadPoolExecutor() as executor:                                                                         │                
                    │ ❱ 402 │   │   if (t == SearchType.Org or t == SearchType.All) and state.content_index.org and                                               │                
                    │       state.search_models.text_search:                                                                                                      │                
                    │   403 │   │   │   # query org-mode notes                                                                                                    │                
                    │   404 │   │   │   search_futures += [                                                                                                       │                
                    │   405 │   │   │   │   executor.submit(                                                                                                      │                
                    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯                
                    AttributeError: 'NoneType' object has no attribute 'org'
leszekhanusz commented 1 year ago

The error disappears once I configure khoj to parse some pdf files.

sabaimran commented 1 year ago

@leszekhanusz Hmm, that's quite strange. Could you walk me through how you did the setup? Did you first configure your file paths in the Org settings in the web UI, and then go to the chat endpoint? Did you click the Configure button in between?

We do need to do better null checking here for the data index before we execute search/chat. Thanks for flagging this.

leszekhanusz commented 1 year ago

How to reproduce:

Ellen7ions commented 1 year ago

I also got this error.

Ellen7ions commented 1 year ago

The above NoneType error occurs when I start the server without adding any files such as pdf or md. After adding a md file, the error was resolved.

Ellen7ions commented 1 year ago

Maybe chat mode assumes that documentation has been added.

sabaimran commented 1 year ago

@Ellen7ions , @leszekhanusz this should be fixed with 996832dc72df2550fef546fef4608ce37201245e.

If you run pip install --pre khoj-assistant and run khoj again, it should work. I was making an assumption in the code that some files were indexed when chat occurs.

The other workaround if you don't want to upgrade the installation is to add @general to the start of all your messages in the chat window.

sabaimran commented 1 year ago

I'm closing this as other users with the issue have reported it's resolved! Let me know if you see it come up again.