/ examples / exp_pool / scorer.py
scorer.py
 1  import asyncio
 2  
 3  from metagpt.exp_pool.scorers import SimpleScorer
 4  
 5  # Request to implement quicksort in Python
 6  REQ = "Write a program to implement quicksort in python."
 7  
 8  # First response: Quicksort implementation without base case
 9  RESP1 = """
10  def quicksort(arr):
11      return quicksort([x for x in arr[1:] if x <= arr[0]]) + [arr[0]] + quicksort([x for x in arr[1:] if x > arr[0]])
12  """
13  
14  # Second response: Quicksort implementation with base case
15  RESP2 = """
16  def quicksort(arr):
17      if len(arr) <= 1:
18          return arr
19      return quicksort([x for x in arr[1:] if x <= arr[0]]) + [arr[0]] + quicksort([x for x in arr[1:] if x > arr[0]])
20  """
21  
22  
23  async def simple():
24      """Evaluates two quicksort implementations using SimpleScorer.
25  
26      Example:
27          {
28              "val": 3,
29              "reason": "The response attempts to implement quicksort but contains a critical flaw: it lacks a base case to terminate the recursion, which will lead to a maximum recursion depth exceeded error for non-empty lists. Additionally, the function does not handle empty lists properly. A correct implementation should include a base case to handle lists of length 0 or 1."
30          }
31      """
32  
33      scorer = SimpleScorer()
34  
35      await scorer.evaluate(req=REQ, resp=RESP1)
36      await scorer.evaluate(req=REQ, resp=RESP2)
37  
38  
39  async def main():
40      await simple()
41  
42  
43  if __name__ == "__main__":
44      asyncio.run(main())