| 1 |
""" |
| 2 |
This is an example of Fake LLM Completer for IPython, as |
| 3 |
well as example on how to configure IPython for LLMs. |
| 4 |
|
| 5 |
8.32 – this is provisional and may change. |
| 6 |
|
| 7 |
To test this you can run the following command from the root of IPython |
| 8 |
directory: |
| 9 |
|
| 10 |
$ ipython --TerminalInteractiveShell.llm_provider_class=examples.auto_suggest_llm.ExampleCompletionProvider |
| 11 |
|
| 12 |
Or you can set the value in your config file, which also allows you to set a |
| 13 |
keyboard shortcut:: |
| 14 |
|
| 15 |
c.TerminalInteractiveShell.llm_provider_class = "examples.auto_suggest_llm.ExampleCompletionProvider" |
| 16 |
c.TerminalInteractiveShell.shortcuts = [ |
| 17 |
{ |
| 18 |
"new_keys": ["c-q"], |
| 19 |
"command": "IPython:auto_suggest.llm_autosuggestion", |
| 20 |
"new_filter": "navigable_suggestions & default_buffer_focused", |
| 21 |
"create": True, |
| 22 |
}, |
| 23 |
] |
| 24 |
|
| 25 |
|
| 26 |
You can use the following configuration option to:: |
| 27 |
|
| 28 |
c.TerminalInteractiveShell.llm_constructor_kwargs = {"model_id": "mymodel"} |
| 29 |
|
| 30 |
|
| 31 |
For convenience and testing you can bind a shortcut at runtime:: |
| 32 |
|
| 33 |
In [1]: from examples.auto_suggest_llm import setup_shortcut |
| 34 |
...: setup_shortcut('c-q') |
| 35 |
|
| 36 |
|
| 37 |
Getting access to history content |
| 38 |
--------------------------------- |
| 39 |
|
| 40 |
This uses the same providers as Jupyter AI, In JupyterAI, providers may get |
| 41 |
access to the current notebook content to pass as to the LLM as context. |
| 42 |
|
| 43 |
Here Jupyter AI documents how to get such context. |
| 44 |
|
| 45 |
https://jupyter-ai.readthedocs.io/en/latest/developers/index.html |
| 46 |
|
| 47 |
|
| 48 |
When reusing these models you may want to pass them more context as well in |
| 49 |
IPython to do so you can set the |
| 50 |
`c.TerminalInteractiveShell.llm_prefix_from_history` to `"no_prefix"`, |
| 51 |
`"input_history"` or a fully qualified name of a function that will get |
| 52 |
imported, get passed a `HistoryManager`, and return a prefix to be added the LLM |
| 53 |
context. |
| 54 |
|
| 55 |
|
| 56 |
For more flexibility, subclass the provider, and access the history of IPython |
| 57 |
via: |
| 58 |
|
| 59 |
``` |
| 60 |
ip = get_ipython() |
| 61 |
hm = ip.history_manager() |
| 62 |
hm.get_range(...) # will let you select how many input/output... etc. |
| 63 |
``` |
| 64 |
|
| 65 |
""" |
| 66 |
|
| 67 |
import asyncio |
| 68 |
import textwrap |
| 69 |
from typing import Any |
| 70 |
from collections.abc import AsyncIterable, AsyncIterator |
| 71 |
|
| 72 |
from jupyter_ai_magics.models.completion import ( |
| 73 |
InlineCompletionList, |
| 74 |
InlineCompletionReply, |
| 75 |
InlineCompletionRequest, |
| 76 |
InlineCompletionStreamChunk, |
| 77 |
) |
| 78 |
from jupyter_ai_magics.providers import BaseProvider |
| 79 |
from langchain_community.llms import FakeListLLM |
| 80 |
|
| 81 |
|
| 82 |
from IPython.terminal.shortcuts.filters import ( |
| 83 |
navigable_suggestions, |
| 84 |
default_buffer_focused, |
| 85 |
) |
| 86 |
from IPython.terminal.shortcuts.auto_suggest import llm_autosuggestion |
| 87 |
|
| 88 |
|
| 89 |
def setup_shortcut(seq): |
| 90 |
import IPython |
| 91 |
|
| 92 |
ip = IPython.get_ipython() |
| 93 |
ip.pt_app.key_bindings.add_binding( |
| 94 |
seq, filter=(navigable_suggestions & default_buffer_focused) |
| 95 |
)(llm_autosuggestion), |
| 96 |
|
| 97 |
|
| 98 |
class ExampleCompletionProvider(BaseProvider, FakeListLLM): # type: ignore[misc, valid-type] |
| 99 |
""" |
| 100 |
This is an example Fake LLM provider for IPython |
| 101 |
|
| 102 |
As of 8.32 this is provisional and may change without any warnings |
| 103 |
""" |
| 104 |
|
| 105 |
id = "my_provider" |
| 106 |
name = "My Provider" |
| 107 |
model_id_key = "model" |
| 108 |
models = ["model_a"] |
| 109 |
|
| 110 |
def __init__(self, **kwargs: Any): |
| 111 |
kwargs["responses"] = ["This fake response will not be used for completion"] |
| 112 |
kwargs["model_id"] = "model_a" |
| 113 |
super().__init__(**kwargs) |
| 114 |
|
| 115 |
async def generate_inline_completions( |
| 116 |
self, request: InlineCompletionRequest |
| 117 |
) -> InlineCompletionReply: |
| 118 |
raise ValueError("IPython 8.32 only support streaming models for now.") |
| 119 |
|
| 120 |
async def stream_inline_completions( |
| 121 |
self, request: InlineCompletionRequest |
| 122 |
) -> AsyncIterator[InlineCompletionStreamChunk]: |
| 123 |
token_1 = f"t{request.number}s0" |
| 124 |
|
| 125 |
yield InlineCompletionReply( |
| 126 |
list=InlineCompletionList( |
| 127 |
items=[ |
| 128 |
{"insertText": "It", "isIncomplete": True, "token": token_1}, |
| 129 |
] |
| 130 |
), |
| 131 |
reply_to=request.number, |
| 132 |
) |
| 133 |
|
| 134 |
reply: InlineCompletionStreamChunk |
| 135 |
async for reply in self._stream( |
| 136 |
textwrap.dedent( |
| 137 |
""" |
| 138 |
was then that the fox appeared. |
| 139 |
“Good morning,” said the fox. |
| 140 |
“Good morning,” the little prince responded politely, although when he turned around he saw nothing. |
| 141 |
“I am right here,” the voice said, “under the apple tree.” |
| 142 |
“Who are you?” asked the little prince, and added, “You are very pretty to look at.” |
| 143 |
“I am a fox,” said the fox. |
| 144 |
“Come and play with me,” proposed the little prince. “I am so unhappy.” |
| 145 |
""" |
| 146 |
).strip(), |
| 147 |
request.number, |
| 148 |
token_1, |
| 149 |
start_with="It", |
| 150 |
): |
| 151 |
yield reply |
| 152 |
|
| 153 |
async def _stream( |
| 154 |
self, sentence: str, request_number: int, token: str, start_with: str = "" |
| 155 |
) -> AsyncIterable[InlineCompletionStreamChunk]: |
| 156 |
suggestion = start_with |
| 157 |
|
| 158 |
for fragment in sentence.split(" "): |
| 159 |
await asyncio.sleep(0.05) |
| 160 |
suggestion += " " + fragment |
| 161 |
yield InlineCompletionStreamChunk( |
| 162 |
type="stream", |
| 163 |
response={"insertText": suggestion, "token": token}, |
| 164 |
reply_to=request_number, |
| 165 |
done=False, |
| 166 |
) |
| 167 |
|
| 168 |
# finally, send a message confirming that we are done |
| 169 |
yield InlineCompletionStreamChunk( |
| 170 |
type="stream", |
| 171 |
response={"insertText": suggestion, "token": token}, |
| 172 |
reply_to=request_number, |
| 173 |
done=True, |
| 174 |
) |