agent / agent.py
sachin-philip's picture
added correct repo
4083851
raw
history blame contribute delete
938 Bytes
import os
from langchain.agents import initialize_agent, Tool
from langchain_community.utilities import SerpAPIWrapper
from langchain_huggingface import HuggingFaceEndpoint
llm = HuggingFaceEndpoint(
repo_id="moonshotai/Kimi-K2-Base",
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
task="text-generation",
temperature=0.7
)
# Optional: Use web search tool
search = SerpAPIWrapper(serpapi_api_key=os.environ["SERPAPI_API_KEY"])
tools = [
Tool(
name="Search",
func=search.run,
description="Search engine for real-time information.",
)
]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
def run_agent(prompt: str) -> str:
try:
return agent.run(prompt)
except Exception as e:
import traceback
error_details = traceback.format_exc()
return f"[Agent Error] {str(e)}\n\nDetails: {error_details}"