fix ReAct loop
Browse files- app/agent/reasoning.py +42 -4
- tests/react_comparison_report.md +28 -27
- tests/test_react_comparison.py +11 -11
app/agent/reasoning.py
CHANGED
|
@@ -125,13 +125,51 @@ def build_reasoning_prompt(
|
|
| 125 |
) -> str:
|
| 126 |
"""Build the prompt for the next reasoning step."""
|
| 127 |
|
| 128 |
-
# Previous steps summary
|
| 129 |
steps_text = ""
|
| 130 |
if previous_steps:
|
| 131 |
-
steps_text = "\n**Các bước đã thực hiện
|
| 132 |
for step in previous_steps:
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Image context
|
| 137 |
image_text = ""
|
|
|
|
| 125 |
) -> str:
|
| 126 |
"""Build the prompt for the next reasoning step."""
|
| 127 |
|
| 128 |
+
# Previous steps summary with FULL observations
|
| 129 |
steps_text = ""
|
| 130 |
if previous_steps:
|
| 131 |
+
steps_text = "\n**Các bước đã thực hiện và KẾT QUẢ:**\n"
|
| 132 |
for step in previous_steps:
|
| 133 |
+
action = step.get('action', 'unknown')
|
| 134 |
+
thought = step.get('thought', '')[:100]
|
| 135 |
+
observation = step.get('observation', [])
|
| 136 |
+
|
| 137 |
+
steps_text += f"\n📍 **Step {step['step']}**: {thought}...\n"
|
| 138 |
+
steps_text += f" Action: `{action}`\n"
|
| 139 |
+
|
| 140 |
+
# Show detailed observation data
|
| 141 |
+
if action == "get_location_coordinates" and observation:
|
| 142 |
+
if isinstance(observation, dict):
|
| 143 |
+
lat = observation.get('lat', 'N/A')
|
| 144 |
+
lng = observation.get('lng', 'N/A')
|
| 145 |
+
steps_text += f" ✅ Kết quả: lat={lat}, lng={lng}\n"
|
| 146 |
+
steps_text += f" ⚠️ ĐÃ CÓ TỌA ĐỘ - KHÔNG CẦN GỌI LẠI get_location_coordinates\n"
|
| 147 |
+
|
| 148 |
+
elif action == "find_nearby_places" and observation:
|
| 149 |
+
if isinstance(observation, list):
|
| 150 |
+
steps_text += f" ✅ Tìm được {len(observation)} địa điểm:\n"
|
| 151 |
+
for i, place in enumerate(observation[:5], 1):
|
| 152 |
+
name = place.get('name', 'Unknown')
|
| 153 |
+
dist = place.get('distance_km', 'N/A')
|
| 154 |
+
rating = place.get('rating', 'N/A')
|
| 155 |
+
steps_text += f" {i}. {name} ({dist}km, ⭐{rating})\n"
|
| 156 |
+
if len(observation) > 5:
|
| 157 |
+
steps_text += f" ... và {len(observation) - 5} địa điểm khác\n"
|
| 158 |
+
steps_text += f" ⚠️ ĐÃ CÓ DANH SÁCH - KHÔNG CẦN GỌI LẠI find_nearby_places\n"
|
| 159 |
+
|
| 160 |
+
elif action == "retrieve_context_text" and observation:
|
| 161 |
+
if isinstance(observation, list):
|
| 162 |
+
steps_text += f" ✅ Tìm được {len(observation)} kết quả text:\n"
|
| 163 |
+
for i, item in enumerate(observation[:3], 1):
|
| 164 |
+
name = item.get('name', 'Unknown')
|
| 165 |
+
steps_text += f" {i}. {name}\n"
|
| 166 |
+
steps_text += f" ⚠️ ĐÃ CÓ KẾT QUẢ TEXT - KHÔNG CẦN GỌI LẠI retrieve_context_text\n"
|
| 167 |
+
|
| 168 |
+
elif observation:
|
| 169 |
+
result_count = len(observation) if isinstance(observation, list) else 1
|
| 170 |
+
steps_text += f" ✅ Kết quả: {result_count} items\n"
|
| 171 |
+
|
| 172 |
+
steps_text += "\n**⚠️ QUAN TRỌNG:** Nếu đã có đủ thông tin từ các bước trên → action = 'finish'\n"
|
| 173 |
|
| 174 |
# Image context
|
| 175 |
image_text = ""
|
tests/react_comparison_report.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# LocalMate Agent Test Report
|
| 2 |
|
| 3 |
-
**Generated:** 2025-12-
|
| 4 |
|
| 5 |
## Summary
|
| 6 |
|
|
@@ -8,63 +8,64 @@
|
|
| 8 |
|--------|-------------|------------|
|
| 9 |
| Total Tests | 1 | 1 |
|
| 10 |
| Success | 1 | 1 |
|
| 11 |
-
| Avg Duration |
|
| 12 |
|
| 13 |
---
|
| 14 |
|
| 15 |
## Detailed Results
|
| 16 |
|
| 17 |
-
### Test Case
|
| 18 |
|
| 19 |
-
**Query:** `
|
| 20 |
|
| 21 |
#### Single Mode
|
| 22 |
|
| 23 |
- **Status:** ✅ Success
|
| 24 |
-
- **Duration:**
|
| 25 |
-
- **Tools Used:**
|
| 26 |
|
| 27 |
**Workflow:**
|
| 28 |
- Intent Analysis
|
| 29 |
Tool: `None` | Results: 0
|
| 30 |
- Tool Planning
|
| 31 |
Tool: `None` | Results: 0
|
| 32 |
-
- Execute
|
| 33 |
-
Tool: `
|
| 34 |
- LLM Synthesis
|
| 35 |
Tool: `None` | Results: 0
|
| 36 |
|
| 37 |
**Response Preview:**
|
| 38 |
-
> Chào bạn! Dựa trên
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
1. **Nhớ Một Người**
|
| 43 |
-
- Địa chỉ: 06 Tr...
|
| 44 |
|
| 45 |
#### ReAct Mode
|
| 46 |
|
| 47 |
- **Status:** ✅ Success
|
| 48 |
-
- **Duration:**
|
| 49 |
-
- **Tools Used:**
|
| 50 |
-
- **Steps:**
|
| 51 |
- **Intent Detected:** react_multi_step
|
| 52 |
|
| 53 |
**Workflow Steps:**
|
| 54 |
-
- Step 1:
|
| 55 |
-
Tool: `
|
| 56 |
-
- Step 2:
|
| 57 |
-
Tool: `
|
| 58 |
-
- Step 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
Tool: `None` | Results: 0
|
| 60 |
|
| 61 |
**Response Preview:**
|
| 62 |
-
> Chào bạn!
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
-
|
| 66 |
-
- Rating: 4.9/5 ⭐
|
| 67 |
-
- Đặc điểm: Khô...
|
| 68 |
|
| 69 |
---
|
| 70 |
|
|
@@ -74,7 +75,7 @@
|
|
| 74 |
|
| 75 |
| Test | Single Mode Tools | ReAct Mode Tools | ReAct Steps |
|
| 76 |
|------|-------------------|------------------|-------------|
|
| 77 |
-
|
|
| 78 |
|
| 79 |
|
| 80 |
### Key Observations
|
|
|
|
| 1 |
# LocalMate Agent Test Report
|
| 2 |
|
| 3 |
+
**Generated:** 2025-12-18 00:47:10
|
| 4 |
|
| 5 |
## Summary
|
| 6 |
|
|
|
|
| 8 |
|--------|-------------|------------|
|
| 9 |
| Total Tests | 1 | 1 |
|
| 10 |
| Success | 1 | 1 |
|
| 11 |
+
| Avg Duration | 16642ms | 58227ms |
|
| 12 |
|
| 13 |
---
|
| 14 |
|
| 15 |
## Detailed Results
|
| 16 |
|
| 17 |
+
### Test Case 2: Location-based search
|
| 18 |
|
| 19 |
+
**Query:** `Nhà hàng gần bãi biển Mỹ Khê`
|
| 20 |
|
| 21 |
#### Single Mode
|
| 22 |
|
| 23 |
- **Status:** ✅ Success
|
| 24 |
+
- **Duration:** 16642ms
|
| 25 |
+
- **Tools Used:** find_nearby_places
|
| 26 |
|
| 27 |
**Workflow:**
|
| 28 |
- Intent Analysis
|
| 29 |
Tool: `None` | Results: 0
|
| 30 |
- Tool Planning
|
| 31 |
Tool: `None` | Results: 0
|
| 32 |
+
- Execute find_nearby_places
|
| 33 |
+
Tool: `find_nearby_places` | Results: 5
|
| 34 |
- LLM Synthesis
|
| 35 |
Tool: `None` | Results: 0
|
| 36 |
|
| 37 |
**Response Preview:**
|
| 38 |
+
> Chào bạn! Dựa trên kết quả tìm kiếm mới nhất, mình đã tìm được những nhà hàng rất gần bãi biển Mỹ Khê cho bạn. Đặc biệt có một số địa điểm cực kỳ gần chỉ dưới 1km:
|
| 39 |
|
| 40 |
+
🍽️ **Top nhà hàng gần biển Mỹ Khê**...
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
#### ReAct Mode
|
| 43 |
|
| 44 |
- **Status:** ✅ Success
|
| 45 |
+
- **Duration:** 58227ms
|
| 46 |
+
- **Tools Used:** get_location_coordinates, find_nearby_places
|
| 47 |
+
- **Steps:** 5
|
| 48 |
- **Intent Detected:** react_multi_step
|
| 49 |
|
| 50 |
**Workflow Steps:**
|
| 51 |
+
- Step 1: Tôi cần tìm tọa độ của bãi biển Mỹ Khê để làm điểm...
|
| 52 |
+
Tool: `get_location_coordinates` | Results: 0
|
| 53 |
+
- Step 2: Tôi đã có tọa độ của bãi biển Mỹ Khê. Bây giờ tôi ...
|
| 54 |
+
Tool: `find_nearby_places` | Results: 5
|
| 55 |
+
- Step 3: Để giải quyết bài toán này, tôi sẽ phân tích các t...
|
| 56 |
+
Tool: `get_location_coordinates` | Results: 0
|
| 57 |
+
- Step 4: Tôi đã có tọa độ của bãi biển Mỹ Khê và đã tìm đượ...
|
| 58 |
+
Tool: `get_location_coordinates` | Results: 0
|
| 59 |
+
- Step 5: Tôi đã có tọa độ của bãi biển Mỹ Khê và đã tìm đượ...
|
| 60 |
Tool: `None` | Results: 0
|
| 61 |
|
| 62 |
**Response Preview:**
|
| 63 |
+
> Chào bạn! Mình đã tìm được những nhà hàng rất gần bãi biển Mỹ Khê cho bạn. Đây là top 3 địa điểm nổi bật nhất:
|
| 64 |
+
|
| 65 |
+
🍽️ **Top 3 nhà hàng gần biển Mỹ Khê**
|
| 66 |
|
| 67 |
+
**1. Be Man Restaurant** ⭐3.7 (0.75 km)
|
| 68 |
+
- *Nhà ...
|
|
|
|
|
|
|
| 69 |
|
| 70 |
---
|
| 71 |
|
|
|
|
| 75 |
|
| 76 |
| Test | Single Mode Tools | ReAct Mode Tools | ReAct Steps |
|
| 77 |
|------|-------------------|------------------|-------------|
|
| 78 |
+
| 2 | find_nearby_places | get_location_coordinates, find_nearby_places | 5 |
|
| 79 |
|
| 80 |
|
| 81 |
### Key Observations
|
tests/test_react_comparison.py
CHANGED
|
@@ -20,7 +20,7 @@ import httpx
|
|
| 20 |
# =============================================================================
|
| 21 |
|
| 22 |
# API Settings
|
| 23 |
-
API_BASE = "
|
| 24 |
USER_ID = "test_comparison"
|
| 25 |
|
| 26 |
# Delay Settings (in seconds)
|
|
@@ -33,18 +33,18 @@ REQUEST_TIMEOUT = 120 # Timeout for each API request
|
|
| 33 |
|
| 34 |
# Test Cases - 10 queries covering different scenarios
|
| 35 |
TEST_CASES = [
|
| 36 |
-
{
|
| 37 |
-
"id": 1,
|
| 38 |
-
"query": "Quán cafe view đẹp",
|
| 39 |
-
"description": "Simple text search - no location",
|
| 40 |
-
"expected_tools": ["retrieve_context_text"],
|
| 41 |
-
},
|
| 42 |
# {
|
| 43 |
-
# "id":
|
| 44 |
-
# "query": "
|
| 45 |
-
# "description": "
|
| 46 |
-
# "expected_tools": ["
|
| 47 |
# },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# {
|
| 49 |
# "id": 3,
|
| 50 |
# "query": "Quán cafe có không gian xanh mát gần Cầu Rồng",
|
|
|
|
| 20 |
# =============================================================================
|
| 21 |
|
| 22 |
# API Settings
|
| 23 |
+
API_BASE = "https://cuong2004-localmate.hf.space/api/v1"
|
| 24 |
USER_ID = "test_comparison"
|
| 25 |
|
| 26 |
# Delay Settings (in seconds)
|
|
|
|
| 33 |
|
| 34 |
# Test Cases - 10 queries covering different scenarios
|
| 35 |
TEST_CASES = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# {
|
| 37 |
+
# "id": 1,
|
| 38 |
+
# "query": "Quán cafe view đẹp",
|
| 39 |
+
# "description": "Simple text search - no location",
|
| 40 |
+
# "expected_tools": ["retrieve_context_text"],
|
| 41 |
# },
|
| 42 |
+
{
|
| 43 |
+
"id": 2,
|
| 44 |
+
"query": "Nhà hàng gần bãi biển Mỹ Khê",
|
| 45 |
+
"description": "Location-based search",
|
| 46 |
+
"expected_tools": ["find_nearby_places"],
|
| 47 |
+
},
|
| 48 |
# {
|
| 49 |
# "id": 3,
|
| 50 |
# "query": "Quán cafe có không gian xanh mát gần Cầu Rồng",
|