Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 2 |
+
from langgraph.prebuilt import tools_condition
|
| 3 |
+
from langgraph.prebuilt import ToolNode
|
| 4 |
+
|
| 5 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 7 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 8 |
+
|
| 9 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 10 |
+
from langchain_core.tools import tool
|
| 11 |
+
|
| 12 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
+
|
| 14 |
+
#load_dotenv()
|
| 15 |
+
|
| 16 |
+
@tool
|
| 17 |
+
def add(a: int, b: int) -> int:
|
| 18 |
+
""" Add a and b """
|
| 19 |
+
return a + b
|
| 20 |
+
@tool
|
| 21 |
+
def subtract(a: int,b: int) -> int:
|
| 22 |
+
""" Subract b from a """
|
| 23 |
+
return a - b
|
| 24 |
+
@tool
|
| 25 |
+
def multiply(a: int,b: int) -> int:
|
| 26 |
+
""" Multiply a and b """
|
| 27 |
+
return a * b
|
| 28 |
+
@tool
|
| 29 |
+
def divide(a: int,b: int) -> float:
|
| 30 |
+
""" Divide a by b """
|
| 31 |
+
if b == 0:
|
| 32 |
+
raise ValueError("Can't divide by 0.")
|
| 33 |
+
return a/b
|
| 34 |
+
|
| 35 |
+
@tool
|
| 36 |
+
def web_search(query: str) -> str:
|
| 37 |
+
""" Search for a query on web and return best 2 result."""
|
| 38 |
+
|
| 39 |
+
search_results = TavilySearchResults(max_results = 3).invoke(input=query)
|
| 40 |
+
|
| 41 |
+
formatted_search_results = "\n\n-----\n\n".join(
|
| 42 |
+
[
|
| 43 |
+
#f'<Result: source = "{result.metadata["source"]}", page = "{result.metadata.get("page","")}">\n{result.page_content}\n </Result>'
|
| 44 |
+
f'<Result: source = "{result.get("url", "")}", page = "{result.get("title","")}">\n{result.get("content","")}\n </Result>'
|
| 45 |
+
for result in search_results
|
| 46 |
+
]
|
| 47 |
+
)
|
| 48 |
+
return {"web_results" : formatted_search_results}
|
| 49 |
+
|
| 50 |
+
@tool
|
| 51 |
+
def wikipedia_search(query: str) -> str:
|
| 52 |
+
""" Search for a query on wikipedia and return best 2 result."""
|
| 53 |
+
|
| 54 |
+
loader = WikipediaLoader(query=query, load_max_docs=2)
|
| 55 |
+
search_results = loader.load() # Now, just call load() without arguments
|
| 56 |
+
|
| 57 |
+
formatted_search_results = "\n\n-----\n\n".join(
|
| 58 |
+
[
|
| 59 |
+
# Each 'result' here is a Document object.
|
| 60 |
+
# Access metadata through .metadata and content through .page_content
|
| 61 |
+
f'<Result: source = "{result.metadata.get("source", "")}", page = "{result.metadata.get("title","")}">\n{result.page_content}\n </Result>'
|
| 62 |
+
for result in search_results
|
| 63 |
+
]
|
| 64 |
+
)
|
| 65 |
+
return {"Wikipedia_results" : formatted_search_results}
|
| 66 |
+
|
| 67 |
+
@tool
|
| 68 |
+
def arxiv_search(query: str) -> str:
|
| 69 |
+
""" Search for a query on arxiv and return best 2 result."""
|
| 70 |
+
|
| 71 |
+
# Similar to WikipediaLoader, query and load_max_docs are passed during initialization
|
| 72 |
+
loader = ArxivLoader(query=query, load_max_docs=2)
|
| 73 |
+
search_results = loader.load() # Call load() without arguments
|
| 74 |
+
|
| 75 |
+
formatted_search_results = "\n\n-----\n\n".join(
|
| 76 |
+
[
|
| 77 |
+
f'<Result: source = "{result.metadata.get("source", "")}", page = "{result.metadata.get("title","")}">\n{result.page_content}\n </Result>'
|
| 78 |
+
for result in search_results
|
| 79 |
+
]
|
| 80 |
+
)
|
| 81 |
+
return {"arxiv_results" : formatted_search_results}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
system_prompt = """You are a general AI assistant. I will ask you a question. Using your tools to report your thoughts, and finish your answer with the following template:
|
| 86 |
+
FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 87 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 88 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 89 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 90 |
+
#Using your tools to 추가하니 툴컬링 하게됨
|
| 91 |
+
system_message = SystemMessage(content=system_prompt)
|
| 92 |
+
|
| 93 |
+
tools = [
|
| 94 |
+
add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def build_graph(provider: str = "google"):
|
| 99 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0 , api_key = "")
|
| 100 |
+
|
| 101 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 102 |
+
|
| 103 |
+
def assistant(state: MessagesState):
|
| 104 |
+
""" Use the tools to answer the query. you have add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search tools."""
|
| 105 |
+
return {"messages": state["messages"] + [llm_with_tools.invoke([system_message]+state["messages"])]}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
builder = StateGraph(MessagesState)
|
| 109 |
+
builder.add_node("assistant", assistant)
|
| 110 |
+
builder.add_node("tools", ToolNode(tools))
|
| 111 |
+
builder.add_edge(START, "assistant")
|
| 112 |
+
builder.add_conditional_edges(
|
| 113 |
+
"assistant",
|
| 114 |
+
tools_condition
|
| 115 |
+
)
|
| 116 |
+
builder.add_edge("tools", "assistant")
|
| 117 |
+
|
| 118 |
+
return builder.compile()
|
| 119 |
+
|
| 120 |
+
# test
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 123 |
+
# Build the graph
|
| 124 |
+
graph = build_graph(provider="google")
|
| 125 |
+
# Run the graph
|
| 126 |
+
messages = [HumanMessage(content=question)]
|
| 127 |
+
messages = graph.invoke({"messages": messages})
|
| 128 |
+
for m in messages["messages"]:
|
| 129 |
+
m.pretty_print()
|
| 130 |
+
|
| 131 |
+
|