Skip to content

Commit ecb7b26

Browse files
authored
New config variable API_BASE_URL and minor bug fixes (#477)
1 parent f4dc37f commit ecb7b26

File tree

8 files changed

+22
-9
lines changed

8 files changed

+22
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ You can setup some parameters in runtime configuration file `~/.config/shell_gpt
382382
```text
383383
# API key, also it is possible to define OPENAI_API_KEY env.
384384
OPENAI_API_KEY=your_api_key
385-
# OpenAI host, useful if you would like to use proxy.
386-
OPENAI_API_HOST=https://api.openai.com
385+
# Base URL of the backend server. If "default" URL will be resolved based on --model.
386+
API_BASE_URL=default
387387
# Max amount of cached message per chat session.
388388
CHAT_CACHE_LENGTH=100
389389
# Chat cache folder.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ classifiers = [
2828
"Programming Language :: Python :: 3.11",
2929
]
3030
dependencies = [
31-
"litellm >= 1.20.1, < 2.0.0",
31+
"litellm == 1.24.5",
3232
"typer >= 0.7.0, < 1.0.0",
3333
"click >= 7.1.1, < 9.0.0",
3434
"rich >= 13.1.0, < 14.0.0",

sgpt/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .app import entry_point
2+
3+
entry_point()

sgpt/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.0"
1+
__version__ = "1.3.1"

sgpt/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"CACHE_LENGTH": int(os.getenv("CHAT_CACHE_LENGTH", "100")),
2424
"REQUEST_TIMEOUT": int(os.getenv("REQUEST_TIMEOUT", "60")),
2525
"DEFAULT_MODEL": os.getenv("DEFAULT_MODEL", "gpt-4-1106-preview"),
26-
"OPENAI_BASE_URL": os.getenv("OPENAI_API_HOST", "https://api.openai.com/v1"),
2726
"DEFAULT_COLOR": os.getenv("DEFAULT_COLOR", "magenta"),
2827
"ROLE_STORAGE_PATH": os.getenv("ROLE_STORAGE_PATH", str(ROLE_STORAGE_PATH)),
2928
"DEFAULT_EXECUTE_SHELL_CMD": os.getenv("DEFAULT_EXECUTE_SHELL_CMD", "false"),
@@ -32,6 +31,7 @@
3231
"OPENAI_FUNCTIONS_PATH": os.getenv("OPENAI_FUNCTIONS_PATH", str(FUNCTIONS_PATH)),
3332
"OPENAI_USE_FUNCTIONS": os.getenv("OPENAI_USE_FUNCTIONS", "true"),
3433
"SHOW_FUNCTIONS_OUTPUT": os.getenv("SHOW_FUNCTIONS_OUTPUT", "false"),
34+
"API_BASE_URL": os.getenv("API_BASE_URL", "default"),
3535
# New features might add their own config variables here.
3636
}
3737

sgpt/handlers/chat_handler.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,20 @@ def list_ids(cls, value: str) -> None:
128128

129129
@classmethod
130130
def show_messages(cls, chat_id: str) -> None:
131+
color = cfg.get("DEFAULT_COLOR")
131132
if "APPLY MARKDOWN" in cls.initial_message(chat_id):
133+
theme = cfg.get("CODE_THEME")
132134
for message in cls.chat_session.get_messages(chat_id):
133135
if message.startswith("assistant:"):
134-
Console().print(Markdown(message))
136+
Console().print(Markdown(message, code_theme=theme))
135137
else:
136-
typer.secho(message, fg=cfg.get("DEFAULT_COLOR"))
138+
typer.secho(message, fg=color)
137139
typer.echo()
138140
return
139141

140142
for index, message in enumerate(cls.chat_session.get_messages(chat_id)):
141-
color = "magenta" if index % 2 == 0 else "green"
142-
typer.secho(message, fg=color)
143+
running_color = color if index % 2 == 0 else "green"
144+
typer.secho(message, fg=running_color)
143145

144146
@classmethod
145147
@option_callback

sgpt/handlers/handler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class Handler:
1919
def __init__(self, role: SystemRole) -> None:
2020
self.role = role
2121

22+
api_base_url = cfg.get("API_BASE_URL")
23+
self.base_url = None if api_base_url == "default" else api_base_url
24+
self.timeout = int(cfg.get("REQUEST_TIMEOUT"))
25+
2226
@property
2327
def printer(self) -> Printer:
2428
use_markdown = "APPLY MARKDOWN" in self.role.role
@@ -78,6 +82,8 @@ def get_completion(
7882
functions=functions,
7983
stream=True,
8084
api_key=cfg.get("OPENAI_API_KEY"),
85+
base_url=self.base_url,
86+
timeout=self.timeout,
8187
):
8288
delta = chunk.choices[0].delta
8389
function_call = delta.get("function_call")

tests/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ def comp_args(role, prompt, **kwargs):
4141
"functions": None,
4242
"stream": True,
4343
"api_key": ANY,
44+
"base_url": ANY,
45+
"timeout": ANY,
4446
**kwargs,
4547
}

0 commit comments

Comments
 (0)