Metadata-Version: 2.5
Name: trame-llm
Version: 1.0.0
Summary: LLM helpers and components for trame
Author: Kitware Inc.
License: Apache Software License
License-File: LICENSE
Keywords: Application,Framework,Interactive,Python,Web
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: fastmcp<=2.13.0.2,>=2.12
Requires-Dist: markdown
Requires-Dist: nh3
Requires-Dist: openai>=1
Requires-Dist: python-dotenv
Requires-Dist: trame-client>=3.10
Provides-Extra: claude
Requires-Dist: claude-agent-sdk>=0.2; extra == 'claude'
Provides-Extra: dev
Requires-Dist: nox; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest-cov>=3; extra == 'dev'
Requires-Dist: pytest>=6; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Provides-Extra: examples
Requires-Dist: trame; extra == 'examples'
Requires-Dist: trame-vtk; extra == 'examples'
Requires-Dist: vtk; extra == 'examples'
Provides-Extra: react
Requires-Dist: trame-mui; extra == 'react'
Provides-Extra: vue
Requires-Dist: trame-vuetify>=3.3; extra == 'vue'
Description-Content-Type: text/x-rst

Trame LLM
----------------------------------------

LLM helpers and components for trame

License
----------------------------------------

This library is OpenSource and follow the Apache Software License

Installation
----------------------------------------

Install the library with the extra for your client:

.. code-block:: console

    pip install "trame-llm[vue]"     # Vue apps (Vuetify)
    pip install "trame-llm[react]"   # React apps (MUI)


Configuration
----------------------------------------

trame-llm works with any OpenAI-compatible API. Environment variables (or a
``.env`` file) choose the provider and model the app starts with:

.. list-table::
   :header-rows: 1

   * - Variable
     - Description
   * - ``LLM_PROVIDER``
     - ``openai`` (default), ``anthropic``, ``gemini``, ``nim``, ``llama``
       or ``claude-code``
   * - ``LLM_MODEL``
     - Model name; each provider has a default (see below)
   * - ``LLM_API_KEY``
     - API key for the chosen provider
   * - ``LLM_BASEURL``
     - API endpoint for the chosen provider, for example a local server

Each provider also reads its own key variable, so several can be ready at once:

.. list-table::
   :header-rows: 1

   * - Provider
     - Key variable
     - Default model
   * - ``openai``
     - ``OPENAI_API_KEY``
     - ``gpt-4o``
   * - ``anthropic``
     - ``ANTHROPIC_API_KEY``
     - ``claude-opus-4-5``
   * - ``gemini``
     - ``GEMINI_API_KEY``
     - ``gemini-2.0-flash``
   * - ``nim``
     - ``NVIDIA_API_KEY``
     - ``meta/llama-3.3-70b-instruct``
   * - ``llama`` (Ollama, LM Studio)
     - none needed
     - ``llama3``

Example ``.env`` for Ollama::

    LLM_PROVIDER=llama
    LLM_MODEL=qwen3-coder-next:q4_K_M
    LLM_BASEURL=http://localhost:11434/v1

While the app runs, ``LLMClient.select(provider=..., model=..., api_key=...,
base_url=...)`` switches provider or model and keeps the conversation, and
``llm.ModelPicker(llm_client=...)`` gives users a form for it. The picker never
sends API keys to the browser; it only shows whether a key is set.

Conversations
----------------------------------------

The chat panel keeps several conversations, each with its own model history,
and saves each one after every turn as a JSON file in
``~/.trame-llm/conversations/<app_name>``. Pass ``conversations_dir`` to save
them elsewhere, or ``conversations_dir=False`` to keep them in memory. The list
in the panel switches, renames, pins and deletes conversations. A reply always
lands in the conversation that asked for it, even if the user has switched to
another one meanwhile. Each turn's steps (tool calls, their results and
progress notes) fold into one collapsible block, so the request and the reply
stay easy to follow. Apps driving the client directly can do the same with
``llm_client.process_prompt(prompt, messages=..., progress=...)``.

