ahnhs2k commited on
Commit
8e7ca4b
ยท
1 Parent(s): 9615107
Files changed (1) hide show
  1. app.py +47 -13
app.py CHANGED
@@ -26,6 +26,13 @@ You MUST:
26
  - No extra text.
27
  """
28
 
 
 
 
 
 
 
 
29
  def clean_answer(text: str) -> str:
30
  if not text:
31
  return ""
@@ -35,8 +42,22 @@ def clean_answer(text: str) -> str:
35
  s = s.strip('"\'`')
36
  if len(s) > 1 and s.endswith("."):
37
  s = s[:-1].strip()
 
 
 
 
38
  return s
39
 
 
 
 
 
 
 
 
 
 
 
40
  # -------------------------------
41
  # State
42
  # -------------------------------
@@ -67,25 +88,38 @@ class BasicAgent:
67
  def __call__(self, question: str) -> str:
68
  print(f"Question: {question[:80]}...")
69
 
70
- # 1) Search
 
 
 
 
 
 
 
 
71
  try:
72
- search_result = search_tool.run(question)
73
- except Exception as e:
74
- print("Search error:", e)
75
- search_result = ""
 
 
 
 
76
 
77
- # 2) Prompt with evidence
78
  prompt = f"""
79
- {SYSTEM_PROMPT}
 
 
 
80
 
81
- Question:
82
- {question}
83
 
84
- Search Results:
85
- {search_result}
86
- """.strip()
87
 
88
- # 3) LLM Answer
89
  response = llm.invoke([HumanMessage(content=prompt)])
90
  answer = clean_answer(response.content)
91
 
 
26
  - No extra text.
27
  """
28
 
29
+ RULES = {
30
+ "number": "Answer with ONLY the number. No words.",
31
+ "date": "Answer with ONLY the year or date. No words.",
32
+ "yesno": "Answer with ONLY Yes or No.",
33
+ "fact": "Answer with ONLY the final answer."
34
+ }
35
+
36
  def clean_answer(text: str) -> str:
37
  if not text:
38
  return ""
 
42
  s = s.strip('"\'`')
43
  if len(s) > 1 and s.endswith("."):
44
  s = s[:-1].strip()
45
+
46
+ if s.lower() in ["yes", "no"]:
47
+ s = s.capitalize()
48
+
49
  return s
50
 
51
+ def classify_question(q: str) -> str:
52
+ ql = q.lower().strip()
53
+ if any(k in ql for k in ["how many", "how much", "number of", "population"]):
54
+ return "number"
55
+ if any(k in ql for k in ["what year", "when", "date"]):
56
+ return "date"
57
+ if ql.startswith(("is ", "are ", "was ", "were ", "did ", "does ")):
58
+ return "yesno"
59
+ return "fact"
60
+
61
  # -------------------------------
62
  # State
63
  # -------------------------------
 
88
  def __call__(self, question: str) -> str:
89
  print(f"Question: {question[:80]}...")
90
 
91
+ qtype = classify_question(question)
92
+ rule = RULES[qtype]
93
+
94
+ # ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ ๋ณด์ • (์ˆซ์ž/๋‚ ์งœ๋Š” Wikipedia ์šฐ์„ )
95
+ search_query = question
96
+ if qtype in ["number", "date"]:
97
+ search_query = question + " site:wikipedia.org"
98
+
99
+ # ๊ฒ€์ƒ‰ (์‹คํŒจ ์‹œ 1ํšŒ ์žฌ์‹œ๋„)
100
  try:
101
+ search_result = search_tool.run(search_query)
102
+ if not search_result:
103
+ raise RuntimeError("empty search")
104
+ except Exception:
105
+ try:
106
+ search_result = search_tool.run(question)
107
+ except Exception:
108
+ search_result = ""
109
 
 
110
  prompt = f"""
111
+ {SYSTEM_PROMPT}
112
+
113
+ Additional Rules:
114
+ - {rule}
115
 
116
+ Question:
117
+ {question}
118
 
119
+ Search Results:
120
+ {search_result}
121
+ """.strip()
122
 
 
123
  response = llm.invoke([HumanMessage(content=prompt)])
124
  answer = clean_answer(response.content)
125