kenqia commited on
Commit
eea5869
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -4
app.py CHANGED
@@ -4,20 +4,85 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ from typing import List, TypedDict, Annotated, Optional
8
+ from langchain_openai import ChatOpenAI
9
+ from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
10
+ from langgraph.graph.message import add_messages
11
+ from langgraph.graph import START, StateGraph
12
+ from langgraph.prebuilt import ToolNode, tools_condition
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
  # --- Basic Agent Definition ---
18
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
19
+
20
+ class AgentState(TypedDict):
21
+ messages: Annotated[list[AnyMessage], add_messages]
22
+
23
  class BasicAgent:
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
+
27
+ self.MODEL_NAME = "qwen3.6-plus"
28
+ # export DASHSCOPE_API_KEY="sk-**"
29
+ self.api_key = os.getenv("DASHSCOPE_API_KEY")
30
+ if not self.api_key:
31
+ raise RuntimeError("NO DASHSCOPE_API_KEY")
32
+
33
+ self.model = ChatOpenAI(
34
+ model=self.MODEL_NAME,
35
+ api_key=self.api_key,
36
+ base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
37
+ temperature=0,
38
+ )
39
+ self.tools = []
40
+ self.chat_with_tools = self.model.bind_tools(self.tools)
41
+ # The graph
42
+ builder = StateGraph(AgentState)
43
+
44
+ # Define nodes: these do the work
45
+ builder.add_node("assistant", self.assistant)
46
+ builder.add_node("tools", ToolNode(self.tools))
47
+
48
+ # Define edges: these determine how the control flow moves
49
+ builder.add_edge(START, "assistant")
50
+ builder.add_conditional_edges(
51
+ "assistant",
52
+ # If the latest message requires a tool, route to tools
53
+ # Otherwise, provide a direct response
54
+ tools_condition,
55
+ )
56
+ builder.add_edge("tools", "assistant")
57
+ self.react_graph = builder.compile()
58
+
59
  def __call__(self, question: str) -> str:
60
+ print(f"Agent received question: {question}")
61
+
62
+ try:
63
+
64
+ answer = self.answer_question(question)
65
+
66
+ return str(answer).strip()
67
+
68
+ except Exception as e:
69
+ print(f"Agent error: {e}")
70
+ return ""
71
+ def assistant(self, state: AgentState):
72
+ sys_msg = SystemMessage(content=f"""
73
+ You are a precise question-answering agent.
74
+ Answer the question directly and concisely.
75
+ Return only the final answer, without explanation, unless explanation is necessary.
76
+ """)
77
+ return {
78
+ "messages": [self.chat_with_tools.invoke([sys_msg] + state["messages"])],
79
+ }
80
+
81
+ def answer_question(self, question: str) -> str:
82
+ # TODO:
83
+ messages = [HumanMessage(content=question)]
84
+ messages = self.react_graph.invoke({"messages" : messages})
85
+ return messages["messages"][-1].content
86
 
87
  def run_and_submit_all( profile: gr.OAuthProfile | None):
88
  """