-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_plugin_development.py
More file actions
620 lines (509 loc) · 19.9 KB
/
Copy path20_plugin_development.py
File metadata and controls
620 lines (509 loc) · 19.9 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
"""
Example 20: Plugin Development
Demonstrates how to create custom plugins for Paracle:
- Provider Plugin: Custom LLM provider (Ollama)
- Tool Plugin: Custom tool (Database query)
- Observer Plugin: Monitoring (Metrics collector)
Requirements:
pip install httpx sqlite3
ollama serve # If testing Ollama provider
"""
import asyncio
import logging
import sqlite3
from pathlib import Path
import httpx
from paracle_plugins import (
ObserverPlugin,
PluginCapability,
PluginMetadata,
PluginType,
ProviderPlugin,
ToolPlugin,
get_plugin_registry,
)
from paracle_plugins.observer_plugin import ExecutionEvent
from paracle_plugins.provider_plugin import (
ChatCompletionRequest,
ChatCompletionResponse,
Message,
)
from paracle_plugins.tool_plugin import ToolExecutionContext, ToolParameter, ToolSchema
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# =============================================================================
# 1. PROVIDER PLUGIN: Ollama Local LLM
# =============================================================================
class OllamaProvider(ProviderPlugin):
"""Ollama local LLM provider plugin.
Enables using locally hosted Ollama models with Paracle.
"""
@property
def metadata(self) -> PluginMetadata:
return PluginMetadata(
name="ollama-provider",
version="1.0.0",
description="Ollama local LLM provider for Paracle",
author="Community Example",
homepage="https://github.com/community/paracle-ollama",
license="MIT",
plugin_type=PluginType.PROVIDER,
capabilities=[PluginCapability.CHAT_COMPLETION, PluginCapability.STREAMING],
dependencies=["httpx>=0.24.0"],
paracle_version=">=0.2.0",
config_schema={
"type": "object",
"properties": {
"base_url": {"type": "string", "default": "http://localhost:11434"},
"default_model": {"type": "string", "default": "llama2"},
"timeout": {"type": "integer", "default": 60},
},
"required": ["base_url"],
},
tags=["llm", "local", "ollama", "provider"],
)
async def initialize(self, config: dict) -> None:
"""Initialize Ollama provider."""
self.base_url = config.get("base_url", "http://localhost:11434")
self.default_model = config.get("default_model", "llama2")
self.timeout = config.get("timeout", 60)
self.client = httpx.AsyncClient(timeout=self.timeout)
logger.info(
f"Initialized Ollama provider: {self.base_url} "
f"(model: {self.default_model})"
)
async def cleanup(self) -> None:
"""Cleanup resources."""
if hasattr(self, "client"):
await self.client.aclose()
logger.info("Ollama provider cleaned up")
def validate_config(self, config: dict) -> bool:
"""Validate configuration."""
if "base_url" not in config:
raise ValueError("base_url is required")
return True
async def health_check(self) -> dict:
"""Check Ollama server health."""
try:
response = await self.client.get(f"{self.base_url}/api/tags")
response.raise_for_status()
models = response.json().get("models", [])
return {
"status": "healthy",
"details": {
"connected": True,
"models_available": len(models),
"base_url": self.base_url,
},
}
except Exception as e:
return {"status": "unhealthy", "error": str(e)}
async def chat_completion(
self, request: ChatCompletionRequest
) -> ChatCompletionResponse:
"""Execute chat completion."""
model = request.model or self.default_model
response = await self.client.post(
f"{self.base_url}/api/chat",
json={
"model": model,
"messages": [
{"role": m.role, "content": m.content} for m in request.messages
],
"stream": False,
"options": {
"temperature": request.temperature or 0.7,
"num_predict": request.max_tokens or 1000,
},
},
)
response.raise_for_status()
data = response.json()
return ChatCompletionResponse(
id=f"ollama-{model}-{hash(data['message']['content'])}",
model=model,
content=data["message"]["content"],
role=data["message"]["role"],
finish_reason="stop",
usage={
"prompt_tokens": data.get("prompt_eval_count", 0),
"completion_tokens": data.get("eval_count", 0),
"total_tokens": data.get("total_duration", 0),
},
)
async def chat_completion_stream(self, request: ChatCompletionRequest):
"""Execute streaming chat completion."""
model = request.model or self.default_model
async with self.client.stream(
"POST",
f"{self.base_url}/api/chat",
json={
"model": model,
"messages": [
{"role": m.role, "content": m.content} for m in request.messages
],
"stream": True,
},
) as response:
async for line in response.aiter_lines():
if line:
yield line
async def list_models(self) -> list[str]:
"""List available Ollama models."""
response = await self.client.get(f"{self.base_url}/api/tags")
response.raise_for_status()
data = response.json()
return [model["name"] for model in data.get("models", [])]
async def get_model_info(self, model_name: str) -> dict:
"""Get detailed model information."""
response = await self.client.post(
f"{self.base_url}/api/show", json={"name": model_name}
)
response.raise_for_status()
return response.json()
# =============================================================================
# 2. TOOL PLUGIN: Database Query Tool
# =============================================================================
class DatabaseTool(ToolPlugin):
"""SQL database query tool plugin.
Enables agents to query SQLite databases.
"""
@property
def metadata(self) -> PluginMetadata:
return PluginMetadata(
name="database-tool",
version="1.0.0",
description="Execute SQL queries on SQLite databases",
author="Community Example",
plugin_type=PluginType.TOOL,
capabilities=[PluginCapability.DATABASE_ACCESS],
paracle_version=">=0.2.0",
config_schema={
"type": "object",
"properties": {
"database_path": {
"type": "string",
"description": "Path to SQLite database",
},
"read_only": {
"type": "boolean",
"default": True,
"description": "Allow only SELECT queries",
},
"max_rows": {"type": "integer", "default": 100},
},
"required": ["database_path"],
},
tags=["database", "sql", "sqlite", "tool"],
)
async def initialize(self, config: dict) -> None:
"""Initialize database tool."""
self.db_path = config["database_path"]
self.read_only = config.get("read_only", True)
self.max_rows = config.get("max_rows", 100)
# Verify database exists
if not Path(self.db_path).exists():
raise FileNotFoundError(f"Database not found: {self.db_path}")
logger.info(
f"Initialized database tool: {self.db_path} "
f"(read_only={self.read_only})"
)
async def cleanup(self) -> None:
"""Cleanup resources."""
logger.info("Database tool cleaned up")
def validate_config(self, config: dict) -> bool:
"""Validate configuration."""
if "database_path" not in config:
raise ValueError("database_path is required")
return True
async def health_check(self) -> dict:
"""Check database accessibility."""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT 1")
conn.close()
return {
"status": "healthy",
"details": {"database": self.db_path, "accessible": True},
}
except Exception as e:
return {"status": "unhealthy", "error": str(e)}
def get_tool_schema(self) -> ToolSchema:
"""Define tool schema for agents."""
return ToolSchema(
name="database_query",
description=("Execute SQL query on database. " "Returns columns and rows."),
parameters=[
ToolParameter(
name="query",
type="string",
description="SQL query to execute",
required=True,
),
ToolParameter(
name="limit",
type="integer",
description="Maximum rows to return",
required=False,
default=100,
),
],
)
async def execute(self, context: ToolExecutionContext, **kwargs) -> dict:
"""Execute database query."""
query = kwargs["query"]
limit = min(kwargs.get("limit", self.max_rows), self.max_rows)
# Validate read-only constraint
if self.read_only:
query_upper = query.strip().upper()
if not query_upper.startswith("SELECT"):
raise ValueError("Only SELECT queries allowed in read-only mode")
# Execute query
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(query)
# Fetch results
rows = cursor.fetchmany(limit)
columns = [desc[0] for desc in cursor.description]
conn.close()
return {
"success": True,
"columns": columns,
"rows": [list(row) for row in rows],
"row_count": len(rows),
"truncated": len(rows) == limit,
}
except sqlite3.Error as e:
return {"success": False, "error": str(e), "error_type": type(e).__name__}
# =============================================================================
# 3. OBSERVER PLUGIN: Simple Metrics Collector
# =============================================================================
class MetricsCollector(ObserverPlugin):
"""Simple metrics collection observer plugin.
Tracks execution metrics in memory.
"""
@property
def metadata(self) -> PluginMetadata:
return PluginMetadata(
name="metrics-collector",
version="1.0.0",
description="Collect execution metrics and statistics",
author="Community Example",
plugin_type=PluginType.OBSERVER,
capabilities=[PluginCapability.METRICS_COLLECTION],
paracle_version=">=0.2.0",
config_schema={
"type": "object",
"properties": {"track_costs": {"type": "boolean", "default": True}},
},
tags=["metrics", "monitoring", "observer"],
)
async def initialize(self, config: dict) -> None:
"""Initialize metrics collector."""
self.track_costs = config.get("track_costs", True)
# Metrics storage
self.executions = {"total": 0, "successful": 0, "failed": 0, "by_agent": {}}
self.llm_calls = {"total": 0, "by_provider": {}, "by_model": {}}
logger.info("Initialized metrics collector")
async def cleanup(self) -> None:
"""Cleanup and report final metrics."""
logger.info("Metrics Summary:")
logger.info(f" Executions: {self.executions}")
logger.info(f" LLM Calls: {self.llm_calls}")
async def health_check(self) -> dict:
"""Return current metrics."""
return {
"status": "healthy",
"details": {"executions": self.executions, "llm_calls": self.llm_calls},
}
async def on_execution_started(self, event: ExecutionEvent) -> None:
"""Track execution start."""
self.executions["total"] += 1
agent_id = event.agent_id or "unknown"
if agent_id not in self.executions["by_agent"]:
self.executions["by_agent"][agent_id] = {
"total": 0,
"successful": 0,
"failed": 0,
}
self.executions["by_agent"][agent_id]["total"] += 1
logger.info(f"Execution started: {event.execution_id} " f"(agent: {agent_id})")
async def on_execution_completed(self, event: ExecutionEvent) -> None:
"""Track successful execution."""
self.executions["successful"] += 1
agent_id = event.agent_id or "unknown"
if agent_id in self.executions["by_agent"]:
self.executions["by_agent"][agent_id]["successful"] += 1
logger.info(f"Execution completed: {event.execution_id}")
async def on_execution_failed(
self, event: ExecutionEvent, error: Exception
) -> None:
"""Track failed execution."""
self.executions["failed"] += 1
agent_id = event.agent_id or "unknown"
if agent_id in self.executions["by_agent"]:
self.executions["by_agent"][agent_id]["failed"] += 1
logger.error(f"Execution failed: {event.execution_id} - {error}")
async def on_llm_call(
self, event: ExecutionEvent, provider: str, model: str
) -> None:
"""Track LLM call."""
self.llm_calls["total"] += 1
# Track by provider
if provider not in self.llm_calls["by_provider"]:
self.llm_calls["by_provider"][provider] = 0
self.llm_calls["by_provider"][provider] += 1
# Track by model
model_key = f"{provider}/{model}"
if model_key not in self.llm_calls["by_model"]:
self.llm_calls["by_model"][model_key] = 0
self.llm_calls["by_model"][model_key] += 1
logger.debug(f"LLM call: {provider}/{model}")
# =============================================================================
# MAIN: Plugin Usage Examples
# =============================================================================
async def example_ollama_provider():
"""Example: Using Ollama provider plugin."""
print("\n" + "=" * 60)
print("EXAMPLE 1: Ollama Provider Plugin")
print("=" * 60)
# Create and register plugin
ollama = OllamaProvider()
await ollama.initialize(
{"base_url": "http://localhost:11434", "default_model": "llama2"}
)
registry = get_plugin_registry()
registry.register("ollama-provider", ollama, {})
# Check health
health = await ollama.health_check()
print(f"\nHealth: {health['status']}")
if health["status"] == "healthy":
print(f"Models available: {health['details']['models_available']}")
# List models
models = await ollama.list_models()
print(f"Models: {models[:3]}...") # Show first 3
# Test chat completion
request = ChatCompletionRequest(
model="llama2",
messages=[Message(role="user", content="Say hello in 5 words")],
temperature=0.7,
)
response = await ollama.chat_completion(request)
print(f"\nResponse: {response.content}")
print(f"Tokens: {response.usage}")
await ollama.cleanup()
print("\n✓ Ollama provider example complete")
async def example_database_tool():
"""Example: Using database tool plugin."""
print("\n" + "=" * 60)
print("EXAMPLE 2: Database Tool Plugin")
print("=" * 60)
# Create test database
db_path = "test_plugin.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)
"""
)
cursor.execute(
"INSERT OR IGNORE INTO users VALUES (1, 'Alice', 'alice@example.com')"
)
cursor.execute("INSERT OR IGNORE INTO users VALUES (2, 'Bob', 'bob@example.com')")
conn.commit()
conn.close()
# Create and register plugin
db_tool = DatabaseTool()
await db_tool.initialize({"database_path": db_path, "read_only": True})
registry = get_plugin_registry()
registry.register("database-tool", db_tool, {})
# Check health
health = await db_tool.health_check()
print(f"\nHealth: {health['status']}")
# Get tool schema
schema = db_tool.get_tool_schema()
print(f"Tool: {schema.name}")
print(f"Parameters: {[p.name for p in schema.parameters]}")
# Execute query
context = ToolExecutionContext(execution_id="test-001", agent_id="test-agent")
result = await db_tool.execute(context, query="SELECT * FROM users", limit=10)
print("\nQuery result:")
print(f" Columns: {result['columns']}")
print(f" Rows: {result['row_count']}")
for row in result["rows"]:
print(f" {row}")
await db_tool.cleanup()
# Cleanup test database
Path(db_path).unlink(missing_ok=True)
print("\n✓ Database tool example complete")
async def example_metrics_collector():
"""Example: Using metrics collector plugin."""
print("\n" + "=" * 60)
print("EXAMPLE 3: Metrics Collector Plugin")
print("=" * 60)
# Create and register plugin
metrics = MetricsCollector()
await metrics.initialize({"track_costs": True})
registry = get_plugin_registry()
registry.register("metrics-collector", metrics, {})
# Simulate execution events
event1 = ExecutionEvent(
event_type="execution_started",
timestamp="2026-01-07T14:30:00Z",
agent_id="coder",
execution_id="exec-001",
)
await metrics.on_execution_started(event1)
event2 = ExecutionEvent(
event_type="llm_call",
timestamp="2026-01-07T14:30:05Z",
agent_id="coder",
execution_id="exec-001",
)
await metrics.on_llm_call(event2, provider="openai", model="gpt-4")
event3 = ExecutionEvent(
event_type="execution_completed",
timestamp="2026-01-07T14:30:10Z",
agent_id="coder",
execution_id="exec-001",
)
await metrics.on_execution_completed(event3)
# Check metrics
health = await metrics.health_check()
print("\nMetrics collected:")
print(f" Executions: {health['details']['executions']}")
print(f" LLM Calls: {health['details']['llm_calls']}")
await metrics.cleanup()
print("\n✓ Metrics collector example complete")
async def main():
"""Run all plugin examples."""
print("\n" + "=" * 60)
print("PLUGIN DEVELOPMENT EXAMPLES")
print("=" * 60)
# Example 1: Ollama Provider (requires Ollama running)
try:
await example_ollama_provider()
except Exception as e:
print(f"\n⚠ Ollama example skipped: {e}")
print(" (Start Ollama with: ollama serve)")
# Example 2: Database Tool
await example_database_tool()
# Example 3: Metrics Collector
await example_metrics_collector()
print("\n" + "=" * 60)
print("ALL EXAMPLES COMPLETE")
print("=" * 60)
print("\nNext steps:")
print("1. Copy plugins to .parac/plugins/")
print("2. Configure in .parac/config/plugins.yaml")
print("3. Load with: paracle plugin load")
print("4. List with: paracle plugin list")
if __name__ == "__main__":
asyncio.run(main())