Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,54 +70,51 @@ smol_agent = CodeAgent(
|
|
| 70 |
max_steps=5
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# ============ 2. LLAMAINDEX SETUP (
|
| 74 |
|
| 75 |
-
# FIX 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def get_tokyo_time() -> str:
|
| 77 |
-
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"""
|
| 81 |
-
return f"Tokyo: {datetime.datetime.now(pytz.timezone('Asia/Tokyo'))}"
|
| 82 |
|
| 83 |
def multiply(a: float, b: float) -> float:
|
| 84 |
-
"""
|
| 85 |
-
|
| 86 |
-
Use this tool for calculations involving multiplication.
|
| 87 |
-
"""
|
| 88 |
-
return a * b
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
|
|
|
|
| 97 |
llama_agent = ReActAgent.from_tools(
|
| 98 |
-
|
| 99 |
-
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 100 |
-
FunctionTool.from_defaults(fn=multiply)
|
| 101 |
-
],
|
| 102 |
llm=llama_llm,
|
| 103 |
-
verbose=True
|
| 104 |
)
|
| 105 |
|
| 106 |
-
# ============ GRADIO UI ============
|
| 107 |
-
|
| 108 |
-
def chat_smol(message, history):
|
| 109 |
-
try:
|
| 110 |
-
return str(smol_agent.run(message))
|
| 111 |
-
except Exception as e:
|
| 112 |
-
return f"SmolAgent Error: {str(e)}"
|
| 113 |
|
| 114 |
def chat_llama(message, history):
|
| 115 |
try:
|
| 116 |
-
#
|
| 117 |
response = llama_agent.chat(message)
|
| 118 |
return str(response)
|
| 119 |
except Exception as e:
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 123 |
gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
|
|
|
|
| 70 |
max_steps=5
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# ============ 2. LLAMAINDEX SETUP (STABLE VERSION) ============
|
| 74 |
|
| 75 |
+
# FIX 1: Use 32B for LlamaIndex too. 7B is too weak for "ReAct" reasoning.
|
| 76 |
+
# FIX 2: Use task="text-generation" (Stable for HF Inference API)
|
| 77 |
+
llama_llm = HuggingFaceInferenceAPI(
|
| 78 |
+
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 79 |
+
token=HF_TOKEN,
|
| 80 |
+
task="text-generation",
|
| 81 |
+
timeout=60
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# FIX 3: ReActAgent needs very clear instructions to stay in "Agent Mode"
|
| 85 |
+
# We define tools with explicit Type Hints and Docstrings
|
| 86 |
def get_tokyo_time() -> str:
|
| 87 |
+
"""Returns the current date and time in Tokyo, Japan."""
|
| 88 |
+
tz = pytz.timezone('Asia/Tokyo')
|
| 89 |
+
return f"Tokyo Time: {datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')}"
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def multiply(a: float, b: float) -> float:
|
| 92 |
+
"""Multiplies two numbers (a and b) and returns the result."""
|
| 93 |
+
return str(a * b)
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Create the tools
|
| 96 |
+
tools = [
|
| 97 |
+
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 98 |
+
FunctionTool.from_defaults(fn=multiply)
|
| 99 |
+
]
|
|
|
|
| 100 |
|
| 101 |
+
# Initialize Agent with a clear "React" header
|
| 102 |
llama_agent = ReActAgent.from_tools(
|
| 103 |
+
tools,
|
|
|
|
|
|
|
|
|
|
| 104 |
llm=llama_llm,
|
| 105 |
+
verbose=True # This will print the "Thought/Action" in your terminal/logs
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# ============ GRADIO UI (STABLE) ============
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def chat_llama(message, history):
|
| 111 |
try:
|
| 112 |
+
# ReAct agents can sometimes take 2-3 "steps" to answer
|
| 113 |
response = llama_agent.chat(message)
|
| 114 |
return str(response)
|
| 115 |
except Exception as e:
|
| 116 |
+
# This will tell you EXACTLY why it failed in the Chat window
|
| 117 |
+
return f"⚠️ LlamaIndex Error: {str(e)}"
|
| 118 |
|
| 119 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 120 |
gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
|