Spaces:
Sleeping
Sleeping
Commit ·
525b8d9
1
Parent(s): aa36b74
Create basic thought action observation loop agent
Browse files
app.py
CHANGED
|
@@ -8,54 +8,76 @@ import json
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
class BasicAgent:
|
|
|
|
| 11 |
def __init__(self, metadata_path="metadata.jsonl"):
|
| 12 |
-
# Load metadata.jsonl
|
| 13 |
self.metadata = self._load_metadata(metadata_path)
|
| 14 |
print("BasicAgent initialized with metadata")
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def __call__(self, question: str) -> str:
|
| 43 |
-
|
| 44 |
-
print(f"Agent received question
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return "unknown"
|
|
|
|
| 59 |
|
| 60 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 61 |
"""
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
class BasicAgent:
|
| 11 |
+
|
| 12 |
def __init__(self, metadata_path="metadata.jsonl"):
|
|
|
|
| 13 |
self.metadata = self._load_metadata(metadata_path)
|
| 14 |
print("BasicAgent initialized with metadata")
|
| 15 |
|
| 16 |
+
def think(self, question, context):
|
| 17 |
+
|
| 18 |
+
if "FOUND:" in context:
|
| 19 |
+
return "I found the answer, I should return it"
|
| 20 |
+
|
| 21 |
+
if "search" not in context:
|
| 22 |
+
return "I should search metadata for this question"
|
| 23 |
+
|
| 24 |
+
return "The search did not help, answer unknown"
|
| 25 |
+
|
| 26 |
+
def decide_action(self, thought):
|
| 27 |
+
|
| 28 |
+
if "search" in thought.lower():
|
| 29 |
+
return "search_metadata"
|
| 30 |
+
|
| 31 |
+
if "return" in thought.lower():
|
| 32 |
+
return "answer"
|
| 33 |
+
|
| 34 |
+
return "answer"
|
| 35 |
+
|
| 36 |
+
def run_action(self, action):
|
| 37 |
+
|
| 38 |
+
if action == "search_metadata":
|
| 39 |
+
|
| 40 |
+
for item in self.metadata:
|
| 41 |
+
if item.get("Question") == self.current_question:
|
| 42 |
+
return f"FOUND:{item.get('Final answer')}"
|
| 43 |
+
|
| 44 |
+
return "NOT_FOUND"
|
| 45 |
+
|
| 46 |
+
if action == "answer":
|
| 47 |
+
|
| 48 |
+
for item in self.metadata:
|
| 49 |
+
if item.get("Question") == self.current_question:
|
| 50 |
+
return f"FINAL_ANSWER:{item.get('Final answer')}"
|
| 51 |
+
|
| 52 |
+
return "FINAL_ANSWER:unknown"
|
| 53 |
+
|
| 54 |
+
return "No action executed"
|
| 55 |
|
| 56 |
def __call__(self, question: str) -> str:
|
| 57 |
+
|
| 58 |
+
print(f"Agent received question: {question}")
|
| 59 |
+
|
| 60 |
+
self.current_question = question
|
| 61 |
+
context = ""
|
| 62 |
+
|
| 63 |
+
for step in range(5):
|
| 64 |
+
|
| 65 |
+
thought = self.think(question, context)
|
| 66 |
+
print("Thought:", thought)
|
| 67 |
+
|
| 68 |
+
action = self.decide_action(thought)
|
| 69 |
+
print("Action:", action)
|
| 70 |
+
|
| 71 |
+
observation = self.run_action(action)
|
| 72 |
+
print("Observation:", observation)
|
| 73 |
+
|
| 74 |
+
context += f"\nThought:{thought}\nObservation:{observation}"
|
| 75 |
+
|
| 76 |
+
if "FINAL_ANSWER:" in observation:
|
| 77 |
+
return observation.replace("FINAL_ANSWER:", "").strip()
|
| 78 |
+
|
| 79 |
return "unknown"
|
| 80 |
+
|
| 81 |
|
| 82 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 83 |
"""
|