Repository navigation
Add a Model tab to the task-instance page for common.ai #74334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
providers/common/ai/src/airflow/providers/common/ai/plugins/_plugin_urls.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # 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. | ||
| """URL helpers shared by this provider's React plugins (``hitl_review``, ``model_panel``).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from urllib.parse import urlparse | ||
|
|
||
| from airflow.providers.common.compat.sdk import conf | ||
|
|
||
|
|
||
| def get_base_url_path(path: str) -> str: | ||
| """Construct URL path with webserver base_url prefix for non-root deployments.""" | ||
| base_url = conf.get("api", "base_url", fallback="/") | ||
| if base_url.startswith(("http://", "https://")): | ||
| base_path = urlparse(base_url).path | ||
| else: | ||
| base_path = base_url | ||
| base_path = base_path.rstrip("/") | ||
| return base_path + path | ||
|
|
||
|
|
||
| def get_bundle_url(plugin_prefix: str, bundle_filename: str) -> str: | ||
| """ | ||
| Return the bundle URL for a plugin's React bundle. | ||
|
|
||
| Uses an absolute URL when api.base_url is a full URL so the bundle loads | ||
| correctly in Vite dev mode, where import() resolves relative to the script | ||
| origin (5173) rather than the document origin (28080). | ||
| """ | ||
| path = get_base_url_path(f"{plugin_prefix}/static/{bundle_filename}") | ||
| base_url = conf.get("api", "base_url", fallback="/") | ||
| if base_url.startswith(("http://", "https://")): | ||
| parsed = urlparse(base_url) | ||
| return f"{parsed.scheme}://{parsed.netloc}" + path | ||
| return path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
providers/common/ai/src/airflow/providers/common/ai/plugins/model_panel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from airflow.plugins_manager import AirflowPlugin | ||
| from airflow.providers.common.ai.plugins._plugin_urls import get_bundle_url | ||
| from airflow.providers.common.compat.version_compat import AIRFLOW_V_3_1_PLUS, AIRFLOW_V_3_4_PLUS | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.plugins_manager import FastAPIAppDict, ReactAppDict | ||
|
|
||
| _PLUGIN_PREFIX = "/ai-model" | ||
|
|
||
|
|
||
| if AIRFLOW_V_3_1_PLUS: | ||
| import mimetypes | ||
| from pathlib import Path | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.staticfiles import StaticFiles | ||
|
|
||
| # Ensure proper MIME type for the plugin bundle (FastAPI serves .cjs as text/plain by default). | ||
| mimetypes.add_type("application/javascript", ".cjs") | ||
|
|
||
| model_panel_app = FastAPI( | ||
| title="AI Model Panel", | ||
| description="Serves the static 'Model' tab bundle for task instances run with common.ai operators.", | ||
| ) | ||
|
|
||
| _WWW_DIR = Path(__file__).parent / "www" | ||
| _dist_dir = _WWW_DIR / "dist" | ||
| if _dist_dir.is_dir(): | ||
| model_panel_app.mount( | ||
| "/static", | ||
| StaticFiles(directory=str(_dist_dir.absolute()), html=True), | ||
| name="model_panel_static", | ||
| ) | ||
|
|
||
|
|
||
| class ModelPanelPlugin(AirflowPlugin): | ||
| """Register the 'Model' tab showing the resolved LLM model name and token usage.""" | ||
|
|
||
| name = "ai_model_panel" | ||
| fastapi_apps: list[FastAPIAppDict] = [] | ||
| react_apps: list[ReactAppDict] = [] | ||
| if AIRFLOW_V_3_1_PLUS: | ||
| fastapi_apps = [ | ||
| { | ||
| "name": "ai-model-panel", | ||
| "app": model_panel_app, | ||
| "url_prefix": _PLUGIN_PREFIX, | ||
| } | ||
| ] | ||
| if AIRFLOW_V_3_4_PLUS: | ||
| # `applies_to` (the per-operator scoping below) only came into core with | ||
| # apache/airflow#69148 (3.4.0). An older core silently accepts the extra key and | ||
| # ignores it, so the tab would show up on every task instance instead and land on | ||
| # the empty state for anything that isn't an LLM/Agent run -- the whole entry is | ||
| # withheld pre-3.4 rather than shipping that unscoped tab. | ||
| react_apps = [ | ||
| { | ||
| "name": "AI Model", | ||
| "bundle_url": get_bundle_url(_PLUGIN_PREFIX, "model.umd.cjs"), | ||
| "destination": "task_instance", | ||
| "url_route": "ai-model", | ||
| # Only LLMOperator and AgentOperator (and their @task.llm/@task.agent | ||
| # TaskFlow forms) push model_name/usage; other operators would show | ||
| # a permanently-empty tab. `operator_name` alone covers both the raw | ||
| # class name and the decorator's display name, since TaskInstance falls | ||
| # back to the class name when there's no custom_operator_name. | ||
| "applies_to": { | ||
|
pierrejeambrun marked this conversation as resolved.
Resolved
|
||
| "operator_names": [ | ||
| "LLMOperator", | ||
| "AgentOperator", | ||
| "@task.llm", | ||
| "@task.agent", | ||
| ], | ||
| }, | ||
| } | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
providers/common/ai/src/airflow/providers/common/ai/plugins/www/pnpm-lock.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.