Skip to content

Commit 12d069e

Browse files
authored
Trim leading whitespace when processing (#900)
1 parent 83c0cf8 commit 12d069e

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/jupyter-ai-magics/jupyter_ai_magics/completion_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def post_process_suggestion(suggestion: str, request: InlineCompletionRequest) -
3838
for identifier in markdown_identifiers.get(language, [language])
3939
] + ["```"]
4040
for opening in bad_openings:
41-
if suggestion.startswith(opening):
42-
suggestion = suggestion[len(opening) :].lstrip()
41+
# ollama models tend to add spurious whitespace
42+
if suggestion.lstrip().startswith(opening):
43+
suggestion = suggestion.lstrip()[len(opening) :].lstrip()
4344
# check for the prefix inclusion (only if there was a bad opening)
4445
if suggestion.startswith(request.prefix):
4546
suggestion = suggestion[len(request.prefix) :]

packages/jupyter-ai/jupyter_ai/tests/completions/test_handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ async def test_handle_request(inline_handler):
102102
("```python\nTest python code\n```", "Test python code"),
103103
("```\ntest\n```\n \n", "test"),
104104
("```hello```world```", "hello```world"),
105+
(" ```\nprint(test)\n```", "print(test)"),
106+
("``` \nprint(test)\n```", "print(test)"),
105107
]
106108

107109

0 commit comments

Comments
 (0)