Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ 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 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langgraph.graph import StateGraph, END
|
| 14 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
@@ -23,18 +24,33 @@ class AgentState(TypedDict):
|
|
| 23 |
def create_langgraph_agent():
|
| 24 |
print("Initializing Advanced LangGraph Agent...")
|
| 25 |
|
| 26 |
-
# 1.
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# 2. Työkalut: Tavily, PythonREPL ja
|
| 30 |
-
tools = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
llm_with_tools = llm.bind_tools(tools)
|
| 32 |
print("LLM and tools initialized.")
|
| 33 |
|
| 34 |
# 3. Agentin solmu (kutsuu kielimallia)
|
| 35 |
def agent_node(state):
|
| 36 |
print("Calling agent node...")
|
| 37 |
-
return {"messages": [llm_with_tools.invoke(state["messages"])
|
| 38 |
|
| 39 |
# 4. Työkalusolmu
|
| 40 |
tool_node = ToolNode(tools)
|
|
@@ -49,7 +65,7 @@ def create_langgraph_agent():
|
|
| 49 |
graph.add_edge("tools", "agent")
|
| 50 |
|
| 51 |
# 6. Graafin kääntäminen ja turvarajan asettaminen
|
| 52 |
-
app = graph.compile() #
|
| 53 |
print("LangGraph agent compiled and ready.")
|
| 54 |
return app
|
| 55 |
|
|
|
|
| 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 |
+
from langchain.tools import WebBrowserTool, FileTool
|
| 13 |
from langchain_openai import ChatOpenAI
|
| 14 |
from langgraph.graph import StateGraph, END
|
| 15 |
from langgraph.prebuilt import ToolNode, tools_condition
|
|
|
|
| 24 |
def create_langgraph_agent():
|
| 25 |
print("Initializing Advanced LangGraph Agent...")
|
| 26 |
|
| 27 |
+
# 1. System prompt GAIA-tyyliin
|
| 28 |
+
system_prompt = """
|
| 29 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 30 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 31 |
+
|
| 32 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 33 |
+
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.
|
| 34 |
+
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.
|
| 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, YouTube, WebBrowser ja File
|
| 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 |
# 3. Agentin solmu (kutsuu kielimallia)
|
| 51 |
def agent_node(state):
|
| 52 |
print("Calling agent node...")
|
| 53 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 54 |
|
| 55 |
# 4. Työkalusolmu
|
| 56 |
tool_node = ToolNode(tools)
|
|
|
|
| 65 |
graph.add_edge("tools", "agent")
|
| 66 |
|
| 67 |
# 6. Graafin kääntäminen ja turvarajan asettaminen
|
| 68 |
+
app = graph.compile() # rekursion raja määritellään invoke-kutsussa
|
| 69 |
print("LangGraph agent compiled and ready.")
|
| 70 |
return app
|
| 71 |
|