Spaces:
Runtime error
Runtime error
| import os | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain.agents import initialize_agent, Tool, AgentType | |
| from langchain_community.tools import DuckDuckGoSearchResults | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain_core.messages import SystemMessage | |
| # API Key automatisch aus Environment ziehen | |
| google_api_key = os.getenv("GOOGLE_API_KEY") | |
| # LLM: Gemini 2.0 Flash | |
| llm = ChatGoogleGenerativeAI( | |
| model="gemini-2.0-flash", | |
| google_api_key=google_api_key, | |
| temperature=0, | |
| max_output_tokens=2048, | |
| system_message=SystemMessage(content=( | |
| "You are a highly accurate AI assistant. " | |
| "You must answer precisely, concisely, and only if you are confident. " | |
| "Use the available tools like Web Search if needed. " | |
| "Always prefer exact information over assumptions." | |
| )) | |
| ) | |
| # Tools: DuckDuckGo Web Search | |
| search_tool = DuckDuckGoSearchResults() | |
| tools = [ | |
| Tool( | |
| name="WebSearch", | |
| func=search_tool.run, | |
| description="Use this to search the internet for up-to-date or unknown information." | |
| ), | |
| ] | |
| # Memory (optional, kann auch weggelassen werden, falls nicht gebraucht) | |
| memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) | |
| # Agent | |
| agent_executor = initialize_agent( | |
| tools=tools, | |
| llm=llm, | |
| agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
| verbose=True, | |
| memory=memory, | |
| handle_parsing_errors=True, | |
| ) | |