yooke commited on
Commit
c4ed60f
·
verified ·
1 Parent(s): 909afab

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +117 -37
agent.py CHANGED
@@ -1,86 +1,97 @@
1
  import os
2
  from dotenv import load_dotenv
3
  from langgraph.graph import START, StateGraph, MessagesState
4
- from langgraph.prebuilt import tools_condition, ToolNode
5
- from langchain_community.tools.tavily_search import TavilySearchResults
 
6
  from langchain_community.document_loaders import WikipediaLoader
7
- from langchain_community.tools import DuckDuckGoSearchRun
8
  from langchain_core.messages import SystemMessage, HumanMessage
9
  from langchain_core.tools import tool
 
10
  from langchain_deepseek import ChatDeepSeek
11
 
12
- load_dotenv()
13
 
 
 
14
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
15
- TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
16
 
17
  if not DEEPSEEK_API_KEY:
18
  raise ValueError("DEEPSEEK_API_KEY not found in environment variables.")
19
  if not TAVILY_API_KEY:
20
- print("Warning: TAVILY_API_KEY not found. Tavily search tool may not work.")
 
21
 
 
 
 
22
  @tool
23
  def wiki_search(query: str) -> str:
 
24
  try:
25
- search_docs = WikipediaLoader(query=query, load_max_docs=2, doc_content_chars_max=2000).load()
26
  if not search_docs:
27
- return "Wikipedia search found no relevant pages."
28
  formatted_search_docs = "\n\n---\n\n".join(
29
  [
30
  f'<Document source="Wikipedia - {doc.metadata.get("source", "")}" page="{doc.metadata.get("page", "")}">\n{doc.page_content}\n</Document>'
31
  for doc in search_docs
32
  ])
33
- return formatted_search_docs
34
  except Exception as e:
35
  return f"An error occurred during Wikipedia search: {e}"
36
 
 
 
 
37
  @tool
38
  def web_search(query: str) -> str:
39
- if not TAVILY_API_KEY:
40
- return "Tavily search is not available because TAVILY_API_KEY is not set."
41
  try:
42
- tavily = TavilySearchResults(max_results=5)
43
  results = tavily.invoke(query)
44
  if not results:
45
- return "Web search (Tavily) found no relevant results."
 
46
  formatted_results = "\n\n---\n\n".join([
47
  f'<SearchResult source="{r["source"]}">\nTitle: {r["title"]}\nContent: {r["content"]}\n</SearchResult>'
48
  for r in results
49
  ])
50
- return formatted_results
51
  except Exception as e:
52
- return f"An error occurred during web search (Tavily): {e}"
53
-
54
- duckduckgo_search_tool_instance = DuckDuckGoSearchRun()
55
-
56
  @tool
57
  def duckduckgo_search(query: str) -> str:
 
58
  try:
59
- results = duckduckgo_search_tool_instance.run(query)
60
- if not results:
 
61
  return "DuckDuckGo search found no relevant results."
62
- return results
63
  except Exception as e:
64
  return f"An error occurred during DuckDuckGo search: {e}"
 
65
 
66
- try:
67
- with open("system_prompt.txt", "r", encoding="utf-8") as f:
68
- system_prompt = f.read()
69
- sys_msg = SystemMessage(content=system_prompt)
70
- except FileNotFoundError:
71
- print("Warning: system_prompt.txt not found. Using a default system message.")
72
- sys_msg = SystemMessage(content="You are a helpful AI assistant.")
73
 
74
  tools = [
75
  wiki_search,
76
- web_search,
77
  duckduckgo_search,
 
78
  ]
79
 
 
80
  def build_graph():
81
  llm = ChatDeepSeek(
82
  model="deepseek-chat",
83
- temperature=0,
84
  max_tokens=None,
85
  timeout=None,
86
  max_retries=2,
@@ -89,19 +100,88 @@ def build_graph():
89
  )
90
  llm_with_tools = llm.bind_tools(tools)
91
 
 
92
  def assistant(state: MessagesState):
93
- print("---Calling Assistant---")
94
- messages_for_llm = [sys_msg] + state["messages"]
95
- result = llm_with_tools.invoke(messages_for_llm)
96
- print(f"---Assistant Response: {result}")
97
- return {"messages": [result]}
 
 
 
 
 
 
98
 
99
  builder = StateGraph(MessagesState)
