{% extends "admin/base.html" %} {% set labels = { 'model': _('Model'), 'api_key_env': _('API key'), 'timeout_seconds': _('Time limit for an ordinary call, seconds'), 'page_read_timeout_seconds': _('Time limit for reading a web page, seconds'), } %} {% set by_name = {} %} {% for row in rows %}{% do by_name.update({row.name: row}) %}{% endfor %} {% macro key_text(k) %}{{ k.name }} – {% if k.source == 'page' %}{{ _('entered here') }}{% elif k.source == 'server' %}{{ _('set on the server') }}{% else %}{{ _('missing') }}{% endif %}{% if k.tail %} (…{{ k.tail }}){% endif %}{% if k.unreadable %}, {{ _('a key entered here cannot be read, enter it again') }}{% endif %}{% endmacro %} {% macro origin(name) %} {% set row = by_name[name] %} {% if row.saved and row.default %} {# Only when it matters: the server has a value of its own, and this one goes before it. #} {{ _('Applies instead of the server\'s value: {value}').format(value=row.default) }} {% endif %} {% for e in errors[name] or [] %}{{ e }}{% endfor %} {% endmacro %} {% block primary_content_inner %} {% asset 'ai_connector/ai-connector-settings' %}

{{ _('AI settings') }}

{{ _('The connection to an AI service that the portal\'s extensions share: model, key and time limits. Changes apply at once, for the whole portal, without a restart. Where nothing is saved here, the server default applies. What each extension asks the AI to do is set on that extension\'s own pages.') }}

{% set problems = checks | selectattr('status', 'equalto', 'error') | list %}

{{ _('Setup') }}

{% if problems %}{{ _('Something needs to be done before everything works:') }} {% else %}{{ _('Everything needed is in place.') }}{% endif %}

{% if resettable and confirm_reset %} {# The second step: what goes and what replaces it, before anything changes. #} {% endif %} {% if test %} {% if test.success %}
{% trans model=test.model, seconds=h.localised_number(test.seconds) %}The AI answered in {{ seconds }} s with the model {{ model }}.{% endtrans %} {% if test.answer %}
{{ _('Test answer:') }} {{ test.answer }}{% endif %}
{% else %}
{{ _('The connection test failed:') }} {{ test.error }} {% if test.hint %}
{{ test.hint }}{% endif %} {% if test.detail %}
{{ test.detail }}{% endif %}
{% endif %} {% endif %} {% set keys_section %} {% set entered = keys | selectattr('source', 'equalto', 'page') | list + keys | selectattr('unreadable') | list %} {% set on_server = keys | selectattr('source', 'equalto', 'server') | list %} {% set key_errors = errors.custom_name or errors.custom_key or errors.custom_confirm or errors.remove_key %} {#- Everything about the API keys in one fold: the keys there are (entered here, and the server's), then adding or replacing one. It sits inside the connection form, so its fields belong to two forms of their own outside it (the form attribute; HTML has no nested forms): ai-key-remove and ai-key-add, at the end of the page. -#} {% if entered or on_server or can_store_keys %}
{{ _('Manage API keys') }}
{% if entered or on_server %} {% for e in errors.remove_key or [] %}{{ e }}{% endfor %} {% elif can_store_keys %}

{{ _('None yet.') }}

{% endif %} {% if can_store_keys %}

{{ _('Add an API key') }}

{{ _('Adds a key without changing the model, for example before you switch service. The key is stored encrypted, goes only to the service you choose as the model, and is never shown again, only its last four characters.') }}

{#- Chosen first: what was sent (after an error), else the first service without a key. -#} {% set without_key = key_services | rejectattr('set') | list %} {% set chosen_service = request.form.get('custom_service', (without_key or key_services or [{'env': 'other'}])[0].env) %}
{{ _('Only for another service: a name of your own that ends in _API_KEY, such as ACME_API_KEY. It is a label, never looked up among the server\'s variables.') }} {% for e in errors.custom_name or [] %}{{ e }}{% endfor %}
{% for e in errors.custom_key or [] %}{{ e }}{% endfor %}
{% for e in errors.custom_confirm or [] %}{{ e }}{% endfor %}
{% endif %}
{% endif %} {% endset %} {% if first_time %}{{ keys_section }}{% endif %} {% if first_time %} {#- First time: no key anywhere (and no AI server of one's own), so there is nothing to choose yet. -#} {% if can_store_keys %}

{{ _('Start with the API key: add the key you got from your AI service under “Manage API keys” above. Then the model is chosen here, among that service\'s models.') }}

{% else %}

{{ _('There is no API key yet, and this portal keeps its keys in the server configuration. Ask whoever runs the server to add one.') }}

{% endif %} {% else %}
{{ h.csrf_input() }} {% set model_block %}
{% set known = suggestions | map(attribute='model') | list %} {% set chosen_model = form.model or recommended %} {% if suggestions %} {#- The list follows the chosen API key: only that service's models (settings.js), with a suggested one. -#} {% endif %} {% if recommended %}{{ _('A suggested model is chosen: check it, then test the connection and save.') }}{% endif %} {{ _('The list holds the models for the services whose key is set. A newer model that is not in the list can be written in full under “Other model”, as the service, a slash and the model, for example gemini/gemini-3.5-flash-lite. Try it with “Test the connection”.') }} {% if library_version %}{{ _('The portal talks to the AI services through LiteLLM {version}.').format(version=library_version) }}{% endif %} {{ origin('model') }}
{% endset %} {% set key_block %} {#- The key in use, chosen among the keys that are there; they are added, replaced and removed under "API keys" below. Choosing a model chooses its service's key, and choosing a key shows that service's models (settings.js). -#}
{% if can_store_keys %}{{ _('Add, replace or remove API keys under “Manage API keys” below.') }} {% else %}{{ _('The keys are kept in the server configuration. Only their last four characters are shown, to tell them apart. To add or change a key, ask whoever runs the server.') }}{% endif %} {{ origin('api_key_env') }}
{% endset %} {{ model_block }} {{ key_block }} {{ keys_section }} {% set timeout_help = { 'timeout_seconds': _('How long to wait for the AI to answer an ordinary call. Usually a few seconds.'), 'page_read_timeout_seconds': _('How long to wait when the AI service fetches a web page itself and reads it (models that can). That usually takes a few seconds but can take longer for large pages, so this limit is longer.'), } %} {% for name in ['timeout_seconds', 'page_read_timeout_seconds'] %}
{{ timeout_help[name] }} {{ origin(name) }}
{% endfor %} {% if api_base %}

{{ _('Service address') }}: {{ api_base }} ({{ _('only in the server configuration') }})

{% endif %}
{% endif %} {% if resettable and not confirm_reset %} {# Only when a value saved here has a server value to go back to; values the server has none for stay. #}
{{ h.csrf_input() }} {% set reset_labels = [] %}{% for name in resettable %}{% do reset_labels.append(labels[name]) %}{% endfor %} {{ _('For: {fields}').format(fields=reset_labels | join(', ')) }}
{% endif %} {#- The forms the fields under "Manage API keys" belong to (their form attribute). -#}
{{ h.csrf_input() }}
{{ h.csrf_input() }}
{% if errors.custom_name or errors.custom_key or errors.custom_confirm or errors.remove_key %} {% endif %} {% endblock %}