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

Add uv pyproject, verbose parameter, and ruff formatting #1

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions ollama-modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

MODEL = os.environ.get("MODEL", "llama3:instruct")


def pull(model: str = MODEL):
subprocess.run(["systemctl", "daemon-reload"])
subprocess.run(["systemctl", "enable", "ollama"])
subprocess.run(["systemctl", "start", "ollama"])
time.sleep(2) # 2s, wait for the service to start
subprocess.run(["ollama", "pull", model], stdout=subprocess.PIPE)


image = (
modal.Image
.debian_slim()
modal.Image.debian_slim()
.apt_install("curl", "systemctl")
.run_commands( # from https://github.com/ollama/ollama/blob/main/docs/linux.md
.run_commands( # from https://github.com/ollama/ollama/blob/main/docs/linux.md
"curl -L https://ollama.com/download/ollama-linux-amd64.tgz -o ollama-linux-amd64.tgz",
"tar -C /usr -xzf ollama-linux-amd64.tgz",
"useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama",
Expand All @@ -34,6 +35,7 @@ def pull(model: str = MODEL):
with image.imports():
import ollama


@app.cls(gpu="a10g", container_idle_timeout=300)
class Ollama:
@build()
Expand All @@ -54,17 +56,17 @@ def load(self):
subprocess.run(["systemctl", "start", "ollama"])

@method()
def infer(self, text: str):
def infer(self, text: str, verbose: bool = False):
stream = ollama.chat(
model=MODEL,
messages=[{'role': 'user', 'content': text}],
stream=True
model=MODEL, messages=[{"role": "user", "content": text}], stream=True
)
for chunk in stream:
yield chunk['message']['content']
print(chunk['message']['content'], end='', flush=True)
yield chunk["message"]["content"]
if verbose:
print(chunk["message"]["content"], end="", flush=True)
return


# Convenience thing, to run using:
#
# $ modal run ollama-modal.py [--lookup] [--text "Why is the sky blue?"]
Expand All @@ -74,5 +76,5 @@ def main(text: str = "Why is the sky blue?", lookup: bool = False):
ollama = modal.Cls.lookup("ollama", "Ollama")
else:
ollama = Ollama()
for chunk in ollama.infer.remote_gen(text):
print(chunk, end='', flush=False)
for chunk in ollama.infer.remote_gen(text, verbose=False):
print(chunk, end="", flush=False)
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
name = "ollama-modal"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"modal>=0.73.51",
"ruff>=0.9.6",
]