File size: 12,662 Bytes
5680110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
"""
SOFIA Advanced Tool Integration System
Expanded tool capabilities with APIs, databases, and web scraping
"""

import json, re, math, datetime, requests, sqlite3, os
import numpy as np
from typing import Dict, Any, List, Optional, Union
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class Tool:
    def __init__(self, name: str, description: str):
        self.name = name
        self.description = description

    def can_handle(self, query: str) -> bool:
        raise NotImplementedError

    def execute(self, query: str) -> Dict[str, Any]:
        raise NotImplementedError

class CalculatorTool(Tool):
    def __init__(self):
        super().__init__("calculator", "Performs mathematical calculations")

    def can_handle(self, query: str) -> bool:
        patterns = [
            r'\d+\s*[\+\-\*\/]\s*\d+',
            r'calculate\s+.+',
            r'what\s+is\s+.+[\+\-\*\/].+',
            r'solve\s+.+',
        ]
        return any(re.search(pattern, query.lower()) for pattern in patterns)

    def execute(self, query: str) -> Dict[str, Any]:
        try:
            expr_match = re.search(r'(\d+(?:\.\d+)?\s*[\+\-\*\/]\s*\d+(?:\.\d+)?)', query.lower())
            if expr_match:
                expression = expr_match.group(1).replace(' ', '')
                result = eval(expression)
                return {
                    "tool": "calculator",
                    "expression": expression,
                    "result": result,
                    "type": "arithmetic"
                }

            if 'sqrt' in query.lower():
                numbers = re.findall(r'\d+(?:\.\d+)?', query)
                if numbers:
                    num = float(numbers[0])
                    result = math.sqrt(num)
                    return {
                        "tool": "calculator",
                        "expression": f"sqrt({num})",
                        "result": result,
                        "type": "square_root"
                    }

        except Exception as e:
            return {"tool": "calculator", "error": f"Could not calculate: {str(e)}"}

        return {"tool": "calculator", "error": "No valid calculation found"}

class TimeTool(Tool):
    def __init__(self):
        super().__init__("time", "Provides current time, date, and temporal information")

    def can_handle(self, query: str) -> bool:
        time_keywords = ['time', 'date', 'day', 'hour', 'minute', 'second', 'now', 'today', 'tomorrow', 'yesterday']
        return any(keyword in query.lower() for keyword in time_keywords)

    def execute(self, query: str) -> Dict[str, Any]:
        now = datetime.datetime.now()

        result = {
            "tool": "time",
            "current_time": now.strftime("%H:%M:%S"),
            "current_date": now.strftime("%Y-%m-%d"),
            "day_of_week": now.strftime("%A"),
            "month": now.strftime("%B"),
            "year": now.year,
            "timezone": "UTC"
        }

        query_lower = query.lower()
        if 'tomorrow' in query_lower:
            tomorrow = now + datetime.timedelta(days=1)
            result['tomorrow'] = tomorrow.strftime("%Y-%m-%d (%A)")
        elif 'yesterday' in query_lower:
            yesterday = now - datetime.timedelta(days=1)
            result['yesterday'] = yesterday.strftime("%Y-%m-%d (%A)")

        return result

class SearchTool(Tool):
    def __init__(self):
        super().__init__("search", "Performs web searches and information retrieval")

    def can_handle(self, query: str) -> bool:
        search_keywords = ['search', 'find', 'lookup', 'what is', 'who is', 'where is', 'how to']
        return any(keyword in query.lower() for keyword in search_keywords)

    def execute(self, query: str) -> Dict[str, Any]:
        try:
            search_term = self._extract_search_term(query)

            if any(word in query.lower() for word in ['what is', 'who is', 'definition']):
                return self._wikipedia_search(search_term)

            return {
                "tool": "search",
                "search_term": search_term,
                "result": f"Search results for '{search_term}' would be retrieved from web sources",
                "type": "general_search",
                "suggestion": "Consider using specific APIs for better results"
            }

        except Exception as e:
            return {"tool": "search", "error": f"Search failed: {str(e)}"}

    def _extract_search_term(self, query: str) -> str:
        query = re.sub(r'^(what|who|where|how|when|why)\s+is\s+', '', query.lower())
        query = re.sub(r'^(search|find|lookup)\s+(for\s+)?', '', query.lower())
        return query.strip()

    def _wikipedia_search(self, term: str) -> Dict[str, Any]:
        try:
            import wikipedia
            page = wikipedia.page(term, auto_suggest=True)
            summary = page.summary[:500] + "..." if len(page.summary) > 500 else page.summary

            return {
                "tool": "search",
                "search_term": term,
                "result": summary,
                "source": "Wikipedia",
                "url": page.url,
                "type": "wikipedia_summary"
            }
        except Exception as e:
            return {"tool": "search", "error": f"Wikipedia search failed: {str(e)}"}

