fix: exit the goose and show the error message when provider environment variable is not set (#103)

This commit is contained in:
Lifei Zhou
2024-10-04 11:17:29 +10:00
committed by GitHub
parent 9e35c6370e
commit 908af7f157
26 changed files with 345 additions and 86 deletions
+2 -5
View File
@@ -16,6 +16,7 @@ PROFILES_CONFIG_PATH = GOOSE_GLOBAL_PATH.joinpath("profiles.yaml")
SESSIONS_PATH = GOOSE_GLOBAL_PATH.joinpath("sessions")
SESSION_FILE_SUFFIX = ".jsonl"
LOG_PATH = GOOSE_GLOBAL_PATH.joinpath("logs")
RECOMMENDED_DEFAULT_PROVIDER = "openai"
@cache
@@ -88,11 +89,7 @@ def default_model_configuration() -> Tuple[str, str, str]:
except Exception:
pass
else:
raise ValueError(
"Could not detect an available provider,"
+ " make sure to plugin a provider or set an env var such as OPENAI_API_KEY"
)
provider = RECOMMENDED_DEFAULT_PROVIDER
recommended = {
"ollama": (OLLAMA_MODEL, OLLAMA_MODEL),
"openai": ("gpt-4o", "gpt-4o-mini"),
+21 -4
View File
@@ -1,8 +1,11 @@
import sys
import traceback
from pathlib import Path
from typing import Any, Dict, List, Optional
from exchange import Message, ToolResult, ToolUse, Text
from exchange import Message, ToolResult, ToolUse, Text, Exchange
from exchange.providers.base import MissingProviderEnvVariableError
from exchange.invalid_choice_error import InvalidChoiceError
from rich import print
from rich.console import RenderableType
from rich.live import Live
@@ -11,7 +14,7 @@ from rich.panel import Panel
from rich.status import Status
from goose.build import build_exchange
from goose.cli.config import ensure_config, session_path, LOG_PATH
from goose.cli.config import PROFILES_CONFIG_PATH, ensure_config, session_path, LOG_PATH
from goose._logger import get_logger, setup_logging
from goose.cli.prompt.goose_prompt_session import GoosePromptSession
from goose.notifier import Notifier
@@ -89,8 +92,7 @@ class Session:
self.profile = profile
self.status_indicator = Status("", spinner="dots")
self.notifier = SessionNotifier(self.status_indicator)
self.exchange = build_exchange(profile=load_profile(profile), notifier=self.notifier)
self.exchange = self._create_exchange()
setup_logging(log_file_directory=LOG_PATH, log_level=log_level)
self.exchange.messages.extend(self._get_initial_messages())
@@ -100,6 +102,21 @@ class Session:
self.prompt_session = GoosePromptSession()
def _create_exchange(self) -> Exchange:
try:
return build_exchange(profile=load_profile(self.profile), notifier=self.notifier)
except MissingProviderEnvVariableError as e:
error_message = f"{e.message}. Please set the required environment variable to continue."
print(Panel(error_message, style="red"))
sys.exit(1)
except InvalidChoiceError as e:
error_message = (
f"[bold red]{e.message}[/bold red].\nPlease check your configuration file at {PROFILES_CONFIG_PATH}.\n"
+ "Configuration doc: https://block-open-source.github.io/goose/configuration.html"
)
print(error_message)
sys.exit(1)
def _get_initial_messages(self) -> List[Message]:
messages = self.load_session()
+5 -2
View File
@@ -1,9 +1,12 @@
from functools import cache
from exchange.invalid_choice_error import InvalidChoiceError
from goose.toolkit.base import Toolkit
from goose.utils import load_plugins
@cache
def get_toolkit(name: str) -> type[Toolkit]:
return load_plugins(group="goose.toolkit")[name]
toolkits = load_plugins(group="goose.toolkit")
if name not in toolkits:
raise InvalidChoiceError("toolkit", name, toolkits.keys())
return toolkits[name]