llm-lab-runtime 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- llm_lab/__init__.py +0 -0
- llm_lab/cli/__init__.py +12 -0
- llm_lab/cli/db/__init__.py +37 -0
- llm_lab/cli/db/deps.py +17 -0
- llm_lab/cli/dependent_typer.py +83 -0
- llm_lab/cli/jobs/__init__.py +134 -0
- llm_lab/cli/jobs/deps.py +76 -0
- llm_lab/cli/secrets/__init__.py +42 -0
- llm_lab/cli/yaml_files.py +19 -0
- llm_lab/database.py +26 -0
- llm_lab/dependencies.py +140 -0
- llm_lab/errors.py +2 -0
- llm_lab/jobs/__init__.py +0 -0
- llm_lab/jobs/access.py +151 -0
- llm_lab/jobs/code.py +31 -0
- llm_lab/jobs/orchestrator.py +323 -0
- llm_lab/jobs/protocol.py +210 -0
- llm_lab/jobs/tables.py +26 -0
- llm_lab/jobs/vast.py +344 -0
- llm_lab/keyring_settings.py +50 -0
- llm_lab/migrations/__init__.py +0 -0
- llm_lab/migrations/env.py +13 -0
- llm_lab/migrations/script.py.mako +17 -0
- llm_lab/migrations/versions/9395d98d9cb4_create_job_transitions.py +31 -0
- llm_lab/migrations/versions/__init__.py +0 -0
- llm_lab/py.typed +0 -0
- llm_lab/runtime/__init__.py +0 -0
- llm_lab/runtime/chat.py +164 -0
- llm_lab/runtime/deps.py +26 -0
- llm_lab/runtime/serving.py +48 -0
- llm_lab/runtime/settings.py +43 -0
- llm_lab/secrets.py +29 -0
- llm_lab/settings.py +141 -0
- llm_lab_runtime-0.2.0.dist-info/METADATA +20 -0
- llm_lab_runtime-0.2.0.dist-info/RECORD +37 -0
- llm_lab_runtime-0.2.0.dist-info/WHEEL +4 -0
- llm_lab_runtime-0.2.0.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""create job transitions"""
|
|
2
|
+
|
|
3
|
+
import sqlalchemy as sa
|
|
4
|
+
from alembic import op
|
|
5
|
+
|
|
6
|
+
revision: str = "9395d98d9cb4"
|
|
7
|
+
down_revision: str | None = None
|
|
8
|
+
branch_labels: str | tuple[str, ...] | None = None
|
|
9
|
+
depends_on: str | tuple[str, ...] | None = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def upgrade() -> None:
|
|
13
|
+
op.create_table(
|
|
14
|
+
"job_transitions",
|
|
15
|
+
sa.Column("sequence", sa.Integer(), nullable=False),
|
|
16
|
+
sa.Column("name", sa.String(), nullable=False),
|
|
17
|
+
sa.Column("condition", sa.String(), nullable=False),
|
|
18
|
+
sa.Column("recorded_at", sa.DateTime(), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False),
|
|
19
|
+
sa.Column("record", sa.String(), nullable=False),
|
|
20
|
+
sa.PrimaryKeyConstraint("sequence"),
|
|
21
|
+
sqlite_autoincrement=True,
|
|
22
|
+
)
|
|
23
|
+
with op.batch_alter_table("job_transitions", schema=None) as batch_op:
|
|
24
|
+
batch_op.create_index("job_transitions_by_name", ["name", "sequence"], unique=False)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def downgrade() -> None:
|
|
28
|
+
with op.batch_alter_table("job_transitions", schema=None) as batch_op:
|
|
29
|
+
batch_op.drop_index("job_transitions_by_name")
|
|
30
|
+
|
|
31
|
+
op.drop_table("job_transitions")
|
|
File without changes
|
llm_lab/py.typed
ADDED
|
File without changes
|
|
File without changes
|
llm_lab/runtime/chat.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from collections.abc import Callable, Sequence
|
|
2
|
+
from typing import Annotated, Literal, Protocol, assert_never
|
|
3
|
+
|
|
4
|
+
from openai import AsyncOpenAI
|
|
5
|
+
from openai.types import CompletionUsage
|
|
6
|
+
from openai.types.chat import (
|
|
7
|
+
ChatCompletionFunctionToolParam,
|
|
8
|
+
ChatCompletionMessage,
|
|
9
|
+
ChatCompletionMessageFunctionToolCall,
|
|
10
|
+
ChatCompletionMessageFunctionToolCallParam,
|
|
11
|
+
ChatCompletionMessageParam,
|
|
12
|
+
ChatCompletionMessageToolCallUnion,
|
|
13
|
+
)
|
|
14
|
+
from openai.types.chat.chat_completion import Choice
|
|
15
|
+
from pydantic import BaseModel, Field, JsonValue, TypeAdapter
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class FunctionTool(BaseModel):
|
|
19
|
+
name: str
|
|
20
|
+
description: str
|
|
21
|
+
parameters: dict[str, JsonValue]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class FunctionCall(BaseModel):
|
|
25
|
+
name: str
|
|
26
|
+
arguments: dict[str, JsonValue]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ToolCall(BaseModel):
|
|
30
|
+
id: str
|
|
31
|
+
function: FunctionCall
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SystemMessage(BaseModel):
|
|
35
|
+
role: Literal["system"] = "system"
|
|
36
|
+
content: str
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class UserMessage(BaseModel):
|
|
40
|
+
role: Literal["user"] = "user"
|
|
41
|
+
content: str
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class AssistantMessage(BaseModel):
|
|
45
|
+
role: Literal["assistant"] = "assistant"
|
|
46
|
+
content: str | None = None
|
|
47
|
+
tool_calls: list[ToolCall] = []
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ToolMessage(BaseModel):
|
|
51
|
+
role: Literal["tool"] = "tool"
|
|
52
|
+
tool_call_id: str
|
|
53
|
+
content: str
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
type ChatMessage = Annotated[SystemMessage | UserMessage | AssistantMessage | ToolMessage, Field(discriminator="role")]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class Usage(BaseModel):
|
|
60
|
+
prompt_tokens: int
|
|
61
|
+
completion_tokens: int
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class Completion(BaseModel):
|
|
65
|
+
message: AssistantMessage
|
|
66
|
+
usage: Usage
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ChatModel(Protocol):
|
|
70
|
+
async def complete(self, messages: Sequence[ChatMessage], tools: Sequence[FunctionTool] = ()) -> Completion: ...
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
type ChatModelFactory = Callable[[str], ChatModel]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def to_openai_tool(tool: FunctionTool) -> ChatCompletionFunctionToolParam:
|
|
77
|
+
return {
|
|
78
|
+
"type": "function",
|
|
79
|
+
"function": {
|
|
80
|
+
"name": tool.name,
|
|
81
|
+
"description": tool.description,
|
|
82
|
+
"parameters": dict[str, object](tool.parameters),
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def to_openai_tool_call(call: ToolCall) -> ChatCompletionMessageFunctionToolCallParam:
|
|
88
|
+
return {
|
|
89
|
+
"id": call.id,
|
|
90
|
+
"type": "function",
|
|
91
|
+
"function": {
|
|
92
|
+
"name": call.function.name,
|
|
93
|
+
"arguments": TypeAdapter(dict[str, JsonValue]).dump_json(call.function.arguments).decode(),
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def to_openai_message(message: ChatMessage) -> ChatCompletionMessageParam:
|
|
99
|
+
match message:
|
|
100
|
+
case SystemMessage(content=content):
|
|
101
|
+
return {"role": "system", "content": content}
|
|
102
|
+
case UserMessage(content=content):
|
|
103
|
+
return {"role": "user", "content": content}
|
|
104
|
+
case AssistantMessage(content=content, tool_calls=[]):
|
|
105
|
+
return {"role": "assistant", "content": content}
|
|
106
|
+
case AssistantMessage(content=content, tool_calls=calls):
|
|
107
|
+
return {
|
|
108
|
+
"role": "assistant",
|
|
109
|
+
"content": content,
|
|
110
|
+
"tool_calls": [to_openai_tool_call(call) for call in calls],
|
|
111
|
+
}
|
|
112
|
+
case ToolMessage(tool_call_id=tool_call_id, content=content):
|
|
113
|
+
return {"role": "tool", "tool_call_id": tool_call_id, "content": content}
|
|
114
|
+
case _:
|
|
115
|
+
assert_never(message)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def from_openai_tool_call(call: ChatCompletionMessageToolCallUnion) -> ToolCall:
|
|
119
|
+
match call:
|
|
120
|
+
case ChatCompletionMessageFunctionToolCall(id=call_id, function=function):
|
|
121
|
+
arguments = TypeAdapter(dict[str, JsonValue]).validate_json(function.arguments)
|
|
122
|
+
return ToolCall(id=call_id, function=FunctionCall(name=function.name, arguments=arguments))
|
|
123
|
+
case _:
|
|
124
|
+
raise ValueError(f"expected a function tool call, got {call!r}")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def from_openai_choices(choices: list[Choice]) -> AssistantMessage:
|
|
128
|
+
match choices:
|
|
129
|
+
case [
|
|
130
|
+
Choice(finish_reason="stop", message=ChatCompletionMessage(content=str() as content, tool_calls=None | []))
|
|
131
|
+
]:
|
|
132
|
+
return AssistantMessage(content=content)
|
|
133
|
+
case [
|
|
134
|
+
Choice(
|
|
135
|
+
finish_reason="tool_calls", message=ChatCompletionMessage(content=content, tool_calls=[_, *_] as calls)
|
|
136
|
+
)
|
|
137
|
+
]:
|
|
138
|
+
return AssistantMessage(content=content, tool_calls=[from_openai_tool_call(call) for call in calls])
|
|
139
|
+
case _:
|
|
140
|
+
raise ValueError(f"expected one reply that stopped by itself or called tools, got {choices!r}")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def from_openai_usage(usage: CompletionUsage | None) -> Usage:
|
|
144
|
+
match usage:
|
|
145
|
+
case CompletionUsage(prompt_tokens=prompt_tokens, completion_tokens=completion_tokens):
|
|
146
|
+
return Usage(prompt_tokens=prompt_tokens, completion_tokens=completion_tokens)
|
|
147
|
+
case None:
|
|
148
|
+
raise ValueError("expected the server to report the token usage of the completion")
|
|
149
|
+
case _:
|
|
150
|
+
assert_never(usage)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class OpenAIChatModel:
|
|
154
|
+
def __init__(self, client: AsyncOpenAI, model: str) -> None:
|
|
155
|
+
self.client = client
|
|
156
|
+
self.model = model
|
|
157
|
+
|
|
158
|
+
async def complete(self, messages: Sequence[ChatMessage], tools: Sequence[FunctionTool] = ()) -> Completion:
|
|
159
|
+
completion = await self.client.chat.completions.create(
|
|
160
|
+
model=self.model,
|
|
161
|
+
messages=[to_openai_message(message) for message in messages],
|
|
162
|
+
tools=[to_openai_tool(tool) for tool in tools],
|
|
163
|
+
)
|
|
164
|
+
return Completion(message=from_openai_choices(completion.choices), usage=from_openai_usage(completion.usage))
|
llm_lab/runtime/deps.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from collections.abc import AsyncIterator
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
from openai import AsyncOpenAI
|
|
5
|
+
from pregel_redux import Depends, Scope
|
|
6
|
+
|
|
7
|
+
from llm_lab.runtime.chat import ChatModel, ChatModelFactory, OpenAIChatModel
|
|
8
|
+
from llm_lab.runtime.settings import RuntimeSettings, get_runtime_settings
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def get_oai_model_factory(
|
|
12
|
+
settings: Annotated[RuntimeSettings, Depends(get_runtime_settings, scope=Scope.run)],
|
|
13
|
+
) -> AsyncIterator[ChatModelFactory]:
|
|
14
|
+
async with AsyncOpenAI(
|
|
15
|
+
base_url=f"{settings.vllm_base_url}/v1",
|
|
16
|
+
api_key=settings.vllm_api_key.get_secret_value(),
|
|
17
|
+
timeout=settings.vllm_request_timeout_seconds,
|
|
18
|
+
) as client:
|
|
19
|
+
|
|
20
|
+
def create_chat_model(model: str) -> ChatModel:
|
|
21
|
+
return OpenAIChatModel(client, model)
|
|
22
|
+
|
|
23
|
+
yield create_chat_model
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
type ChatModelFactoryDep = Annotated[ChatModelFactory, Depends(get_oai_model_factory, scope=Scope.run)]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import time
|
|
3
|
+
from collections.abc import Generator, Sequence
|
|
4
|
+
from contextlib import contextmanager
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from llm_lab.runtime.settings import RuntimeSettings
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def build_vllm_command(model: str, vllm_args: Sequence[str], port: int) -> list[str]:
|
|
12
|
+
return ["vllm", "serve", model, "--host", "127.0.0.1", "--port", str(port), *vllm_args]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def is_healthy(http: httpx.Client) -> bool:
|
|
16
|
+
try:
|
|
17
|
+
return http.get("/health").status_code == httpx.codes.OK
|
|
18
|
+
except httpx.TransportError:
|
|
19
|
+
return False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class VllmServer:
|
|
23
|
+
def __init__(self, settings: RuntimeSettings) -> None:
|
|
24
|
+
self.settings = settings
|
|
25
|
+
|
|
26
|
+
def wait_until_ready(self, process: subprocess.Popen[bytes]) -> None:
|
|
27
|
+
deadline = time.monotonic() + self.settings.vllm_startup_timeout_seconds
|
|
28
|
+
with httpx.Client(base_url=self.settings.vllm_base_url, timeout=self.settings.vllm_poll_seconds) as http:
|
|
29
|
+
while (status := process.poll()) is None:
|
|
30
|
+
if is_healthy(http):
|
|
31
|
+
return
|
|
32
|
+
if time.monotonic() > deadline:
|
|
33
|
+
raise TimeoutError(f"vLLM was not ready after {self.settings.vllm_startup_timeout_seconds} seconds")
|
|
34
|
+
time.sleep(self.settings.vllm_poll_seconds)
|
|
35
|
+
raise RuntimeError(f"vLLM exited with status {status} before it was ready")
|
|
36
|
+
|
|
37
|
+
@contextmanager
|
|
38
|
+
def serve(self, model: str, vllm_args: Sequence[str] = ()) -> Generator[None]:
|
|
39
|
+
with subprocess.Popen(build_vllm_command(model, vllm_args, self.settings.vllm_port)) as process:
|
|
40
|
+
try:
|
|
41
|
+
self.wait_until_ready(process)
|
|
42
|
+
yield
|
|
43
|
+
finally:
|
|
44
|
+
process.terminate()
|
|
45
|
+
try:
|
|
46
|
+
process.wait(timeout=self.settings.vllm_stop_timeout_seconds)
|
|
47
|
+
except subprocess.TimeoutExpired:
|
|
48
|
+
process.kill()
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from functools import cache
|
|
2
|
+
|
|
3
|
+
from pydantic import Field, PositiveFloat, PositiveInt, SecretStr
|
|
4
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class RuntimeSettings(BaseSettings):
|
|
8
|
+
model_config = SettingsConfigDict(env_prefix="LLM_LAB_")
|
|
9
|
+
|
|
10
|
+
vllm_port: PositiveInt = Field(
|
|
11
|
+
default=8000, description="Port of the vLLM server on the loopback interface.", examples=[8000]
|
|
12
|
+
)
|
|
13
|
+
vllm_startup_timeout_seconds: PositiveFloat = Field(
|
|
14
|
+
default=3600,
|
|
15
|
+
description="Time limit in seconds for vLLM to download and load the model.",
|
|
16
|
+
examples=[3600],
|
|
17
|
+
)
|
|
18
|
+
vllm_poll_seconds: PositiveFloat = Field(
|
|
19
|
+
default=5, description="Delay in seconds between two health checks of vLLM.", examples=[5, 0.5]
|
|
20
|
+
)
|
|
21
|
+
vllm_api_key: SecretStr = Field(
|
|
22
|
+
default=SecretStr("NOT_REQUIRED"),
|
|
23
|
+
description="API key for vLLM.",
|
|
24
|
+
)
|
|
25
|
+
vllm_request_timeout_seconds: PositiveFloat = Field(
|
|
26
|
+
default=600,
|
|
27
|
+
description="Time limit in seconds for one chat completion request to vLLM.",
|
|
28
|
+
examples=[600],
|
|
29
|
+
)
|
|
30
|
+
vllm_stop_timeout_seconds: PositiveFloat = Field(
|
|
31
|
+
default=60,
|
|
32
|
+
description="Time in seconds that vLLM gets to stop before it is killed.",
|
|
33
|
+
examples=[60],
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def vllm_base_url(self) -> str:
|
|
38
|
+
return f"http://127.0.0.1:{self.vllm_port}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@cache
|
|
42
|
+
def get_runtime_settings() -> RuntimeSettings:
|
|
43
|
+
return RuntimeSettings()
|
llm_lab/secrets.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from pydantic import SecretStr
|
|
2
|
+
|
|
3
|
+
from llm_lab.errors import CommandError
|
|
4
|
+
from llm_lab.keyring_settings import KeyringEntry, SecretName, read_keyring_entry
|
|
5
|
+
from llm_lab.settings import Settings
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def find_secret_entry(name: SecretName) -> KeyringEntry:
|
|
9
|
+
entry = read_keyring_entry(Settings.model_fields[name.name], name.name)
|
|
10
|
+
if entry is None:
|
|
11
|
+
raise TypeError(f"the Settings field {name.name!r} has no KeyringEntry annotation")
|
|
12
|
+
return entry
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SettingsSecretSource:
|
|
16
|
+
def __init__(self, settings: Settings) -> None:
|
|
17
|
+
self.settings = settings
|
|
18
|
+
|
|
19
|
+
def read(self, name: SecretName) -> SecretStr:
|
|
20
|
+
match getattr(self.settings, name.name):
|
|
21
|
+
case SecretStr() as secret:
|
|
22
|
+
return secret
|
|
23
|
+
case None:
|
|
24
|
+
raise CommandError(f"The secret {name} is not set. Run `llm-lab secrets set {name}`.")
|
|
25
|
+
case value:
|
|
26
|
+
raise TypeError(f"the Settings field {name.name!r} has the type {type(value).__name__}")
|
|
27
|
+
|
|
28
|
+
def resolve(self, secret: str) -> str:
|
|
29
|
+
return self.read(SecretName(secret)).get_secret_value()
|
llm_lab/settings.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from functools import cache, cached_property
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Annotated
|
|
5
|
+
|
|
6
|
+
from pydantic import Field, PositiveFloat, PositiveInt, SecretStr
|
|
7
|
+
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict
|
|
8
|
+
from sqlalchemy import URL, Engine, create_engine, event
|
|
9
|
+
from sqlalchemy.engine.interfaces import DBAPIConnection
|
|
10
|
+
from sqlalchemy.pool import ConnectionPoolEntry
|
|
11
|
+
|
|
12
|
+
from llm_lab.dependencies import Depends
|
|
13
|
+
from llm_lab.keyring_settings import KeyringEntry, SecretName, SecretSettingsSource, read_keyring_password
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def apply_sqlite_pragmas(dbapi_connection: DBAPIConnection, connection_record: ConnectionPoolEntry) -> None:
|
|
17
|
+
cursor = dbapi_connection.cursor()
|
|
18
|
+
cursor.execute("PRAGMA foreign_keys=ON")
|
|
19
|
+
cursor.execute("PRAGMA journal_mode=WAL")
|
|
20
|
+
cursor.execute("PRAGMA synchronous=NORMAL")
|
|
21
|
+
cursor.execute("PRAGMA busy_timeout=5000")
|
|
22
|
+
cursor.close()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Settings(BaseSettings):
|
|
26
|
+
model_config = SettingsConfigDict(env_prefix="LLM_LAB_")
|
|
27
|
+
|
|
28
|
+
job_database_path: Path = Field(
|
|
29
|
+
default=Path(".llm-lab/jobs.sqlite3"),
|
|
30
|
+
description="Path of the SQLite database file that holds the state transitions of each job.",
|
|
31
|
+
examples=[".llm-lab/jobs.sqlite3", "/var/lib/llm-lab/jobs.sqlite3"],
|
|
32
|
+
)
|
|
33
|
+
hf_token: Annotated[SecretStr | None, KeyringEntry(service="llm-lab", username=SecretName.hf_token)] = Field(
|
|
34
|
+
default=None,
|
|
35
|
+
description="Hugging Face access token that a job receives as HF_TOKEN when its manifest lists HF_TOKEN in "
|
|
36
|
+
"secret_environment. Set it with `llm-lab secrets set HF_TOKEN`.",
|
|
37
|
+
)
|
|
38
|
+
vast_api_key: Annotated[SecretStr | None, KeyringEntry(service="llm-lab", username=SecretName.vast_api_key)] = (
|
|
39
|
+
Field(
|
|
40
|
+
default=None,
|
|
41
|
+
description="Vast.ai account API key that each request to the Vast.ai API sends. "
|
|
42
|
+
"Set it with `llm-lab secrets set VAST_API_KEY`.",
|
|
43
|
+
)
|
|
44
|
+
)
|
|
45
|
+
vast_api_url: str = Field(
|
|
46
|
+
default="https://console.vast.ai/api",
|
|
47
|
+
description="Base address of the Vast.ai API, without the version path.",
|
|
48
|
+
examples=["https://console.vast.ai/api"],
|
|
49
|
+
)
|
|
50
|
+
vast_request_timeout_seconds: PositiveFloat = Field(
|
|
51
|
+
default=60, description="Time limit in seconds for one HTTP request to Vast.ai.", examples=[60, 15.5]
|
|
52
|
+
)
|
|
53
|
+
vast_request_attempts: PositiveInt = Field(
|
|
54
|
+
default=5,
|
|
55
|
+
description="Maximum number of sends of one request when Vast.ai returns a rate limit or a server error.",
|
|
56
|
+
examples=[5],
|
|
57
|
+
)
|
|
58
|
+
vast_retry_delay_seconds: PositiveFloat = Field(
|
|
59
|
+
default=2,
|
|
60
|
+
description="Delay in seconds before the second send of a request. "
|
|
61
|
+
"Each later send multiplies the delay by the number of sends made.",
|
|
62
|
+
examples=[2, 0.5],
|
|
63
|
+
)
|
|
64
|
+
vast_offer_limit: PositiveInt = Field(
|
|
65
|
+
default=10,
|
|
66
|
+
description="Maximum number of offers that one search returns. A job tries them in order of price.",
|
|
67
|
+
examples=[10],
|
|
68
|
+
)
|
|
69
|
+
vast_log_tail_lines: PositiveInt = Field(
|
|
70
|
+
default=200,
|
|
71
|
+
description="Number of last log lines that are read from an instance to find the exit code of the job.",
|
|
72
|
+
examples=[200],
|
|
73
|
+
)
|
|
74
|
+
vast_ssh_port: PositiveInt = Field(
|
|
75
|
+
default=2222,
|
|
76
|
+
description="Port of the SSH server in the container of a job. Vast.ai maps it to a public port.",
|
|
77
|
+
examples=[2222],
|
|
78
|
+
)
|
|
79
|
+
job_access_dir: Path = Field(
|
|
80
|
+
default=Path(".llm-lab/access"),
|
|
81
|
+
description="Directory that holds the throwaway SSH key and the pinned host key of each running job.",
|
|
82
|
+
examples=[".llm-lab/access"],
|
|
83
|
+
)
|
|
84
|
+
job_downloads_dir: Path = Field(
|
|
85
|
+
default=Path(".llm-lab/downloads"),
|
|
86
|
+
description="Directory where `llm-lab jobs destroy` puts the files that it downloads from each job.",
|
|
87
|
+
examples=[".llm-lab/downloads"],
|
|
88
|
+
)
|
|
89
|
+
job_builds_dir: Path = Field(
|
|
90
|
+
default=Path(".llm-lab/builds"),
|
|
91
|
+
description="Directory where `llm-lab jobs submit` puts the wheel that it builds for each job.",
|
|
92
|
+
examples=[".llm-lab/builds"],
|
|
93
|
+
)
|
|
94
|
+
job_code_directory: str = Field(
|
|
95
|
+
default="/workspace/code",
|
|
96
|
+
pattern=r"^/",
|
|
97
|
+
description="Directory on the instance that receives the wheel of a job.",
|
|
98
|
+
examples=["/workspace/code"],
|
|
99
|
+
)
|
|
100
|
+
job_code_timeout_seconds: PositiveInt = Field(
|
|
101
|
+
default=900,
|
|
102
|
+
description="Time limit in seconds for the wheel of a job to reach its instance. After it, `llm-lab jobs "
|
|
103
|
+
"submit` destroys the instance, and an instance that still waits reports the exit code 124.",
|
|
104
|
+
examples=[900],
|
|
105
|
+
)
|
|
106
|
+
job_code_poll_seconds: PositiveFloat = Field(
|
|
107
|
+
default=10,
|
|
108
|
+
description="Delay in seconds between two attempts to upload the wheel of a job to its instance.",
|
|
109
|
+
examples=[10, 2.5],
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def settings_customise_sources(
|
|
114
|
+
cls,
|
|
115
|
+
settings_cls: type[BaseSettings],
|
|
116
|
+
init_settings: PydanticBaseSettingsSource,
|
|
117
|
+
env_settings: PydanticBaseSettingsSource,
|
|
118
|
+
dotenv_settings: PydanticBaseSettingsSource,
|
|
119
|
+
file_secret_settings: PydanticBaseSettingsSource,
|
|
120
|
+
) -> tuple[PydanticBaseSettingsSource, ...]:
|
|
121
|
+
return (
|
|
122
|
+
init_settings,
|
|
123
|
+
env_settings,
|
|
124
|
+
SecretSettingsSource(settings_cls, lambda entry: os.environ.get(entry.username)),
|
|
125
|
+
SecretSettingsSource(settings_cls, read_keyring_password),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
@cached_property
|
|
129
|
+
def database_engine(self) -> Engine:
|
|
130
|
+
self.job_database_path.parent.mkdir(parents=True, exist_ok=True)
|
|
131
|
+
engine = create_engine(URL.create("sqlite", database=str(self.job_database_path)))
|
|
132
|
+
event.listen(engine, "connect", apply_sqlite_pragmas)
|
|
133
|
+
return engine
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@cache
|
|
137
|
+
def get_settings() -> Settings:
|
|
138
|
+
return Settings()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
type SettingsDep = Annotated[Settings, Depends(get_settings)]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: llm-lab-runtime
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Run jobs on rented Vast.ai instances and manage their lifecycle.
|
|
5
|
+
Requires-Dist: alembic>=1.20.1
|
|
6
|
+
Requires-Dist: cryptography>=50.0.2
|
|
7
|
+
Requires-Dist: httpx>=0.28.1
|
|
8
|
+
Requires-Dist: keyring>=25.7.0
|
|
9
|
+
Requires-Dist: pycountry>=26.2.16
|
|
10
|
+
Requires-Dist: pydantic>=2.14.4
|
|
11
|
+
Requires-Dist: pydantic-extra-types>=2.11.1
|
|
12
|
+
Requires-Dist: pydantic-settings>=2.16.3
|
|
13
|
+
Requires-Dist: pyyaml>=6.0.4
|
|
14
|
+
Requires-Dist: questionary>=2.1.1
|
|
15
|
+
Requires-Dist: sqlalchemy>=2.1.9
|
|
16
|
+
Requires-Dist: typer>=0.29.2
|
|
17
|
+
Requires-Dist: openai>=3.60.0 ; extra == 'runtime'
|
|
18
|
+
Requires-Dist: pregel-redux>=0.6.0 ; extra == 'runtime'
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Provides-Extra: runtime
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
llm_lab/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
llm_lab/cli/__init__.py,sha256=szm4KxAx64md3QSiKckfjw3lbC7eoGjWKPSI3KrTwDs,277
|
|
3
|
+
llm_lab/cli/db/__init__.py,sha256=cx7cJPGdZIEj4WPjeN6hzc0U4K9UTX5j00kafAR3Mis,1176
|
|
4
|
+
llm_lab/cli/db/deps.py,sha256=zwnFn69Pbr8zZXWcnWOAYpvNUFK3I4aQz5ZLMbC_fUM,517
|
|
5
|
+
llm_lab/cli/dependent_typer.py,sha256=8nPsk9dFI1tSQ1xaW1aXW6VqBbLERbmxmh1DNBjVxp8,2985
|
|
6
|
+
llm_lab/cli/jobs/__init__.py,sha256=2x3RYJDRCLh6KgPmtDEoFPELK_76ZKt5EJhT69gSpGs,4542
|
|
7
|
+
llm_lab/cli/jobs/deps.py,sha256=wS3Mx3ilQwZsQJPkV03wMJjdMufSjoAOheH7qa2nXRA,2316
|
|
8
|
+
llm_lab/cli/secrets/__init__.py,sha256=ot7J09_nRl92y01WhKyQEsQVEKiuHyErHkrA822fMrc,1236
|
|
9
|
+
llm_lab/cli/yaml_files.py,sha256=sXcaMSPL5Hbl1A6EYUSC-VBJJzP6a16Pp1t5TzzWTEY,663
|
|
10
|
+
llm_lab/database.py,sha256=GlGxDxRxtS2gFDLr6Z8Rl_kjWPYOJIt21nHxCu5z7T8,903
|
|
11
|
+
llm_lab/dependencies.py,sha256=GA61DUxyVmbass4gUZC7m7tIRaJfgcbhb2NPoNnUzJk,5608
|
|
12
|
+
llm_lab/errors.py,sha256=GBd-Fw-pvtsUNg6wrub7yhY2venv1MD1eMuJZebJiMY,40
|
|
13
|
+
llm_lab/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
llm_lab/jobs/access.py,sha256=7-tTufOXd5eEBUJvujrZjABWwKELNmm473aXqxM7wv8,5211
|
|
15
|
+
llm_lab/jobs/code.py,sha256=jInkxTu5pI5512ytQj6IG2jhlQvrOVb9JWKDD07iRPg,1349
|
|
16
|
+
llm_lab/jobs/orchestrator.py,sha256=pgj9wk55jrKabi7Id3F0TEa4YyaVf35Us-HQuQ3JtoM,12356
|
|
17
|
+
llm_lab/jobs/protocol.py,sha256=yrrrPVBrHlJYQP4oE5w-C3Y1aAyTnehaP5GUajilAW0,7333
|
|
18
|
+
llm_lab/jobs/tables.py,sha256=vvUCUh2dkXbzi3u_I_qa7Z5fOY_-KG99iZpXhX8Qii4,932
|
|
19
|
+
llm_lab/jobs/vast.py,sha256=rUeqjj5jcbHbfUfru5BRCRaClKBYI9N95UBgMDlw6ek,13110
|
|
20
|
+
llm_lab/keyring_settings.py,sha256=F99LEhxYQuHzBJRhaMEPDbI_8q3xKaMaEzgEmo3lZxw,1715
|
|
21
|
+
llm_lab/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
|
+
llm_lab/migrations/env.py,sha256=6dYg5na0USji4gPuVXJY6wvK2xp1Sldodvlzz-Eu0dU,524
|
|
23
|
+
llm_lab/migrations/script.py.mako,sha256=tgSJXw2EZZjDl6RFPFL7sZAGVY4YfcZj71TtzKNB6Uo,451
|
|
24
|
+
llm_lab/migrations/versions/9395d98d9cb4_create_job_transitions.py,sha256=ysQ_QRDqcOilAF3c7SFpCE35bWjpEiSVDu1cXplMVw0,1111
|
|
25
|
+
llm_lab/migrations/versions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
|
+
llm_lab/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
27
|
+
llm_lab/runtime/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
28
|
+
llm_lab/runtime/chat.py,sha256=g_rz3BCezrUOJwJbQCXW7EMVxzVAOtEvVAPQMOcrsK8,5453
|
|
29
|
+
llm_lab/runtime/deps.py,sha256=28fgPYnZwzIuJxiNq3Wc-eP9iEW_ihlEyX_LROsfESc,913
|
|
30
|
+
llm_lab/runtime/serving.py,sha256=3txReTcT536YCDWhYY1lktFK8gQE1kE7LI0q9aA7-UU,1891
|
|
31
|
+
llm_lab/runtime/settings.py,sha256=Hg4lFHk0NXLlsuPMW_2yQViobcb2g1d8AV1AMDP3MQc,1452
|
|
32
|
+
llm_lab/secrets.py,sha256=bqA4At0R-Jti9T4-HKrEybSFsfwza7MPn_pfNHe3WYw,1107
|
|
33
|
+
llm_lab/settings.py,sha256=dH8Xo40l1aNsLgenNv6cGZzta0-txilWTWcASMPiZ3I,5928
|
|
34
|
+
llm_lab_runtime-0.2.0.dist-info/WHEEL,sha256=-gI5GRFdiNNn2CFz-ptlExISMINC-k8bFxeojAlHbrM,81
|
|
35
|
+
llm_lab_runtime-0.2.0.dist-info/entry_points.txt,sha256=oieBS3y4awSrQ3ZmboSFz0YTX0JdktidbWHyu3K6YHk,45
|
|
36
|
+
llm_lab_runtime-0.2.0.dist-info/METADATA,sha256=SdzGg3MPkGhnjDkSGLadyDM6Ux52Q10hF6HYmxusk_M,691
|
|
37
|
+
llm_lab_runtime-0.2.0.dist-info/RECORD,,
|