forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultimodal_demo.py
More file actions
349 lines (283 loc) · 10.4 KB
/
multimodal_demo.py
File metadata and controls
349 lines (283 loc) · 10.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Multimodal and RAG Examples
This demonstrates vision, audio, and RAG capabilities for multimodal agents.
"""
from agent_control_plane import (
VisionCapability,
AudioCapability,
VectorStoreIntegration,
RAGPipeline,
ImageInput,
AudioInput,
VectorDocument,
ImageFormat,
AudioFormat,
VectorStoreType,
create_multimodal_suite
)
import base64
def example_vision_analysis():
"""Example: Analyzing images with vision capability"""
print("=== Vision Capability Example ===\n")
vision = VisionCapability()
# Simulated image (in production, would be actual base64-encoded image)
image = ImageInput(
image_data="simulated_base64_encoded_image_data",
format=ImageFormat.PNG,
metadata={"source": "user_upload", "size": 1024}
)
# Analyze image
result = vision.analyze_image(
image=image,
prompt="Describe what you see in this image"
)
print("Image Analysis:")
print(f" Success: {result['success']}")
print(f" Safety checked: {result['safety_checked']}")
print(f" Image hash: {result['image_hash']}")
print()
# Check image safety
safety = vision.check_image_safety(image)
print("Image Safety Check:")
print(f" Safe: {safety['safe']}")
print(f" Violations: {safety['violations']}")
print()
print(f"Supported formats: {', '.join(vision.get_supported_formats())}")
print()
def example_audio_processing():
"""Example: Processing audio with transcription"""
print("=== Audio Capability Example ===\n")
audio_cap = AudioCapability()
# Simulated audio input
audio = AudioInput(
audio_data="simulated_base64_encoded_audio_data",
format=AudioFormat.MP3,
duration_seconds=45.0,
sample_rate=44100
)
# Transcribe audio
result = audio_cap.transcribe(audio, language="en")
print("Audio Transcription:")
print(f" Success: {result['success']}")
print(f" Duration: {result['duration']} seconds")
print(f" Language: {result['language']}")
print()
# Check audio safety
safety = audio_cap.check_audio_safety(audio)
print("Audio Safety Check:")
print(f" Safe: {safety['safe']}")
print(f" Violations: {safety['violations']}")
print()
print(f"Supported formats: {', '.join(audio_cap.get_supported_formats())}")
print()
def example_vector_store():
"""Example: Using vector store for document storage"""
print("=== Vector Store Example ===\n")
vector_store = VectorStoreIntegration(
store_type=VectorStoreType.IN_MEMORY,
collection_name="knowledge_base"
)
# Add documents
documents = [
VectorDocument(
doc_id="doc1",
content="Agent Control Plane provides governance for AI agents",
embedding=[0.1, 0.2, 0.3, 0.4, 0.5],
metadata={"source": "documentation", "category": "overview"}
),
VectorDocument(
doc_id="doc2",
content="Constitutional AI helps align agent behavior with values",
embedding=[0.2, 0.3, 0.4, 0.5, 0.6],
metadata={"source": "documentation", "category": "safety"}
),
VectorDocument(
doc_id="doc3",
content="RAG enables knowledge-grounded generation",
embedding=[0.3, 0.4, 0.5, 0.6, 0.7],
metadata={"source": "documentation", "category": "features"}
)
]
result = vector_store.add_documents(documents)
print(f"Added Documents:")
print(f" Count: {result['added_count']}")
print(f" IDs: {', '.join(result['document_ids'])}")
print()
# Search for similar documents
query_embedding = [0.15, 0.25, 0.35, 0.45, 0.55]
results = vector_store.search(
query_embedding=query_embedding,
top_k=2
)
print(f"Search Results (top 2):")
for i, doc in enumerate(results, 1):
print(f" {i}. {doc['content']}")
print(f" Similarity: {doc['similarity']:.3f}")
print(f" Metadata: {doc['metadata']}")
print()
# Get stats
stats = vector_store.get_stats()
print(f"Vector Store Stats:")
print(f" Type: {stats['store_type']}")
print(f" Collection: {stats['collection_name']}")
print(f" Documents: {stats['document_count']}")
print()
def example_rag_pipeline():
"""Example: RAG (Retrieval-Augmented Generation) pipeline"""
print("=== RAG Pipeline Example ===\n")
# Set up vector store with knowledge
vector_store = VectorStoreIntegration(
store_type=VectorStoreType.IN_MEMORY,
collection_name="ai_knowledge"
)
knowledge_docs = [
VectorDocument(
doc_id="safety1",
content="AI safety focuses on ensuring AI systems behave as intended and don't cause unintended harm. Key approaches include alignment, robustness, and interpretability.",
embedding=[0.8, 0.7, 0.6, 0.5, 0.4]
),
VectorDocument(
doc_id="governance1",
content="AI governance involves establishing policies, standards, and oversight mechanisms to ensure responsible AI development and deployment.",
embedding=[0.7, 0.8, 0.5, 0.6, 0.3]
),
VectorDocument(
doc_id="agents1",
content="Autonomous agents are AI systems that can perceive their environment and take actions to achieve goals with minimal human intervention.",
embedding=[0.6, 0.5, 0.8, 0.7, 0.2]
)
]
vector_store.add_documents(knowledge_docs)
# Create RAG pipeline
rag = RAGPipeline(vector_store)
# Query with RAG
query = "What is AI safety?"
query_embedding = [0.75, 0.65, 0.55, 0.45, 0.35]
result = rag.query(
query_text=query,
query_embedding=query_embedding,
top_k=2
)
print(f"RAG Query: {query}")
print(f" Retrieved: {len(result['retrieved_documents'])} documents")
print()
print("Retrieved Context:")
for i, doc in enumerate(result['retrieved_documents'], 1):
print(f" [{i}] {doc['content'][:80]}...")
print()
print("RAG Prompt (for LLM):")
print(result['rag_prompt'][:200] + "...")
print()
print(f"Citations: {result['citations']}")
print()
def example_multimodal_input():
"""Example: Processing multimodal input (text + image + audio)"""
print("=== Multimodal Input Example ===\n")
from agent_control_plane.multimodal import MultimodalInput, ModalityType
# Create multimodal input
mm_input = MultimodalInput(
text="Analyze this medical scan and audio recording",
images=[
ImageInput(
image_data="medical_scan_image",
format=ImageFormat.PNG,
metadata={"type": "x-ray"}
)
],
audio=[
AudioInput(
audio_data="doctor_notes_audio",
format=AudioFormat.WAV,
duration_seconds=120.0
)
]
)
modalities = mm_input.get_modalities()
print("Multimodal Input:")
print(f" Text: {mm_input.text[:50]}...")
print(f" Images: {len(mm_input.images)}")
print(f" Audio: {len(mm_input.audio)}")
print(f" Modalities: {[m.value for m in modalities]}")
print()
def example_rag_with_metadata_filtering():
"""Example: RAG with metadata filtering"""
print("=== RAG with Metadata Filtering ===\n")
vector_store = VectorStoreIntegration(
store_type=VectorStoreType.IN_MEMORY
)
# Add documents with metadata
docs = [
VectorDocument(
doc_id="tech1",
content="Machine learning algorithms learn from data",
embedding=[0.5, 0.5, 0.5, 0.5, 0.5],
metadata={"category": "technical", "difficulty": "beginner"}
),
VectorDocument(
doc_id="tech2",
content="Neural networks consist of interconnected layers",
embedding=[0.6, 0.5, 0.5, 0.5, 0.4],
metadata={"category": "technical", "difficulty": "advanced"}
),
VectorDocument(
doc_id="business1",
content="AI can improve business efficiency and decision-making",
embedding=[0.5, 0.6, 0.5, 0.4, 0.5],
metadata={"category": "business", "difficulty": "beginner"}
)
]
vector_store.add_documents(docs)
# Search with metadata filter
query_embedding = [0.55, 0.55, 0.5, 0.5, 0.45]
# Filter for technical content only
results = vector_store.search(
query_embedding=query_embedding,
top_k=5,
filter_metadata={"category": "technical"}
)
print("Search Results (technical only):")
for doc in results:
print(f" - {doc['content']}")
print(f" Category: {doc['metadata']['category']}")
print(f" Difficulty: {doc['metadata']['difficulty']}")
print()
def example_integrated_multimodal():
"""Example: Using complete multimodal suite"""
print("=== Integrated Multimodal Suite ===\n")
suite = create_multimodal_suite()
vision = suite["vision"]
audio = suite["audio"]
vector_store = suite["vector_store"]
rag = suite["rag_pipeline"]
print("Multimodal Suite Components:")
print(f" ✅ Vision Capability")
print(f" ✅ Audio Capability")
print(f" ✅ Vector Store ({vector_store.store_type.value})")
print(f" ✅ RAG Pipeline")
print()
# Example workflow
print("Example Workflow:")
print(" 1. Process image with vision capability")
print(" 2. Transcribe audio with audio capability")
print(" 3. Store results in vector store")
print(" 4. Use RAG to answer questions about the content")
print()
if __name__ == "__main__":
print("Agent Control Plane - Multimodal & RAG Examples")
print("=" * 70)
print()
example_vision_analysis()
print("\n" + "=" * 70 + "\n")
example_audio_processing()
print("\n" + "=" * 70 + "\n")
example_vector_store()
print("\n" + "=" * 70 + "\n")
example_rag_pipeline()
print("\n" + "=" * 70 + "\n")
example_multimodal_input()
print("\n" + "=" * 70 + "\n")
example_rag_with_metadata_filtering()
print("\n" + "=" * 70 + "\n")
example_integrated_multimodal()