Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
from langchain.agents import initialize_agent, Tool, AgentType
|
| 4 |
-
from langchain_community.tools import DuckDuckGoSearchResults
|
|
|
|
|
|
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
from langchain_core.messages import SystemMessage
|
|
|
|
| 7 |
|
| 8 |
# API Key automatisch aus Environment ziehen
|
| 9 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
|
@@ -17,23 +20,64 @@ llm = ChatGoogleGenerativeAI(
|
|
| 17 |
system_message=SystemMessage(content=(
|
| 18 |
"You are a highly accurate AI assistant. "
|
| 19 |
"You must answer precisely, concisely, and only if you are confident. "
|
| 20 |
-
"Use the available tools like Web Search if needed. "
|
| 21 |
"Always prefer exact information over assumptions."
|
| 22 |
))
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
tools = [
|
| 29 |
Tool(
|
| 30 |
name="WebSearch",
|
| 31 |
-
func=
|
| 32 |
description="Use this to search the internet for up-to-date or unknown information."
|
| 33 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
]
|
| 35 |
|
| 36 |
-
# Memory (optional,
|
| 37 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 38 |
|
| 39 |
# Agent
|
|
@@ -46,3 +90,4 @@ agent_executor = initialize_agent(
|
|
| 46 |
handle_parsing_errors=True,
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
from langchain.agents import initialize_agent, Tool, AgentType
|
| 4 |
+
from langchain_community.tools import DuckDuckGoSearchResults, WikipediaQueryRun
|
| 5 |
+
from langchain_experimental.tools import PythonREPLTool
|
| 6 |
+
from langchain.tools import tool
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
from langchain_core.messages import SystemMessage
|
| 9 |
+
import pandas as pd
|
| 10 |
|
| 11 |
# API Key automatisch aus Environment ziehen
|
| 12 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 20 |
system_message=SystemMessage(content=(
|
| 21 |
"You are a highly accurate AI assistant. "
|
| 22 |
"You must answer precisely, concisely, and only if you are confident. "
|
| 23 |
+
"Use the available tools like Web Search, Wikipedia, Python REPL, or Table Analysis if needed. "
|
| 24 |
"Always prefer exact information over assumptions."
|
| 25 |
))
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Tool 1: Web Search
|
| 29 |
+
web_search = DuckDuckGoSearchResults()
|
| 30 |
|
| 31 |
+
# Tool 2: Wikipedia Search
|
| 32 |
+
wiki_search = WikipediaQueryRun()
|
| 33 |
+
|
| 34 |
+
# Tool 3: Python REPL
|
| 35 |
+
python_repl = PythonREPLTool()
|
| 36 |
+
|
| 37 |
+
# Tool 4: Analyze CSV files (sehr einfaches Tool)
|
| 38 |
+
@tool
|
| 39 |
+
def analyze_csv(content: str) -> str:
|
| 40 |
+
"""Analyzes CSV data and provides basic statistics and insights."""
|
| 41 |
+
try:
|
| 42 |
+
from io import StringIO
|
| 43 |
+
df = pd.read_csv(StringIO(content))
|
| 44 |
+
return str(df.describe())
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Failed to analyze CSV: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# Tool 5: Analyze Excel files
|
| 49 |
+
@tool
|
| 50 |
+
def analyze_excel(content: bytes) -> str:
|
| 51 |
+
"""Analyzes Excel data and provides basic statistics and insights."""
|
| 52 |
+
try:
|
| 53 |
+
from io import BytesIO
|
| 54 |
+
df = pd.read_excel(BytesIO(content))
|
| 55 |
+
return str(df.describe())
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Failed to analyze Excel: {str(e)}"
|
| 58 |
+
|
| 59 |
+
# Alle Tools zusammen
|
| 60 |
tools = [
|
| 61 |
Tool(
|
| 62 |
name="WebSearch",
|
| 63 |
+
func=web_search.run,
|
| 64 |
description="Use this to search the internet for up-to-date or unknown information."
|
| 65 |
),
|
| 66 |
+
Tool(
|
| 67 |
+
name="WikipediaSearch",
|
| 68 |
+
func=wiki_search.run,
|
| 69 |
+
description="Use this to search Wikipedia articles when a direct lookup of factual information is needed."
|
| 70 |
+
),
|
| 71 |
+
Tool(
|
| 72 |
+
name="Python_REPL",
|
| 73 |
+
func=python_repl.run,
|
| 74 |
+
description="Use this for math problems, small code executions, or calculations."
|
| 75 |
+
),
|
| 76 |
+
analyze_csv,
|
| 77 |
+
analyze_excel,
|
| 78 |
]
|
| 79 |
|
| 80 |
+
# Memory (optional, für Chat-History)
|
| 81 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 82 |
|
| 83 |
# Agent
|
|
|
|
| 90 |
handle_parsing_errors=True,
|
| 91 |
)
|
| 92 |
|
| 93 |
+
|