File size: 938 Bytes
648a915
6b8cb8c
 
 
648a915
 
6b8cb8c
4083851
61857d6
6b8cb8c
 
 
648a915
61857d6
648a915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b8cb8c
 
 
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
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}"