Skip to content

Commit

Permalink
apply_post_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
femto committed Oct 17, 2024
1 parent 1cf50a2 commit fbb3f66
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion metagpt/minion/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from metagpt.minion.utils import extract_number_from_string, extract_python


class PostProcessingType(Enum):
class PostProcessingType(str, Enum):
NONE = "none"
EXTRACT_NUMBER = "extract_number_from_string"
EXTRACT_MATH_ANSWER = "extract_math_answer"
Expand Down
2 changes: 1 addition & 1 deletion metagpt/minion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def extract_python(text: str) -> str:
matches = re.findall(pattern, text, re.DOTALL)
if matches:
return matches[0].strip()
return ""
return text # assume we already extracted


def main():
Expand Down
22 changes: 7 additions & 15 deletions metagpt/minion/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from metagpt.actions.action_node import ActionNode
from metagpt.logs import logger
from metagpt.minion.check import CheckMinion
from metagpt.minion.input import Input
from metagpt.minion.input import Input, PostProcessingType
from metagpt.minion.minion import (
MINION_REGISTRY,
MINION_ROUTE_DOWNSTREAM,
Expand All @@ -47,7 +47,7 @@
)
from metagpt.minion.symbol_table import Symbol
from metagpt.minion.task_graph import convert_tasks_to_graph
from metagpt.minion.utils import most_similar_minion
from metagpt.minion.utils import extract_python, most_similar_minion
from metagpt.utils.custom_decoder import CustomDecoder


Expand Down Expand Up @@ -228,8 +228,8 @@ async def execute(self):
)
self.answer_node = node

if self.input.query_type == "code_solution" or self.input.post_processing == "extract_python":
self.answer = self.extract_python_code(node.content)
if self.input.query_type == "code_solution" or self.input.post_processing == PostProcessingType.EXTRACT_PYTHON:
self.answer = extract_python(node.content)
else:
self.answer = extract_final_answer(node.content)

Expand All @@ -248,14 +248,6 @@ async def execute(self):
self.raw_answer = self.input.raw_answer = node.content
return self.answer

def extract_python_code(self, content):
# Regex pattern to extract code inside ```python ``` blocks
pattern = r"```python\s*(.*?)\s*```"
match = re.search(pattern, content, re.DOTALL)
if match:
return match.group(1).strip()
return None


@register_route_downstream
class DotMinion(Minion):
Expand Down Expand Up @@ -886,9 +878,9 @@ async def invoke_minion(self, klass):
klass = MINION_ROUTE_DOWNSTREAM.get(klass, CotMinion)
self.current_minion = klass(input=self.input, brain=self.brain)
self.add_followers(self.current_minion)
await self.current_minion.execute()
self.answer = self.input.answer = self.current_minion.answer
return self.current_minion.answer
result = await self.current_minion.execute()
self.answer = self.input.answer = result
return result

async def choose_minion_and_run(self):
choose_template = Template(SMART_PROMPT_TEMPLATE)
Expand Down

0 comments on commit fbb3f66

Please sign in to comment.