Spaces:
Runtime error
Runtime error
Update langgraph_agent.py
Browse files- langgraph_agent.py +42 -2
langgraph_agent.py
CHANGED
|
@@ -5,11 +5,12 @@ from langchain_core.messages import AnyMessage, HumanMessage
|
|
| 5 |
from langchain_core.runnables import RunnableConfig
|
| 6 |
from langgraph.prebuilt import create_react_agent
|
| 7 |
from langgraph.prebuilt.chat_agent_executor import AgentState
|
| 8 |
-
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
from langchain_experimental.utilities import PythonREPL
|
| 11 |
from langchain_community.tools import WikipediaQueryRun
|
| 12 |
from langchain_community.utilities import WikipediaAPIWrapper
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
@@ -28,6 +29,45 @@ duckduckgo = DuckDuckGoSearchRun()
|
|
| 28 |
python_repl = PythonREPL()
|
| 29 |
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
|
| 32 |
file_path = config["configurable"].get("file_path", "")
|
| 33 |
system_msg = (
|
|
@@ -44,6 +84,6 @@ def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
|
|
| 44 |
|
| 45 |
react_agent = create_react_agent(
|
| 46 |
model=chat,
|
| 47 |
-
tools=[duckduckgo, wikipedia],
|
| 48 |
prompt=prompt,
|
| 49 |
)
|
|
|
|
| 5 |
from langchain_core.runnables import RunnableConfig
|
| 6 |
from langgraph.prebuilt import create_react_agent
|
| 7 |
from langgraph.prebuilt.chat_agent_executor import AgentState
|
| 8 |
+
from smolagents import VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
from langchain_experimental.utilities import PythonREPL
|
| 11 |
from langchain_community.tools import WikipediaQueryRun
|
| 12 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 13 |
+
from langchain_core.tools import tool
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
|
|
| 29 |
python_repl = PythonREPL()
|
| 30 |
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 31 |
|
| 32 |
+
# Instantiate smolagents tools
|
| 33 |
+
visit_webpage_tool = VisitWebpageTool()
|
| 34 |
+
python_interpreter_tool = PythonInterpreterTool()
|
| 35 |
+
speech_to_text_tool = SpeechToTextTool()
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def visit_webpage(url: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Visit a webpage and return its content as a string.
|
| 41 |
+
Args:
|
| 42 |
+
url (str): The URL of the webpage to visit.
|
| 43 |
+
Returns:
|
| 44 |
+
str: The content of the webpage.
|
| 45 |
+
"""
|
| 46 |
+
return visit_webpage_tool(url=url)
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def python_interpreter(code: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Execute Python code and return the output as a string.
|
| 52 |
+
Args:
|
| 53 |
+
code (str): The Python code to execute.
|
| 54 |
+
Returns:
|
| 55 |
+
str: The output of the executed code.
|
| 56 |
+
"""
|
| 57 |
+
return python_interpreter_tool(code=code)
|
| 58 |
+
|
| 59 |
+
@tool
|
| 60 |
+
def speech_to_text(audio_path: str) -> str:
|
| 61 |
+
"""
|
| 62 |
+
Convert speech in an audio file to text.
|
| 63 |
+
Args:
|
| 64 |
+
audio_path (str): The path to the audio file.
|
| 65 |
+
Returns:
|
| 66 |
+
str: The transcribed text from the audio.
|
| 67 |
+
"""
|
| 68 |
+
return speech_to_text_tool(audio_path=audio_path)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
|
| 72 |
file_path = config["configurable"].get("file_path", "")
|
| 73 |
system_msg = (
|
|
|
|
| 84 |
|
| 85 |
react_agent = create_react_agent(
|
| 86 |
model=chat,
|
| 87 |
+
tools=[duckduckgo, wikipedia, visit_webpage, python_interpreter, speech_to_text],
|
| 88 |
prompt=prompt,
|
| 89 |
)
|