Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,58 +1,48 @@
|
|
| 1 |
import os
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
from langchain_community.tools import DuckDuckGoSearchResults
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Tools
|
| 16 |
search_tool = DuckDuckGoSearchResults()
|
| 17 |
-
python_tool = PythonREPLTool()
|
| 18 |
|
| 19 |
tools = [
|
| 20 |
Tool(
|
| 21 |
-
name="
|
| 22 |
func=search_tool.run,
|
| 23 |
-
description="
|
| 24 |
-
),
|
| 25 |
-
Tool(
|
| 26 |
-
name="Python_REPL",
|
| 27 |
-
func=python_tool.run,
|
| 28 |
-
description="Useful for math, calculations, or running simple python code."
|
| 29 |
),
|
| 30 |
]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
system_prompt = """You are an expert AI assistant specialized in answering exam-style factual questions.
|
| 35 |
-
Follow these guidelines:
|
| 36 |
-
|
| 37 |
-
- Use the Search tool when external knowledge is needed (especially about recent events or niche topics).
|
| 38 |
-
- Use the Python_REPL tool for any math calculations, even if simple.
|
| 39 |
-
- Always attempt to provide a direct and concise answer without extra commentary.
|
| 40 |
-
- Do not apologize or state limitations.
|
| 41 |
-
- If a file is attached, explain how you would process it or the key steps to extract an answer.
|
| 42 |
-
- When dates are mentioned, be very precise and double-check calculations using the appropriate tools.
|
| 43 |
-
- If unsure, use the Search tool before responding.
|
| 44 |
-
|
| 45 |
-
Respond directly to the user’s question based solely on facts and without unnecessary elaboration.
|
| 46 |
-
Only provide what is explicitly asked for.
|
| 47 |
-
"""
|
| 48 |
|
| 49 |
-
# Agent
|
| 50 |
agent_executor = initialize_agent(
|
| 51 |
-
tools,
|
| 52 |
-
llm,
|
| 53 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 54 |
verbose=True,
|
|
|
|
| 55 |
handle_parsing_errors=True,
|
| 56 |
-
system_message=SystemMessagePromptTemplate.from_template(system_prompt),
|
| 57 |
)
|
| 58 |
|
|
|
|
| 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")
|
| 10 |
+
|
| 11 |
+
# LLM: Gemini 2.0 Flash
|
| 12 |
+
llm = ChatGoogleGenerativeAI(
|
| 13 |
+
model="gemini-2.0-flash",
|
| 14 |
+
google_api_key=google_api_key,
|
| 15 |
+
temperature=0,
|
| 16 |
+
max_output_tokens=2048,
|
| 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 |
+
# Tools: DuckDuckGo Web Search
|
| 26 |
search_tool = DuckDuckGoSearchResults()
|
|
|
|
| 27 |
|
| 28 |
tools = [
|
| 29 |
Tool(
|
| 30 |
+
name="WebSearch",
|
| 31 |
func=search_tool.run,
|
| 32 |
+
description="Use this to search the internet for up-to-date or unknown information."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
),
|
| 34 |
]
|
| 35 |
|
| 36 |
+
# Memory (optional, kann auch weggelassen werden, falls nicht gebraucht)
|
| 37 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Agent
|
| 40 |
agent_executor = initialize_agent(
|
| 41 |
+
tools=tools,
|
| 42 |
+
llm=llm,
|
| 43 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 44 |
verbose=True,
|
| 45 |
+
memory=memory,
|
| 46 |
handle_parsing_errors=True,
|
|
|
|
| 47 |
)
|
| 48 |
|