Spaces:
Sleeping
Sleeping
File size: 11,151 Bytes
790e0e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | """
AI Agent Tools - Function definitions for OpenAI function calling
"""
import json
from typing import Dict, Any, Optional
import logging
from database.db_manager import DatabaseManager
from support.github_integration import GitHubSupport
class AgentTools:
"""Tools available to the AI agent via function calling"""
def __init__(self, db_manager: DatabaseManager, github_support: Optional[GitHubSupport] = None):
self.db_manager = db_manager
self.github_support = github_support
self.logger = logging.getLogger(__name__)
@staticmethod
def get_tool_definitions() -> list:
"""
Get OpenAI function definitions for all available tools
Returns:
List of tool definitions in OpenAI format
"""
return [
{
"type": "function",
"function": {
"name": "query_database",
"description": "Execute a SQL SELECT query on the car prices database. Use this to retrieve specific data based on user questions. Only SELECT queries are allowed for safety. The database contains car auction data with columns: year, make, model, trim, body, transmission, vin, state, condition, odometer, color, interior, seller, mmr, sellingprice, saledate.",
"parameters": {
"type": "object",
"properties": {
"sql_query": {
"type": "string",
"description": "The SQL SELECT query to execute. Must be a valid SELECT statement. Example: 'SELECT AVG(sellingprice) FROM cars WHERE make = \"BMW\"'"
}
},
"required": ["sql_query"]
}
}
},
{
"type": "function",
"function": {
"name": "get_database_statistics",
"description": "Get comprehensive statistics and aggregated information about the car prices database. Use this when user asks for general information, overview, or statistics about the data. Returns total records, price statistics, top makes/models, condition distribution, and year range.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "create_support_ticket",
"description": "Create a support ticket to reach a human for help. Use this when the user explicitly asks for human support, or when you cannot answer their question adequately. The ticket will be created as a GitHub issue.",
"parameters": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "Brief title summarizing the support request"
},
"description": {
"type": "string",
"description": "Detailed description of the issue or question, including conversation context"
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high"],
"description": "Priority level of the support request"
}
},
"required": ["title", "description"]
}
}
},
{
"type": "function",
"function": {
"name": "generate_chart",
"description": "Generate a dynamic chart based on a SQL query. Use this when the user asks for a chart, visualization, or comparison that would look better as a graph. You must provide a valid SQL SELECT query and chart configurations.",
"parameters": {
"type": "object",
"properties": {
"sql_query": {
"type": "string",
"description": "SQL SELECT query to get data for the chart. Example: 'SELECT make, AVG(sellingprice) FROM cars GROUP BY make'"
},
"chart_type": {
"type": "string",
"enum": ["bar", "column", "line", "pie", "scatter"],
"description": "Type of chart to generate"
},
"title": {
"type": "string",
"description": "Title of the chart"
},
"x_label": {
"type": "string",
"description": "Label for the X-axis (column name from query)"
},
"y_label": {
"type": "string",
"description": "Label for the Y-axis (column name from query)"
}
},
"required": ["sql_query", "chart_type", "title"]
}
}
}
]
def execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
"""
Execute a tool based on function call from OpenAI
Args:
tool_name: Name of the tool to execute
arguments: Arguments for the tool
Returns:
Result dictionary from the tool execution
"""
self.logger.info(f"Executing tool: {tool_name} with args: {arguments}")
if tool_name == "query_database":
return self._query_database(arguments.get("sql_query", ""))
elif tool_name == "get_database_statistics":
return self._get_database_statistics()
elif tool_name == "create_support_ticket":
return self._create_support_ticket(
title=arguments.get("title", ""),
description=arguments.get("description", ""),
priority=arguments.get("priority", "medium")
)
elif tool_name == "generate_chart":
return self._generate_chart(
sql_query=arguments.get("sql_query", ""),
chart_type=arguments.get("chart_type", "bar"),
title=arguments.get("title", ""),
x_label=arguments.get("x_label"),
y_label=arguments.get("y_label")
)
else:
return {
"success": False,
"error": f"Unknown tool: {tool_name}"
}
def _query_database(self, sql_query: str) -> Dict[str, Any]:
"""Execute a database query"""
self.logger.info(f"Executing query: {sql_query}")
result = self.db_manager.execute_query(sql_query)
# Format result for AI consumption
if result['success']:
# Limit data sent to AI to avoid token limits
data = result['data']
if len(data) > 100:
return {
"success": True,
"message": f"Query returned {len(data)} rows (showing first 100)",
"data": data[:100],
"row_count": len(data),
"truncated": True
}
else:
return {
"success": True,
"message": f"Query returned {len(data)} rows",
"data": data,
"row_count": len(data),
"truncated": False
}
else:
return {
"success": False,
"error": result['error']
}
def _get_database_statistics(self) -> Dict[str, Any]:
"""Get database statistics"""
self.logger.info("Retrieving database statistics")
stats = self.db_manager.get_statistics()
if stats:
return {
"success": True,
"statistics": stats
}
else:
return {
"success": False,
"error": "Failed to retrieve statistics"
}
def _create_support_ticket(self, title: str, description: str, priority: str = "medium") -> Dict[str, Any]:
"""Create a support ticket"""
self.logger.info(f"Creating support ticket: {title}")
if self.github_support:
result = self.github_support.create_issue(
title=title,
body=description,
labels=["support", f"priority-{priority}"]
)
return result
else:
# Mock support ticket if GitHub not configured
return {
"success": True,
"message": "Support ticket created (mock mode - GitHub not configured)",
"ticket_id": "MOCK-001",
"title": title,
"priority": priority
}
def _generate_chart(
self,
sql_query: str,
chart_type: str,
title: str,
x_label: Optional[str] = None,
y_label: Optional[str] = None
) -> Dict[str, Any]:
"""Execute query and return chart configuration"""
self.logger.info(f"Generating chart: {chart_type} - {title}")
# Execute query first
query_result = self._query_database(sql_query)
if query_result['success']:
data = query_result['data']
if not data:
return {
"success": False,
"error": "Query returned no data for the chart."
}
# Use provided labels or infer from data
cols = list(data[0].keys())
x_axis = x_label if x_label in cols else cols[0]
y_axis = y_label if y_label in cols else (cols[1] if len(cols) > 1 else cols[0])
return {
"success": True,
"is_chart": True,
"chart_config": {
"type": chart_type,
"title": title,
"x_label": x_axis,
"y_label": y_axis,
"data": data
},
"message": f"Successfully generated {chart_type} chart: {title}"
}
else:
return query_result
|