mchinea commited on
Commit
0d8c865
·
1 Parent(s): 2f8eaba

update tools

Browse files
Files changed (1) hide show
  1. tools.py +25 -42
tools.py CHANGED
@@ -144,46 +144,8 @@ def convert_units(value: float, from_unit: str, to_unit: str) -> float:
144
  return conversions[key](value)
145
 
146
 
147
- def convert_query_to_pandas_syntax(natural_query: str, column_names: list) -> str:
148
- """
149
- Converts a natural language query to pandas query syntax using basic heuristics.
150
-
151
- Args:
152
- natural_query: A string with a question or filter expression in plain English.
153
- column_names: List of column names from the DataFrame.
154
-
155
- Returns:
156
- A best-effort string in pandas query() format.
157
- """
158
- # Preprocess query
159
- query = natural_query.lower().strip()
160
-
161
- # Heuristic rules
162
- rules = [
163
- (r"(\w+) greater than (\d+)", r"\1 > \2"),
164
- (r"(\w+) less than (\d+)", r"\1 < \2"),
165
- (r"(\w+) equal to ['\"]?([\w\s]+)['\"]?", r"\1 == '\2'"),
166
- (r"(\w+) not equal to ['\"]?([\w\s]+)['\"]?", r"\1 != '\2'"),
167
- (r"(\w+) more than (\d+)", r"\1 > \2"),
168
- (r"(\w+) less than or equal to (\d+)", r"\1 <= \2"),
169
- (r"(\w+) greater than or equal to (\d+)", r"\1 >= \2"),
170
- (r"(\w+) is ['\"]?([\w\s]+)['\"]?", r"\1 == '\2'"),
171
- ]
172
-
173
- for pattern, replacement in rules:
174
- if re.search(pattern, query):
175
- query = re.sub(pattern, replacement, query)
176
- break
177
-
178
- # Handle AND/OR logic
179
- query = query.replace(" and ", " and ")
180
- query = query.replace(" or ", " or ")
181
-
182
- return query
183
-
184
-
185
  @tool
186
- def query_table_data(file_path: str, query_pandas_syntax: str, sheet_name: str = None) -> str:
187
  """
188
  Loads a table from CSV or Excel and filters it using a pandas query.
189
 
@@ -208,8 +170,30 @@ def query_table_data(file_path: str, query_pandas_syntax: str, sheet_name: str =
208
  else:
209
  raise ValueError(f"Unsupported file extension: {ext}")
210
  try:
211
- filtered_df = df.query(query_pandas_syntax)
212
- return filtered_df.head(10).to_markdown(index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  except Exception as e:
214
  raise ValueError(f"Invalid query: {query_pandas_syntax}. Error: {e}")
215
  except ImportError:
@@ -368,7 +352,6 @@ level1_tools = [
368
  web_search,
369
  arvix_search,
370
  convert_units,
371
- convert_query_to_pandas_syntax,
372
  query_table_data,
373
  download_file_from_url,
374
  save_and_read_file,
 
144
  return conversions[key](value)
145
 
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  @tool
148
+ def query_table_data(file_path: str, query: str, sheet_name: str = None) -> str:
149
  """
150
  Loads a table from CSV or Excel and filters it using a pandas query.
151
 
 
170
  else:
171
  raise ValueError(f"Unsupported file extension: {ext}")
172
  try:
173
+ #Converts a natural language query to pandas query syntax using basic heuristics.
174
+ # Preprocess query
175
+ query_l = query.lower().strip()
176
+ # Heuristic rules
177
+ rules = [
178
+ (r"(\w+) greater than (\d+)", r"\1 > \2"),
179
+ (r"(\w+) less than (\d+)", r"\1 < \2"),
180
+ (r"(\w+) equal to ['\"]?([\w\s]+)['\"]?", r"\1 == '\2'"),
181
+ (r"(\w+) not equal to ['\"]?([\w\s]+)['\"]?", r"\1 != '\2'"),
182
+ (r"(\w+) more than (\d+)", r"\1 > \2"),
183
+ (r"(\w+) less than or equal to (\d+)", r"\1 <= \2"),
184
+ (r"(\w+) greater than or equal to (\d+)", r"\1 >= \2"),
185
+ (r"(\w+) is ['\"]?([\w\s]+)['\"]?", r"\1 == '\2'"),
186
+ ]
187
+
188
+ for pattern, replacement in rules:
189
+ if re.search(pattern, query):
190
+ query = re.sub(pattern, replacement, query)
191
+ break
192
+ # Handle AND/OR logic
193
+ query_pandas_syntax = query.replace(" and ", " and ")
194
+ query_pandas_syntaxs = query.replace(" or ", " or ")
195
+ filtered_df = df.query(query_pandas_syntax)
196
+ return filtered_df.head(10).to_markdown(index=False)
197
  except Exception as e:
198
  raise ValueError(f"Invalid query: {query_pandas_syntax}. Error: {e}")
199
  except ImportError:
 
352
  web_search,
353
  arvix_search,
354
  convert_units,
 
355
  query_table_data,
356
  download_file_from_url,
357
  save_and_read_file,