contextengine 0.1.2
contextengine: ^0.1.2 copied to clipboard
Provider-agnostic AI orchestration, memory, and context-optimization layer for Flutter apps.
import 'dart:io';
import 'package:contextengine/contextengine.dart';
/// Example: using ContextEngine with a mock provider, full pipeline.
///
/// Run: dart example/main.dart
void main() async {
// 1. Set up the engine with all features enabled.
final graphAdapter = LocalGraphAdapter();
final memory = MemoryEngine(
graphAdapter: graphAdapter,
sessionId: 'pet_bruno',
);
final vectorStore = InMemoryVectorStore();
final hybridRetrieval = HybridRetrieval(
graphAdapter: graphAdapter,
vectorStore: vectorStore,
);
final costEstimator = CostEstimator();
final engine = ContextEngine(
provider: MockProvider(name: 'mock', defaultModel: 'gpt-4o-mini'),
storage: InMemoryStorage(),
memory: memory,
vectorStore: vectorStore,
hybridRetrieval: hybridRetrieval,
costEstimator: costEstimator,
config: ContextEngineConfig(
tokenBudget: TokenBudget(maxInputTokens: 6000),
strategy: ContextStrategy.hybrid,
systemPrompt: 'You are a helpful pet health assistant.',
enableCostEstimation: true,
privacyPolicy: PrivacyPolicy(redact: {Field.email, Field.phone}),
),
);
await engine.initialize(sessionId: 'pet_bruno');
// 2. Direct fact ingestion (Lane 1 — no LLM cost).
await engine.remember(Fact(
id: 'fact_1',
subject: 'Bruno',
relation: 'HAS_ALLERGY',
object: 'Chicken',
confidence: 1.0,
));
await engine.remember(Fact(
id: 'fact_2',
subject: 'Bruno',
relation: 'IS_A',
object: 'Dog',
));
await engine.remember(Fact(
id: 'fact_3',
subject: 'Bruno',
relation: 'LIKES',
object: 'Walks',
));
print('--- Facts stored ---');
final facts = await engine.getAllFacts();
for (final f in facts) {
print(' $f');
}
// 3. Conversational message (Lane 2 — full pipeline).
print('\n--- Sending message ---');
final response = await engine.sendMessage('His ear is bothering him again');
print('Response: ${response.text}');
// 4. Observability — inspect what was sent.
final debug = engine.lastPromptDebug()!;
print('\n--- Pipeline log ---');
for (final line in debug.pipelineLog) {
print(' $line');
}
print('\n--- Token usage ---');
print(' ${engine.lastTokenUsage()}');
// 5. Cost estimation.
print('\n--- Cost ---');
print(' Estimated cost: \$${engine.lastCostEstimate()?.toStringAsFixed(6)}');
// 6. Streaming.
print('\n--- Streaming message ---');
final stream = engine.sendMessageStream('Tell me about Bruno allergies');
await for (final chunk in stream) {
stdout.write(chunk.text);
}
print('');
// 7. Show fact history after extraction.
await Future.delayed(const Duration(milliseconds: 100));
print('\n--- All facts after extraction ---');
final allFacts = await engine.getAllFacts();
for (final f in allFacts) {
print(' $f');
}
// 8. Clean up.
engine.dispose();
print('\nDone.');
}