100
  builder.add_node("assistant", assistant)
101
  builder.add_node("tools", ToolNode(tools))
102
 
103
  builder.add_edge(START, "assistant")
104
- builder.add_conditional_edges("assistant", tools_condition)
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  builder.add_edge("tools", "assistant")
106
 
107
- return builder.compile()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  from dotenv import load_dotenv
3
  from langgraph.graph import START, StateGraph, MessagesState
4
+ from langgraph.prebuilt import tools_condition
5
+ from langgraph.prebuilt import ToolNode
6
+ from langchain_community.tools.tavily_search import TavilySearchResults # 已经导入了
7
  from langchain_community.document_loaders import WikipediaLoader
8
+ from langchain_community.document_loaders import ArxivLoader
9
  from langchain_core.messages import SystemMessage, HumanMessage
10
  from langchain_core.tools import tool
11
+ # from langchain_openai import ChatOpenAI
12
  from langchain_deepseek import ChatDeepSeek
13
 
 
14
 
15
+ # load_dotenv() # 假设你在 app.py 或其他地方加载了 .env
16
+ # Ensure API keys are set
17
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
18
+ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY") # 需要在 Space Secrets 中添加 TAVILY_API_KEY
19
 
20
  if not DEEPSEEK_API_KEY:
21
  raise ValueError("DEEPSEEK_API_KEY not found in environment variables.")
22
  if not TAVILY_API_KEY:
23
+ # Tavily is critical for most questions, raise error if not set
24
+ raise ValueError("TAVILY_API_KEY not found in environment variables. Please add it to your Space Secrets.")
25
 
26
+
27
+
28
+ # Keep Wikipedia and Arxiv, but the general search will be more used
29
  @tool
30
  def wiki_search(query: str) -> str:
31
+ "Using Wikipedia, search for a query and return up to 2 relevant results."
32
  try:
33
+ search_docs = WikipediaLoader(query=query, load_max_docs=2, doc_content_chars_max=2000).load() # Limit content length
34
  if not search_docs:
35
+ return "Wikipedia search found no relevant pages."
36
  formatted_search_docs = "\n\n---\n\n".join(
37
  [
38
  f'<Document source="Wikipedia - {doc.metadata.get("source", "")}" page="{doc.metadata.get("page", "")}">\n{doc.page_content}\n</Document>'
39
  for doc in search_docs
40
  ])
41
+ return formatted_search_docs # Return string directly
42
  except Exception as e:
43
  return f"An error occurred during Wikipedia search: {e}"
44
 
45
+
46
+
47
+ # *** ADD TAVILY WEB SEARCH TOOL ***
48
  @tool
49
  def web_search(query: str) -> str:
50
+ """Search the web for a query using Tavily and return relevant snippets."""
 
51
  try:
52
+ tavily = TavilySearchResults(max_results=5) # Get up to 5 results
53
  results = tavily.invoke(query)
54
  if not results:
55
+ return "Web search found no relevant results."
56
+ # Format Tavily results
57
  formatted_results = "\n\n---\n\n".join([
58
  f'<SearchResult source="{r["source"]}">\nTitle: {r["title"]}\nContent: {r["content"]}\n</SearchResult>'
59
  for r in results
60
  ])
61
+ return formatted_results # Return string directly
62
  except Exception as e:
63
+ return f"An error occurred during web search: {e}"
64
+
 
 
65
  @tool
66
  def duckduckgo_search(query: str) -> str:
67
+ """Search the web for a query using DuckDuckGo and return relevant snippets."""
68
  try:
69
+ search_tool = DuckDuckGoSearchRun()
70
+ results = search_tool.invoke(query)
71
+ if not results or results.strip() == "":
72
  return "DuckDuckGo search found no relevant results."
73
+ return f"<SearchResult source=\"DuckDuckGo\">{results}</SearchResult>"
74
  except Exception as e:
75
  return f"An error occurred during DuckDuckGo search: {e}"
76
+
77
 
78
+ # load the system prompt from the file
79
+ # Ensure this file exists and has the content from Step 2
80
+ with open("system_prompt.txt", "r", encoding="utf-8") as f:
81
+ system_prompt = f.read()
82
+ sys_msg = SystemMessage(content=system_prompt)
 
 
83
 
84
  tools = [
85
  wiki_search,
 
86
  duckduckgo_search,
87
+ web_search, # *** ADDED TAVILY WEB SEARCH ***
88
  ]
