Lee-W commented on code in PR #76802:
URL: https://github.com/apache/airflow/pull/76802#discussion_r4705361384


##########
providers/common/ai/docs/self_hosted_models.rst:
##########
@@ -360,6 +360,57 @@ server. When it points at Ollama, pass ``OllamaBackend`` 
as the factory
     hook = PydanticAIHook(llm_conn_id="pydanticai_default", 
model_id="openai:llama3")
     hook.ensure_model_ready(backend_factory=OllamaBackend)
 
+For LM Studio, pass ``LMStudioBackend``. It needs LM Studio 0.4.0 or later, the
+release that introduced the native
+`v1 REST API <https://lmstudio.ai/docs/developer/rest>`__ (see also the
+`API changelog <https://lmstudio.ai/docs/developer/api-changelog>`__). It lists
+the models with
+`GET /api/v1/models <https://lmstudio.ai/docs/developer/rest/list>`__ and sends
+`POST /api/v1/models/load <https://lmstudio.ai/docs/developer/rest/load>`__
+only when the model has no loaded instance.
+
+.. code-block:: python
+
+    from airflow.providers.common.ai.hooks.lmstudio import LMStudioBackend
+
+    hook = PydanticAIHook(llm_conn_id="pydanticai_default", 
model_id="openai:qwen3")
+    hook.ensure_model_ready(backend_factory=LMStudioBackend)
+
+Things to know:
+
+- **List first, then load.** LM Studio answers a load request for a model that
+  is already loaded by loading a second instance of it, so the backend lists 
the
+  models first and sends the load request only when the model has no loaded
+  instance. The load request returns once the model is loaded. A failed load is
+  reported with the server's error message when the response has one, or the
+  start of the response body otherwise.
+- **Model name and list shape.** The model name is compared with the ``key`` of
+  the listed models, so use the model key that LM Studio lists. A
+  ``loaded_instances`` that is missing, empty or not a list counts as not
+  loaded, and an entry without a ``key`` never matches. If the list is not a
+  JSON object with a ``models`` list of objects, for example when a proxy
+  answers with a login page, ``ValueError`` is raised instead of loading 
anyway.
+- **Timeout.** The list request gets ``timeout`` seconds and the load request
+  gets the time the list request left. Each is an httpx2 timeout, which applies
+  to the connect, write and read phases separately, so both requests together
+  can take longer than ``timeout``. A timeout message names the time that
+  request was given.
+- **Concurrent tasks.** The list and the load are two requests, not one atomic
+  step, so two tasks that start together can both see the model as not loaded
+  and both send a load request.

Review Comment:
   the bullet now says each of those loads is another instance that stays in 
memory until it is unloaded



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to