-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_interface.py
More file actions
362 lines (305 loc) · 11.4 KB
/
agent_interface.py
File metadata and controls
362 lines (305 loc) · 11.4 KB
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
"""AI Agent interface for seamless book access."""
import numpy as np
from typing import List, Dict, Any, Optional, Union
from dataclasses import dataclass
from embeddings import EmbeddingEngine, VectorArithmetic
from vector_store import VectorStore, SearchResult
from config import DEFAULT_CONFIG
@dataclass
class AgentResponse:
"""Structured response for AI agent queries."""
query: str
results: List[SearchResult]
context: str # Combined context for the agent
metadata_summary: Dict[str, Any]
class CybersecurityBookAgent:
"""
High-level interface for AI agents to seamlessly access book knowledge.
Capabilities:
- Natural language queries
- Vector arithmetic (concept algebra)
- Category-filtered search
- Severity-filtered search
- Analogical reasoning
"""
def __init__(
self,
vector_store: Optional[VectorStore] = None,
embedding_engine: Optional[EmbeddingEngine] = None,
config=DEFAULT_CONFIG
):
self.config = config
# Initialize or use provided components
self._vector_store = vector_store
self._embedding_engine = embedding_engine
self._vector_arithmetic = None
@property
def vector_store(self) -> VectorStore:
if self._vector_store is None:
self._vector_store = VectorStore(
collection_name=self.config.vector_db.collection_name,
persist_directory=self.config.vector_db.persist_directory
)
return self._vector_store
@property
def embedding_engine(self) -> EmbeddingEngine:
if self._embedding_engine is None:
self._embedding_engine = EmbeddingEngine(
model_name=self.config.embedding.model_name,
device=self.config.embedding.device
)
return self._embedding_engine
@property
def vector_arithmetic(self) -> VectorArithmetic:
if self._vector_arithmetic is None:
self._vector_arithmetic = VectorArithmetic(self.embedding_engine)
return self._vector_arithmetic
def query(
self,
question: str,
n_results: int = 5,
use_hybrid: bool = True,
include_parent_context: bool = True
) -> AgentResponse:
"""
Query the book with a natural language question.
Args:
question: Natural language query
n_results: Number of results to return
use_hybrid: Use hybrid (dense + sparse) search
include_parent_context: Include parent chunks for full context
Returns:
AgentResponse with results and combined context
"""
# Generate query embedding
query_embedding = self.embedding_engine.embed_query(question)
# Search
if use_hybrid:
results = self.vector_store.hybrid_search(
query_embedding=query_embedding,
query_text=question,
n_results=n_results
)
else:
results = self.vector_store.search(
query_embedding=query_embedding,
n_results=n_results,
include_parent=include_parent_context
)
# Build combined context
context = self._build_context(results, include_parent_context)
# Summarize metadata
metadata_summary = self._summarize_metadata(results)
return AgentResponse(
query=question,
results=results,
context=context,
metadata_summary=metadata_summary
)
def concept_search(
self,
expression: str,
n_results: int = 5
) -> AgentResponse:
"""
Search using vector arithmetic (concept algebra).
Example expressions:
- "Zero Trust + Cloud Architecture"
- "SQL Injection - Web Application + Database"
- "Ransomware + Prevention"
This enables the famous King - Man + Woman = Queen style reasoning.
"""
# Compute the concept vector
concept_vector = self.vector_arithmetic.compute(expression)
# Search for nearest concepts
results = self.vector_store.search(
query_embedding=concept_vector,
n_results=n_results,
include_parent=True
)
context = self._build_context(results, include_parent=True)
metadata_summary = self._summarize_metadata(results)
return AgentResponse(
query=f"Concept: {expression}",
results=results,
context=context,
metadata_summary=metadata_summary
)
def analogy_search(
self,
a: str,
b: str,
c: str,
n_results: int = 5
) -> AgentResponse:
"""
Search using analogical reasoning.
"A is to B as C is to ?"
Example:
- analogy("SQL Injection", "Web Application", "Memory Corruption")
- Returns content related to "Memory Corruption" exploits in applications
"""
# Compute analogy vector
analogy_vector = self.vector_arithmetic.analogy(a, b, c)
# Search
results = self.vector_store.search(
query_embedding=analogy_vector,
n_results=n_results,
include_parent=True
)
context = self._build_context(results, include_parent=True)
metadata_summary = self._summarize_metadata(results)
return AgentResponse(
query=f"Analogy: {a} → {b} :: {c} → ?",
results=results,
context=context,
metadata_summary=metadata_summary
)
def search_by_category(
self,
question: str,
category: str,
n_results: int = 5
) -> AgentResponse:
"""
Search within a specific cybersecurity category.
Categories:
- network_security
- web_security
- cryptography
- malware
- authentication
- incident_response
- compliance
- cloud_security
- memory_safety
"""
query_embedding = self.embedding_engine.embed_query(question)
results = self.vector_store.filter_by_category(
query_embedding=query_embedding,
category=category,
n_results=n_results
)
context = self._build_context(results, include_parent=True)
metadata_summary = self._summarize_metadata(results)
return AgentResponse(
query=f"{question} [Category: {category}]",
results=results,
context=context,
metadata_summary=metadata_summary
)
def search_by_severity(
self,
question: str,
severity: str,
n_results: int = 5
) -> AgentResponse:
"""
Search for content with specific severity level.
Severities: critical, high, medium, low
"""
query_embedding = self.embedding_engine.embed_query(question)
results = self.vector_store.filter_by_severity(
query_embedding=query_embedding,
severity=severity,
n_results=n_results
)
context = self._build_context(results, include_parent=True)
metadata_summary = self._summarize_metadata(results)
return AgentResponse(
query=f"{question} [Severity: {severity}]",
results=results,
context=context,
metadata_summary=metadata_summary
)
def _build_context(
self,
results: List[SearchResult],
include_parent: bool = True
) -> str:
"""Build combined context string for AI agent consumption."""
context_parts = []
for i, result in enumerate(results, 1):
part = f"[Source {i}]"
# Add metadata breadcrumbs
if result.metadata.get('chapter'):
part += f"\nChapter: {result.metadata['chapter']}"
if result.metadata.get('section'):
part += f"\nSection: {result.metadata['section']}"
if result.metadata.get('severity'):
part += f"\nSeverity: {result.metadata['severity']}"
part += f"\nRelevance Score: {result.score:.3f}"
# Add content (prefer parent context if available)
if include_parent and result.parent_content:
part += f"\n\nContext:\n{result.parent_content}"
part += f"\n\nRelevant excerpt:\n{result.content}"
else:
part += f"\n\nContent:\n{result.content}"
context_parts.append(part)
return "\n\n" + "="*60 + "\n\n".join(context_parts)
def _summarize_metadata(self, results: List[SearchResult]) -> Dict[str, Any]:
"""Summarize metadata across all results."""
chapters = set()
sections = set()
categories = set()
severities = set()
cves = set()
for result in results:
meta = result.metadata
if meta.get('chapter'):
chapters.add(meta['chapter'])
if meta.get('section'):
sections.add(meta['section'])
if meta.get('categories'):
cats = meta['categories']
if isinstance(cats, str):
categories.update(cats.split(','))
else:
categories.update(cats)
if meta.get('severity'):
severities.add(meta['severity'])
if meta.get('cves'):
cve_list = meta['cves']
if isinstance(cve_list, str):
cves.update(cve_list.split(','))
else:
cves.update(cve_list)
return {
'chapters': list(chapters),
'sections': list(sections),
'categories': list(categories),
'severities': list(severities),
'cves': list(cves),
'result_count': len(results)
}
def get_stats(self) -> Dict[str, Any]:
"""Get statistics about the encoded book."""
return {
'total_chunks': self.vector_store.get_chunk_count(),
'embedding_model': self.config.embedding.model_name,
'embedding_dimension': self.embedding_engine.dimension,
'collection_name': self.config.vector_db.collection_name
}
# Convenience functions for direct use
def create_agent(
db_path: str = "./vector_db",
collection_name: str = "cybersecurity_book"
) -> CybersecurityBookAgent:
"""Create an agent connected to an existing encoded book."""
from config import PipelineConfig, VectorDBConfig
config = PipelineConfig(
vector_db=VectorDBConfig(
collection_name=collection_name,
persist_directory=db_path
)
)
return CybersecurityBookAgent(config=config)
def quick_query(
question: str,
db_path: str = "./vector_db",
n_results: int = 5
) -> str:
"""Quick query function that returns just the context string."""
agent = create_agent(db_path)
response = agent.query(question, n_results=n_results)
return response.context