Skip to content

Commit

Permalink
add message and dont crash when no provider set
Browse files Browse the repository at this point in the history
  • Loading branch information
PCSwingle committed Apr 10, 2024
1 parent 5ea60f6 commit fe6366a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ There are a few options to provide Mentat with your OpenAI API key:
2. Run `export OPENAI_API_KEY=<your key here>` prior to running Mentat
3. Place the previous command in your `.bashrc` or `.zshrc` to export your key on every terminal startup

If you want to use a models through Azure, Ollama or other service see [this doc](https://docs.mentat.ai/en/latest/user/alternative_models.html) for details.
If you want to use a models through Azure, Ollama or other services see [this doc](https://docs.mentat.ai/en/latest/user/alternative_models.html) for details.

# 🚀 Usage

Expand Down
21 changes: 14 additions & 7 deletions mentat/llm_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
)
from openai.types.chat.completion_create_params import ResponseFormat
from PIL import Image
from spice import APIConnectionError, Spice, SpiceError, SpiceMessage, SpiceResponse, StreamingSpiceResponse
from spice import APIConnectionError, Spice, SpiceMessage, SpiceResponse, StreamingSpiceResponse
from spice.errors import NoAPIKeyError
from spice.models import WHISPER_1
from spice.providers import OPEN_AI
from spice.spice import InvalidModelError

from mentat.errors import MentatError, ReturnToUser
from mentat.session_context import SESSION_CONTEXT
Expand Down Expand Up @@ -72,9 +73,12 @@ async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return await func(*args, **kwargs)
except APIConnectionError:
raise MentatError("API connection error: please check your internet connection and" " try again.")
except SpiceError as e:
raise MentatError(f"API error: {e}")
raise MentatError("API connection error: please check your internet connection and try again.")
except InvalidModelError:
SESSION_CONTEXT.get().stream.send(
"Unknown model. Use /config provider <provider> and try again.", style="error"
)
raise ReturnToUser()

return async_wrapper
else:
Expand All @@ -84,9 +88,12 @@ def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return func(*args, **kwargs)
except APIConnectionError:
raise MentatError("API connection error: please check your internet connection and" " try again.")
except SpiceError as e:
raise MentatError(f"API error: {e}")
raise MentatError("API connection error: please check your internet connection and try again.")
except InvalidModelError:
SESSION_CONTEXT.get().stream.send(
"Unknown model. Use /config provider <provider> and try again.", style="error"
)
raise ReturnToUser()

return sync_wrapper

Expand Down
1 change: 1 addition & 0 deletions mentat/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ async def _main(self):
stream.send(None, channel="client_exit")
break
except ReturnToUser:
stream.send(None, channel="loading", terminate=True)
need_user_request = True
continue
except (
Expand Down

0 comments on commit fe6366a

Please sign in to comment.