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 daf02a7 commit 715600a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 92 deletions.
6 changes: 4 additions & 2 deletions examples/smart_minion/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ async def smart_brain():
query='''
from typing import List def has_close_elements(numbers: List[float], threshold: float) -> bool: """ Check if in given list of numbers, are any two numbers closer to each other than given threshold. >>> has_close_elements([1.0, 2.0, 3.0], 0.5) False >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) True """''',
route="cot",
query_type="code_solution",
# query_type="code_solution",
post_processing="extract_python",
)
print(obs)

Expand All @@ -151,7 +152,8 @@ async def smart_brain():
strange_sort_list([]) == []
'''""",
route="cot",
query_type="code_solution",
# query_type="code_solution",
post_processing="extract_python",
)
print(obs)

Expand Down
78 changes: 0 additions & 78 deletions metagpt/minion/extract_answer.py

This file was deleted.

6 changes: 5 additions & 1 deletion metagpt/minion/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

from pydantic import BaseModel, Field

from metagpt.minion.answer_extraction import extract_math_answer
from metagpt.minion.symbol_table import SymbolTable
from metagpt.utils.math_utils import extract_math_answer, extract_number_from_string
from metagpt.minion.utils import extract_number_from_string, extract_python


class PostProcessingType(Enum):
NONE = "none"
EXTRACT_NUMBER = "extract_number_from_string"
EXTRACT_MATH_ANSWER = "extract_math_answer"
EXTRACT_PYTHON = "extract_python"


class EnsembleStrategyType(Enum):
Expand Down Expand Up @@ -161,6 +163,8 @@ def apply_post_processing(self, raw_answer: str) -> Any:
return extract_number_from_string(raw_answer)
elif self.post_processing == PostProcessingType.EXTRACT_MATH_ANSWER:
return extract_math_answer(raw_answer)
elif self.post_processing == PostProcessingType.EXTRACT_PYTHON:
return extract_python(raw_answer)
else:
return raw_answer

Expand Down
20 changes: 9 additions & 11 deletions metagpt/minion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ def extract_number_from_string(price_str):
# return None # Return None if there is an error


def extract_math_answer(text: str) -> str:
# Look for the answer within \boxed{...}
boxed_match = re.search(r"\\boxed{(.*?)}", text)
if boxed_match:
return boxed_match.group(1)

# If no \boxed{...}, return the last sentence
sentences = text.split(".")
return sentences[-1].strip() if sentences else ""


def compare_number_result(result, correct_answer, tolerance=0.0):
try:
return abs(float(result) - float(correct_answer)) <= tolerance
Expand Down Expand Up @@ -240,6 +229,15 @@ def process_image(image_input):
raise ValueError("Input is not a recognized image format (base64 string, file path, or PIL Image)")


def extract_python(text: str) -> str:
"""Extract Python code from text, typically enclosed in triple backticks."""
pattern = r"```python\s*(.*?)```"
matches = re.findall(pattern, text, re.DOTALL)
if matches:
return matches[0].strip()
return ""


def main():
result = get_synonyms("Trigonometry")
print(result)
Expand Down

0 comments on commit 715600a

Please sign in to comment.