Spaces:
Build error
Build error
Update langgraph_agent.py
Browse files5 unmodified lines678910113 unmodified lines1516171819202122232425262728293031323334353637383940414243444546477 unmodified lines555657585960616263646566671 unmodified line69707172737410 unmodified lines85868788899019 unmodified lines1101111121131141157 unmodified lines1231241251261271288 unmodified lines1371381391401411425 unmodified linesfrom PIL import Image as PILImagefrom huggingface_hub import InferenceClientfrom langgraph.graph import START, StateGraph, MessagesStatefrom langgraph.prebuilt import tools_condition, ToolNodefrom langchain_openai import ChatOpenAI3 unmodified linesfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_core.tools import tool# Correct import for GoogleSearchAPIWrapperfrom langchain_google_community import GoogleSearchAPIWrapper@tooldef multiply(a: int, b: int) -> int:return a * b@tooldef add(a: int, b: int) -> int:return a + b@tooldef subtract(a: int, b: int) -> int:return a - b@tooldef divide(a: int, b: int) -> float:if b == 0:raise ValueError("Cannot divide by zero.")return a / b@tooldef modulus(a: int, b: int) -> int:return a % b@tooldef wiki_search(query: str) -> dict:try:docs = WikipediaLoader(query=query, load_max_docs=2, lang="en").load()if not docs:7 unmodified linesprint(f"Error in wiki_search tool: {e}")return {"wiki_results": f"Error occurred while searching Wikipedia for '{query}'. Details: {str(e)}"}# Instantiate GoogleSearchAPIWrappersearch = GoogleSearchAPIWrapper()@tooldef google_web_search(query: str) -> str:try:# Use the run method of the GoogleSearchAPIWrapper instancereturn search.run(query)except Exception as e:print(f"Error in google_web_search tool: {e}")1 unmodified line@tooldef arvix_search(query: str) -> dict:docs = ArxivLoader(query=query, load_max_docs=3).load()formatted = "\n\n---\n\n".join(f'<Document source="{d.metadata["source"]}"/>\n{d.page_content[:1000]}'10 unmodified lines@tooldef read_file_content(file_path: str) -> Dict[str, str]:try:_, file_extension = os.path.splitext(file_path)file_extension = file_extension.lower()19 unmodified lines@tooldef python_interpreter(code: str) -> Dict[str, str]:old_stdout = io.StringIO()with contextlib.redirect_stdout(old_stdout):try:7 unmodified lines@tooldef describe_image(image_path: str) -> Dict[str, str]:if not HF_INFERENCE_CLIENT:return {"error": "Hugging Face API token not configured for image description. Cannot use this tool."}try:8 unmodified lines@tooldef transcribe_audio(audio_path: str) -> Dict[str, str]:if not HF_INFERENCE_CLIENT:return {"error": "Hugging Face API token not configured for audio transcription. Cannot use this tool."}try:5 unmodified lines67891011123 unmodified lines16171819202122232425262728293031323334353637383940414243444546474849505152537 unmodified lines6162636465666768697071721 unmodified line7475767778798010 unmodified lines9192939495969719 unmodified lines1171181191201211221237 unmodified lines1311321331341351361378 unmodified lines1461471481491501511525 unmodified linesfrom PIL import Image as PILImagefrom huggingface_hub import InferenceClientfrom langgraph.graph import START, StateGraph, MessagesStatefrom langgraph.prebuilt import tools_condition, ToolNodefrom langchain_openai import ChatOpenAI3 unmodified linesfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_core.tools import toolfrom langchain_google_community import GoogleSearchAPIWrapper@tooldef multiply(a: int, b: int) -> int:"""Multiply two integers."""return a * b@tooldef add(a: int, b: int) -> int:"""Add two integers."""return a + b@tooldef subtract(a: int, b: int) -> int:"""Subtract the second integer from the first."""return a - b@tooldef divide(a: int, b: int) -> float:"""Divide first integer by second; error if divisor is zero."""if b == 0:raise ValueError("Cannot divide by zero.")return a / b@tooldef modulus(a: int, b: int) -> int:"""Return the remainder of dividing first integer by second."""return a % b@tooldef wiki_search(query: str) -> dict:"""Search Wikipedia for a query and return up to 2 documents."""try:docs = WikipediaLoader(query=query, load_max_docs=2, lang="en").load()if not docs:7 unmodified linesprint(f"Error in wiki_search tool: {e}")return {"wiki_results": f"Error occurred while searching Wikipedia for '{query}'. Details: {str(e)}"}search = GoogleSearchAPIWrapper()@tooldef google_web_search(query: str) -> str:"""Perform a web search (via Google Custom Search) and return results."""try:return search.run(query)except Exception as e:print(f"Error in google_web_search tool: {e}")1 unmodified line@tooldef arvix_search(query: str) -> dict:"""Search arXiv for a query and return up to 3 paper excerpts."""docs = ArxivLoader(query=query, load_max_docs=3).load()formatted = "\n\n---\n\n".join(f'<Document source="{d.metadata["source"]}"/>\n{d.page_content[:1000]}'10 unmodified lines@tooldef read_file_content(file_path: str) -> Dict[str, str]:"""Reads the content of a file and returns its primary information. For text/code/excel, returns content. For media, returns a prompt to use specific tools."""try:_, file_extension = os.path.splitext(file_path)file_extension = file_extension.lower()19 unmodified lines@tooldef python_interpreter(code: str) -> Dict[str, str]:"""Executes Python code and returns its standard output. If there's an error during execution, it returns the error message."""old_stdout = io.StringIO()with contextlib.redirect_stdout(old_stdout):try:7 unmodified lines@tooldef describe_image(image_path: str) -> Dict[str, str]:"""Generates a textual description for an image file (JPEG, JPG, PNG) using an image-to-text model from the Hugging Face Inference API. Requires HF_API_TOKEN environment variable to be set."""if not HF_INFERENCE_CLIENT:return {"error": "Hugging Face API token not configured for image description. Cannot use this tool."}try:8 unmodified lines@tooldef transcribe_audio(audio_path: str) -> Dict[str, str]:"""Transcribes an audio file (e.g., MP3) to text using an automatic speech recognition model from the Hugging Face Inference API. Requires HF_API_TOKEN environment variable to be set."""if not HF_INFERENCE_CLIENT:return {"error": "Hugging Face API token not configured for audio transcription. Cannot use this tool."}try:
langgraph_agent.py+13-3