🐛 display llamafile errs and only models.start once

This commit is contained in:
Nate Sesti 2023-12-04 13:14:04 -08:00
parent 0d916b08dc
commit 5ed07d8097
6 changed files with 72 additions and 32 deletions

View file

@ -38,6 +38,7 @@ from .main import (
StepDescription,
UpdateStep,
)
from .models import Models
from .observation import Observation
from .sdk import ContinueSDK
@ -82,12 +83,14 @@ class Autopilot:
gui: AbstractGUIProtocolServer,
config: ContinueConfig,
context_manager: ContextManager,
models: Models,
):
self.session_state = session_state
self.ide = ide
self.gui = gui
self.config = config
self.context_manager = context_manager
self.models = models
@property
def policy(self) -> Policy:
@ -95,7 +98,7 @@ class Autopilot:
@property
def sdk(self) -> ContinueSDK:
return ContinueSDK(self.config, self.ide, self.gui, self) # type: ignore because of circular import
return ContinueSDK(self.config, self.ide, self.gui, self, self.models) # type: ignore because of circular import
class Config:
arbitrary_types_allowed = True

View file

@ -70,17 +70,12 @@ class ContinueSDK(AbstractContinueSDK):
ide: AbstractIdeProtocolServer,
gui: AbstractGUIProtocolServer,
autopilot: Autopilot,
models: Models,
):
self.ide = ide
self.gui = gui
self.config = config
self.models = config.construct_models()
self.models.start(
self.ide.window_info.unique_id,
self.config.system_message,
self.config.completion_options.temperature,
self.config.llm_request_hook,
)
self.models = models
self.__autopilot = autopilot
@property

View file

