-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Xuyang1/refine task and implement workflow task as example #1528
Merged
peteryang1
merged 4 commits into
finco
from
xuyang1/refine_task_and_implement_workflow_task_as_example
May 31, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import fire | ||
from qlib.finco.task import WorkflowManager | ||
from dotenv import load_dotenv | ||
from qlib import auto_init | ||
|
||
|
||
def main(prompt): | ||
def main(prompt=None): | ||
load_dotenv(verbose=True, override=True) | ||
wm = WorkflowManager() | ||
wm.run(prompt) | ||
|
||
|
||
if __name__ == "__main__": | ||
auto_init() | ||
fire.Fire(main) |
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
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
from pathlib import Path | ||
from typing import Any, List | ||
from qlib.log import get_module_logger | ||
from qlib.typehint import Literal | ||
from qlib.finco.conf import Config | ||
from qlib.finco.llm import try_create_chat_completion | ||
from qlib.finco.utils import parse_json | ||
from jinja2 import Template | ||
|
||
import abc | ||
import copy | ||
import logging | ||
|
||
class Task: | ||
|
||
class Task(): | ||
""" | ||
The user's intention, which was initially represented by a prompt, is achieved through a sequence of tasks. | ||
This class doesn't have to be abstract, but it is abstract in the sense that it is not supposed to be instantiated directly because it doesn't have any implementation. | ||
|
||
Some thoughts: | ||
- Do we have to split create a new concept of Action besides Task? | ||
|
@@ -19,34 +29,131 @@ class Task: | |
- Edit Task: it is supposed to edit the code base directly. | ||
""" | ||
|
||
def __init__(self, context=None) -> None: | ||
pass | ||
|
||
## all subclass should implement this method to determine task type | ||
@abc.abstractclassmethod | ||
def __init__(self) -> None: | ||
self._context_manager = None | ||
self.executed = False | ||
|
||
def summarize(self) -> str: | ||
"""After the execution of the task, it is supposed to generated some context about the execution""" | ||
return "" | ||
raise NotImplementedError | ||
|
||
def update_context(self, latest_context): | ||
"""assign the workflow context manager to the task""" | ||
"""then all tasks can use this context manager to share the same context""" | ||
def assign_context_manager(self, context_manager): | ||
... | ||
self._context_manager = context_manager | ||
|
||
def execution(self) -> Any: | ||
def execution(self, **kwargs) -> Any: | ||
"""The execution results of the task""" | ||
pass | ||
raise NotImplementedError | ||
|
||
def interact(self) -> Any: | ||
"""The user can interact with the task""" | ||
"""All sub classes should implement the interact method to determine the next task""" | ||
"""In continous mode, this method will not be called and the next task will be determined by the execution method only""" | ||
raise NotImplementedError("The interact method is not implemented, but workflow not in continous mode") | ||
|
||
class PlanTask(Task): | ||
def execute(self) -> List[Task]: | ||
return [] | ||
class WorkflowTask(Task): | ||
"""This task is supposed to be the first task of the workflow""" | ||
def __init__(self,) -> None: | ||
super().__init__() | ||
self.__DEFAULT_WORKFLOW_SYSTEM_PROMPT = """ | ||
Your task is to determine the workflow in Qlib (supervised learning or reinforcemtn learning) ensureing the workflow can meet the user's requirements. | ||
|
||
The user will provide the requirements, you will provide only the output the choice in exact format specified below with no explanation or conversation. | ||
|
||
class WorkflowTask(PlanTask): | ||
"""make the choice which main workflow (RL, SL) will be used""" | ||
Example input 1: | ||
Help me build a build a low turnover quant investment strategy that focus more on long turn return in China a stock market. | ||
|
||
def execute(self): | ||
... | ||
Example output 1: | ||
workflow: supervised learning | ||
|
||
Example input 2: | ||
Help me build a build a pipeline to determine the best selling point of a stock in a day or half a day in USA stock market. | ||
|
||
Example output 2: | ||
workflow: reinforcemtn learning | ||
""" | ||
|
||
self.__DEFAULT_WORKFLOW_USER_PROMPT = ( | ||
"User input: '{{user_prompt}}'\n" | ||
"Please provide the workflow in Qlib (supervised learning or reinforcemtn learning) ensureing the workflow can meet the user's requirements.\n" | ||
"Response only with the output in the exact format specified in the system prompt, with no explanation or conversation.\n" | ||
) | ||
self.__DEFAULT_USER_PROMPT = "Please help me build a low turnover strategy that focus more on longterm return in China a stock market." | ||
self.logger = get_module_logger("fincoWorkflowTask", level=logging.INFO) | ||
|
||
"""make the choice which main workflow (RL, SL) will be used""" | ||
def execute(self,) -> List[Task]: | ||
user_prompt = self._context_manager.get_context("user_prompt") | ||
user_prompt = user_prompt if user_prompt is not None else self.__DEFAULT_USER_PROMPT | ||
system_prompt = self.__DEFAULT_WORKFLOW_SYSTEM_PROMPT | ||
prompt_workflow_selection = Template( | ||
self.__DEFAULT_WORKFLOW_USER_PROMPT | ||
).render(user_prompt=user_prompt) | ||
messages = [ | ||
{ | ||
"role": "system", | ||
"content": system_prompt, | ||
}, | ||
{ | ||
"role": "user", | ||
"content": prompt_workflow_selection, | ||
}, | ||
] | ||
response = try_create_chat_completion(messages=messages) | ||
workflow = response.split(":")[1].strip().lower() | ||
self.executed = True | ||
self._context_manager.set_context("workflow", workflow) | ||
if workflow == "supervised learning": | ||
return [SLTask()] | ||
elif workflow == "reinforcement learning": | ||
return [RLTask()] | ||
else: | ||
raise ValueError(f"The workflow: {workflow} is not supported") | ||
|
||
def interact(self) -> Any: | ||
assert self.executed == True, "The workflow task has not been executed yet" | ||
## TODO use logger | ||
self.logger.info( | ||
f"The workflow has been determined to be ---{self._context_manager.get_context('workflow')}---" | ||
) | ||
self.logger.info( | ||
"Enter 'y' to authorise command,'s' to run self-feedback commands, " | ||
"'n' to exit program, or enter feedback for WorkflowTask" | ||
) | ||
try: | ||
answer = input("You answer is:") | ||
except KeyboardInterrupt: | ||
self.logger.info("User has exited the program") | ||
exit() | ||
if answer.lower().strip() == "y": | ||
return | ||
else: | ||
# TODO add self feedback | ||
raise ValueError("The input cannot be interpreted as a valid input") | ||
|
||
|
||
class PlanTask(Task): | ||
def execute(self, prompt) -> List[Task]: | ||
return [] | ||
|
||
class SLTask(PlanTask): | ||
def __init__(self,) -> None: | ||
super().__init__() | ||
|
||
def exeute(self): | ||
""" | ||
return a list of interested tasks | ||
Copy the template project maybe a part of the task | ||
""" | ||
return [] | ||
|
||
class RLTask(PlanTask): | ||
def __init__(self,) -> None: | ||
super().__init__() | ||
def exeute(self): | ||
""" | ||
return a list of interested tasks | ||
|
@@ -58,6 +165,29 @@ def exeute(self): | |
class ActionTask(Task): | ||
def execute(self) -> Literal["fail", "success"]: | ||
return "success" | ||
|
||
"""Context Manager stores the context of the workflow""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring will be better |
||
"""All context are key value pairs which saves the input, output and status of the whole workflow""" | ||
class WorkflowContextManager(): | ||
def __init__(self) -> None: | ||
self.context = {} | ||
self.logger = get_module_logger("fincoWorkflowContextManager") | ||
|
||
def set_context(self, key, value): | ||
if key in self.context: | ||
self.logger.warning("The key already exists in the context, the value will be overwritten") | ||
self.context[key] = value | ||
|
||
def get_context(self, key): | ||
if key not in self.context: | ||
self.logger.warning("The key doesn't exist in the context") | ||
return None | ||
return self.context[key] | ||
|
||
"""return a deep copy of the context""" | ||
"""TODO: do we need to return a deep copy?""" | ||
def get_all_context(self): | ||
return copy.deepcopy(self.context) | ||
|
||
|
||
class WorkflowManager: | ||
|
@@ -69,13 +199,14 @@ def __init__(self, name="project", output_path=None) -> None: | |
self._output_path = Path.cwd() / name | ||
else: | ||
self._output_path = Path(output_path) | ||
self._context = [] | ||
self._context = WorkflowContextManager() | ||
|
||
def add_context(self, task_res): | ||
self._context.append(task_res) | ||
"""Direct call set_context method of the context manager""" | ||
def set_context(self, key, value): | ||
self._context.set_context(key, value) | ||
|
||
def get_context(self): | ||
"""TODO: context manger?""" | ||
def get_context(self) -> WorkflowContextManager: | ||
return self._context | ||
|
||
def run(self, prompt: str) -> Path: | ||
""" | ||
|
@@ -101,16 +232,23 @@ def run(self, prompt: str) -> Path: | |
# - The generated tasks can't be changed after geting new information from the execution retuls. | ||
# - But it is required in some cases, if we want to build a external dataset, it maybe have to plan like autogpt... | ||
|
||
cfg = Config() | ||
|
||
# NOTE: list may not be enough for general task list | ||
task_list = [WorkflowTask(prompt)] | ||
self.set_context("user_prompt", prompt) | ||
task_list = [WorkflowTask()] | ||
while len(task_list): | ||
# task_list.ap | ||
"""task list is not long, so sort it is not a big problem""" | ||
"""TODO: sort the task list based on the priority of the task""" | ||
# task_list = sorted(task_list, key=lambda x: x.task_type) | ||
t = task_list.pop(0) | ||
t.update_context(self.get_context()) | ||
t.assign_context_manager(self._context) | ||
res = t.execute() | ||
if isinstance(t, PlanTask): | ||
if not cfg.continous_mode: | ||
res = t.interact() | ||
if isinstance(t.task_type, WorkflowTask) or isinstance(t.task_type, PlanTask): | ||
task_list.extend(res) | ||
elif isinstance(t, ActionTask): | ||
elif isinstance(t.task_type, ActionTask): | ||
if res != "success": | ||
... | ||
# TODO: handle the unexpected execution Error | ||
|
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,9 @@ | ||
import json | ||
|
||
def parse_json(response): | ||
try: | ||
return json.loads(response) | ||
except json.decoder.JSONDecodeError: | ||
pass | ||
|
||
raise Exception(f"Failed to parse response: {response}, please report it or help us to fix it.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring