josix commented on code in PR #41554: URL: https://github.com/apache/airflow/pull/41554#discussion_r1723257205
########## airflow/providers/openai/triggers/openai.py: ########## @@ -0,0 +1,116 @@ +# 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 + +import asyncio +import time +from typing import Any, AsyncIterator + +from airflow.providers.openai.hooks.openai import BatchStatus, OpenAIHook +from airflow.triggers.base import BaseTrigger, TriggerEvent + + +class OpenAIBatchTrigger(BaseTrigger): + """Triggers OpenAI Batch API.""" + + def __init__( + self, + conn_id: str, + batch_id: str, + poll_interval: float, + end_time: float, + ): + super().__init__() + self.conn_id = conn_id + self.poll_interval = poll_interval + self.batch_id = batch_id + self.end_time = end_time + + def serialize(self) -> tuple[str, dict[str, Any]]: + """Serialize OpenAIBatchTrigger arguments and class path.""" + return ( + "airflow.providers.openai.triggers.openai.OpenAIBatchTrigger", + { + "conn_id": self.conn_id, + "batch_id": self.batch_id, + "poll_interval": self.poll_interval, + "end_time": self.end_time, + }, + ) + + async def run(self) -> AsyncIterator[TriggerEvent]: + """Make connection to OpenAI Client, and poll the status of batch.""" + hook = OpenAIHook(conn_id=self.conn_id) + try: + while (batch := hook.get_batch(self.batch_id)) and batch.status in { + BatchStatus.VALIDATING, + BatchStatus.IN_PROGRESS, + BatchStatus.FINALIZING, + }: Review Comment: I made this check as a class method of BatchStatus so that the binding between status and the check could be stronger. -- 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]
