Skip to content

Commit

Permalink
Merge pull request #222 from rhatdan/containers
Browse files Browse the repository at this point in the history
Add --container and --nocontainer
  • Loading branch information
rhatdan authored Oct 2, 2024
2 parents 6248ce7 + 3a601ed commit 140a5e5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
1 change: 1 addition & 0 deletions .codespelldict
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
hugginface->huggingface
ramalamay->ramalama
ramamlama->ramalama
olama->ollama
Expand Down
5 changes: 4 additions & 1 deletion docs/ramalama.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ $ cat /usr/share/ramalama/shortnames.conf

## GLOBAL OPTIONS

#### **--container**
run RamaLama in the default container (default: True)
use environment variable "RAMALAMA_IN_CONTAINER=false" to change default.

#### **--dryrun**
show container runtime command without executing it (default: False)

Expand All @@ -75,7 +79,6 @@ show this help message and exit

#### **--nocontainer**
do not run RamaLama in the default container (default: False)
use environment variable "RAMALAMA_IN_CONTAINER=false" to change default.

#### **--runtime**
specify the runtime to use, valid options are 'llama.cpp' and 'vllm' (default: llama.cpp)
Expand Down
38 changes: 26 additions & 12 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class HelpException(Exception):

def use_container():
transport = os.getenv("RAMALAMA_IN_CONTAINER", "true")
return transport.lower() == "true"
if transport != "":
return transport.lower() == "true"

if in_container() or sys.platform == "darwin":
return False
return True


def init_cli():
Expand All @@ -40,9 +45,17 @@ def init_cli():
)
parser.add_argument("--dry-run", dest="dryrun", action="store_true", help=argparse.SUPPRESS)
parser.add_argument(
"--nocontainer",
default=not use_container(),
"--container",
dest="container",
default=use_container(),
action="store_true",
help="run RamaLama in the default container",
)
parser.add_argument(
"--nocontainer",
dest="container",
default=False,
action="store_false",
help="do not run RamaLama in the default container",
)
parser.add_argument(
Expand Down Expand Up @@ -86,7 +99,7 @@ def init_cli():
def login_parser(subparsers):
parser = subparsers.add_parser("login", help="login to remote registry")
# Do not run in a container
parser.add_argument("--nocontainer", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.add_argument("-p", "--password", dest="password", help="password for registry")
parser.add_argument(
"--password-stdin", dest="passwordstdin", action="store_true", help="take the password for registry from stdin"
Expand All @@ -108,7 +121,7 @@ def login_cli(args):
def logout_parser(subparsers):
parser = subparsers.add_parser("logout", help="logout from remote registry")
# Do not run in a container
parser.add_argument("--nocontainer", default=True, action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.add_argument("--token", help="token for registry")
parser.add_argument("TRANSPORT", nargs="?", type=str, default="") # positional argument
parser.add_argument("TRANSPORT", nargs="?", type=str, default="") # positional argument
Expand Down Expand Up @@ -181,7 +194,7 @@ def containers_parser(subparsers):
parser = subparsers.add_parser("containers", aliases=["ps"], help="list all RamaLama containers")
parser.add_argument("--format", help="pretty-print containers to JSON or using a Go template")
parser.add_argument("-n", "--noheading", dest="noheading", action="store_true", help="do not display heading")
parser.add_argument("--nocontainer", default=True, action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.set_defaults(func=list_containers)


Expand Down Expand Up @@ -283,7 +296,7 @@ def list_cli(args):
def help_parser(subparsers):
parser = subparsers.add_parser("help", help="help about any command")
# Do not run in a container
parser.add_argument("--nocontainer", default=True, action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.set_defaults(func=help_cli)


Expand Down Expand Up @@ -353,7 +366,7 @@ def serve_parser(subparsers):


def serve_cli(args):
if args.nocontainer:
if not args.container:
args.detach = False
model = New(args.MODEL)
model.serve(args)
Expand All @@ -366,7 +379,7 @@ def stop_cli(args):

def stop_parser(subparsers):
parser = subparsers.add_parser("stop", help="stop named container that is running AI Model")
parser.add_argument("--nocontainer", default=True, action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.add_argument("-a", "--all", action="store_true", help="stop all RamaLama containers")
parser.add_argument(
"--ignore", action="store_true", help="ignore errors when specified RamaLama container is missing"
Expand Down Expand Up @@ -402,7 +415,7 @@ def stop_container(args):
def version_parser(subparsers):
parser = subparsers.add_parser("version", help="display version of AI Model")
# Do not run in a container
parser.add_argument("--nocontainer", default=True, action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--container", default=False, action="store_false", help=argparse.SUPPRESS)
parser.set_defaults(func=version)


Expand Down Expand Up @@ -442,15 +455,16 @@ def run_container(args):
if hasattr(args, "generate") and args.generate:
return False

if args.nocontainer:
if not args.container:
if hasattr(args, "name") and args.name:
raise IndexError("--nocontainer and --name options conflict. --name requires a container.")

# --nocontainer implies --detach=false
if hasattr(args, "detach"):
args.detach = False
return False

if args.nocontainer or in_container() or sys.platform == "darwin":
if in_container():
return False

conman = container_manager()
Expand Down
30 changes: 19 additions & 11 deletions ramalama/huggingface.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from ramalama.common import run_cmd, exec_cmd, perror
from ramalama.common import run_cmd, exec_cmd
from ramalama.model import Model

missing_hugginface="""
missing_huggingface = """
Huggingface models requires the huggingface-cli and tldm modules.
These modules can be installed via PyPi tools like pip, pip3, pipx or via
distribution package managers like dnf or apt. Example:
Expand All @@ -29,9 +29,12 @@ def try_download(store, model, directory, filename):
proc = download(store, model, directory, filename)
return proc.stdout.decode("utf-8")
except FileNotFoundError as e:
raise NotImplementedError("""\
raise NotImplementedError(
"""\
%s
%s""" % (str(e).strip("'"), missing_hugginface))
%s"""
% (str(e).strip("'"), missing_huggingface)
)


class Huggingface(Model):
Expand All @@ -46,11 +49,12 @@ def login(self, args):
try:
self.exec(conman_args)
except FileNotFoundError as e:
raise NotImplementedError("""\
%s
%s
""" % str(e).strip("'"), missing_hugginface)
raise NotImplementedError(
"""\
%s
%s"""
% (str(e).strip("'"), missing_huggingface)
)

def logout(self, args):
conman_args = ["huggingface-cli", "logout"]
Expand Down Expand Up @@ -96,8 +100,12 @@ def exec(self, args):
try:
exec_cmd(args)
except FileNotFoundError as e:
raise NotImplementedError("""\
raise NotImplementedError(
"""\
%s
%s
""" % str(e).strip("'"), missing_hugginface)
"""
% str(e).strip("'"),
missing_huggingface,
)
8 changes: 7 additions & 1 deletion ramalama/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

"""Version of RamaLamaPy."""

__version__ = importlib.metadata.version("ramalama")

__version__ = 0


def version(args):
try:
__version__ = importlib.metadata.version("ramalama")
except:
pass

print("ramalama version " + __version__)

0 comments on commit 140a5e5

Please sign in to comment.