def load_csv(file_path: str, dataset_name: str, tool_context: ToolContext) -> dict:
print(f"📂 Loading CSV: {file_path} as '{dataset_name}'")
try:
df = pd.read_csv(file_path)
result = DATA_STORE.add_dataset(dataset_name, df, source=file_path)
datasets = tool_context.state.get("loaded_datasets", [])
If dataset_name does not exist in datasets
datasets.append(dataset_name)
tool_context.state["loaded_datasets"] = datasetsSummary =
tool_context.state["active_dataset"] = dataset_name
summary = {
"status": "success",
"message": result,
"preview": {
"columns": list(df.columns),
"shape": [int(df.shape[0]), int(df.shape[1])],
"dtypes": {k: str(v) for k, v in df.dtypes.items()},
"sample": make_serializable(df.head(3).to_dict(orient="records"))
}
}
return make_serializable(summary)
Except Exception As e.Return
return {"status": "error", "message"The f"Failed to load CSV: {str(e)}"}
def create_sample_dataset(dataset_type: str, dataset_name: str, tool_context: ToolContext) -> dict:
print(f"📂 Creating sample dataset: {dataset_type} as '{dataset_name}'")
np.random.seed(42)
if dataset_type == "sales":
N = 500
Dates = pd.date_rangeDataFrame () = df"2023-01-01", periods=n, freq="D")
df = pd.DataFrame({
"order_id": range(1000, 1000 + n),
"date"Dates[:n].astype(str),
"product": np.random.choice(["Laptop", "Phone", "Tablet", "Watch", "Headphones"], n),
"category": np.random.choice(["Electronics", "Accessories"], n, p=[0.6, 0.4]),
"region": np.random.choice(["North", "South", "East", "West"], n),
"quantity": np.random.randint(1, 10, n),
"unit_price": np.random.uniform(50, 1500, n).round(2),
"discount": np.random.choice([0.0, 0.05, 0.10, 0.15, 0.20], n),
"customer_type": np.random.choice(["New", "Returning", "VIP"], n, p=[0.3, 0.5, 0.2])
})
df["revenue"] = (df["quantity"] * df["unit_price"] * (1 - df["discount"])).round(2)
df["profit_margin"] = np.random.uniform(0.15, 0.45, n).round(3)
df["profit"] = (df["revenue"] * df["profit_margin"]).round(2)
elif dataset_type == "customers":
N = 300DataFrame () = df
df = pd.DataFrame({
"customer_id": range(5000, 5000 + n),
"age": np.random.randint(18, 75, n),
"gender": np.random.choice(["M", "F", "Other"], n, p=[0.48, 0.48, 0.04]),
"income": np.random.lognormal(10.5, 0.5, n).round(0),
"education": np.random.choice(["High School", "Bachelor", "Master", "PhD"], n, p=[0.25, 0.45, 0.22, 0.08]),
"membership_years": np.random.exponential(3, n).round(1),
"total_purchases": np.random.randint(1, 100, n),
"avg_order_value": np.random.uniform(25, 500, n).round(2),
"satisfaction_score": np.clip(np.random.normal(7.5, 1.5, n), 1, 10).round(1),
"churn_risk": np.random.choice(["Low", "Medium", "High"], n, p=[0.6, 0.3, 0.1])
})
df["lifetime_value"] = (df["total_purchases"] * df["avg_order_value"]).round(2)
elif dataset_type == "timeseries":
Dates = pd.date_range"2022-01-01", "2024-01-01", freq="D")
Len (dates) = n
Trend = np.linspace (100, 200, N)DataFrame () = df
seasonal = 30 * np.sin(np.linspace(0, 6 * np.pi, n))
noise = np.random.normal(0, 10, n)
df = pd.DataFrame({
"date": dates.astype(str),
"value"Round(2)
"volume": np.random.randint(1000, 10000, n),
"category": np.random.choice(["A", "B", "C"], n)
})
elif dataset_type == "survey":
N = 200DataFrame () = df
df = pd.DataFrame({
"respondent_id"The range is n+1 + 1.Return
"age_group": np.random.choice(["18-24", "25-34", "35-44", "45-54", "55+"], n),
"q1_satisfaction": np.random.randint(1, 6, n),
"q2_likelihood_recommend": np.random.randint(0, 11, n),
"q3_ease_of_use": np.random.randint(1, 6, n),
"q4_value_for_money": np.random.randint(1, 6, n),
"q5_support_quality": np.random.randint(1, 6, n),
"response_time_mins": np.random.exponential(10, n).round(1)
})
else:
return {"status": "error", "message"The f"Unknown dataset type: {dataset_type}. Use: sales, customers, timeseries, survey"}
result = DATA_STORE.add_dataset(dataset_name, df, source=f"sample_{dataset_type}")
datasets = tool_context.state.get("loaded_datasets", [])
If dataset_name does not exist in datasets
datasets.append(dataset_name)
tool_context.state["loaded_datasets"] = datasets
tool_context.state["active_dataset"] = dataset_name
return make_serializable({
"status": "success",
"message": result,
"description"The f"Created sample {dataset_type} dataset",
"columns": list(df.columns),
"shape": [int(df.shape[0]), int(df.shape[1])],
"sample": df.head(3).to_dict(orient="records")
})
def list_available_datasets(tool_context: ToolContext) -> dict:
print("📋 Listing datasets")
datasets = DATA_STORE.list_datasets()
If you do not have the data:Return
return {"status": "info", "message": "No datasets loaded. Use create_sample_dataset or load_csv."}
info = {}
For name in data sets:
ds=DATA_STORE.datasets[name]
The following information is provided by[name] = {
"rows": int(ds["shape"][0]),
"columns": int(ds["shape"][1]),
"column_names""["columns"]
}
return make_serializable({
"status": "success",
"datasets": info,
"active_dataset": tool_context.state.get("active_dataset")
})
print("✅ Data loading tools defined!")
Trending
- Lean and Mathlib Communities Certify Claude-Assisted Riemann Proof – Unite.AI
- AWS Routes HyperPod Inference Throughout Areas From One Endpoint – Unite.AI
- DailyAI Briefing: A Lean-checked Riemann proof, plus 4 extra
- Anthropic Shares Drop 8.5% in Second Session Throughout AI Selloff – Unite.AI
- OpenAI Ships GPT-Stay-1.1 for Voice Brokers, Cuts Idle Value to $0.02/Min
- Copywriting: The Full Information | Copyblogger
- AI Governance in 2027: Accountability Strikes Into the Workflow – Unite.AI
- Reply Engine Optimization (AEO): The Full Information

