@@ -174,7 +174,7 @@ class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory):
174174 # This is the instance of the LLM provider from jupyter-ai to which we forward the request
175175 # to generate inline completions.
176176 _llm_provider : Any | None
177- _llm_prefixer : callable = lambda self , x : "wrong"
177+ _llm_prefixer : Callable = lambda self , x : "wrong"
178178
179179 def __init__ (self ):
180180 super ().__init__ ()
@@ -325,17 +325,14 @@ async def _trigger_llm(self, buffer) -> None:
325325 """
326326 # we likely want to store the current cursor position, and cancel if the cursor has moved.
327327 try :
328- import jupyter_ai_magics
329328 import jupyter_ai .completions .models as jai_models
330329 except ModuleNotFoundError :
331330 jai_models = None
332331 if not self ._llm_provider :
333332 warnings .warn ("No LLM provider found, cannot trigger LLM completions" )
334333 return
335334 if jai_models is None :
336- warnings .warn (
337- "LLM Completion requires `jupyter_ai_magics` and `jupyter_ai` to be installed"
338- )
335+ warnings .warn ("LLM Completion requires `jupyter_ai` to be installed" )
339336
340337 self ._cancel_running_llm_task ()
341338
@@ -365,7 +362,7 @@ async def _trigger_llm_core(self, buffer: Buffer):
365362 Unlike with JupyterAi, as we do not have multiple cell, the cell id
366363 is always set to `None`.
367364
368- We set the prefix to the current cell content, but could also inset the
365+ We set the prefix to the current cell content, but could also insert the
369366 rest of the history or even just the non-fail history.
370367
371368 In the same way, we do not have cell id.
@@ -378,21 +375,27 @@ async def _trigger_llm_core(self, buffer: Buffer):
378375 providers.
379376 """
380377 try :
381- import jupyter_ai_magics
382378 import jupyter_ai .completions .models as jai_models
383379 except ModuleNotFoundError :
384380 jai_models = None
385381
382+ if not jai_models :
383+ raise ValueError ("jupyter-ai is not installed" )
384+
385+ if not self ._llm_provider :
386+ raise ValueError ("No LLM provider found, cannot trigger LLM completions" )
387+
386388 hm = buffer .history .shell .history_manager
387389 prefix = self ._llm_prefixer (hm )
388390 get_ipython ().log .debug ("prefix: %s" , prefix )
389391
390392 self ._request_number += 1
391393 request_number = self ._request_number
394+
392395 request = jai_models .InlineCompletionRequest (
393396 number = request_number ,
394- prefix = prefix + buffer .document .text ,
395- suffix = "" ,
397+ prefix = prefix + buffer .document .text_before_cursor ,
398+ suffix = buffer . document . text_after_cursor ,
396399 mime = "text/x-python" ,
397400 stream = True ,
398401 path = None ,
@@ -438,7 +441,7 @@ async def llm_autosuggestion(event: KeyPressEvent):
438441 doc = event .current_buffer .document
439442 lines_to_insert = max (0 , _MIN_LINES - doc .line_count + doc .cursor_position_row )
440443 for _ in range (lines_to_insert ):
441- event .current_buffer .insert_text ("\n " , move_cursor = False )
444+ event .current_buffer .insert_text ("\n " , move_cursor = False , fire_event = False )
442445
443446 await provider ._trigger_llm (event .current_buffer )
444447
0 commit comments