zozo123 commented on code in PR #76157:
URL: https://github.com/apache/airflow/pull/76157#discussion_r4666703796


##########
providers/databricks/docs/operators/genie_operator.rst:
##########
@@ -0,0 +1,97 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+ ..   http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+
+
+.. _howto/operator:DatabricksGenieOperator:
+
+Ask a Databricks Genie space
+============================
+
+Use 
:class:`~airflow.providers.databricks.operators.genie.DatabricksGenieOperator` 
to ask a Genie space
+one question as a standalone task. The operator returns the same bounded 
result as
+:ref:`DatabricksGenieHook <howto/databricks-genie>` through XCom: the space, 
conversation and message
+IDs, the message status, the text answer, generated queries and capped query 
results.
+
+.. exampleinclude:: 
/../../databricks/tests/system/databricks/example_databricks_genie.py
+    :language: python
+    :start-after: [START howto_operator_databricks_genie]
+    :end-before: [END howto_operator_databricks_genie]
+
+By default the operator starts a new conversation. Set ``conversation_id`` to 
ask a follow-up in an
+existing conversation. The operator never deletes or cancels conversations, so 
a conversation you
+supply stays yours.
+
+Set ``deferrable=True`` to release the worker while Genie works. The trigger 
only reads the message
+status; the worker fetches the answer and query results when the task resumes, 
so the trigger event
+never carries Genie output.
+
+.. exampleinclude:: 
/../../databricks/tests/system/databricks/example_databricks_genie.py
+    :language: python
+    :start-after: [START howto_operator_databricks_genie_deferrable]
+    :end-before: [END howto_operator_databricks_genie_deferrable]
+
+``timeout`` limits how long one try waits for the answer. Timing out does not 
cancel the message.
+
+Durable execution
+-----------------
+
+On Airflow 3.3 and later the operator is durable by default. It stores the 
conversation and message
+IDs in the :doc:`task state store 
<apache-airflow:core-concepts/task-state-store>` before it waits, so
+a retry after a worker crash, a timeout or a failed trigger reconnects to the 
same message instead of
+asking again. See :doc:`apache-airflow:core-concepts/resumable-tasks` for how 
resumable tasks work.
+On retry the operator checks the stored message:
+
+* A message that is still running is awaited again.
+* A completed message returns its answer without asking again.
+* A message that failed, was cancelled or whose result expired is asked again 
in a new message.
+* A stored message that no longer exists (Genie answers 404), or that was 
asked for a different
+  space, conversation or question, is asked again.
+
+Genie may also answer 403 for a message that does not exist, so it does not 
reveal which resources
+are there. The operator treats only 404 as a missing message, so a stored 
message that Genie hides
+behind a 403 fails the try instead of being asked again. Retrying or clearing 
the task fails the same
+way, because the stored IDs stay in the task state for that DAG run. First 
check that the connection
+can still read the Genie space and fix its permissions if it cannot. If access 
is fine, the stored
+message is gone. To recover that DAG run, either set ``conversation_id`` and 
``message_id`` to attach

Review Comment:
   Thanks, you are right, and I confirmed it in core: clear_task_instances 
calls _discard_task_state_store unless keep_task_state is set. Fixed in 
e252449f3a. A new "Clearing the task" section has a table of what a default 
clear and a keep task state clear do for a running, completed, failed, 
unconfirmed or 403 hidden message, with the 3.4 and 3.5 split. The unconfirmed 
section now says a default clear asks again with no check, and both 
AirflowFailException messages say the same. There is also a run scoped attach 
example using `{{ params.genie_message_id }}` from the system test. 
conversation_id and message_id were already templated, and new tests cover 
rendering them from params and the default clear case.



##########
providers/databricks/docs/operators/genie.rst:
##########
@@ -0,0 +1,68 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+ ..   http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+.. _howto/databricks-genie:
+
+Databricks Genie consultations
+==============================
+
+The ``DatabricksGenieHook`` submits a question to a Genie space, waits for the 
message to finish,
+and returns a bounded result with the space, conversation, and message IDs. It 
uses the
+credentials in an Airflow ``databricks`` connection and implements Common AI's 
public
+``BaseManagedAgentHook`` contract.
+
+Install the optional Common AI integration when using its managed-agent 
toolset:
+
+.. code-block:: bash
+
+    pip install 'apache-airflow-providers-databricks[common.ai]'
+
+.. code-block:: python
+
+    from airflow.providers.common.ai.toolsets import ManagedAgentToolset
+    from airflow.providers.databricks.hooks.genie import DatabricksGenieHook
+
+    hook = DatabricksGenieHook(databricks_conn_id="databricks_analytics")
+    toolset = ManagedAgentToolset(
+        hook.agent("your-genie-space-id"),
+        tool_name="ask_analytics_genie",
+        description="Answers questions about the analytics tables in this 
Genie space.",
+        max_retries=0,
+    )

Review Comment:
   Good idea, thank you. Done in e252449f3a: the toolset and direct consult 
snippets now live in the Genie system test between START and END markers and 
are pulled in with exampleinclude. The attach example in genie_operator.rst 
works the same way.



-- 
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