Spaces:
Runtime error
Runtime error
Create Agent.py
Browse files
Agent.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import Tool
|
| 2 |
+
from langraph.graph import StateGraph, END
|
| 3 |
+
from langchain.agents import AgentExecutor, initialize_agent
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.memory import ConversationBufferMemory
|
| 6 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
+
|
| 8 |
+
def calculator_tool(input: str) -> str:
|
| 9 |
+
try:
|
| 10 |
+
return str(eval(input))
|
| 11 |
+
except Exception as e:
|
| 12 |
+
return f"Error: {e}"
|
| 13 |
+
|
| 14 |
+
calc_tool = Tool(name="Calculator", func=calculator_tool, description="Perform basic math.")
|
| 15 |
+
search_tool = DuckDuckGoSearchRun()
|
| 16 |
+
|
| 17 |
+
import wikipedia
|
| 18 |
+
def search_wikipedia(query: str) -> str:
|
| 19 |
+
try:
|
| 20 |
+
return wikipedia.summary(query, sentences=3)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Wikipedia Error: {str(e)}"
|
| 23 |
+
|
| 24 |
+
wikipedia_tool = Tool(
|
| 25 |
+
name="Wikipedia Search",
|
| 26 |
+
func=search_wikipedia,
|
| 27 |
+
description="Use this to search and summarize articles from Wikipedia."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
import arxiv
|
| 31 |
+
def search_arxiv(query: str) -> str:
|
| 32 |
+
try:
|
| 33 |
+
search = arxiv.Search(
|
| 34 |
+
query=query,
|
| 35 |
+
max_results=1,
|
| 36 |
+
sort_by=arxiv.SortCriterion.Relevance
|
| 37 |
+
)
|
| 38 |
+
for result in search.results():
|
| 39 |
+
return f"{result.title}\n\n{result.summary}\n\nPDF: {result.pdf_url}"
|
| 40 |
+
return "No relevant arXiv papers found."
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"arXiv Error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
arxiv_tool = Tool(
|
| 45 |
+
name="arXiv Search",
|
| 46 |
+
func=search_arxiv,
|
| 47 |
+
description="Search for scientific papers on arXiv.org"
|
| 48 |
+
)
|
| 49 |
+
class GaiaState:
|
| 50 |
+
input: str
|
| 51 |
+
intermediate_steps: list = []
|
| 52 |
+
output: str = ""
|
| 53 |
+
|
| 54 |
+
# Initialize tools and agent
|
| 55 |
+
llm = ChatOpenAI(temperature=0)
|
| 56 |
+
tools = [calc_tool, search_tool, arxiv_tool, wikipedia_tool] # Add more tools as needed
|
| 57 |
+
|
| 58 |
+
agent = initialize_agent(
|
| 59 |
+
tools=tools,
|
| 60 |
+
llm=llm,
|
| 61 |
+
agent_type="openai-functions",
|
| 62 |
+
verbose=True
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def run_agent(state: GaiaState) -> GaiaState:
|
| 66 |
+
result = agent.run(state.input)
|
| 67 |
+
state.output = result
|
| 68 |
+
state.intermediate_steps.append(result)
|
| 69 |
+
return state
|
| 70 |
+
|
| 71 |
+
graph = StateGraph(GaiaState)
|
| 72 |
+
|
| 73 |
+
graph.add_node("run_agent", run_agent)
|
| 74 |
+
graph.set_entry_point("run_agent")
|
| 75 |
+
graph.add_edge("run_agent", END)
|
| 76 |
+
|
| 77 |
+
# Compile the graph
|
| 78 |
+
app = graph.compile()
|