Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container improvements #540

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FROM python:3-slim

ENV SHELL_INTERACTION=false
ENV OVERWRITE_OS_NAME=""
ENV OVERWRITE_SHELL_NAME=""

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ENV SHELL_INTERACTION=false
ENV OVERWRITE_OS_NAME=""
ENV OVERWRITE_SHELL_NAME=""
ARG OVERWRITE_OS_NAME=default
ARG OVERWRITE_SHELL_NAME=default
ENV SHELL_INTERACTION=false
ENV OVERWRITE_OS_NAME=${OVERWRITE_OS_NAME}
ENV OVERWRITE_SHELL_NAME=${OVERWRITE_SHELL_NAME}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you misunderstand Docker's ARG:
These are env variables that only exist during the building process of the container image.
Once the image exists, these are gone forever.
Please refer the dockerfile arg documentation for a better, more complete explanation.

ENV sets environment variables that remain, and can be used set as arguments with --env as desired when starting the container

WORKDIR /app
COPY . /app

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,16 @@ docker run --rm \
ghcr.io/ther1d/shell_gpt --chat rainbow "what are the colors of a rainbow"
```

When using a container, please note:
* The \[E\]xecute option for --shell with interaction will not work, since it would try this Execute in the docker container.
=> setting the `SHELL_INTERACTION` environment variable to false , makes sense.
* Since, most likely the os and shell of your container are not identical to the environment you want help with:
set the environment variables `OVERWRITE_OS_NAME` and `OVERWRITE_SHELL_NAME` according to your setup.


Example of a conversation, using an alias and the `OPENAI_API_KEY` environment variable:
```shell
alias sgpt="docker run --rm --env OPENAI_API_KEY --volume gpt-cache:/tmp/shell_gpt ghcr.io/ther1d/shell_gpt"
alias sgpt="docker run --rm --env OPENAI_API_KEY --env SHELL_INTERACTION=false OVERWRITE_OS_NAME=Debian --env OVERWRITE_SHELL_NAME=bash --volume gpt-cache:/tmp/shell_gpt ghcr.io/ther1d/shell_gpt"
Robinsane marked this conversation as resolved.
Show resolved Hide resolved
export OPENAI_API_KEY="your OPENAI API key"
sgpt --chat rainbow "what are the colors of a rainbow"
sgpt --chat rainbow "inverse the list of your last answer"
Expand All @@ -475,4 +482,13 @@ You also can use the provided `Dockerfile` to build your own image:
docker build -t sgpt .
```

Example environment variables for a working Ollama setup, using Docker:
* ENV DEFAULT_MODEL=ollama/mistral:7b-instruct-v0.2-q4_K_M
* ENV API_BASE_URL=http://10.10.10.10:11434
* ENV USE_LITELLM=true
* ENV OPENAI_API_KEY=bad_key
* ENV SHELL_INTERACTION=false
* ENV OVERWRITE_OS_NAME="Linux/Red Hat Enterprise Linux 8.8 (Ootpa)"
* ENV OVERWRITE_SHELL_NAME=/bin/bash

Additional documentation: [Azure integration](https://github.com/TheR1D/shell_gpt/wiki/Azure), [Ollama integration](https://github.com/TheR1D/shell_gpt/wiki/Ollama).
2 changes: 1 addition & 1 deletion sgpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main(
rich_help_panel="Assistance Options",
),
interaction: bool = typer.Option(
True,
True if cfg.get("SHELL_INTERACTION") != "false" else False,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
True if cfg.get("SHELL_INTERACTION") != "false" else False,
cfg.get("SHELL_INTERACTION") == "true",

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it this way to ensure the normal default behaviour is set, also when SHELL_INTERACTION contains an unintended / weird value

help="Interactive mode for --shell option.",
rich_help_panel="Assistance Options",
),
Expand Down
3 changes: 3 additions & 0 deletions sgpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"API_BASE_URL": os.getenv("API_BASE_URL", "default"),
"PRETTIFY_MARKDOWN": os.getenv("PRETTIFY_MARKDOWN", "true"),
"USE_LITELLM": os.getenv("USE_LITELLM", "false"),
"SHELL_INTERACTION ": os.getenv("SHELL_INTERACTION ", "true"),
"OVERWRITE_OS_NAME": os.getenv("OVERWRITE_OS_NAME", ""),
"OVERWRITE_SHELL_NAME ": os.getenv("OVERWRITE_SHELL_NAME", ""),
Robinsane marked this conversation as resolved.
Show resolved Hide resolved
# New features might add their own config variables here.
}

Expand Down
32 changes: 19 additions & 13 deletions sgpt/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,28 @@ def get_role_name(cls, initial_message: str) -> Optional[str]:

@classmethod
def _os_name(cls) -> str:
current_platform = platform.system()
if current_platform == "Linux":
return "Linux/" + distro_name(pretty=True)
if current_platform == "Windows":
return "Windows " + platform.release()
if current_platform == "Darwin":
return "Darwin/MacOS " + platform.mac_ver()[0]
return current_platform
if cfg.get("OVERWRITE_OS_NAME") != "":
return cfg.get("OVERWRITE_OS_NAME")
else:
current_platform = platform.system()
if current_platform == "Linux":
return "Linux/" + distro_name(pretty=True)
if current_platform == "Windows":
return "Windows " + platform.release()
if current_platform == "Darwin":
return "Darwin/MacOS " + platform.mac_ver()[0]
return current_platform
Robinsane marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def _shell_name(cls) -> str:
current_platform = platform.system()
if current_platform in ("Windows", "nt"):
is_powershell = len(getenv("PSModulePath", "").split(pathsep)) >= 3
return "powershell.exe" if is_powershell else "cmd.exe"
return basename(getenv("SHELL", "/bin/sh"))
if cfg.get("OVERWRITE_SHELL_NAME") != "":
return cfg.get("OVERWRITE_SHELL_NAME")
else:
current_platform = platform.system()
if current_platform in ("Windows", "nt"):
is_powershell = len(getenv("PSModulePath", "").split(pathsep)) >= 3
return "powershell.exe" if is_powershell else "cmd.exe"
return basename(getenv("SHELL", "/bin/sh"))
Robinsane marked this conversation as resolved.
Show resolved Hide resolved

@property
def _exists(self) -> bool:
Expand Down