mirjam-m commited on
Commit
e7e8f66
·
1 Parent(s): a76fad9
Files changed (1) hide show
  1. app.py +62 -13
app.py CHANGED
@@ -21,13 +21,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
 
22
  # --- Basic Agent Definition ---
23
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
24
- class BasicAgent:
25
- def __init__(self):
26
- self.model = ChatOpenAI(model="gpt-4o", temperature=0)
27
 
28
- def __call__(self, question: str) -> str:
29
- print(f"Agent received question (first 50 chars): {question[:50]}...")
30
- prompt = f"""
31
  You are a general AI assistant. I will ask you a question.
32
  Report your thoughts,always use at least 2 sources to double check your answer, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
33
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
@@ -56,16 +51,70 @@ The part after `FINAL ANSWER: ` **MUST** contain **ONLY** the final answer, form
56
 
57
  Question: {question}
58
  """
59
- messages = [HumanMessage(content=prompt)]
60
- response = self.model.invoke(messages)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  txt = response.text().split("FINAL ANSWER:")
63
  print(f"Agent returning answer: {txt}")
64
-
65
- if len(txt) > 1:
66
- return txt[-1].strip()
67
- else:
68
- return "I don't know."
 
 
 
 
 
 
69
 
70
 
71
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
21
 
22
  # --- Basic Agent Definition ---
23
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
24
 
25
+ PROMPT = """
 
 
26
  You are a general AI assistant. I will ask you a question.
27
  Report your thoughts,always use at least 2 sources to double check your answer, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
28
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
 
51
 
52
  Question: {question}
53
  """
 
 
54
 
55
+
56
+ class AnswerState(TypedDict):
57
+ question: str
58
+ answer: Optional[str]
59
+ messages: List[Dict[str, Any]]
60
+
61
+
62
+ class BasicAgent:
63
+ def __init__(self):
64
+ self.model = ChatOpenAI(model="gpt-4o", temperature=0)
65
+ self.graph = StateGraph(AnswerState)
66
+ self.graph.add_node("log_question", self.log_question)
67
+ self.graph.add_node("try_answer", self.try_answer)
68
+ self.graph.add_node("final_answer", self.final_answer)
69
+
70
+ self.graph.add_edge(START, "log_question")
71
+ self.graph.add_edge("log_question", "try_answer")
72
+
73
+ # Add conditional edges
74
+ self.graph.add_conditional_edges(
75
+ "try_answer",
76
+ self.try_answer,
77
+ {"FINAL_ANSWER": "final_answer", "SOME_TOOL": "final_answer"},
78
+ )
79
+
80
+ self.graph.add_edge("final_answer", END)
81
+ self.compiled_graph = self.graph.compile()
82
+
83
+ def __call__(self, question: str) -> str:
84
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
85
+ res = self.compiled_graph.invoke(
86
+ {
87
+ "question": question,
88
+ "answer": None,
89
+ "messages": [],
90
+ }
91
+ )
92
+ return str(res)
93
+
94
+ def log_question(self, state: AnswerState) -> None:
95
+ print(f"Agent received question (first 50 chars): {state['question']}...")
96
+
97
+ def final_answer(self, state: AnswerState) -> str:
98
+ print(f"Agent returning answer: {state['answer']}")
99
+ return state["answer"] if state["answer"] else "I don't know"
100
+
101
+ def try_answer(self, state: AnswerState) -> str:
102
+
103
+ messages = [HumanMessage(content=PROMPT.format(question=state["question"]))]
104
+ response = self.model.invoke(messages)
105
  txt = response.text().split("FINAL ANSWER:")
106
  print(f"Agent returning answer: {txt}")
107
+ answer = txt[-1].strip() if len(txt) > 1 else "I don't know."
108
+ print(f"Agent returning answer: {state['answer']}")
109
+ state["messages"] = state.get("messages", []) + [
110
+ {
111
+ "role": "assistant",
112
+ "content": f"FINAL ANSWER: {state['answer']}",
113
+ }
114
+ ]
115
+ state["answer"] = answer
116
+
117
+ return "FINAL_ANSWER"
118
 
119
 
120
  def run_and_submit_all(profile: gr.OAuthProfile | None):