Bhanu-Chander-ABB commited on
Commit
2af4dae
·
1 Parent(s): 51b4921

ReAct prompt

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -728,6 +728,7 @@ def max_object_in_video(video_url: str, object_label: str = "bird") -> str:
728
 
729
  return f"Maximum {object_label} count in a single frame: {max_count}"
730
 
 
731
  def extract_final_answer(output: str) -> str:
732
  # Try to extract answer after [YOUR FINAL ANSWER] or Final Answer:
733
  match = re.search(r"\[YOUR FINAL ANSWER\]\s*(.+)", output)
@@ -738,7 +739,7 @@ def extract_final_answer(output: str) -> str:
738
  return match.group(1).strip()
739
  # Fallback: return the whole output if no match
740
  return output.strip()
741
-
742
 
743
  ##-- Tool Discovery ---
744
  # Use @tool for each function.
@@ -776,34 +777,48 @@ tool_descriptions = "\n".join(f"- {tool.name}: {tool.description}" for tool in t
776
  # --- System Prompt for the Agent ---
777
 
778
  system_prompt = f"""
779
- You are a general AI assistant. I will ask you a question. Report your thoughts as required, and finish your answer with the following template: [YOUR FINAL ANSWER].
780
- YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings, and should not include any explanations, thoughts, or tool calls. Don't give output anything else except the final answer.
781
- First check if you can answer the question by yourself without the need for a tool. If you can answer without hallucination then answer the question directly. If you cannot, use the tools provided to answer the question.
 
 
 
 
782
 
783
- You also have access to a set of tools, which you can use to answer the question. Utilize the tools if you can't answer the question directly. The available tools are:
 
 
 
784
  {tool_descriptions}
785
 
786
  If the question is related to sports, awards, historical facts or similar topic that can be answered from wikipedia, you should use the 'wikipedia_and_generalknowledge_search'.
787
  If the question is based on current events or news kind, then you can utilize the tool 'current_events_news_search_tool' to fetch relevant page information and answer from it.
 
788
  You must not use multiple tools in a single call. Don't hallucinate.
789
 
790
  Instructions to follow for YOUR FINAL ANSWER:
 
 
 
 
 
 
 
791
  - Don't include explanations, thoughts, or tool calls in YOUR FINAL ANSWER.
792
- - YOUR FINAL ANSWER should be a single value (number, string, or comma-separated list).
793
- - If your example thought and Final Answer is something like 'Thought:Final Answer: The country with the least number of athletes at the 1928 Summer Olympics was Luxembourg, which had only 2 athletes.' then your output should be just: 'Luxembourg'
794
-
795
 
796
  **Examples:**
797
  Q: Which country had the least number of athletes at the 1928 Summer Olympics?
 
 
 
 
798
  YOUR FINAL ANSWER: Luxembourg
799
 
800
- Q: List the top 3 programming languages.
 
801
  YOUR FINAL ANSWER: Python, JavaScript, Java
802
 
803
- Q: What is the maximum number os birds in the video https://www.youtube.com/watch?v=example?
804
- YOUR FINAL ANSWER: 12
805
-
806
- If after 12 iterations also a tool usage is not useful then try to answer directly based on your knowledge. If you cannot answer then just say "no_answer" as YOUR FINAL ANSWER.
807
  """
808
  # If your final answer is something like 'there were 5 studio albums published between 2000 and 2009' then modify YOUR FINAL ANSWER as: '5'
809
  # If your final answer is something like 'b, e' then YOUR FINAL ANSWER be: 'b, e'
@@ -871,7 +886,7 @@ chat_llm = ChatHuggingFace(llm=llm)
871
  chat_llm = ChatOpenAI(
872
  openai_api_key=OPENAI_KEY,
873
  model_name=OPENAI_MODEL,
874
- temperature=0.1,
875
  # max_tokens=10
876
  )
877
 
@@ -956,8 +971,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
956
  try:
957
  # full_prompt = f"{system_prompt}\n Input Question: {question_text}"
958
  # submitted_answer = agent.run(full_prompt)
959
- submitted_answer_raw = agent.run(question_text)
960
- submitted_answer = extract_final_answer(submitted_answer_raw)
 
961
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
962
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
963
  except Exception as e:
 
728
 
729
  return f"Maximum {object_label} count in a single frame: {max_count}"
730
 
731
+ '''
732
  def extract_final_answer(output: str) -> str:
733
  # Try to extract answer after [YOUR FINAL ANSWER] or Final Answer:
734
  match = re.search(r"\[YOUR FINAL ANSWER\]\s*(.+)", output)
 
739
  return match.group(1).strip()
740
  # Fallback: return the whole output if no match
741
  return output.strip()
742
+ '''
743
 
744
  ##-- Tool Discovery ---
745
  # Use @tool for each function.
 
777
  # --- System Prompt for the Agent ---
778
 
779
  system_prompt = f"""
780
+ You are a general AI assistant.You should think through problems step-by-step and use tools if needed.
781
+
782
+ Use this reasoning format repeatedly:
783
+ Thought: (what you think is happening or what you want to do next)
784
+ Action: (the tool to use, if needed)
785
+ Action Input: (input to the tool)
786
+ Observation: (result of the tool call)
787
 
788
+ Repeat this process as needed. ONLY AFTER finishing your reasoning and tool use, provide:
789
+ YOUR FINAL ANSWER: (a number, string, or comma-separated list — no explanation, no thoughts, just the answer)
790
+
791
+ You also have access to a set of tools, which you can use to answer the question. The available tools are:
792
  {tool_descriptions}
793
 
794
  If the question is related to sports, awards, historical facts or similar topic that can be answered from wikipedia, you should use the 'wikipedia_and_generalknowledge_search'.
795
  If the question is based on current events or news kind, then you can utilize the tool 'current_events_news_search_tool' to fetch relevant page information and answer from it.
796
+ If the tool returns a long text, table, or list, extract only the most relevant information/paragraphs or data from which you can derive the answer, and return that as your final answer.
797
  You must not use multiple tools in a single call. Don't hallucinate.
798
 
799
  Instructions to follow for YOUR FINAL ANSWER:
800
+ Guidance:
801
+ - Prefer reasoning and tool use before jumping to final answer.
802
+ - Only use one tool per iteration.
803
+ - If the question relates to historical data or general knowledge, use 'wikipedia_and_generalknowledge_search'.
804
+ - If it relates to recent news, use 'current_events_news_search_tool'.
805
+ - If tool output is long, extract only what is relevant and use it to answer the question.
806
+ - Never hallucinate. If you cannot answer even after 12 iterations, return YOUR FINAL ANSWER as: no_answer
807
  - Don't include explanations, thoughts, or tool calls in YOUR FINAL ANSWER.
 
 
 
808
 
809
  **Examples:**
810
  Q: Which country had the least number of athletes at the 1928 Summer Olympics?
811
+ Thought: I should look up historical Olympic data.
812
+ Action: wikipedia_and_generalknowledge_search
813
+ Action Input: least number of athletes 1928 Summer Olympics
814
+ Observation: The country with the least number of athletes was Luxembourg with 2 athletes.
815
  YOUR FINAL ANSWER: Luxembourg
816
 
817
+ Q: What are the top 3 programming languages?
818
+ Thought: This is common knowledge.
819
  YOUR FINAL ANSWER: Python, JavaScript, Java
820
 
821
+ If after 12 iterations also a tool usage is not useful then try to answer directly based on your knowledge without any hallucination. If you cannot answer then just say "no_answer" as YOUR FINAL ANSWER.
 
 
 
822
  """
823
  # If your final answer is something like 'there were 5 studio albums published between 2000 and 2009' then modify YOUR FINAL ANSWER as: '5'
824
  # If your final answer is something like 'b, e' then YOUR FINAL ANSWER be: 'b, e'
 
886
  chat_llm = ChatOpenAI(
887
  openai_api_key=OPENAI_KEY,
888
  model_name=OPENAI_MODEL,
889
+ temperature=0.15,
890
  # max_tokens=10
891
  )
892
 
 
971
  try:
972
  # full_prompt = f"{system_prompt}\n Input Question: {question_text}"
973
  # submitted_answer = agent.run(full_prompt)
974
+ # submitted_answer_raw = agent.run(question_text)
975
+ submitted_answer = agent.run(question_text)
976
+ # submitted_answer = extract_final_answer(submitted_answer_raw)
977
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
978
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
979
  except Exception as e: