Wayne0102 commited on
Commit
c2d8082
·
verified ·
1 Parent(s): 3f9419f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -22
app.py CHANGED
@@ -10,7 +10,7 @@ from llama_index.core.agent import ReActAgent
10
  from llama_index.core.tools import FunctionTool
11
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
12
 
13
- # ============ 1. SMOLAGENTS TOOLS (Strict Format) ============
14
 
15
  @tool
16
  def time_tool(timezone: str) -> str:
@@ -53,7 +53,8 @@ def weather_tool(location: str) -> str:
53
  try:
54
  geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
55
  geo_res = requests.get(geo_url).json()
56
- if not geo_res.get('results'): return "Location not found."
 
57
  data = geo_res['results'][0]
58
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={data['latitude']}&longitude={data['longitude']}&current=temperature_2m"
59
  w_res = requests.get(weather_url).json()
@@ -61,6 +62,8 @@ def weather_tool(location: str) -> str:
61
  except Exception as e:
62
  return f"Error: {str(e)}"
63
 
 
 
64
  HF_TOKEN = os.getenv("HF_TOKEN")
65
  smol_model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=HF_TOKEN)
66
 
@@ -70,19 +73,18 @@ smol_agent = CodeAgent(
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')
@@ -90,42 +92,96 @@ def get_tokyo_time() -> str:
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")
 
121
 
122
- with gr.Tab("Smolagents (Alfred)"):
123
- gr.Markdown("Uses `Qwen2.5-Coder-32B`")
124
- gr.ChatInterface(chat_smol)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
- with gr.Tab("LlamaIndex (ReAct)"):
127
- gr.Markdown("Uses `Qwen2.5-7B`")
128
- gr.ChatInterface(chat_llama)
 
 
 
 
 
 
 
129
 
130
  if __name__ == "__main__":
131
  demo.launch()
 
10
  from llama_index.core.tools import FunctionTool
11
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
12
 
13
+ # ============ 1. SMOLAGENTS TOOLS ============
14
 
15
  @tool
16
  def time_tool(timezone: str) -> str:
 
53
  try:
54
  geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
55
  geo_res = requests.get(geo_url).json()
56
+ if not geo_res.get('results'):
57
+ return "Location not found."
58
  data = geo_res['results'][0]
59
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={data['latitude']}&longitude={data['longitude']}&current=temperature_2m"
60
  w_res = requests.get(weather_url).json()
 
62
  except Exception as e:
63
  return f"Error: {str(e)}"
64
 
65
+ # ============ SMOLAGENTS SETUP ============
66
+
67
  HF_TOKEN = os.getenv("HF_TOKEN")
68
  smol_model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=HF_TOKEN)
69
 
 
73
  max_steps=5
74
  )
75
 
76
+ # ============ 2. LLAMAINDEX SETUP ============
77
 
78
+ # Use 32B model for better reasoning
 
79
  llama_llm = HuggingFaceInferenceAPI(
80
  model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
81
  token=HF_TOKEN,
82
  task="text-generation",
83
+ timeout=120, # Increased timeout
84
+ context_window=32768, # Set context window
85
  )
86
 
87
+ # Define tools with clear docstrings
 
88
  def get_tokyo_time() -> str:
89
  """Returns the current date and time in Tokyo, Japan."""
90
  tz = pytz.timezone('Asia/Tokyo')
 
92
 
93
  def multiply(a: float, b: float) -> float:
94
  """Multiplies two numbers (a and b) and returns the result."""
95
+ return a * b
96
+
97
+ def get_current_weather(city: str) -> str:
98
+ """Gets the current weather for a given city."""
99
+ try:
100
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
101
+ geo_res = requests.get(geo_url).json()
102
+ if not geo_res.get('results'):
103
+ return f"Could not find location: {city}"
104
+ data = geo_res['results'][0]
105
+ weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={data['latitude']}&longitude={data['longitude']}&current=temperature_2m"
106
+ w_res = requests.get(weather_url).json()
107
+ return f"Weather in {data['name']}: {w_res['current']['temperature_2m']}°C"
108
+ except Exception as e:
109
+ return f"Weather API Error: {str(e)}"
110
 
111
  # Create the tools
112
  tools = [
113
  FunctionTool.from_defaults(fn=get_tokyo_time),
114
+ FunctionTool.from_defaults(fn=multiply),
115
+ FunctionTool.from_defaults(fn=get_current_weather)
116
  ]
117
 
118
+ # Initialize ReAct Agent with system prompt
119
  llama_agent = ReActAgent.from_tools(
120
  tools,
121
  llm=llama_llm,
122
+ verbose=True,
123
+ max_iterations=10 # Allow more steps for complex queries
124
  )
125
 
126
+ # ============ CHAT FUNCTIONS ============
127
+
128
+ def chat_smol(message, history):
129
+ """Handler for Smolagents chat"""
130
+ try:
131
+ response = smol_agent.run(message)
132
+ return str(response)
133
+ except Exception as e:
134
+ return f"⚠️ Smolagents Error: {str(e)}\n\nPlease try rephrasing your question."
135
 
136
  def chat_llama(message, history):
137
+ """Handler for LlamaIndex chat"""
138
  try:
139
+ # Chat method returns a response object
140
  response = llama_agent.chat(message)
141
  return str(response)
142
+ except AttributeError as e:
143
+ # Handle potential API issues
144
+ return f"⚠️ LlamaIndex API Error: {str(e)}\n\nTry: 'What is 5 times 7?' or 'What time is it in Tokyo?'"
145
  except Exception as e:
146
+ return f"⚠️ LlamaIndex Error: {str(e)}\n\nThe agent may need a clearer question."
147
+
148
+ # ============ GRADIO UI ============
149
 
150
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
151
  gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
152
+ gr.Markdown("Compare two different agentic frameworks using the same LLM")
153
 
154
+ with gr.Tab("🔧 Smolagents (Alfred)"):
155
+ gr.Markdown("""
156
+ **Model:** Qwen2.5-Coder-32B-Instruct
157
+ **Tools:** Web Search, Time, Weather, Translation
158
+
159
+ Try: "What's the weather in Paris?" or "Translate 'hello' to Japanese"
160
+ """)
161
+ gr.ChatInterface(
162
+ chat_smol,
163
+ examples=[
164
+ "What's the weather in Tokyo?",
165
+ "What time is it in New York?",
166
+ "Translate 'good morning' to Spanish"
167
+ ]
168
+ )
169
+
170
+ with gr.Tab("🧠 LlamaIndex (ReAct)"):
171
+ gr.Markdown("""
172
+ **Model:** Qwen2.5-Coder-32B-Instruct
173
+ **Tools:** Tokyo Time, Multiply, Weather
174
 
175
+ Try: "What is 8 times 12?" or "What time is it in Tokyo?"
176
+ """)
177
+ gr.ChatInterface(
178
+ chat_llama,
179
+ examples=[
180
+ "What is 15 times 23?",
181
+ "What time is it in Tokyo?",
182
+ "What's the weather in London?"
183
+ ]
184
+ )
185
 
186
  if __name__ == "__main__":
187
  demo.launch()