ai¶
panel_flowdash.ai
¶
The project file's ai section: model providers, the models people may pick and their limits.
ai:
default: openai:gpt-6-luna
providers:
openrouter:
api_key: ${OPENROUTER_API_KEY}
ollama:
base_url: http://127.0.0.1:11434
models:
- id: openai:gpt-6-luna
label: Luna
offered_to: [assistant, explore]
settings: {max_tokens: 1000}
monthly_limit: 50
person_limit: 5
fallback: anthropic:claude-haiku-5.5
A provider's api_key is a secret (see panel_flowdash.secret_store): a
${VARIABLE} reference or a value stored encrypted from the admin view.
A provider without one uses its usual environment variable, e.g.
OPENAI_API_KEY. A model is offered to the assistant and/or Explore
(offered_to) and, with groups, only to members of those groups.
settings are pydantic-ai model settings merged over the app's
agent_model_settings for that model's requests. When a model's spend
this month reaches monthly_limit (US dollars), or one person's spend on
it reaches person_limit, requests use its fallback instead, or are
refused when it has none under its own limits.
Models given in code (agent_models, --agent-model) come first; an
entry here with the same id adds its offer, settings and limit to them.
Needs nothing beyond panel-flowdash; the model objects are made with
pydantic-ai when the agent extra is installed.
ADMIN_LOG_SIZE = 500
module-attribute
¶
Fetch = Callable[[str, Mapping[str, str]], t.Awaitable[tuple[int, str]]]
module-attribute
¶
FetchBody = Callable[[str, Mapping[str, str]], t.Awaitable[tuple[int, str]]]
module-attribute
¶
KEY_CHECKS = {}
module-attribute
¶
MODEL_LIST_TTL = 600
module-attribute
¶
MODEL_SETTINGS = {'temperature': ('Temperature', 0.0, 2.0, False), 'max_tokens': ('Max tokens', 1, None, True), 'timeout': ('Timeout', 1, None, False)}
module-attribute
¶
OLLAMA_URL = 'http://127.0.0.1:11434'
module-attribute
¶
OPENS_FLUSH_SECONDS = 60.0
module-attribute
¶
PROVIDERS = {p.name: p for p in (Provider('openai', 'OpenAI', 'OPENAI_API_KEY', 'https://api.openai.com/v1/models'), Provider('anthropic', 'Anthropic', 'ANTHROPIC_API_KEY', 'https://api.anthropic.com/v1/models', auth='x-api-key', models_url='https://api.anthropic.com/v1/models?limit=1000'), Provider('openrouter', 'OpenRouter', 'OPENROUTER_API_KEY', 'https://openrouter.ai/api/v1/key', blurb='One key for models of many providers', models_url='https://openrouter.ai/api/v1/models'), Provider('google-gla', 'Google', 'GOOGLE_API_KEY', 'https://generativelanguage.googleapis.com/v1beta/models', auth='x-goog-api-key', models_url='https://generativelanguage.googleapis.com/v1beta/models?pageSize=1000'), Provider('groq', 'Groq', 'GROQ_API_KEY', 'https://api.groq.com/openai/v1/models'), Provider('mistral', 'Mistral', 'MISTRAL_API_KEY', 'https://api.mistral.ai/v1/models'), Provider('ollama', 'Ollama', None, '/api/tags', auth='none', blurb='Models running on a local server', local=True))}
module-attribute
¶
SURFACES = {'assistant': 'Assistant', 'explore': 'Explore'}
module-attribute
¶
USAGE_FILE = Path('.flowdash') / 'usage.json'
module-attribute
¶
_LEDGERS = {}
module-attribute
¶
_LEDGERS_LOCK = threading.Lock()
module-attribute
¶
_MODEL_KEYS = ('id', 'label', 'description', 'offered_to', 'groups', 'settings', 'monthly_limit', 'person_limit', 'fallback')
module-attribute
¶
_MODEL_LISTS = {}
module-attribute
¶
_NOT_CHAT = ('embedding', 'whisper', 'tts', 'dall-e', 'moderation', 'transcribe', 'babbage', 'davinci', 'guard', 'image', 'audio', 'realtime', 'gpt-live')
module-attribute
¶
accepts_dashboard_id = accepts_old_keyword('dashboard_id', 'asset_id')
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Identity
dataclass
¶
The resolved principal for a session.
groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
oauth_user = None
class-attribute
instance-attribute
¶
system_user = None
class-attribute
instance-attribute
¶
user
instance-attribute
¶
user_info = field(default_factory=dict)
class-attribute
instance-attribute
¶
user_names
property
¶
All names this identity may be referenced by in a rule.
in_groups(groups)
¶
Whether the identity belongs to any of groups.
is_user(users)
¶
Whether the identity matches any of users (OAuth or system name).
KeyCheck
dataclass
¶
ModelEntry
dataclass
¶
A model as the admin view and the sessions see it: from code, the project file or both.
locked names where a model given in code is set; its id, label
and description are then read-only.
description = None
class-attribute
instance-attribute
¶
fallback = None
class-attribute
instance-attribute
¶
groups = ()
class-attribute
instance-attribute
¶
id
instance-attribute
¶
label = None
class-attribute
instance-attribute
¶
locked = None
class-attribute
instance-attribute
¶
model = None
class-attribute
instance-attribute
¶
monthly_limit = None
class-attribute
instance-attribute
¶
name
property
¶
Return the label, or the id.
offered_to = tuple(SURFACES)
class-attribute
instance-attribute
¶
person_limit = None
class-attribute
instance-attribute
¶
provider
property
¶
Return the provider's name.
settings = field(default_factory=dict)
class-attribute
instance-attribute
¶
offered(identity, surface)
¶
Whether the model is offered to identity in surface, assistant or explore.
to_data()
¶
Return the project file entry of the model.
ModelList
dataclass
¶
ProjectError
¶
Bases: ValueError
The problems of a project file, each with the file, line and column it concerns.
problems = list(problems)
instance-attribute
¶
Provider
dataclass
¶
A model provider: how its key is found and how a key is tested.
auth = 'bearer'
class-attribute
instance-attribute
¶
blurb = ''
class-attribute
instance-attribute
¶
env
instance-attribute
¶
label
instance-attribute
¶
local = False
class-attribute
instance-attribute
¶
models_url = None
class-attribute
instance-attribute
¶
Where the provider lists its models, if not at test_url.
name
instance-attribute
¶
test_url
instance-attribute
¶
SecretError
¶
Bases: ValueError
A secret that cannot be resolved; the message never holds its value.
SecretInfo
dataclass
¶
What the admin view may show about a secret: where it comes from, never its value.
source is "env", "stored" or None (not set). env names
the variable of a reference; last4 is the value's last four
characters when it resolves, and error why it does not.
Section
dataclass
¶
One top-level section of a project file, as a loader receives it.
context
property
¶
Return what the app loaded the project with, e.g. its data sources.
name
instance-attribute
¶
project
instance-attribute
¶
value
instance-attribute
¶
position(path=())
¶
Return the line and column of the value at path in this section.
problem(path, message, key=False)
¶
Return a problem at the value at path in this section (with key, at its key).
warn(path, message, key=False)
¶
Report a problem at path that does not stop the section from loading.
SectionError
¶
Bases: ValueError
A section loader's problem at path inside its section.
path is e.g. ("capacity", "table"). With key=True the problem
is reported where the last key of path is written instead of its value.
UsageLedger
¶
What models cost, who opened the app and its dashboards, and what admins did, by the project.
It is a JSON file. Without a path everything is kept in memory for the process. Every
process serving the project adds to the same file: each change is a
read-modify-write under an OS file lock (usage.json.lock), so
several workers, or people sharing the project on a network drive, do
not lose each other's spend.
path = Path(path) if path is not None else None
instance-attribute
¶
add(model_id, cost, when=None, user=None)
¶
Count a request by user to model_id that cost cost US dollars (None: unknown).
admin_log()
¶
Return the admin log, oldest first.
dashboards()
¶
Return when each dashboard was last opened, by id, including opens not written yet.
flush_opens()
¶
Write the opens noted since the last write.
forget_dashboards(asset_ids)
¶
Drop what is noted about the dashboards asset_ids, e.g. once deleted.
month(model_id, when=None)
¶
Return the US dollars spent on model_id this month and the number of requests.
opened(asset_id, when=None)
¶
Note that someone opened the dashboard asset_id.
Opens are kept in memory and written together at most every
OPENS_FLUSH_SECONDS (and when the process exits), so opening a
dashboard does not take the file lock each time.
people()
¶
Return who opened the app, with when they were last seen and their groups then.
people_spent(model_id, when=None)
¶
Return the US dollars each person spent on model_id this month, by user name.
person_spent(model_id, user)
¶
Return the US dollars user spent on model_id this month (0 without a user).
preference(user, key, default=None)
¶
Return what user last chose for key, e.g. "home", else default.
record(user, action, dashboards=(), when=None, **details)
¶
Add an admin's action on dashboards (each an id and title) to the admin log.
see(user, groups=(), when=None)
¶
Note that user, a member of groups, opened the app.
set_preference(user, key, value)
¶
Remember value as what user chose for key, kept with their record of visits.
spent(model_id)
¶
Return the US dollars spent on model_id this month.
_created(entry)
¶
Return when a listed model was created, as a timestamp, or 0 if the list does not say.
_dollars(amount)
¶
_entry(data, **extra)
¶
_fetch(url, headers)
async
¶
_fetch_body(url, headers)
async
¶
_model_names(name, data)
¶
Return the chat model names in provider name's list response data, newest first.
_model_problems(section, i, model, seen)
¶
_number(value)
¶
_renamed_keys(data)
¶
Read a usage file written before dashboards became assets with today's keys.
_request(provider, key, base_url, url)
¶
Return url (a path on a local server) and the headers that carry key.
_retry_sharing(operation)
¶
Run operation, retrying a Windows sharing violation for a bounded time.
accepts_old_keyword(old, new)
¶
Return a decorator letting a function taking new also accept the deprecated keyword old.
check_key(name, key=None, base_url=None, fetch=None)
async
¶
Ask provider name whether key is valid (or a local server whether it is up).
check_model_settings(settings)
¶
Return the problems of a model's settings, each with its path inside them.
check_secret(value, what)
¶
Return why value may not stand for a secret in the project file, or None.
default_model_id(ai, entries, code_default)
¶
Return the default model: the one given in code, else the file's, else the first.
describe_secret(value, environ=None)
¶
Return where value comes from and whether it resolves, without its value.
file_lock(path)
¶
Hold an exclusive lock on path (fcntl on POSIX, msvcrt on Windows), across processes.
key_status(ai, name)
¶
Return where provider name's key comes from and the last test of it.
ledger_for(directory)
¶
Return the spend ledger of the project in directory (in memory without one).
list_models(name, key=None, base_url=None, fetch=None)
async
¶
Ask provider name which chat models key can use, as pydantic-ai ids, newest first.
Answers for the same provider, key and server are reused for
:data:MODEL_LIST_TTL seconds. A provider that cannot be reached or
refuses the key gives no ids and a message saying why.
load_ai(section)
¶
Check the ai section and return it as plain data.
model_choice(entry, ai, default=None)
¶
Return the panel_agents.ModelChoice a session offers for entry.
The project's default model is tagged as such in the model menus.
model_entries(ai, code_models=(), locked=None)
¶
Return the models: those given in code (code_models, ModelChoices) first, then the file's.
A file entry with the id of a model given in code adds its offer, settings, limit and fallback to it.
model_object(entry, ai)
¶
Return the pydantic-ai model entry runs with when its provider's key or URL is in the file.
The key goes to the provider's client, never into os.environ. None
leaves the id to pydantic-ai, which reads the usual environment
variable, e.g. when the file sets no key or pydantic-ai is missing.
month_key(when=None)
¶
Return the month spend is counted in, e.g. 2026-11 (UTC).
provider_config(ai, name)
¶
Return the project file's settings of provider name.
provider_key(ai, name)
¶
Return how provider name's key is written: the file's, else its usual variable.
provider_name(model_id)
¶
Return the provider a pydantic-ai model id names, e.g. openai for openai:gpt-5.
resolve_secret(value, environ=None)
¶
Return the secret value stands for, or None when it is not set.
A reference reads its environment variable, an encrypted value is
decrypted. Raises SecretError for a variable that is not set or a value
that cannot be decrypted.
select_model(entries, model_id, spent, person_spent=None)
¶
Return the model a request picked as model_id runs with, and a note on why if another.
A model whose spend this month (spent) reached its monthly_limit,
or whose spend by the person asking (person_spent) reached its
person_limit, gives way to its fallback, and that one to its own;
None (with the reason) when none is under its limits.