1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
"""Configuration loaded from environment variables.
Agentux is provider-agnostic: it speaks the OpenAI-compatible HTTP API, and
every provider that exposes that shape (OpenAI, Ollama, LiteLLM, vLLM, Qwen,
LM Studio, ...) works by changing ``AGENTUX_BASE_URL`` and ``AGENTUX_MODEL``.
No credentials are ever read from a file or hardcoded in the source.
"""
from __future__ import annotations
import os
from dataclasses import dataclass, field
from typing import Optional
from urllib.parse import urlparse
from .errors import ConfigurationError
DEFAULT_BASE_URL = "https://api.openai.com/v1"
DEFAULT_MODEL = "gpt-4o-mini"
DEFAULT_TEMPERATURE = 0.0
DEFAULT_MAX_TOKENS = 4096
DEFAULT_MAX_STEPS = 25
DEFAULT_TIMEOUT = 120
#: Hosts that serve models locally and therefore need no credential.
#: Ollama, LM Studio, vLLM and friends listen on loopback without auth.
LOCAL_HOSTS = {
"localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]",
"host.docker.internal",
}
#: Keeps ``repr(config)`` from ever leaking the key into logs or tracebacks.
REDACTED = "***"
def is_local_url(base_url: str) -> bool:
"""Whether ``base_url`` points at a server on this machine.
Local servers (Ollama, LM Studio, a local vLLM) do not authenticate, so
requiring a key for them would be busywork.
"""
try:
host = (urlparse(base_url).hostname or "").lower()
except ValueError:
return False
return host in LOCAL_HOSTS
@dataclass
class Config:
"""Runtime settings for one Agentux session."""
api_key: Optional[str] = None
base_url: str = DEFAULT_BASE_URL
model: str = DEFAULT_MODEL
temperature: float = DEFAULT_TEMPERATURE
max_tokens: int = DEFAULT_MAX_TOKENS
max_steps: int = DEFAULT_MAX_STEPS
timeout: float = DEFAULT_TIMEOUT
#: Working directory the agent is confined to and runs commands in.
working_dir: str = field(default_factory=os.getcwd)
def __repr__(self) -> str:
key = REDACTED if self.api_key else None
return (
f"Config(api_key={key!r}, base_url={self.base_url!r}, "
f"model={self.model!r}, temperature={self.temperature!r}, "
f"max_tokens={self.max_tokens!r}, max_steps={self.max_steps!r}, "
f"timeout={self.timeout!r}, working_dir={self.working_dir!r})"
)
def __str__(self) -> str:
return self.__repr__()
def validate(self) -> "Config":
"""Fail fast on settings that cannot possibly work."""
if not self.base_url:
raise ConfigurationError(
"AGENTUX_BASE_URL is empty. Set it to your provider's "
f"OpenAI-compatible endpoint (default: {DEFAULT_BASE_URL})."
)
if not self.base_url.startswith(("http://", "https://")):
raise ConfigurationError(
f"AGENTUX_BASE_URL must start with http:// or https://, got {self.base_url!r}."
)
if not self.model:
raise ConfigurationError("AGENTUX_MODEL is empty. Set the model name to use.")
if self.max_steps < 1:
raise ConfigurationError("AGENTUX_MAX_STEPS must be at least 1.")
if self.max_tokens < 1:
raise ConfigurationError("AGENTUX_MAX_TOKENS must be at least 1.")
if self.timeout <= 0:
raise ConfigurationError("AGENTUX_TIMEOUT must be greater than 0.")
return self
@property
def chat_url(self) -> str:
"""The chat-completions endpoint for this provider."""
return f"{self.base_url.rstrip('/')}/chat/completions"
@property
def models_url(self) -> str:
"""The models-listing endpoint for this provider."""
return f"{self.base_url.rstrip('/')}/models"
@property
def is_local(self) -> bool:
"""Whether this points at a server running on this machine."""
return is_local_url(self.base_url)
def _env_str(name: str, default: str) -> str:
value = os.getenv(name)
return value.strip() if value and value.strip() else default
def _env_float(name: str, default: float) -> float:
raw = os.getenv(name)
if raw is None or not raw.strip():
return default
try:
return float(raw)
except ValueError:
raise ConfigurationError(
f"{name} must be a number, got {raw!r}. Fix it by setting "
f"{name} to a numeric value, or unset it to use {default}."
)
def _env_int(name: str, default: int) -> int:
raw = os.getenv(name)
if raw is None or not raw.strip():
return default
try:
return int(raw)
except ValueError:
raise ConfigurationError(
f"{name} must be an integer, got {raw!r}. Fix it by setting "
f"{name} to a whole number, or unset it to use {default}."
)
def load_config(require_api_key: bool = True) -> Config:
"""Build a :class:`Config` from ``AGENTUX_*`` environment variables.
An API key is required for remote providers but not for local ones:
Ollama and friends listen on loopback without authentication, so
``AGENTUX_API_KEY`` is filled in with a placeholder when the base URL is
local and no key was supplied.
"""
api_key = os.getenv("AGENTUX_API_KEY", "").strip() or None
base_url = _env_str("AGENTUX_BASE_URL", DEFAULT_BASE_URL)
if require_api_key and not api_key:
if is_local_url(base_url):
# Ollama ignores the header; send a placeholder so the provider
# layer has something to work with.
api_key = "ollama"
else:
raise ConfigurationError(
"AGENTUX_API_KEY is not set.\n"
"Set it to your provider's API key, for example:\n"
' export AGENTUX_API_KEY="..."\n'
"\n"
"Using a local model? Point Agentux at Ollama and no key is "
"needed:\n"
' export AGENTUX_BASE_URL="http://localhost:11434/v1"\n'
' export AGENTUX_MODEL="qwen2.5-coder"'
)
config = Config(
api_key=api_key,
base_url=base_url,
model=_env_str("AGENTUX_MODEL", DEFAULT_MODEL),
temperature=_env_float("AGENTUX_TEMPERATURE", DEFAULT_TEMPERATURE),
max_tokens=_env_int("AGENTUX_MAX_TOKENS", DEFAULT_MAX_TOKENS),
max_steps=_env_int("AGENTUX_MAX_STEPS", DEFAULT_MAX_STEPS),
timeout=_env_float("AGENTUX_TIMEOUT", DEFAULT_TIMEOUT),
working_dir=_env_str("AGENTUX_CWD", os.getcwd()),
)
return config.validate()
|