continue-custom/server/continuedev/libs/llm/prompt_utils.py
Nate Sesti f09150617e
Preview (#541)
* Strong typing (#533)

* refactor: ♻️ get rid of continuedev.src.continuedev structure

* refactor: ♻️ switching back to server folder

* feat: ✨ make config.py imports shorter

* feat: 🔖 publish as pre-release vscode extension

* refactor: ♻️ refactor and add more completion params to ui

* build: 🏗️ download from preview S3

* fix: 🐛 fix paths

* fix: 💚 package:pre-release

* ci: 💚 more time for tests

* fix: 💚 fix build scripts

* fix: 🐛 fix import in run.py

* fix: 🔖 update version to try again

* ci: 💚 Update package.json version [skip ci]

* refactor: 🔥 don't check for old extensions version

* fix: 🐛 small bug fixes

* fix: 🐛 fix config.py import paths

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 platform-specific builds test #1

* feat: 💚 ship with binary

* fix: 💚 fix copy statement to include.exe for windows

* fix: 💚 cd extension before packaging

* chore: 🔊 count tokens generated

* fix: 💚 remove npm_config_arch

* fix: 💚 publish as pre-release!

* chore: 🔖 update version

* perf: 💚 hardcode distro paths

* fix: 🐛 fix yaml syntax error

* chore: 🔖 update version

* fix: 💚 update permissions and version

* feat: 🐛 kill old server if needed

* feat: 💄 update marketplace icon for pre-release

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ auto-reload for config.py

* feat: 🔧 update default config.py imports

* feat: ✨ codelens in config.py

* feat: ✨ select model param count from UI

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ more model options, ollama error handling

* perf: ⚡ don't show server loading immediately

* fix: 🐛 fixing small UI details

* ci: 💚 Update package.json version [skip ci]

* feat: 🚀 headers param on LLM class

* fix: 🐛 fix headers for openai.;y

* feat: ✨ highlight code on cmd+shift+L

* ci: 💚 Update package.json version [skip ci]

* feat: 💄 sticky top bar in gui.tsx

* fix: 🔊 websocket logging and horizontal scrollbar

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ allow AzureOpenAI Service through GGML

* ci: 💚 Update package.json version [skip ci]

* fix: 🐛 fix automigration

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 upload binaries in ci, download apple silicon

* chore: 🔥 remove notes

* fix: 💚 use curl to download binary

* fix: 💚 set permissions on apple silicon binary

* fix: 💚 testing

* fix: 💚 cleanup file

* fix: 💚 fix preview.yaml

* fix: 💚 only upload once per binary

* fix: 💚 install rosetta

* ci: 💚 download binary after tests

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 prepare ci for merge to main

---------

Co-authored-by: GitHub Action <action@github.com>
2023-10-09 18:37:27 -07:00

76 lines
2.8 KiB
Python

from typing import Dict, List, Union
from ...models.filesystem import RangeInFileWithContents
from ...models.filesystem_edit import FileEdit
class MarkdownStyleEncoderDecoder:
# Filename -> the part of the file you care about
range_in_files: List[RangeInFileWithContents]
def __init__(self, range_in_files: List[RangeInFileWithContents]):
self.range_in_files = range_in_files
def encode(self) -> str:
return "\n\n".join(
[
f"File ({rif.filepath})\n```\n{rif.contents}\n```"
for rif in self.range_in_files
]
)
def _suggestions_to_file_edits(self, suggestions: Dict[str, str]) -> List[FileEdit]:
file_edits: List[FileEdit] = []
for suggestion_filepath, suggestion in suggestions.items():
matching_rifs = list(
filter(lambda r: r.filepath == suggestion_filepath, self.range_in_files)
)
if len(matching_rifs) > 0:
range_in_file = matching_rifs[0]
file_edits.append(
FileEdit(
range=range_in_file.range,
filepath=range_in_file.filepath,
replacement=suggestion,
)
)
return file_edits
def _decode_to_suggestions(self, completion: str) -> Dict[str, str]:
if len(self.range_in_files) == 0:
return {}
if "```" not in completion:
completion = "```\n" + completion + "\n```"
if completion.strip().splitlines()[0].strip() == "```":
first_filepath = self.range_in_files[0].filepath
completion = f"File ({first_filepath})\n" + completion
suggestions: Dict[str, str] = {}
current_file_lines: List[str] = []
current_filepath: Union[str, None] = None
last_was_file = False
inside_file = False
for line in completion.splitlines():
if line.strip().startswith("File ("):
last_was_file = True
current_filepath = line.strip()[6:-1]
elif last_was_file and line.startswith("```"):
last_was_file = False
inside_file = True
elif inside_file:
if line.startswith("```"):
inside_file = False
suggestions[current_filepath] = "\n".join(current_file_lines)
current_file_lines = []
current_filepath = None
else:
current_file_lines.append(line)
return suggestions
def decode(self, completion: str) -> List[FileEdit]:
suggestions = self._decode_to_suggestions(completion)
file_edits = self._suggestions_to_file_edits(suggestions)
return file_edits