89
 
90
+
91
  def build_graph():
92
  llm = ChatDeepSeek(
93
  model="deepseek-chat",
94
+ temperature=0, # Keep low for factual answers
95
  max_tokens=None,
96
  timeout=None,
97
  max_retries=2,
 
100
  )
101
  llm_with_tools = llm.bind_tools(tools)
102
 
103
+
104
  def assistant(state: MessagesState):
105
+ """Assistant node: invoke LLM with tools."""
106
+ print("---Calling Assistant---") # Added print for debugging
107
+
108
+ # 确保系统消息在消息列表的开头
109
+ messages = state["messages"]
110
+ if not any(isinstance(m, SystemMessage) for m in messages):
111
+ messages = [SystemMessage(content=system_prompt)] + messages
112
+
113
+ result = llm_with_tools.invoke(messages)
114
+ print(f"---Assistant Response: {result}") # Added print for debugging
115
+ return {"messages": [result]}
116
 
117
  builder = StateGraph(MessagesState)
118
  builder.add_node("assistant", assistant)
119
  builder.add_node("tools", ToolNode(tools))
120
 
121
  builder.add_edge(START, "assistant")
122
+
123
+ # The tools_condition checks if the last message from "assistant" is a tool call.
124
+ # If yes, it transitions to "tools".
125
+ # If no, the graph implicitly ends. This is how the agent stops.
126
+ builder.add_conditional_edges(
127
+ "assistant",
128
+ tools_condition,
129
+ # If tool_condition is false (no tool calls detected), the default is None,
130
+ # which implicitly ends the graph execution for that path.
131
+ # We don't need to explicitly define other paths here for a simple graph.
132
+ )
133
+
134
+ # After a tool is executed, the result is added to the state, and the control
135
+ # goes back to the assistant to process the tool result and decide the next step.
136
  builder.add_edge("tools", "assistant")
137
 
138
+ # You can optionally increase the recursion limit if your graph is expected to be complex,
139
+ # but it's better to fix the LLM's logic via the prompt first.
140
+ # return builder.compile(recursion_limit=50) # Example of increasing limit
141
+ return builder.compile()
142
+
143
+
144
+ if __name__ == "__main__":
145
+ # Example Usage (for local testing)
146
+ # To run this part, make sure you have DEEPSEEK_API_KEY and TAVILY_API_KEY
147
+ # set in your environment or a .env file loaded beforehand.
148
+ # If running locally, you'd typically use `load_dotenv()` here or in app.py
149
+
150
+ # Test questions covering different tool needs
151
+ questions_for_testing = [
152
+ "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?", # Web Search
153
+ "In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species seen?", # Requires video analysis (will likely fail with current tools)
154
+ ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI", # Text manipulation (no tool needed)
155
+ "What is 12345 * 6789?", # Calculator
156
+ "Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2023?", # Web Search/Wikipedia
157
+ "What country had the least number of athletes at the 1928 Summer Olympics?", # Web Search
158
+ "Review the chess position provided in the image. It is black's turn. Provide the correct next move from this position: [Describe the position or mention image input which is not supported]", # Requires image analysis (will likely fail)
159
+ # Add more questions from your evaluation set to test
160
+ ]
161
+
162
+
163
+ graph = build_graph()
164
+
165
+ # Optional: Draw graph
166
+ # try:
167
+ # png_data = graph.get_graph().draw_mermaid_png()
168
+ # with open("graph.png", "wb") as f:
169
+ # f.write(png_data)
170
+ # print("Graph visualization saved to graph.png")
171
+ # except Exception as e:
172
+ # print(f"Could not draw graph: {e}")
173
+
174
+
175
+ print("\n--- Running single question tests ---")
176
+ for i, question in enumerate(questions_for_testing):
177
+ print(f"\n--- Testing Question {i+1}: {question}")
178
+ try:
179
+ # LangGraph returns the final state after execution completes or hits recursion limit
180
+ final_state = graph.invoke({"messages": [HumanMessage(content=question)]})
181
+ print("\n--- Final State Messages ---")
182
+ for m in final_state["messages"]:
183
+ m.pretty_print()
184
+ print("-" * 30)
185
+ except Exception as e:
186
+ print(f"--- Error running graph for this question: {e}")
187
+ print("-" * 30)