Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,10 @@ from langchain.agents import AgentExecutor
|
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
from langchain_community.tools.youtube.search import YouTubeSearchTool
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
from langchain_openai import ChatOpenAI
|
| 14 |
from langgraph.graph import StateGraph, END
|
| 15 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
@@ -35,28 +38,32 @@ If you are asked for a string, don't use articles, neither abbreviations (e.g. f
|
|
| 35 |
If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 36 |
"""
|
| 37 |
llm = ChatOpenAI(model="gpt-4o", temperature=0, system_message=system_prompt)
|
| 38 |
-
|
| 39 |
-
# 2. Työkalut: Tavily, PythonREPL
|
| 40 |
tools = [
|
| 41 |
TavilySearchResults(max_results=3),
|
| 42 |
PythonREPLTool(),
|
| 43 |
YouTubeSearchTool(),
|
| 44 |
-
WebBrowserTool(), # selainpohjaiset haut ja interaktio
|
| 45 |
-
FileTool() # tiedostojen luku- ja kirjoitustyökalut
|
| 46 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
llm_with_tools = llm.bind_tools(tools)
|
| 48 |
print("LLM and tools initialized.")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
def agent_node(state):
|
| 52 |
print("Calling agent node...")
|
| 53 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 54 |
|
| 55 |
-
#
|
| 56 |
tool_node = ToolNode(tools)
|
| 57 |
print("Graph nodes defined.")
|
| 58 |
|
| 59 |
-
#
|
| 60 |
graph = StateGraph(AgentState)
|
| 61 |
graph.add_node("agent", agent_node)
|
| 62 |
graph.add_node("tools", tool_node)
|
|
@@ -64,13 +71,13 @@ If you are asked for a comma separated list, apply the above rules depending on
|
|
| 64 |
graph.add_conditional_edges("agent", tools_condition)
|
| 65 |
graph.add_edge("tools", "agent")
|
| 66 |
|
| 67 |
-
#
|
| 68 |
app = graph.compile() # rekursion raja määritellään invoke-kutsussa
|
| 69 |
print("LangGraph agent compiled and ready.")
|
| 70 |
return app
|
| 71 |
|
| 72 |
# Agentin suoritusfunktio
|
| 73 |
-
def run_agent(agent_executor, question: str) -> str:
|
| 74 |
print(f"Agent received question: {question}")
|
| 75 |
final_answer = ""
|
| 76 |
try:
|
|
|
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
from langchain_community.tools.youtube.search import YouTubeSearchTool
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
+
# Playwright-selain-työkalut
|
| 13 |
+
from langchain_community.agent_toolkits.playwright.toolkit import PlayWrightBrowserToolkit
|
| 14 |
+
# Tiedostohallinnan työkalut
|
| 15 |
+
from langchain_community.agent_toolkits.file_management.toolkit import FileManagementToolkit
|
| 16 |
from langchain_openai import ChatOpenAI
|
| 17 |
from langgraph.graph import StateGraph, END
|
| 18 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
|
|
| 38 |
If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 39 |
"""
|
| 40 |
llm = ChatOpenAI(model="gpt-4o", temperature=0, system_message=system_prompt)
|
| 41 |
+
|
| 42 |
+
# 2. Työkalut: Tavily, PythonREPL ja YouTube
|
| 43 |
tools = [
|
| 44 |
TavilySearchResults(max_results=3),
|
| 45 |
PythonREPLTool(),
|
| 46 |
YouTubeSearchTool(),
|
|
|
|
|
|
|
| 47 |
]
|
| 48 |
+
# 3. Lisätään selain- ja tiedosto-työkalut PlayWright- ja FileManagement-toolkiteista
|
| 49 |
+
browser_toolkit = PlayWrightBrowserToolkit()
|
| 50 |
+
tools.extend(browser_toolkit.get_tools())
|
| 51 |
+
file_toolkit = FileManagementToolkit(root_dir=".")
|
| 52 |
+
tools.extend(file_toolkit.get_tools())
|
| 53 |
+
|
| 54 |
llm_with_tools = llm.bind_tools(tools)
|
| 55 |
print("LLM and tools initialized.")
|
| 56 |
|
| 57 |
+
# 4. Agentin solmu (kutsuu kielimallia)
|
| 58 |
def agent_node(state):
|
| 59 |
print("Calling agent node...")
|
| 60 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 61 |
|
| 62 |
+
# 5. Työkalusolmu
|
| 63 |
tool_node = ToolNode(tools)
|
| 64 |
print("Graph nodes defined.")
|
| 65 |
|
| 66 |
+
# 6. Graafin määritys
|
| 67 |
graph = StateGraph(AgentState)
|
| 68 |
graph.add_node("agent", agent_node)
|
| 69 |
graph.add_node("tools", tool_node)
|
|
|
|
| 71 |
graph.add_conditional_edges("agent", tools_condition)
|
| 72 |
graph.add_edge("tools", "agent")
|
| 73 |
|
| 74 |
+
# 7. Graafin kääntäminen
|
| 75 |
app = graph.compile() # rekursion raja määritellään invoke-kutsussa
|
| 76 |
print("LangGraph agent compiled and ready.")
|
| 77 |
return app
|
| 78 |
|
| 79 |
# Agentin suoritusfunktio
|
| 80 |
+
def run_agent(agent_executor, question: str) -> str:
|
| 81 |
print(f"Agent received question: {question}")
|
| 82 |
final_answer = ""
|
| 83 |
try:
|