Spaces:
Sleeping
Sleeping
Delete agent.py
#3
by
yokewang
- opened
agent.py
DELETED
|
@@ -1,228 +0,0 @@
|
|
| 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 |
-
@tool
|
| 79 |
-
def arithmetic(expression: str) -> str:
|
| 80 |
-
"""执行数学计算并返回结果。支持基本的算术运算如加减乘除、幂运算等。"""
|
| 81 |
-
try:
|
| 82 |
-
# 使用Python的eval函数安全地计算表达式
|
| 83 |
-
# 限制只能使用基本算术运算,不允许导入模块或执行其他危险操作
|
| 84 |
-
allowed_names = {"__builtins__": {}}
|
| 85 |
-
allowed_symbols = {}
|
| 86 |
-
result = eval(expression, allowed_names, allowed_symbols)
|
| 87 |
-
return str(result)
|
| 88 |
-
except Exception as e:
|
| 89 |
-
return f"计算表达式时出错: {e}"
|
| 90 |
-
|
| 91 |
-
# load the system prompt from the file
|
| 92 |
-
# Ensure this file exists and has the content from Step 2
|
| 93 |
-
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 94 |
-
system_prompt = f.read()
|
| 95 |
-
sys_msg = SystemMessage(content=system_prompt)
|
| 96 |
-
|
| 97 |
-
tools = [
|
| 98 |
-
wiki_search,
|
| 99 |
-
duckduckgo_search,
|
| 100 |
-
web_search,
|
| 101 |
-
arithmetic,
|
| 102 |
-
]
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def build_graph():
|
| 106 |
-
llm = ChatDeepSeek(
|
| 107 |
-
model="deepseek-chat",
|
| 108 |
-
temperature=0, # Keep low for factual answers
|
| 109 |
-
max_tokens=None,
|
| 110 |
-
timeout=None,
|
| 111 |
-
max_retries=2,
|
| 112 |
-
api_key=DEEPSEEK_API_KEY,
|
| 113 |
-
base_url="https://api.deepseek.com"
|
| 114 |
-
)
|
| 115 |
-
llm_with_tools = llm.bind_tools(tools)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
def assistant(state: MessagesState):
|
| 119 |
-
"""Assistant node: invoke LLM with tools."""
|
| 120 |
-
print("---Calling Assistant---") # Added print for debugging
|
| 121 |
-
|
| 122 |
-
# 确保系统消息在消息列表的开头
|
| 123 |
-
messages = state["messages"]
|
| 124 |
-
if not any(isinstance(m, SystemMessage) for m in messages):
|
| 125 |
-
messages = [SystemMessage(content=system_prompt)] + messages
|
| 126 |
-
|
| 127 |
-
result = llm_with_tools.invoke(messages)
|
| 128 |
-
# print(f"---Assistant Response: {result}") # Added print for debugging
|
| 129 |
-
return {"messages": [result]}
|
| 130 |
-
|
| 131 |
-
builder = StateGraph(MessagesState)
|
| 132 |
-
builder.add_node("assistant", assistant)
|
| 133 |
-
builder.add_node("tools", ToolNode(tools))
|
| 134 |
-
|
| 135 |
-
builder.add_edge(START, "assistant")
|
| 136 |
-
|
| 137 |
-
# The tools_condition checks if the last message from "assistant" is a tool call.
|
| 138 |
-
# If yes, it transitions to "tools".
|
| 139 |
-
# If no, the graph implicitly ends. This is how the agent stops.
|
| 140 |
-
builder.add_conditional_edges(
|
| 141 |
-
"assistant",
|
| 142 |
-
tools_condition,
|
| 143 |
-
# If tool_condition is false (no tool calls detected), the default is None,
|
| 144 |
-
# which implicitly ends the graph execution for that path.
|
| 145 |
-
# We don't need to explicitly define other paths here for a simple graph.
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
# After a tool is executed, the result is added to the state, and the control
|
| 149 |
-
# goes back to the assistant to process the tool result and decide the next step.
|
| 150 |
-
builder.add_edge("tools", "assistant")
|
| 151 |
-
|
| 152 |
-
# You can optionally increase the recursion limit if your graph is expected to be complex,
|
| 153 |
-
# but it's better to fix the LLM's logic via the prompt first.
|
| 154 |
-
# return builder.compile(recursion_limit=50) # Example of increasing limit
|
| 155 |
-
return builder.compile()
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
if __name__ == "__main__":
|
| 159 |
-
# Example Usage (for local testing)
|
| 160 |
-
# To run this part, make sure you have DEEPSEEK_API_KEY and TAVILY_API_KEY
|
| 161 |
-
# set in your environment or a .env file loaded beforehand.
|
| 162 |
-
# If running locally, you'd typically use `load_dotenv()` here or in app.py
|
| 163 |
-
|
| 164 |
-
# Test questions covering different tool needs
|
| 165 |
-
questions_for_testing = [
|
| 166 |
-
"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?", # Web Search
|
| 167 |
-
"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)
|
| 168 |
-
".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI", # Text manipulation (no tool needed)
|
| 169 |
-
"What is 12345 * 6789?", # Calculator
|
| 170 |
-
"Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2023?", # Web Search/Wikipedia
|
| 171 |
-
"What country had the least number of athletes at the 1928 Summer Olympics?", # Web Search
|
| 172 |
-
"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)
|
| 173 |
-
# Add more questions from your evaluation set to test
|
| 174 |
-
]
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
graph = build_graph()
|
| 178 |
-
|
| 179 |
-
# Optional: Draw graph
|
| 180 |
-
# try:
|
| 181 |
-
# png_data = graph.get_graph().draw_mermaid_png()
|
| 182 |
-
# with open("graph.png", "wb") as f:
|
| 183 |
-
# f.write(png_data)
|
| 184 |
-
# print("Graph visualization saved to graph.png")
|
| 185 |
-
# except Exception as e:
|
| 186 |
-
# print(f"Could not draw graph: {e}")
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
print("\n--- Running single question tests ---")
|
| 190 |
-
for i, question in enumerate(questions_for_testing):
|
| 191 |
-
print(f"\n--- Testing Question {i+1}: {question}")
|
| 192 |
-
try:
|
| 193 |
-
# LangGraph returns the final state after execution completes or hits recursion limit
|
| 194 |
-
final_state = graph.invoke({"messages": [SystemMessage(content=system_prompt), HumanMessage(content=question)]})
|
| 195 |
-
|
| 196 |
-
# 在这里添加您的处理答案代码
|
| 197 |
-
def process_answer(answer):
|
| 198 |
-
"""处理最终答案,去除可能的解释性文本"""
|
| 199 |
-
# 如果答案包含"FINAL ANSWER:",提取实际答案部分
|
| 200 |
-
if "FINAL ANSWER:" in answer.upper():
|
| 201 |
-
import re
|
| 202 |
-
match = re.search(r'(?i)FINAL ANSWER:\s*(.*)', answer)
|
| 203 |
-
if match:
|
| 204 |
-
return match.group(1).strip()
|
| 205 |
-
|
| 206 |
-
# 如果答案较长且包含多个句子,尝试提取最后一句作为答案
|
| 207 |
-
if len(answer.split()) > 15 and "." in answer:
|
| 208 |
-
sentences = answer.split(".")
|
| 209 |
-
# 过滤掉空字符串
|
| 210 |
-
sentences = [s.strip() for s in sentences if s.strip()]
|
| 211 |
-
if sentences:
|
| 212 |
-
return sentences[-1].strip()
|
| 213 |
-
|
| 214 |
-
return answer.strip()
|
| 215 |
-
|
| 216 |
-
# 在提交答案前应用处理
|
| 217 |
-
final_answer = final_state["messages"][-1].content
|
| 218 |
-
processed_answer = process_answer(final_answer)
|
| 219 |
-
# 打印处理后的答案
|
| 220 |
-
print(f"\n--- Processed Answer: {processed_answer}")
|
| 221 |
-
|
| 222 |
-
print("\n--- Final State Messages ---")
|
| 223 |
-
for m in final_state["messages"]:
|
| 224 |
-
m.pretty_print()
|
| 225 |
-
print("-" * 30)
|
| 226 |
-
except Exception as e:
|
| 227 |
-
print(f"--- Error running graph for this question: {e}")
|
| 228 |
-
print("-" * 30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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