-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move methods generating completion replies to provider
Add an example completion provider Adjust name of variable to reflect new implementation
- Loading branch information
1 parent
c12b7fd
commit 45e2e56
Showing
9 changed files
with
372 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/jupyter-ai-magics/jupyter_ai_magics/_completion.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Dict | ||
|
||
from .models.completion import InlineCompletionRequest | ||
|
||
|
||
def token_from_request(request: InlineCompletionRequest, suggestion: int): | ||
"""Generate a deterministic token (for matching streamed messages) | ||
using request number and suggestion number""" | ||
return f"t{request.number}s{suggestion}" | ||
|
||
|
||
def template_inputs_from_request(request: InlineCompletionRequest) -> Dict: | ||
suffix = request.suffix.strip() | ||
filename = request.path.split("/")[-1] if request.path else "untitled" | ||
|
||
return { | ||
"prefix": request.prefix, | ||
"suffix": suffix, | ||
"language": request.language, | ||
"filename": filename, | ||
"stop": ["\n```"], | ||
} | ||
|
||
|
||
def post_process_suggestion(suggestion: str, request: InlineCompletionRequest) -> str: | ||
"""Remove spurious fragments from the suggestion. | ||
While most models (especially instruct and infill models do not require | ||
any pre-processing, some models such as gpt-4 which only have chat APIs | ||
may require removing spurious fragments. This function uses heuristics | ||
and request data to remove such fragments. | ||
""" | ||
# gpt-4 tends to add "```python" or similar | ||
language = request.language or "python" | ||
markdown_identifiers = {"ipython": ["ipython", "python", "py"]} | ||
bad_openings = [ | ||
f"```{identifier}" | ||
for identifier in markdown_identifiers.get(language, [language]) | ||
] + ["```"] | ||
for opening in bad_openings: | ||
if suggestion.startswith(opening): | ||
suggestion = suggestion[len(opening) :].lstrip() | ||
# check for the prefix inclusion (only if there was a bad opening) | ||
if suggestion.startswith(request.prefix): | ||
suggestion = suggestion[len(request.prefix) :] | ||
break | ||
return suggestion |
81 changes: 81 additions & 0 deletions
81
packages/jupyter-ai-magics/jupyter_ai_magics/models/completion.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from typing import List, Literal, Optional | ||
|
||
from langchain.pydantic_v1 import BaseModel | ||
|
||
|
||
class InlineCompletionRequest(BaseModel): | ||
"""Message send by client to request inline completions. | ||
Prefix/suffix implementation is used to avoid the need for synchronising | ||
the notebook state at every key press (subject to change in future).""" | ||
|
||
# unique message ID generated by the client used to identify replies and | ||
# to easily discard replies for older requests | ||
number: int | ||
# prefix should include full text of the current cell preceding the cursor | ||
prefix: str | ||
# suffix should include full text of the current cell preceding the cursor | ||
suffix: str | ||
# media type for the current language, e.g. `text/x-python` | ||
mime: str | ||
# whether to stream the response (if supported by the model) | ||
stream: bool | ||
# path to the notebook of file for which the completions are generated | ||
path: Optional[str] | ||
# language inferred from the document mime type (if possible) | ||
language: Optional[str] | ||
# identifier of the cell for which the completions are generated if in a notebook | ||
# previous cells and following cells can be used to learn the wider context | ||
cell_id: Optional[str] | ||
|
||
|
||
class InlineCompletionItem(BaseModel): | ||
"""The inline completion suggestion to be displayed on the frontend. | ||
See JupyterLab `InlineCompletionItem` documentation for the details. | ||
""" | ||
|
||
insertText: str | ||
filterText: Optional[str] | ||
isIncomplete: Optional[bool] | ||
token: Optional[str] | ||
|
||
|
||
class CompletionError(BaseModel): | ||
type: str | ||
traceback: str | ||
|
||
|
||
class InlineCompletionList(BaseModel): | ||
"""Reflection of JupyterLab's `IInlineCompletionList`.""" | ||
|
||
items: List[InlineCompletionItem] | ||
|
||
|
||
class InlineCompletionReply(BaseModel): | ||
"""Message sent from model to client with the infill suggestions""" | ||
|
||
list: InlineCompletionList | ||
# number of request for which we are replying | ||
reply_to: int | ||
error: Optional[CompletionError] | ||
|
||
|
||
class InlineCompletionStreamChunk(BaseModel): | ||
"""Message sent from model to client with the infill suggestions""" | ||
|
||
type: Literal["stream"] = "stream" | ||
response: InlineCompletionItem | ||
reply_to: int | ||
done: bool | ||
error: Optional[CompletionError] | ||
|
||
|
||
__all__ = [ | ||
"InlineCompletionRequest", | ||
"InlineCompletionItem", | ||
"CompletionError", | ||
"InlineCompletionList", | ||
"InlineCompletionReply", | ||
"InlineCompletionStreamChunk", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.