Spaces:
Runtime error
Runtime error
Update langgraph_agent.py
Browse files- langgraph_agent.py +14 -68
langgraph_agent.py
CHANGED
|
@@ -1,19 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
-
from langchain_core.messages import AnyMessage
|
| 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
|
| 10 |
-
|
| 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 |
|
|
|
|
| 17 |
BASE_URL = os.getenv("BASE_URL")
|
| 18 |
OKTA_ACCESS_TOKEN = os.getenv("OKTA_ACCESS_TOKEN")
|
| 19 |
SUBSCRIPTION_KEY = os.getenv("SUBSCRIPTION_KEY")
|
|
@@ -25,80 +23,28 @@ chat = ChatOpenAI(
|
|
| 25 |
default_headers={"Subscription-Key": SUBSCRIPTION_KEY},
|
| 26 |
)
|
| 27 |
|
| 28 |
-
duckduckgo = DuckDuckGoSearchRun()
|
| 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 |
-
@tool
|
| 71 |
-
def read_file_contents(file_path: str) -> str:
|
| 72 |
-
"""
|
| 73 |
-
Read and return the contents of a file as a string.
|
| 74 |
-
Args:
|
| 75 |
-
file_path (str): The path to the file to read.
|
| 76 |
-
Returns:
|
| 77 |
-
str: The contents of the file, or an error message if the file cannot be read.
|
| 78 |
-
"""
|
| 79 |
-
try:
|
| 80 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 81 |
-
return f.read()
|
| 82 |
-
except Exception as e:
|
| 83 |
-
return f"Error reading file: {e}"
|
| 84 |
-
|
| 85 |
|
| 86 |
def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
|
| 87 |
file_path = config["configurable"].get("file_path", "")
|
| 88 |
system_msg = (
|
| 89 |
f"You are an AI assistant evaluated on the GAIA benchmark. "
|
| 90 |
-
|
| 91 |
f"Use the available tools to find answers when needed. "
|
| 92 |
-
|
| 93 |
f"Provide only the direct answer, with no extra explanation, formatting, or restating the question. "
|
| 94 |
f"For example, if asked 'What is the capital of France?', answer 'Paris'. "
|
|
|
|
| 95 |
)
|
| 96 |
if file_path:
|
| 97 |
-
system_msg +=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
return [{"role": "system", "content": system_msg}] + state["messages"]
|
| 99 |
|
| 100 |
react_agent = create_react_agent(
|
| 101 |
model=chat,
|
| 102 |
-
tools=[
|
| 103 |
prompt=prompt,
|
| 104 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain_core.messages import AnyMessage
|
| 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 tools import visit_webpage, transcript_tool_langchain, read_file_contents, google_serper_search, python_repl_tool, youtube_transcript_tool, wikipedia
|
| 10 |
+
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
|
| 15 |
BASE_URL = os.getenv("BASE_URL")
|
| 16 |
OKTA_ACCESS_TOKEN = os.getenv("OKTA_ACCESS_TOKEN")
|
| 17 |
SUBSCRIPTION_KEY = os.getenv("SUBSCRIPTION_KEY")
|
|
|
|
| 23 |
default_headers={"Subscription-Key": SUBSCRIPTION_KEY},
|
| 24 |
)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
|
| 28 |
file_path = config["configurable"].get("file_path", "")
|
| 29 |
system_msg = (
|
| 30 |
f"You are an AI assistant evaluated on the GAIA benchmark. "
|
| 31 |
+
f"Answer questions using only verified information. "
|
| 32 |
f"Use the available tools to find answers when needed. "
|
| 33 |
+
f"If you do not have enough information, reply: 'I do not have enough information to answer the question.' "
|
| 34 |
f"Provide only the direct answer, with no extra explanation, formatting, or restating the question. "
|
| 35 |
f"For example, if asked 'What is the capital of France?', answer 'Paris'. "
|
| 36 |
+
f"When returning comma separated lists as answer, provide a space after each comma (e.g., 'item1, item2, item3'). "
|
| 37 |
)
|
| 38 |
if file_path:
|
| 39 |
+
system_msg += (
|
| 40 |
+
f" If the question refers to a file, use the file path provided in your context to access the file, "
|
| 41 |
+
f"and do not use the file name mentioned in the question. Use only the file path variable for file operations."
|
| 42 |
+
f" The file path is: {file_path}."
|
| 43 |
+
)
|
| 44 |
return [{"role": "system", "content": system_msg}] + state["messages"]
|
| 45 |
|
| 46 |
react_agent = create_react_agent(
|
| 47 |
model=chat,
|
| 48 |
+
tools=[wikipedia, visit_webpage, transcript_tool_langchain, read_file_contents, google_serper_search, python_repl_tool, youtube_transcript_tool],
|
| 49 |
prompt=prompt,
|
| 50 |
)
|