ractogateway.ollama_developer_kit.server
OllamaServerManager — start and stop an Ollama server from Python.
Useful when you want to control the Ollama process lifecycle (custom port, programmatic startup/shutdown) directly from your application code.
Usage:
from ractogateway import ollama_developer_kit as local
with local.OllamaServerManager(port=11500) as srv:
kit = local.Chat(model="llama3.2", base_url=srv.base_url)
response = kit.chat(local.ChatConfig(user_message="Hello!"))
print(response.content)
Or manually:
srv = OllamaServerManager(port=11500)
srv.start()
...
srv.stop()
- class ractogateway.ollama_developer_kit.server.OllamaServerManager(*, host='127.0.0.1', port=11434, startup_timeout=30.0, ollama_bin='ollama')[source]
Bases:
objectManage the lifecycle of an Ollama server subprocess.
The server is started with the
OLLAMA_HOSTenvironment variable set to{host}:{port}, which makes Ollama listen on the requested address.- Parameters:
host (
str) – Bind address. Defaults to"127.0.0.1"(localhost only).port (
int) – TCP port for the Ollama REST API. Defaults to11434(the standard Ollama port). Change this to run multiple Ollama instances or avoid conflicts with an already-running server.startup_timeout (
float) – Seconds to wait for the server to become ready after starting the subprocess. RaisesTimeoutErrorif the server doesn’t respond within this window.ollama_bin (
str) – Path to theollamaexecutable. Defaults to"ollama"(looked up via PATH).
- base_url
The full
http://{host}:{port}URL of the managed server. Use this to construct aOllamaDeveloperKit:kit = local.Chat(model="llama3.2", base_url=srv.base_url)
- Type:
Examples
Context manager (recommended — guarantees cleanup):
with OllamaServerManager(port=11500) as srv: kit = local.Chat(model="llama3.2", base_url=srv.base_url) print(kit.chat(local.ChatConfig(user_message="Hi")).content)
Manual start / stop:
srv = OllamaServerManager(port=11500) srv.start() try: ... finally: srv.stop()
- property base_url: str
Return
http://{host}:{port}.
- property is_running: bool
Truewhen the subprocess is alive.
- start()[source]
Start the Ollama server subprocess.
Returns self so that the call can be chained:
srv = OllamaServerManager(port=11500).start()
- Raises:
RuntimeError – If the server is already running.
FileNotFoundError – If the
ollamabinary cannot be found.TimeoutError – If the server does not become ready within startup_timeout seconds.
- Return type:
OllamaServerManager
- stop()[source]
Stop the Ollama server subprocess gracefully.
Sends SIGTERM first; if the process doesn’t exit within 5 seconds, SIGKILL is used. Silently does nothing if the server is not running.
- Return type:
- pull(model)[source]
Pull model from the Ollama library into the running server.
Equivalent to running
ollama pull <model>in a shell, but scoped to the server managed by this instance viaOLLAMA_HOST.- Parameters:
model (
str) – Model name, e.g."llama3.2","nomic-embed-text".- Raises:
RuntimeError – If the server is not running.
subprocess.CalledProcessError – If
ollama pullexits with a non-zero status.
- Return type:
- list_models()[source]
Return the names of locally-available models on this server.
Uses a lightweight HTTP request to the
/api/tagsendpoint instead of a subprocess so it works even whenollamaCLI is not on PATH.