@ -29,10 +29,19 @@ async def get_headless_autopilot(
# "HEADLESS", _config.system_message, _config.completion_options.temperature
# )
models = _config.construct_models()
models.start(
ide.window_info.unique_id,
_config.system_message,
_config.completion_options.temperature,
_config.llm_request_hook,
)
return Autopilot(
session_state=state or SessionState.from_empty(),
ide=ide,
gui=CommandLineGUI(),
config=_config,
context_manager=context_manager,
models=models,
)

View file

@ -1,9 +1,9 @@
import asyncio
import socket
import subprocess
from typing import Any, AsyncGenerator, Coroutine, List, Optional
from typing import List, Optional
from continuedev.core.main import ChatMessage
from continuedev.core.main import ChatMessage, ContinueCustomException
from continuedev.models.llm import CompletionOptions
from .llamacpp import LlamaCpp
@ -45,7 +45,9 @@ class Llamafile(LlamaCpp):
llamafile_command: Optional[str] = None
def check_and_start(self) -> bool:
llamafile_process: Optional[subprocess.Popen] = None
def check_and_start(self, should_raise: bool) -> bool:
# Try to start the llamafile if it's not already started on port 8080
if not self.llamafile_command:
return False
@ -53,14 +55,28 @@ class Llamafile(LlamaCpp):
port = int((self.api_base or ":8080").split(":")[-1])
if not test_port(port):
subprocess.Popen(
self.llamafile_command.split(" "),
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
close_fds=True,
start_new_session=True,
shell=True,
)
if should_raise and self.llamafile_process:
# If already tried to start and it isn't started, raise an exception
_, err = self.llamafile_process.communicate()
err = err.decode("utf-8") if err else ""
err_string = f"Error starting llamafile\n\n{err}"
raise ContinueCustomException(err_string, err_string)
import threading
def process_run():
p = subprocess.Popen(
self.llamafile_command.split(" "),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
start_new_session=True,
shell=True,
)
self.llamafile_process = p
t = threading.Thread(target=process_run)
t.start()
return True
@ -68,26 +84,33 @@ class Llamafile(LlamaCpp):
def start(self, unique_id: str | None = None):
super().start(unique_id)
self.check_and_start()
self.check_and_start(False)
def dict(self, **kwargs):
d = super().dict(**kwargs)
if "llamafile_process" in d:
del d["llamafile_process"]
return d
async def _stream_chat(
self, messages: List[ChatMessage], options: CompletionOptions
):
if self.check_and_start():
if self.check_and_start(True):
await asyncio.sleep(STARTUP_DELAY)
async for message in super()._stream_chat(messages, options):
yield message
async def _stream_complete(self, prompt, options):
if self.check_and_start():
if self.check_and_start(True):
await asyncio.sleep(STARTUP_DELAY)
async for message in super()._stream_complete(prompt, options):
yield message
async def _complete(self, prompt: str, options: CompletionOptions):
if self.check_and_start():
if self.check_and_start(True):
await asyncio.sleep(STARTUP_DELAY)
return await super()._complete(prompt, options)

View file

@ -41,7 +41,7 @@ class GUIProtocolServer(AbstractGUIProtocolServer):
get_autopilot: Callable[[SessionState], Optional[Autopilot]],
get_context_item: Callable[[str, str], Awaitable[Optional[ContextItem]]],
get_config: Callable[[], Optional[ContinueConfig]],
reload_config: AsyncFunc,
reload_config: Callable,
open_config: Callable[[], Awaitable[None]],
):
self.window_id = window_id
@ -78,11 +78,11 @@ class GUIProtocolServer(AbstractGUIProtocolServer):
posthog_logger.capture_event(
"set_system_message", {"system_message": sys_message}
)
await self.reload_config()
self.reload_config()
await self.send_config_update()
elif msg.message_type == "set_temperature":
SerializedContinueConfig.set_temperature(float(data["temperature"]))
await self.reload_config()
self.reload_config()
await self.send_config_update()
elif msg.message_type == "add_model_for_role":
await self.add_model_for_role(
@ -96,7 +96,7 @@ class GUIProtocolServer(AbstractGUIProtocolServer):
async def set_model_for_role_from_title(self, role: str, title: str):
SerializedContinueConfig.set_model_for_role(title, role)
await self.reload_config()
self.reload_config()
await self.send_config_update()
async def delete_model_at_index(self, index: int):
@ -104,13 +104,13 @@ class GUIProtocolServer(AbstractGUIProtocolServer):
models = config.models
if title := models[index].title:
SerializedContinueConfig.delete_model(title)
await self.reload_config()
self.reload_config()
await self.send_config_update()
async def add_model_for_role(self, role: str, model: ModelDescription):
SerializedContinueConfig.add_model(model)
SerializedContinueConfig.set_model_for_role(model.title, role)
await self.reload_config()
self.reload_config()
await self.send_config_update()
_running_autopilots: Dict[str, Autopilot] = {}

View file

@ -7,6 +7,7 @@ from ..core.autopilot import Autopilot
from ..core.config import ContinueConfig, SerializedContinueConfig
from ..core.context import ContextManager
from ..core.main import SessionState
from ..core.models import Models
from ..libs.constants.default_config import default_config_json
from ..libs.index.build_index import build_index
from ..libs.util.create_async_task import create_async_task
@ -27,6 +28,7 @@ class Window:
gui: Optional[GUIProtocolServer] = None
config: ContinueConfig
context_manager: ContextManager = ContextManager()
models: Models
_error_loading_config: Optional[str] = None
_last_valid_config: Optional[ContinueConfig] = None
@ -48,7 +50,7 @@ class Window:
)
def __init__(self, config: Optional[ContinueConfig] = None) -> None:
self.config = config or self.load_config() or ContinueConfig()
self.reload_config()
def get_autopilot(self, session_state: SessionState) -> Optional[Autopilot]:
if self.ide is None or self.gui is None or self.config is None:
@ -60,6 +62,7 @@ class Window:
gui=self.gui,
config=self.config,
context_manager=self.context_manager,
models=self.models,
)
def get_config(self) -> Optional[ContinueConfig]:
@ -68,8 +71,15 @@ class Window:
def is_closed(self) -> bool:
return self.ide is None and self.gui is None
async def reload_config(self):
def reload_config(self):
self.config = self.load_config()
self.models = self.config.construct_models()
self.models.start(
"None" if self.ide is None else self.ide.window_info.unique_id,
self.config.system_message,
self.config.completion_options.temperature,
self.config.llm_request_hook,
)
async def display_config_error(self):
if self._error_loading_config is not None:
@ -157,7 +167,7 @@ class Window:
or filepath.endswith(".continue/config.json")
or filepath.endswith(".continue\\config.json")
):
await self.reload_config()
self.reload_config()
if self.gui is not None:
await self.gui.send_config_update()