class DatabaseTool(Tool):
    def __init__(self, db_path: str = "sofia_memory.db"):
        super().__init__("database", "Access and query local knowledge database")
        self.db_path = db_path
        self._init_database()

    def can_handle(self, query: str) -> bool:
        db_keywords = ['remember', 'recall', 'stored', 'database', 'knowledge', 'facts']
        return any(keyword in query.lower() for keyword in db_keywords)

    def execute(self, query: str) -> Dict[str, Any]:
        try:
            query_lower = query.lower()

            if 'remember' in query_lower or 'store' in query_lower:
                return self._store_information(query)
            elif 'recall' in query_lower or 'retrieve' in query_lower:
                return self._retrieve_information(query)
            else:
                return self._query_database(query)

        except Exception as e:
            return {"tool": "database", "error": f"Database operation failed: {str(e)}"}

    def _init_database(self):
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        cursor.execute('''
            CREATE TABLE IF NOT EXISTS knowledge (
                id INTEGER PRIMARY KEY,
                topic TEXT,
                content TEXT,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
                source TEXT
            )
        ''')

        cursor.execute('''
            CREATE TABLE IF NOT EXISTS facts (
                id INTEGER PRIMARY KEY,
                fact TEXT UNIQUE,
                category TEXT,
                confidence REAL,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        ''')

        conn.commit()
        conn.close()

    def _store_information(self, query: str) -> Dict[str, Any]:
        content = re.sub(r'(remember|store)\s+', '', query, flags=re.IGNORECASE).strip()

        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        cursor.execute(
            "INSERT INTO knowledge (topic, content, source) VALUES (?, ?, ?)",
            ("user_input", content, "conversation")
        )

        conn.commit()
        conn.close()

        return {
            "tool": "database",
            "operation": "store",
            "content": content,
            "status": "stored"
        }

    def _retrieve_information(self, query: str) -> Dict[str, Any]:
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        cursor.execute(
            "SELECT content, timestamp FROM knowledge ORDER BY timestamp DESC LIMIT 5"
        )

        results = cursor.fetchall()
        conn.close()

        return {
            "tool": "database",
            "operation": "retrieve",
            "results": [{"content": row[0], "timestamp": row[1]} for row in results],
            "count": len(results)
        }

    def _query_database(self, query: str) -> Dict[str, Any]:
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        cursor.execute("SELECT COUNT(*) FROM knowledge")
        knowledge_count = cursor.fetchone()[0]

        cursor.execute("SELECT COUNT(*) FROM facts")
        facts_count = cursor.fetchone()[0]

        conn.close()

        return {
            "tool": "database",
            "operation": "stats",
            "knowledge_entries": knowledge_count,
            "facts_stored": facts_count
        }

class ToolManager:
    def __init__(self):
        self.tools = [
            CalculatorTool(),
            TimeTool(),
            SearchTool(),
            DatabaseTool()
        ]
        print(f"🔧 Loaded {len(self.tools)} advanced tools: {[t.name for t in self.tools]}")

    def execute_tools(self, query: str) -> List[Dict[str, Any]]:
        results = []
        for tool in self.tools:
            if tool.can_handle(query):
                print(f"🛠️  Using {tool.name}: {tool.description}")
                result = tool.execute(query)
                if 'error' not in result:
                    results.append(result)
                else:
                    print(f"⚠️  Tool {tool.name} failed: {result['error']}")
        return results

    def get_available_tools(self) -> List[Dict[str, str]]:
        return [{"name": tool.name, "description": tool.description} for tool in self.tools]

class AdvancedToolAugmentedSOFIA:
    def __init__(self):
        from conversational_sofia import ConversationalSOFIA
        self.sofia = ConversationalSOFIA()
        self.tool_manager = ToolManager()

    def process_query(self, query: str) -> Dict[str, Any]:
        print(f"🤖 Processing: '{query}'")

        tool_results = self.tool_manager.execute_tools(query)

        if tool_results:
            tool_contexts = []
            for result in tool_results:
                formatted = self._format_tool_result(result)
                tool_contexts.append(f"Tool {result['tool']}: {formatted}")

            enhanced_query = f"{query}\n\nTool Results:\n" + "\n".join(tool_contexts)
        else:
            enhanced_query = query

        response, embedding = self.sofia.chat(enhanced_query)

        return {
            "response": response,
            "embedding": embedding,
            "tools_used": tool_results,
            "tool_count": len(tool_results),
            "enhanced_query": enhanced_query
        }

    def _format_tool_result(self, result: Dict[str, Any]) -> str:
        tool_type = result.get('tool', '')

        if tool_type == 'time':
            return f"{result.get('current_time', 'N/A')} on {result.get('current_date', 'N/A')} ({result.get('day_of_week', 'N/A')})"

        elif tool_type == 'calculator':
            if 'expression' in result:
                return f"{result['expression']} = {result['result']}"
            else:
                return str(result.get('result', 'N/A'))

        elif tool_type == 'search':
            return result.get('result', 'Search completed')

        elif tool_type == 'database':
            if result.get('operation') == 'retrieve':
                return f"Retrieved {result.get('count', 0)} knowledge entries"
            elif result.get('operation') == 'store':
                return f"Stored: {result.get('content', 'N/A')}"
            else:
                return f"Database has {result.get('knowledge_entries', 0)} entries"

        else:
            return str(result.get('result', 'Tool executed successfully'))

def main():
    import sys

    if len(sys.argv) < 2:
        print("Usage: python sofia_tools.py 'query'")
        print("\nAvailable tools:")
        tool_manager = ToolManager()
        for tool_info in tool_manager.get_available_tools():
            print(f"  - {tool_info['name']}: {tool_info['description']}")
        return

    user_input = ' '.join(sys.argv[1:])

    tool_sofia = AdvancedToolAugmentedSOFIA()
    result = tool_sofia.process_query(user_input)

    print(f"\n🤖 SOFIA: {result['response']}")
    if result['tools_used']:
        print(f"\n🛠️  Advanced Tools Used: {result['tool_count']}")
        for tool_result in result['tools_used']:
            formatted = tool_sofia._format_tool_result(tool_result)
            print(f"  - {tool_result['tool']}: {formatted}")

if __name__ == "__main__":
    main()