mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-14 15:25:17 +02:00
update simple_scorer
This commit is contained in:
parent
f61506bd32
commit
1ead3e4d80
6 changed files with 71 additions and 75 deletions
|
|
@ -2,6 +2,8 @@ import asyncio
|
|||
|
||||
import pytest
|
||||
|
||||
from metagpt.config2 import Config
|
||||
from metagpt.configs.exp_pool_config import ExperiencePoolConfig
|
||||
from metagpt.exp_pool.context_builders import SimpleContextBuilder
|
||||
from metagpt.exp_pool.decorator import ExpCacheHandler, exp_cache
|
||||
from metagpt.exp_pool.manager import ExperienceManager
|
||||
|
|
@ -20,6 +22,8 @@ class TestExpCacheHandler:
|
|||
def mock_exp_manager(self, mocker):
|
||||
manager = mocker.MagicMock(spec=ExperienceManager)
|
||||
manager.storage = mocker.MagicMock(spec=SimpleEngine)
|
||||
manager.config = mocker.MagicMock(spec=Config)
|
||||
manager.config.exp_pool = ExperiencePoolConfig()
|
||||
manager.query_exps = mocker.AsyncMock()
|
||||
manager.create_exp = mocker.MagicMock()
|
||||
return manager
|
||||
|
|
@ -131,9 +135,10 @@ class TestExpCacheHandler:
|
|||
|
||||
class TestExpCache:
|
||||
@pytest.fixture
|
||||
def mock_exp_manager(self, mocker):
|
||||
def mock_exp_manager(self, mocker, mock_config):
|
||||
manager = mocker.MagicMock(spec=ExperienceManager)
|
||||
manager.storage = mocker.MagicMock(spec=SimpleEngine)
|
||||
manager.config = mock_config
|
||||
manager.query_exps = mocker.AsyncMock()
|
||||
manager.create_exp = mocker.MagicMock()
|
||||
return manager
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from metagpt.exp_pool.schema import Score
|
||||
|
|
@ -20,30 +22,43 @@ class TestSimpleScorer:
|
|||
assert isinstance(scorer.llm, BaseLLM)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evaluate(self, simple_scorer, mock_llm):
|
||||
# Mock function to evaluate
|
||||
def mock_func(a, b):
|
||||
"""This is a mock function."""
|
||||
return a + b
|
||||
async def test_evaluate(self, simple_scorer, mock_llm, mocker):
|
||||
# Mock request and response
|
||||
req = "What is the capital of France?"
|
||||
resp = "The capital of France is Paris."
|
||||
|
||||
# Mock LLM response
|
||||
mock_llm.aask.return_value = '```json\n{"val": 8, "reason": "Good performance"}\n```'
|
||||
mock_llm_response = '{"val": 9, "reason": "Accurate and concise answer"}'
|
||||
mock_llm.aask.return_value = f"```json\n{mock_llm_response}\n```"
|
||||
|
||||
# Mock CodeParser.parse_code
|
||||
mocker.patch("metagpt.utils.common.CodeParser.parse_code", return_value=mock_llm_response)
|
||||
|
||||
# Test evaluate method
|
||||
result = await simple_scorer.evaluate(mock_func, 5, args=(2, 3), kwargs={})
|
||||
result = await simple_scorer.evaluate(req, resp)
|
||||
|
||||
# Assert LLM was called with correct prompt
|
||||
expected_prompt = SIMPLE_SCORER_TEMPLATE.format(
|
||||
func_name=mock_func.__name__,
|
||||
func_doc=mock_func.__doc__,
|
||||
func_signature="(a, b)",
|
||||
func_args=(2, 3),
|
||||
func_kwargs={},
|
||||
func_result=5,
|
||||
)
|
||||
expected_prompt = SIMPLE_SCORER_TEMPLATE.format(req=req, resp=resp)
|
||||
mock_llm.aask.assert_called_once_with(expected_prompt)
|
||||
|
||||
# Assert the result is correct
|
||||
assert isinstance(result, Score)
|
||||
assert result.val == 8
|
||||
assert result.reason == "Good performance"
|
||||
assert result.val == 9
|
||||
assert result.reason == "Accurate and concise answer"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evaluate_invalid_response(self, simple_scorer, mock_llm, mocker):
|
||||
# Mock request and response
|
||||
req = "What is the capital of France?"
|
||||
resp = "The capital of France is Paris."
|
||||
|
||||
# Mock LLM response with invalid JSON
|
||||
mock_llm_response = "Invalid JSON"
|
||||
mock_llm.aask.return_value = f"```json\n{mock_llm_response}\n```"
|
||||
|
||||
# Mock CodeParser.parse_code
|
||||
mocker.patch("metagpt.utils.common.CodeParser.parse_code", return_value=mock_llm_response)
|
||||
|
||||
# Test evaluate method with invalid response
|
||||
with pytest.raises(json.JSONDecodeError):
|
||||
await simple_scorer.evaluate(req, resp)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue