Spaces:
Sleeping
Sleeping
Update app.py
Browse files69 unmodified lines70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212369 unmodified linesmax_steps=5)# ============ 2. LLAMAINDEX SETUP (Fixed) ============# FIX 1: Define real functions with docstrings so the Agent knows what they dodef get_tokyo_time() -> str:"""Returns the current time in Tokyo, Japan.Use this tool when the user asks for the time in Tokyo."""return f"Tokyo: {datetime.datetime.now(pytz.timezone('Asia/Tokyo'))}"def multiply(a: float, b: float) -> float:"""Multiplies two numbers a and b.Use this tool for calculations involving multiplication."""return a * b# FIX 2: Change task to 'text-generation'llama_llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-7B-Instruct",token=HF_TOKEN,task="text-generation")llama_agent = ReActAgent.from_tools([FunctionTool.from_defaults(fn=get_tokyo_time),FunctionTool.from_defaults(fn=multiply)],llm=llama_llm,verbose=True)# ============ GRADIO UI ============def chat_smol(message, history):try:return str(smol_agent.run(message))except Exception as e:return f"SmolAgent Error: {str(e)}"def chat_llama(message, history):try:# LlamaIndex keeps its own history, so we just pass the messageresponse = llama_agent.chat(message)return str(response)except Exception as e:return f"LlamaIndex Error: {str(e)}"with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 🤖 Multi-Framework AI Agent Space")69 unmodified lines70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912069 unmodified linesmax_steps=5)# ============ 2. LLAMAINDEX SETUP (STABLE VERSION) ============# FIX 1: Use 32B for LlamaIndex too. 7B is too weak for "ReAct" reasoning.# FIX 2: Use task="text-generation" (Stable for HF Inference API)llama_llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct",token=HF_TOKEN,task="text-generation",timeout=60)# FIX 3: ReActAgent needs very clear instructions to stay in "Agent Mode"# We define tools with explicit Type Hints and Docstringsdef get_tokyo_time() -> str:"""Returns the current date and time in Tokyo, Japan."""tz = pytz.timezone('Asia/Tokyo')return f"Tokyo Time: {datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')}"def multiply(a: float, b: float) -> float:"""Multiplies two numbers (a and b) and returns the result."""return str(a * b)# Create the toolstools = [FunctionTool.from_defaults(fn=get_tokyo_time),FunctionTool.from_defaults(fn=multiply)]# Initialize Agent with a clear "React" headerllama_agent = ReActAgent.from_tools(tools,llm=llama_llm,verbose=True # This will print the "Thought/Action" in your terminal/logs)# ============ GRADIO UI (STABLE) ============def chat_llama(message, history):try:# ReAct agents can sometimes take 2-3 "steps" to answerresponse = llama_agent.chat(message)return str(response)except Exception as e:# This will tell you EXACTLY why it failed in the Chat windowreturn f"⚠️ LlamaIndex Error: {str(e)}"with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
app.py+29-32