Spaces:
Runtime error
Runtime error
File size: 1,463 Bytes
beb1eb8 3ed16ee beb1eb8 3ed16ee beb1eb8 3ed16ee beb1eb8 3ed16ee beb1eb8 3ed16ee beb1eb8 3ed16ee 39cd847 3ed16ee beb1eb8 3ed16ee beb1eb8 3ed16ee beb1eb8 39cd847 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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,
)
|