justinpakzad commented on code in PR #68942: URL: https://github.com/apache/airflow/pull/68942#discussion_r3501960204
########## providers/snowflake/src/airflow/providers/snowflake/hooks/snowflake_cortex_agent.py: ########## @@ -0,0 +1,167 @@ +# 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 Any + +import requests + +from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook + +AGENT_REQUEST_TIMEOUT = 60 # Prevent hanging agent requests. + + +class SnowflakeCortexAgentHook(SnowflakeHook): + """Hook for interacting with Snowflake Cortex Agents.""" + + def _get_base_url(self) -> str: + conn_config = self._get_static_conn_params + + host = conn_config.get("host") + if host: + return f"https://{host}" + + return f"https://{conn_config['account']}.snowflakecomputing.com" + + def _get_access_token(self) -> str: + conn_config = self._get_conn_params() + + token = conn_config.get("token") + if not token: + raise ValueError( + "Snowflake connection does not provide an OAuth access token. " + "Cortex Agents require OAuth authentication." + ) + + return token + + def _request( + self, + *, + method: str, + endpoint: str, + payload: dict[str, Any] | None = None, + ) -> dict[str, Any]: + + response = requests.request( + method=method, + url=f"{self._get_base_url()}{endpoint}", + headers={ + "Authorization": f"Bearer {self._get_access_token()}", + "Content-Type": "application/json", + }, + json=payload, + timeout=AGENT_REQUEST_TIMEOUT, + ) + + response.raise_for_status() + + return response.json() + + def run_agent( + self, + *, + database: str, + schema: str, + agent_name: str, + messages: list[dict[str, Any]], + thread_id: int | None = None, + parent_message_id: int | None = None, + stream: bool = False, Review Comment: When `stream=True` the response is returned as SSE events (as per the Snowflake docs). This would break the `_request()` method which returns json directly. I wonder if it's worth removing the parameter and keeping it hardcoded as False. There are probably use cases but not sure if the additional complexity is worth it. That being said, I will leave that up to you if you feel like implementing it. ########## providers/snowflake/src/airflow/providers/snowflake/hooks/snowflake_cortex_agent.py: ########## @@ -0,0 +1,167 @@ +# 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 Any + +import requests + +from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook + +AGENT_REQUEST_TIMEOUT = 60 # Prevent hanging agent requests. Review Comment: I feel like 60 seconds is a bit too short given agents can plan, call tools, evaluate responses, etc. It might make sense to make this a parameter that the user can configure. For what it's worth, Snowflake docs say they cap the Cortex Agent request timeout to 15 minute ########## providers/snowflake/src/airflow/providers/snowflake/hooks/snowflake_cortex_agent.py: ########## @@ -0,0 +1,167 @@ +# 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 Any + +import requests + +from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook + +AGENT_REQUEST_TIMEOUT = 60 # Prevent hanging agent requests. + + +class SnowflakeCortexAgentHook(SnowflakeHook): + """Hook for interacting with Snowflake Cortex Agents.""" + + def _get_base_url(self) -> str: + conn_config = self._get_static_conn_params + + host = conn_config.get("host") + if host: + return f"https://{host}" + + return f"https://{conn_config['account']}.snowflakecomputing.com" + + def _get_access_token(self) -> str: + conn_config = self._get_conn_params() + + token = conn_config.get("token") + if not token: + raise ValueError( + "Snowflake connection does not provide an OAuth access token. " + "Cortex Agents require OAuth authentication." + ) + + return token + + def _request( + self, + *, + method: str, + endpoint: str, + payload: dict[str, Any] | None = None, + ) -> dict[str, Any]: + + response = requests.request( + method=method, + url=f"{self._get_base_url()}{endpoint}", + headers={ + "Authorization": f"Bearer {self._get_access_token()}", + "Content-Type": "application/json", + }, + json=payload, + timeout=AGENT_REQUEST_TIMEOUT, + ) + + response.raise_for_status() Review Comment: This make sense but we would lose the response that Snowflake returns which includes an error message that might be useful. Maybe worth adding a check first (e.g., `if response.status_code >= 400`) then logging the error with the response before raising (something along those lines). ########## providers/snowflake/src/airflow/providers/snowflake/hooks/snowflake_cortex_agent.py: ########## @@ -0,0 +1,167 @@ +# 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 Any + +import requests + +from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook + +AGENT_REQUEST_TIMEOUT = 60 # Prevent hanging agent requests. + + +class SnowflakeCortexAgentHook(SnowflakeHook): + """Hook for interacting with Snowflake Cortex Agents.""" + + def _get_base_url(self) -> str: + conn_config = self._get_static_conn_params + + host = conn_config.get("host") + if host: + return f"https://{host}" + + return f"https://{conn_config['account']}.snowflakecomputing.com" + + def _get_access_token(self) -> str: + conn_config = self._get_conn_params() + + token = conn_config.get("token") + if not token: + raise ValueError( + "Snowflake connection does not provide an OAuth access token. " + "Cortex Agents require OAuth authentication." + ) Review Comment: I think the error message here is a bit misleading here as Cortex Agents do not strictly require OAuth. You can authenticate with key-pair or PATs as well. Non-blocking but maybe worth considering support for key-pair at some point. It's a pretty common auth method and would make this more accessible in my opinion. -- 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]
