Lee-W commented on code in PR #73466: URL: https://github.com/apache/airflow/pull/73466#discussion_r4069208529
########## providers/common/ai/src/airflow/providers/common/ai/utils/schema_context.py: ########## @@ -0,0 +1,24 @@ +# 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. +"""Helpers for building schema context strings for LLM operators.""" + +from __future__ import annotations + + +def format_columns_for_prompt(columns: list[dict[str, str]]) -> str: Review Comment: since we already know what these columns are, make it a typed dict would be better. ########## providers/common/ai/src/airflow/providers/common/ai/toolsets/datafusion.py: ########## @@ -190,20 +190,19 @@ def _list_tables(self) -> str: return json.dumps({"error": str(ex)}) def _get_schema(self, table_name: str) -> str: - engine = self._get_engine() - # session_context lookup is required here instead of engine.registered_tables, - # because registered_tables only tracks tables registered via datasource config. - # When allow_writes is enabled, the agent may create temporary in-memory tables - # that would not be captured there. - if not engine.session_context.table_exist(table_name): - return json.dumps({"error": f"Table {table_name!r} is not available"}) - # Intentionally using session_context instead of engine.get_schema() — - # the latter returns a pre-formatted string intended for other operators, - # not a JSON-compatible format. - # TODO: refactor engine.get_schema() to return JSON and update this accordingly - table = engine.session_context.table(table_name) - columns = [{"name": f.name, "type": str(f.type)} for f in table.schema()] - return json.dumps(columns) + try: + engine = self._get_engine() + # session_context lookup is required here instead of engine.registered_tables, + # because registered_tables only tracks tables registered via datasource config. + # When allow_writes is enabled, the agent may create temporary in-memory tables + # that would not be captured there. + if not engine.session_context.table_exist(table_name): + return json.dumps({"error": f"Table {table_name!r} is not available"}) + columns = engine.get_schema(table_name) + return json.dumps(columns) + except Exception as ex: Review Comment: Is is possible to make it a narrower excetpion ########## providers/common/ai/src/airflow/providers/common/ai/operators/llm_schema_compare.py: ########## @@ -275,11 +276,12 @@ def _df_engine(self): engine = DataFusionEngine() return engine - def _introspect_schema_from_datafusion(self, ds_config: DataSourceConfig): + def _introspect_schema_from_datafusion(self, ds_config: DataSourceConfig) -> str: self._df_engine.register_datasource(ds_config) - schema_text = self._df_engine.get_schema(ds_config.table_name) + columns = self._df_engine.get_schema(ds_config.table_name) + col_info = format_columns_for_prompt(columns) - return f"Source: {ds_config.conn_id} \nFormat: ({ds_config.format})\nTable: {ds_config.table_name}\nColumns: {schema_text}" + return f"Source: {ds_config.conn_id} \nFormat: ({ds_config.format})\nTable: {ds_config.table_name}\nColumns: {col_info}" Review Comment: ```suggestion return ( f"Source: {ds_config.conn_id} \n" f"Format: ({ds_config.format})\n" f"Table: {ds_config.table_name}\n" f"Columns: {col_info}" ) ``` not sure whether ruff allow us to do it -- 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]
