-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 and add some AWEL examples (#815)
- AWEL supports http trigger. - Disassemble the KBQA into some atomic operator. - Add some AWEL examples.
- Loading branch information
Showing
33 changed files
with
1,199 additions
and
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""AWEL: Simple rag example | ||
Example: | ||
.. code-block:: shell | ||
curl -X POST http://127.0.0.1:5000/api/v1/awel/trigger/examples/simple_rag \ | ||
-H "Content-Type: application/json" -d '{ | ||
"conv_uid": "36f0e992-8825-11ee-8638-0242ac150003", | ||
"model_name": "proxyllm", | ||
"chat_mode": "chat_knowledge", | ||
"user_input": "What is DB-GPT?", | ||
"select_param": "default" | ||
}' | ||
""" | ||
|
||
from pilot.awel import HttpTrigger, DAG, MapOperator | ||
from pilot.scene.operator._experimental import ( | ||
ChatContext, | ||
PromptManagerOperator, | ||
ChatHistoryStorageOperator, | ||
ChatHistoryOperator, | ||
EmbeddingEngingOperator, | ||
BaseChatOperator, | ||
) | ||
from pilot.scene.base import ChatScene | ||
from pilot.openapi.api_view_model import ConversationVo | ||
from pilot.model.base import ModelOutput | ||
from pilot.model.operator.model_operator import ModelOperator | ||
|
||
|
||
class RequestParseOperator(MapOperator[ConversationVo, ChatContext]): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
async def map(self, input_value: ConversationVo) -> ChatContext: | ||
return ChatContext( | ||
current_user_input=input_value.user_input, | ||
model_name=input_value.model_name, | ||
chat_session_id=input_value.conv_uid, | ||
select_param=input_value.select_param, | ||
chat_scene=ChatScene.ChatKnowledge, | ||
) | ||
|
||
|
||
with DAG("simple_rag_example") as dag: | ||
trigger_task = HttpTrigger( | ||
"/examples/simple_rag", methods="POST", request_body=ConversationVo | ||
) | ||
req_parse_task = RequestParseOperator() | ||
prompt_task = PromptManagerOperator() | ||
history_storage_task = ChatHistoryStorageOperator() | ||
history_task = ChatHistoryOperator() | ||
embedding_task = EmbeddingEngingOperator() | ||
chat_task = BaseChatOperator() | ||
model_task = ModelOperator() | ||
output_parser_task = MapOperator(lambda out: out.to_dict()["text"]) | ||
|
||
( | ||
trigger_task | ||
>> req_parse_task | ||
>> prompt_task | ||
>> history_storage_task | ||
>> history_task | ||
>> embedding_task | ||
>> chat_task | ||
>> model_task | ||
>> output_parser_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
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
Oops, something went wrong.