MCP servers
----------------------------------------

An app gives the model tools by registering MCP servers. A controller object
with an ``mcp`` property runs in the app's own process; a running server is
reached by URL (streamable HTTP, or SSE for URLs ending in ``/sse``):

.. code-block:: python

    llm_client.register_mcp(controller)
    await llm_client.add_mcp_server("http://localhost:8765/mcp")
    llm_client.set_tool_enabled("multiply", False)

``llm.McpServers(llm_client=...)`` lets users do the same: add a server by URL,
reconnect or remove it, and turn its tools on or off.

Claude Code
----------------------------------------

The ``claude-code`` provider runs each turn through Claude Code with the Claude
login on the machine running the app, so people can use their Claude plan
instead of an API key.

Set it up on the machine that runs the app:

1. Install the extra. It brings the Agent SDK, which bundles Claude Code:

   .. code-block:: console

       pip install "trame-llm[claude]"

2. Sign in with your Claude account. The command prints a link; over SSH, open
   it in your local browser and paste the code back. ``status`` shows who is
   signed in.

   .. code-block:: console

       python -m trame_llm.claude_code login
       python -m trame_llm.claude_code status

3. Start the app with ``LLM_PROVIDER=claude-code`` (and ``LLM_MODEL`` set to
   ``sonnet``, ``opus`` or ``haiku``), or choose "Claude Code" in the model
   picker.

On a server where an interactive login is awkward,
``python -m trame_llm.claude_code setup-token`` creates a long-lived token;
give it to the app as ``CLAUDE_CODE_OAUTH_TOKEN``.

Claude Code can only call the app's MCP tools: its own tools (shell, files,
web) are off, the user's Claude Code settings are not loaded, and tools turned
off in the MCP servers panel are denied. Each conversation resumes its Claude
Code session. One login serves everyone using the app, so this suits a local
app or one process per user. Programmatic use of Claude Code draws on the
plan's Agent SDK allowance; check Anthropic's current terms.

Message colors
----------------------------------------

Each message has a tone that names its meaning: ``user``, ``assistant``,
``info``, ``tool``, ``success``, ``warning`` or ``error``. The Vue and React
panels both color messages from the matching CSS variable, so an app can
restyle them in one place:

.. code-block:: css

    :root {
      --trame-llm-assistant: #d1c4e9;
    }

Examples
----------------------------------------

``examples/widgets/app.py`` shows the chat panel with a sample conversation
that covers each message style, and the model picker in the app bar. Run it
with the Vue or the React client:

.. code-block:: console

    python examples/widgets/app.py
    python examples/widgets/app.py --react

From a source checkout, build the React components first with
``npm ci && npm run build`` in ``react-components``.

``examples/widgets/mcp_server.py`` is a small MCP server to add by URL
(``http://localhost:8765/mcp``). ``examples/cone/app.py`` drives a VTK cone
through an MCP server.

Development setup
----------------------------------------

We recommend using uv for setting up and managing a virtual environment for your development.

.. code-block:: console

    # Create venv and install all dependencies
    uv sync --all-extras --dev

    # Activate environment
    source .venv/bin/activate

    # Install commit analysis
    pre-commit install
    pre-commit install --hook-type commit-msg

    # Allow live code edit
    uv pip install -e .

For running tests and checks, you can run ``nox``.

.. code-block:: console

    # run all
    nox

    # lint
    nox -s lint

    # tests
    nox -s tests

Professional Support
----------------------------------------

* `Training <https://www.kitware.com/courses/trame/>`_: Learn how to confidently use trame from the expert developers at Kitware.
* `Support <https://www.kitware.com/trame/support/>`_: Our experts can assist your team as you build your web application and establish in-house expertise.
* `Custom Development <https://www.kitware.com/trame/support/>`_: Leverage Kitware’s 25+ years of experience to quickly build your web application.
