-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(awel): AWEL supports http trigger
- Loading branch information
Showing
20 changed files
with
655 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
"""AWEL: Simple chat dag example | ||
Example: | ||
.. code-block:: shell | ||
curl -X POST http://127.0.0.1:5000/api/v1/awel/trigger/examples/simple_chat \ | ||
-H "Content-Type: application/json" -d '{ | ||
"model": "proxyllm", | ||
"user_input": "hello" | ||
}' | ||
""" | ||
from typing import Dict | ||
from pydantic import BaseModel, Field | ||
|
||
from pilot.awel import DAG, HttpTrigger, MapOperator | ||
from pilot.scene.base_message import ModelMessage | ||
from pilot.model.base import ModelOutput | ||
from pilot.model.operator.model_operator import ModelOperator | ||
|
||
|
||
class TriggerReqBody(BaseModel): | ||
model: str = Field(..., description="Model name") | ||
user_input: str = Field(..., description="User input") | ||
|
||
|
||
class RequestHandleOperator(MapOperator[TriggerReqBody, Dict]): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
async def map(self, input_value: TriggerReqBody) -> Dict: | ||
hist = [] | ||
hist.append(ModelMessage.build_human_message(input_value.user_input)) | ||
hist = list(h.dict() for h in hist) | ||
params = { | ||
"prompt": input_value.user_input, | ||
"messages": hist, | ||
"model": input_value.model, | ||
"echo": False, | ||
} | ||
print(f"Receive input value: {input_value}") | ||
return params | ||
|
||
|
||
with DAG("dbgpt_awel_simple_dag_example") as dag: | ||
# Receive http request and trigger dag to run. | ||
trigger = HttpTrigger( | ||
"/examples/simple_chat", methods="POST", request_body=TriggerReqBody | ||
) | ||
request_handle_task = RequestHandleOperator() | ||
model_task = ModelOperator() | ||
# type(out) == ModelOutput | ||
model_parse_task = MapOperator(lambda out: out.to_dict()) | ||
trigger >> request_handle_task >> model_task >> model_parse_task |
This file contains 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,32 @@ | ||
"""AWEL: Simple dag example | ||
Example: | ||
.. code-block:: shell | ||
curl -X GET http://127.0.0.1:5000/api/v1/awel/trigger/examples/hello\?name\=zhangsan | ||
""" | ||
from pydantic import BaseModel, Field | ||
|
||
from pilot.awel import DAG, HttpTrigger, MapOperator | ||
|
||
|
||
class TriggerReqBody(BaseModel): | ||
name: str = Field(..., description="User name") | ||
age: int = Field(18, description="User age") | ||
|
||
|
||
class RequestHandleOperator(MapOperator[TriggerReqBody, str]): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
async def map(self, input_value: TriggerReqBody) -> str: | ||
print(f"Receive input value: {input_value}") | ||
return f"Hello, {input_value.name}, your age is {input_value.age}" | ||
|
||
|
||
with DAG("simple_dag_example") as dag: | ||
trigger = HttpTrigger("/examples/hello", request_body=TriggerReqBody) | ||
map_node = RequestHandleOperator() | ||
trigger >> map_node |
This file contains 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 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,7 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Trigger(ABC): | ||
@abstractmethod | ||
async def trigger(self) -> None: | ||
"""Trigger the workflow or a specific operation in the workflow.""" |
This file contains 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 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,42 @@ | ||
from typing import Dict, Optional | ||
import logging | ||
from pilot.component import BaseComponent, ComponentType, SystemApp | ||
from .loader import DAGLoader, LocalFileDAGLoader | ||
from .base import DAG | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DAGManager(BaseComponent): | ||
name = ComponentType.AWEL_DAG_MANAGER | ||
|
||
def __init__(self, system_app: SystemApp, dag_filepath: str): | ||
super().__init__(system_app) | ||
self.dag_loader = LocalFileDAGLoader(dag_filepath) | ||
self.system_app = system_app | ||
self.dag_map: Dict[str, DAG] = {} | ||
|
||
def init_app(self, system_app: SystemApp): | ||
self.system_app = system_app | ||
|
||
def load_dags(self): | ||
dags = self.dag_loader.load_dags() | ||
triggers = [] | ||
for dag in dags: | ||
dag_id = dag.dag_id | ||
if dag_id in self.dag_map: | ||
raise ValueError(f"Load DAG error, DAG ID {dag_id} has already exist") | ||
triggers += dag.trigger_nodes | ||
from ..trigger.trigger_manager import DefaultTriggerManager | ||
|
||
trigger_manager: DefaultTriggerManager = self.system_app.get_component( | ||
ComponentType.AWEL_TRIGGER_MANAGER, | ||
DefaultTriggerManager, | ||
default_component=None, | ||
) | ||
if trigger_manager: | ||
for trigger in triggers: | ||
trigger_manager.register_trigger(trigger) | ||
trigger_manager.after_register() | ||
else: | ||
logger.warn("No trigger manager, not register dag trigger") |
Oops, something went wrong.