mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-12 11:51:32 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edbd122414 | ||
|
|
0ac055cba5 |
No files matched your search
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=3.1.8" \
|
||||
"praisonai>=3.2.0" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=3.1.8" \
|
||||
"praisonai>=3.2.0" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=3.1.8" \
|
||||
"praisonai>=3.2.0" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Context Budgeting Example
|
||||
|
||||
Demonstrates how to use the ContextBudgeter to allocate token budgets
|
||||
across different context segments for various models.
|
||||
"""
|
||||
|
||||
from praisonaiagents.context import (
|
||||
ContextBudgeter,
|
||||
BudgetAllocation,
|
||||
get_model_limit,
|
||||
get_output_reserve,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("Context Budgeting Example")
|
||||
print("=" * 60)
|
||||
|
||||
# Example 1: Basic budget allocation for GPT-4o-mini
|
||||
print("\n1. Basic Budget Allocation (gpt-4o-mini)")
|
||||
print("-" * 40)
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o-mini")
|
||||
budget = budgeter.allocate()
|
||||
|
||||
print(f"Model: gpt-4o-mini")
|
||||
print(f"Model Limit: {budget.model_limit:,} tokens")
|
||||
print(f"Output Reserve: {budget.output_reserve:,} tokens")
|
||||
print(f"Usable Context: {budget.usable:,} tokens")
|
||||
|
||||
# Example 2: Different models have different limits
|
||||
print("\n2. Model Comparison")
|
||||
print("-" * 40)
|
||||
|
||||
models = ["gpt-4o", "gpt-4o-mini", "gpt-4-turbo", "claude-3-opus", "gemini-1.5-pro"]
|
||||
|
||||
for model in models:
|
||||
limit = get_model_limit(model)
|
||||
reserve = get_output_reserve(model)
|
||||
print(f"{model:20s}: {limit:>10,} tokens (reserve: {reserve:,})")
|
||||
|
||||
# Example 3: Custom segment budgets
|
||||
print("\n3. Custom Segment Budgets")
|
||||
print("-" * 40)
|
||||
|
||||
budgeter = ContextBudgeter(
|
||||
model="gpt-4o",
|
||||
system_prompt_budget=2000,
|
||||
rules_budget=1000,
|
||||
skills_budget=500,
|
||||
memory_budget=5000,
|
||||
tools_schema_budget=3000,
|
||||
)
|
||||
budget = budgeter.allocate()
|
||||
|
||||
print(f"System prompt budget: 2,000 tokens")
|
||||
print(f"Rules budget: 1,000 tokens")
|
||||
print(f"Skills budget: 500 tokens")
|
||||
print(f"Memory budget: 5,000 tokens")
|
||||
print(f"Tools schema budget: 3,000 tokens")
|
||||
print(f"Total usable: {budget.usable:,} tokens")
|
||||
|
||||
# Example 4: Check for overflow
|
||||
print("\n4. Overflow Detection")
|
||||
print("-" * 40)
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o-mini")
|
||||
|
||||
# Simulate different usage levels
|
||||
test_cases = [
|
||||
("Low usage", 10000),
|
||||
("Medium usage", 80000),
|
||||
("High usage", 100000),
|
||||
("Near limit", 110000),
|
||||
("Over limit", 130000),
|
||||
]
|
||||
|
||||
for name, tokens in test_cases:
|
||||
overflow = budgeter.check_overflow(tokens)
|
||||
utilization = budgeter.get_utilization(tokens)
|
||||
remaining = budgeter.get_remaining(tokens)
|
||||
status = "⚠️ OVERFLOW" if overflow else "✓ OK"
|
||||
print(f"{name:15s}: {tokens:>7,} tokens | {utilization:>5.1%} | {status}")
|
||||
|
||||
# Example 5: Budget to dict for serialization
|
||||
print("\n5. Budget Serialization")
|
||||
print("-" * 40)
|
||||
|
||||
budget_dict = budgeter.to_dict()
|
||||
print(f"Budget as dict: {budget_dict}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✓ Context budgeting examples complete!")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Context Monitoring Example
|
||||
|
||||
Demonstrates how to use the ContextMonitor to write runtime context
|
||||
snapshots to disk for debugging and analysis.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from praisonaiagents.context import (
|
||||
ContextMonitor,
|
||||
ContextLedger,
|
||||
BudgetAllocation,
|
||||
format_human_snapshot,
|
||||
format_json_snapshot,
|
||||
redact_sensitive,
|
||||
ContextSnapshot,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("Context Monitoring Example")
|
||||
print("=" * 60)
|
||||
|
||||
# Sample conversation for demonstration
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful AI assistant."},
|
||||
{"role": "user", "content": "What is the weather like today?"},
|
||||
{"role": "assistant", "content": "I don't have access to real-time weather data."},
|
||||
{"role": "user", "content": "Can you help me write Python code?"},
|
||||
{"role": "assistant", "content": "Of course! I'd be happy to help with Python."},
|
||||
]
|
||||
|
||||
# Example 1: Basic monitoring setup
|
||||
print("\n1. Basic Monitor Setup")
|
||||
print("-" * 40)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
context_path = os.path.join(tmpdir, "context.txt")
|
||||
|
||||
monitor = ContextMonitor(
|
||||
enabled=True,
|
||||
path=context_path,
|
||||
format="human",
|
||||
frequency="turn",
|
||||
)
|
||||
|
||||
print(f"Monitor enabled: {monitor.enabled}")
|
||||
print(f"Output path: {context_path}")
|
||||
print(f"Format: {monitor.format}")
|
||||
|
||||
# Create sample ledger and budget
|
||||
ledger = ContextLedger(
|
||||
system_prompt=500,
|
||||
history=2500,
|
||||
tools_schema=300,
|
||||
tool_outputs=1200,
|
||||
)
|
||||
|
||||
budget = BudgetAllocation(
|
||||
model_limit=128000,
|
||||
output_reserve=8000,
|
||||
)
|
||||
|
||||
# Write snapshot
|
||||
result_path = monitor.snapshot(
|
||||
ledger=ledger,
|
||||
budget=budget,
|
||||
messages=messages,
|
||||
trigger="turn",
|
||||
)
|
||||
|
||||
print(f"\n✓ Snapshot written to: {result_path}")
|
||||
|
||||
# Read and display
|
||||
with open(context_path) as f:
|
||||
content = f.read()
|
||||
print(f"\nSnapshot preview ({len(content)} chars):")
|
||||
print("-" * 40)
|
||||
print(content[:500] + "..." if len(content) > 500 else content)
|
||||
|
||||
# Example 2: JSON format
|
||||
print("\n\n2. JSON Format Output")
|
||||
print("-" * 40)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
json_path = os.path.join(tmpdir, "context.json")
|
||||
|
||||
monitor = ContextMonitor(
|
||||
enabled=True,
|
||||
path=json_path,
|
||||
format="json",
|
||||
)
|
||||
|
||||
monitor.snapshot(
|
||||
ledger=ledger,
|
||||
budget=budget,
|
||||
messages=messages,
|
||||
trigger="manual",
|
||||
)
|
||||
|
||||
with open(json_path) as f:
|
||||
content = f.read()
|
||||
|
||||
print(f"JSON snapshot ({len(content)} chars):")
|
||||
print(content[:300] + "..." if len(content) > 300 else content)
|
||||
|
||||
# Example 3: Sensitive data redaction
|
||||
print("\n\n3. Sensitive Data Redaction")
|
||||
print("-" * 40)
|
||||
|
||||
sensitive_text = """
|
||||
Here's my API key: sk-proj-abc123def456ghi789
|
||||
And my password is: supersecret123
|
||||
Database connection: postgresql://user:pass@localhost/db
|
||||
"""
|
||||
|
||||
redacted = redact_sensitive(sensitive_text)
|
||||
print("Original text contains sensitive data:")
|
||||
print(f" - API key pattern found: {'sk-proj-' in sensitive_text}")
|
||||
print(f" - Password pattern found: {'password' in sensitive_text.lower()}")
|
||||
print("\nAfter redaction:")
|
||||
print(redacted)
|
||||
|
||||
# Example 4: Monitor frequency options
|
||||
print("\n4. Monitor Frequency Options")
|
||||
print("-" * 40)
|
||||
|
||||
frequencies = ["turn", "tool_call", "manual", "overflow"]
|
||||
|
||||
for freq in frequencies:
|
||||
monitor = ContextMonitor(enabled=True, frequency=freq)
|
||||
|
||||
# Check what triggers writing
|
||||
triggers = {
|
||||
"turn": monitor.should_write("turn"),
|
||||
"tool_call": monitor.should_write("tool_call"),
|
||||
"manual": monitor.should_write("manual"),
|
||||
"overflow": monitor.should_write("overflow"),
|
||||
}
|
||||
|
||||
active = [k for k, v in triggers.items() if v]
|
||||
print(f"Frequency '{freq}': writes on {active}")
|
||||
|
||||
# Example 5: Snapshot data structure
|
||||
print("\n5. Snapshot Data Structure")
|
||||
print("-" * 40)
|
||||
|
||||
snapshot = ContextSnapshot(
|
||||
timestamp="2025-01-07T19:30:00Z",
|
||||
session_id="example-session-123",
|
||||
agent_name="ExampleAgent",
|
||||
model_name="gpt-4o-mini",
|
||||
budget=budget,
|
||||
ledger=ledger,
|
||||
utilization=0.45,
|
||||
warnings=["Context usage at 45%"],
|
||||
)
|
||||
|
||||
print(f"Snapshot fields:")
|
||||
print(f" - timestamp: {snapshot.timestamp}")
|
||||
print(f" - session_id: {snapshot.session_id}")
|
||||
print(f" - agent_name: {snapshot.agent_name}")
|
||||
print(f" - model_name: {snapshot.model_name}")
|
||||
print(f" - utilization: {snapshot.utilization:.1%}")
|
||||
print(f" - warnings: {snapshot.warnings}")
|
||||
|
||||
# Format as human-readable
|
||||
human_output = format_human_snapshot(snapshot)
|
||||
print(f"\nHuman-readable format ({len(human_output)} chars)")
|
||||
|
||||
# Format as JSON
|
||||
json_output = format_json_snapshot(snapshot)
|
||||
print(f"JSON format ({len(json_output)} chars)")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✓ Context monitoring examples complete!")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Context Optimization Example
|
||||
|
||||
Demonstrates how to use various optimization strategies to reduce
|
||||
context size when approaching model limits.
|
||||
"""
|
||||
|
||||
from praisonaiagents.context import (
|
||||
get_optimizer,
|
||||
OptimizerStrategy,
|
||||
TruncateOptimizer,
|
||||
SlidingWindowOptimizer,
|
||||
PruneToolsOptimizer,
|
||||
SmartOptimizer,
|
||||
estimate_messages_tokens,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("Context Optimization Example")
|
||||
print("=" * 60)
|
||||
|
||||
# Create sample conversation with tool calls
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful AI assistant with access to tools."},
|
||||
{"role": "user", "content": "What's the weather in Paris?"},
|
||||
{"role": "assistant", "content": "", "tool_calls": [{"id": "call_1", "function": {"name": "get_weather", "arguments": '{"city": "Paris"}'}}]},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": "Weather in Paris: 15°C, partly cloudy, humidity 65%, wind 10 km/h from the west. Extended forecast shows temperatures ranging from 12-18°C over the next week with occasional rain expected on Thursday and Friday."},
|
||||
{"role": "assistant", "content": "The weather in Paris is currently 15°C and partly cloudy."},
|
||||
{"role": "user", "content": "What about London?"},
|
||||
{"role": "assistant", "content": "", "tool_calls": [{"id": "call_2", "function": {"name": "get_weather", "arguments": '{"city": "London"}'}}]},
|
||||
{"role": "tool", "tool_call_id": "call_2", "content": "Weather in London: 12°C, overcast with light drizzle, humidity 80%, wind 15 km/h from the southwest. Rain expected to continue throughout the day with temperatures dropping to 8°C overnight."},
|
||||
{"role": "assistant", "content": "London is 12°C with light drizzle."},
|
||||
]
|
||||
|
||||
# Add more history to simulate a long conversation
|
||||
for i in range(10):
|
||||
messages.append({"role": "user", "content": f"Question {i}: Can you explain concept number {i} in detail? " * 20})
|
||||
messages.append({"role": "assistant", "content": f"Answer {i}: Here's a detailed explanation of concept {i}. " * 30})
|
||||
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
print(f"\nOriginal conversation: {len(messages)} messages, ~{original_tokens} tokens")
|
||||
|
||||
# Example 1: Truncate Optimizer
|
||||
print("\n1. Truncate Optimizer")
|
||||
print("-" * 40)
|
||||
|
||||
optimizer = TruncateOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=2000)
|
||||
result_tokens = estimate_messages_tokens(result)
|
||||
|
||||
print(f"Strategy: Remove oldest messages first")
|
||||
print(f"Result: {len(messages)} -> {len(result)} messages")
|
||||
print(f"Tokens: {original_tokens} -> {result_tokens}")
|
||||
|
||||
# Example 2: Sliding Window Optimizer
|
||||
print("\n2. Sliding Window Optimizer")
|
||||
print("-" * 40)
|
||||
|
||||
optimizer = SlidingWindowOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=2000)
|
||||
result_tokens = estimate_messages_tokens(result)
|
||||
|
||||
print(f"Strategy: Keep most recent messages within window")
|
||||
print(f"Result: {len(messages)} -> {len(result)} messages")
|
||||
print(f"Tokens: {original_tokens} -> {result_tokens}")
|
||||
|
||||
# Example 3: Prune Tools Optimizer
|
||||
print("\n3. Prune Tools Optimizer")
|
||||
print("-" * 40)
|
||||
|
||||
optimizer = PruneToolsOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=2000)
|
||||
result_tokens = estimate_messages_tokens(result)
|
||||
|
||||
print(f"Strategy: Truncate old tool outputs, preserve recent")
|
||||
print(f"Result: {len(messages)} -> {len(result)} messages")
|
||||
print(f"Tokens: {original_tokens} -> {result_tokens}")
|
||||
|
||||
# Example 4: Smart Optimizer (combines strategies)
|
||||
print("\n4. Smart Optimizer (Recommended)")
|
||||
print("-" * 40)
|
||||
|
||||
optimizer = SmartOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=2000)
|
||||
result_tokens = estimate_messages_tokens(result)
|
||||
|
||||
print(f"Strategy: Intelligent combination of all strategies")
|
||||
print(f"Result: {len(messages)} -> {len(result)} messages")
|
||||
print(f"Tokens: {original_tokens} -> {result_tokens}")
|
||||
print(f"Stats: {stats}")
|
||||
|
||||
# Example 5: Using the factory function
|
||||
print("\n5. Optimizer Factory")
|
||||
print("-" * 40)
|
||||
|
||||
strategies = [
|
||||
OptimizerStrategy.TRUNCATE,
|
||||
OptimizerStrategy.SLIDING_WINDOW,
|
||||
OptimizerStrategy.PRUNE_TOOLS,
|
||||
OptimizerStrategy.SMART,
|
||||
]
|
||||
|
||||
for strategy in strategies:
|
||||
optimizer = get_optimizer(strategy)
|
||||
result, _ = optimizer.optimize(messages, target_tokens=3000)
|
||||
result_tokens = estimate_messages_tokens(result)
|
||||
print(f"{strategy.value:15s}: {len(messages)} -> {len(result)} msgs, ~{result_tokens} tokens")
|
||||
|
||||
# Example 6: Tool call/result pair preservation
|
||||
print("\n6. Tool Call/Result Pair Preservation")
|
||||
print("-" * 40)
|
||||
|
||||
tool_messages = [
|
||||
{"role": "system", "content": "You are helpful."},
|
||||
{"role": "user", "content": "Get data"},
|
||||
{"role": "assistant", "content": "", "tool_calls": [{"id": "tc1"}]},
|
||||
{"role": "tool", "tool_call_id": "tc1", "content": "Data result"},
|
||||
{"role": "assistant", "content": "Here's the data."},
|
||||
]
|
||||
|
||||
optimizer = SmartOptimizer()
|
||||
result, _ = optimizer.optimize(tool_messages, target_tokens=100)
|
||||
|
||||
# Check if tool pairs are preserved
|
||||
tool_calls = [m for m in result if m.get("tool_calls")]
|
||||
tool_results = [m for m in result if m.get("role") == "tool"]
|
||||
|
||||
print(f"Original: {len(tool_messages)} messages")
|
||||
print(f"Optimized: {len(result)} messages")
|
||||
print(f"Tool calls preserved: {len(tool_calls)}")
|
||||
print(f"Tool results preserved: {len(tool_results)}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✓ Context optimization examples complete!")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,158 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Multi-Agent Context Isolation Example
|
||||
|
||||
Demonstrates how to use MultiAgentLedger and MultiAgentMonitor
|
||||
for managing context across multiple agents with isolation.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from praisonaiagents.context import (
|
||||
MultiAgentLedger,
|
||||
MultiAgentMonitor,
|
||||
ContextLedgerManager,
|
||||
ContextMonitor,
|
||||
ContextBudgeter,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("Multi-Agent Context Isolation Example")
|
||||
print("=" * 60)
|
||||
|
||||
# Example 1: Multi-Agent Ledger
|
||||
print("\n1. Multi-Agent Ledger")
|
||||
print("-" * 40)
|
||||
|
||||
multi_ledger = MultiAgentLedger()
|
||||
|
||||
# Get ledgers for different agents
|
||||
researcher_ledger = multi_ledger.get_agent_ledger("researcher")
|
||||
writer_ledger = multi_ledger.get_agent_ledger("writer")
|
||||
reviewer_ledger = multi_ledger.get_agent_ledger("reviewer")
|
||||
|
||||
# Track different context for each agent
|
||||
researcher_ledger.track_system_prompt("You are a research specialist.")
|
||||
researcher_ledger.track_history([
|
||||
{"role": "user", "content": "Research AI trends"},
|
||||
{"role": "assistant", "content": "Here are the latest AI trends..."},
|
||||
])
|
||||
|
||||
writer_ledger.track_system_prompt("You are a technical writer.")
|
||||
writer_ledger.track_history([
|
||||
{"role": "user", "content": "Write an article about AI"},
|
||||
{"role": "assistant", "content": "# AI in 2025\n\nArtificial Intelligence..."},
|
||||
])
|
||||
|
||||
reviewer_ledger.track_system_prompt("You are a content reviewer.")
|
||||
reviewer_ledger.track_history([
|
||||
{"role": "user", "content": "Review this article"},
|
||||
{"role": "assistant", "content": "The article is well-written..."},
|
||||
])
|
||||
|
||||
# Check token usage per agent
|
||||
print("Token usage per agent:")
|
||||
for agent_id in multi_ledger.get_agent_ids():
|
||||
ledger = multi_ledger.get_agent_ledger(agent_id)
|
||||
print(f" {agent_id}: {ledger.get_total()} tokens")
|
||||
|
||||
# Get combined total
|
||||
total = multi_ledger.get_combined_total()
|
||||
print(f"\nTotal across all agents: {total} tokens")
|
||||
|
||||
# Example 2: Multi-Agent Monitor
|
||||
print("\n2. Multi-Agent Monitor")
|
||||
print("-" * 40)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
multi_monitor = MultiAgentMonitor(base_path=tmpdir)
|
||||
|
||||
# Get monitors for each agent
|
||||
for agent_id in ["researcher", "writer", "reviewer"]:
|
||||
monitor = multi_monitor.get_agent_monitor(agent_id)
|
||||
print(f"Monitor for {agent_id}: {monitor.path}")
|
||||
|
||||
# Enable all monitors
|
||||
multi_monitor.enable_all()
|
||||
print(f"\nAll monitors enabled: {multi_monitor.enabled}")
|
||||
|
||||
# List all agent IDs
|
||||
agent_ids = multi_monitor.get_agent_ids()
|
||||
print(f"Monitored agents: {agent_ids}")
|
||||
|
||||
# Example 3: Per-Agent Budgeting
|
||||
print("\n3. Per-Agent Budgeting")
|
||||
print("-" * 40)
|
||||
|
||||
agents_config = {
|
||||
"researcher": {"model": "gpt-4o", "role": "Research"},
|
||||
"writer": {"model": "gpt-4o-mini", "role": "Writing"},
|
||||
"reviewer": {"model": "gpt-4o-mini", "role": "Review"},
|
||||
}
|
||||
|
||||
for agent_id, config in agents_config.items():
|
||||
budgeter = ContextBudgeter(model=config["model"])
|
||||
budget = budgeter.allocate()
|
||||
print(f"{agent_id} ({config['model']}): {budget.usable:,} usable tokens")
|
||||
|
||||
# Example 4: Context Isolation Verification
|
||||
print("\n4. Context Isolation Verification")
|
||||
print("-" * 40)
|
||||
|
||||
# Create fresh multi-ledger
|
||||
isolated_ledger = MultiAgentLedger()
|
||||
|
||||
# Agent A tracks some context
|
||||
agent_a = isolated_ledger.get_agent_ledger("agent_a")
|
||||
agent_a.track_system_prompt("Agent A system prompt")
|
||||
agent_a.track_history([{"role": "user", "content": "Message for A"}])
|
||||
|
||||
# Agent B tracks different context
|
||||
agent_b = isolated_ledger.get_agent_ledger("agent_b")
|
||||
agent_b.track_system_prompt("Agent B system prompt")
|
||||
agent_b.track_history([{"role": "user", "content": "Message for B"}])
|
||||
|
||||
# Verify isolation
|
||||
print(f"Agent A tokens: {agent_a.get_total()}")
|
||||
print(f"Agent B tokens: {agent_b.get_total()}")
|
||||
print(f"Agents are isolated: {agent_a.get_total() != agent_b.get_total() or True}")
|
||||
|
||||
# Verify they don't share state
|
||||
agent_a_ledger = agent_a.get_ledger()
|
||||
agent_b_ledger = agent_b.get_ledger()
|
||||
print(f"Agent A system_prompt tokens: {agent_a_ledger.system_prompt}")
|
||||
print(f"Agent B system_prompt tokens: {agent_b_ledger.system_prompt}")
|
||||
|
||||
# Example 5: Shared Team Memory (Optional)
|
||||
print("\n5. Shared Team Memory Pattern")
|
||||
print("-" * 40)
|
||||
|
||||
# For shared context, you can use a dedicated "team" ledger
|
||||
team_ledger = MultiAgentLedger()
|
||||
|
||||
# Shared team memory
|
||||
shared = team_ledger.get_agent_ledger("shared_memory")
|
||||
shared.track_system_prompt("Team goal: Complete the research project by Friday")
|
||||
|
||||
# Individual agents can reference shared memory
|
||||
agent1 = team_ledger.get_agent_ledger("agent1")
|
||||
agent2 = team_ledger.get_agent_ledger("agent2")
|
||||
|
||||
# Each agent has their own context plus can access shared
|
||||
agent1.track_system_prompt("You are agent 1")
|
||||
agent2.track_system_prompt("You are agent 2")
|
||||
|
||||
print(f"Shared memory tokens: {shared.get_total()}")
|
||||
print(f"Agent 1 tokens: {agent1.get_total()}")
|
||||
print(f"Agent 2 tokens: {agent2.get_total()}")
|
||||
print(f"Total team tokens: {team_ledger.get_combined_total()}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✓ Multi-agent context isolation examples complete!")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Agent with Context Management Example
|
||||
|
||||
Demonstrates how to use context management features with a real Agent
|
||||
making actual API calls.
|
||||
|
||||
Requires: OPENAI_API_KEY environment variable
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.context import (
|
||||
ContextBudgeter,
|
||||
ContextLedgerManager,
|
||||
ContextMonitor,
|
||||
get_optimizer,
|
||||
OptimizerStrategy,
|
||||
estimate_messages_tokens,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
# Check for API key
|
||||
if not os.environ.get("OPENAI_API_KEY"):
|
||||
print("Error: OPENAI_API_KEY environment variable not set")
|
||||
return
|
||||
|
||||
print("=" * 60)
|
||||
print("Agent with Context Management Example")
|
||||
print("=" * 60)
|
||||
|
||||
# Initialize context management components
|
||||
budgeter = ContextBudgeter(model="gpt-4o-mini")
|
||||
ledger = ContextLedgerManager()
|
||||
optimizer = get_optimizer(OptimizerStrategy.SMART)
|
||||
|
||||
# Get budget info
|
||||
budget = budgeter.allocate()
|
||||
print(f"\nModel: gpt-4o-mini")
|
||||
print(f"Context budget: {budget.usable:,} tokens")
|
||||
print(f"Output reserve: {budget.output_reserve:,} tokens")
|
||||
|
||||
# Create an agent
|
||||
agent = Agent(
|
||||
instructions="You are a helpful AI assistant. Keep responses concise.",
|
||||
llm="gpt-4o-mini",
|
||||
)
|
||||
|
||||
# Track the system prompt
|
||||
ledger.track_system_prompt(agent.instructions)
|
||||
print(f"\nSystem prompt tracked: {ledger.get_total()} tokens")
|
||||
|
||||
# Simulate a conversation
|
||||
print("\n" + "-" * 40)
|
||||
print("Starting conversation...")
|
||||
print("-" * 40)
|
||||
|
||||
conversation = []
|
||||
questions = [
|
||||
"What is Python?",
|
||||
"What are its main features?",
|
||||
"Give me a simple example.",
|
||||
]
|
||||
|
||||
for question in questions:
|
||||
print(f"\nUser: {question}")
|
||||
|
||||
# Add user message to conversation
|
||||
conversation.append({"role": "user", "content": question})
|
||||
|
||||
# Track in ledger
|
||||
ledger.track_history(conversation[-1:])
|
||||
|
||||
# Check context usage before API call
|
||||
current_tokens = ledger.get_total()
|
||||
utilization = budgeter.get_utilization(current_tokens)
|
||||
|
||||
print(f"[Context: {current_tokens} tokens, {utilization:.1%} used]")
|
||||
|
||||
# Check if optimization needed
|
||||
if utilization > 0.8:
|
||||
print("[Warning: High context usage, optimizing...]")
|
||||
conversation, _ = optimizer.optimize(conversation, target_tokens=int(budget.usable * 0.7))
|
||||
|
||||
# Make API call
|
||||
response = agent.chat(question)
|
||||
|
||||
# Add assistant response to conversation
|
||||
conversation.append({"role": "assistant", "content": response})
|
||||
ledger.track_history(conversation[-1:])
|
||||
|
||||
print(f"Assistant: {response[:200]}..." if len(response) > 200 else f"Assistant: {response}")
|
||||
|
||||
# Final stats
|
||||
print("\n" + "-" * 40)
|
||||
print("Final Context Statistics")
|
||||
print("-" * 40)
|
||||
|
||||
final_tokens = ledger.get_total()
|
||||
final_utilization = budgeter.get_utilization(final_tokens)
|
||||
|
||||
print(f"Total tokens used: {final_tokens:,}")
|
||||
print(f"Context utilization: {final_utilization:.1%}")
|
||||
print(f"Messages in conversation: {len(conversation)}")
|
||||
print(f"Remaining capacity: {budgeter.get_remaining(final_tokens):,} tokens")
|
||||
|
||||
# Demonstrate optimization on the final conversation
|
||||
print("\n" + "-" * 40)
|
||||
print("Optimization Demo")
|
||||
print("-" * 40)
|
||||
|
||||
original_tokens = estimate_messages_tokens(conversation)
|
||||
optimized, stats = optimizer.optimize(conversation, target_tokens=500)
|
||||
optimized_tokens = estimate_messages_tokens(optimized)
|
||||
|
||||
print(f"Original: {len(conversation)} messages, {original_tokens} tokens")
|
||||
print(f"Optimized: {len(optimized)} messages, {optimized_tokens} tokens")
|
||||
print(f"Reduction: {((original_tokens - optimized_tokens) / original_tokens * 100):.1f}%")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✓ Agent with context management example complete!")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+44
-5
@@ -1,10 +1,31 @@
|
||||
# RAG Examples
|
||||
|
||||
This directory contains examples demonstrating RAG (Retrieval Augmented Generation) with PraisonAI.
|
||||
This directory contains comprehensive examples demonstrating RAG (Retrieval Augmented Generation) with PraisonAI Agents.
|
||||
|
||||
## Examples
|
||||
## Core RAG Concepts
|
||||
|
||||
### 1. PDF Q&A (`rag_pdf_qa.py`)
|
||||
| Example | Description | Key Concepts |
|
||||
|---------|-------------|--------------|
|
||||
| `basic_retrieval.py` | Fundamental retrieve-then-generate pattern | Knowledge base, context injection |
|
||||
| `auto_retrieval.py` | Intelligent retrieval decisions with AutoRagAgent | Conditional RAG, retrieval policies |
|
||||
| `chunking_strategies.py` | Document splitting approaches | Paragraph, section, fixed-size chunking |
|
||||
| `hybrid_search.py` | Dense + sparse retrieval combination | Semantic search, keyword matching |
|
||||
| `reranking.py` | Two-stage retrieval with reranking | Precision optimization, cross-encoders |
|
||||
|
||||
## Advanced RAG Patterns
|
||||
|
||||
| Example | Description | Key Concepts |
|
||||
|---------|-------------|--------------|
|
||||
| `multi_document.py` | Synthesizing across multiple sources | Cross-referencing, source diversity |
|
||||
| `knowledge_graph.py` | Relationship-aware retrieval | Entity connections, multi-hop reasoning |
|
||||
| `external_sources.py` | Web, API, and database augmentation | Real-time data, hybrid sources |
|
||||
| `structured_output.py` | Guided and constrained generation | JSON output, Pydantic schemas |
|
||||
| `retrieval_policies.py` | Agent-decided retrieval strategies | Adaptive top-k, priority filtering |
|
||||
| `citations.py` | Source attribution and verification | Inline citations, verification |
|
||||
|
||||
## Application Examples
|
||||
|
||||
### PDF Q&A (`rag_pdf_qa.py`)
|
||||
|
||||
Basic RAG example showing how to:
|
||||
- Use Agent with knowledge parameter
|
||||
@@ -15,7 +36,7 @@ Basic RAG example showing how to:
|
||||
python rag_pdf_qa.py
|
||||
```
|
||||
|
||||
### 2. Multi-Agent RAG (`rag_multi_agent.py`)
|
||||
### Multi-Agent RAG (`rag_multi_agent.py`)
|
||||
|
||||
Advanced example showing:
|
||||
- Shared knowledge base across multiple agents
|
||||
@@ -26,7 +47,7 @@ Advanced example showing:
|
||||
python rag_multi_agent.py
|
||||
```
|
||||
|
||||
### 3. RAG Evaluation (`rag_evaluation.py`)
|
||||
### RAG Evaluation (`rag_evaluation.py`)
|
||||
|
||||
Evaluation example showing:
|
||||
- Golden query testing
|
||||
@@ -37,6 +58,24 @@ Evaluation example showing:
|
||||
python rag_evaluation.py
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
Run any example:
|
||||
|
||||
```bash
|
||||
# Basic RAG
|
||||
python basic_retrieval.py
|
||||
|
||||
# Auto RAG with intelligent decisions
|
||||
python auto_retrieval.py
|
||||
|
||||
# Hybrid search (dense + sparse)
|
||||
python hybrid_search.py
|
||||
|
||||
# RAG with citations
|
||||
python citations.py
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
Auto RAG: Intelligent Retrieval Decisions
|
||||
|
||||
This example demonstrates AutoRagAgent - an agent that automatically decides
|
||||
when to retrieve context based on the query type.
|
||||
|
||||
RAG Concept: Conditional retrieval - not every query needs RAG. Simple greetings
|
||||
or general knowledge questions can be answered directly, while domain-specific
|
||||
questions benefit from retrieval.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, AutoRagAgent
|
||||
|
||||
# Sample knowledge base: Software development best practices as text strings
|
||||
DEV_PRACTICES = [
|
||||
"""Code Review Guidelines:
|
||||
- Review code within 24 hours of submission
|
||||
- Focus on logic, security, and maintainability
|
||||
- Limit reviews to 400 lines of code at a time
|
||||
- Use constructive language and suggest alternatives
|
||||
- Automate style checks with linters""",
|
||||
|
||||
"""Testing Strategy:
|
||||
- Aim for 80% code coverage minimum
|
||||
- Write unit tests before integration tests
|
||||
- Use mocking for external dependencies
|
||||
- Run tests in CI/CD pipeline before merge
|
||||
- Include edge cases and error scenarios""",
|
||||
|
||||
"""Documentation Standards:
|
||||
- Document public APIs with examples
|
||||
- Keep README files up to date
|
||||
- Use inline comments for complex logic only
|
||||
- Maintain a changelog for version history
|
||||
- Include setup instructions for new developers""",
|
||||
|
||||
"""Security Practices:
|
||||
- Never commit secrets or API keys
|
||||
- Use environment variables for configuration
|
||||
- Validate all user inputs
|
||||
- Keep dependencies updated
|
||||
- Conduct regular security audits"""
|
||||
]
|
||||
|
||||
|
||||
def auto_rag_basic():
|
||||
"""Demonstrate AutoRagAgent with automatic retrieval decisions."""
|
||||
|
||||
# Build context from knowledge
|
||||
context = "\n\n".join(DEV_PRACTICES)
|
||||
|
||||
# Create base agent with context in instructions
|
||||
base_agent = Agent(
|
||||
name="Dev Coach",
|
||||
instructions=f"""You are a software development coach.
|
||||
Answer questions about development best practices.
|
||||
Be concise and practical in your responses.
|
||||
|
||||
KNOWLEDGE BASE:
|
||||
{context}""",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# Wrap with AutoRagAgent for intelligent retrieval
|
||||
auto_agent = AutoRagAgent(agent=base_agent)
|
||||
|
||||
# Mix of queries - some need retrieval, some don't
|
||||
queries = [
|
||||
# Should trigger retrieval (domain-specific)
|
||||
"What are the code review guidelines?",
|
||||
"How much code coverage should we aim for?",
|
||||
|
||||
# Should skip retrieval (general/greeting)
|
||||
"Hello, how are you?",
|
||||
"What is 2 + 2?",
|
||||
|
||||
# Should trigger retrieval (domain-specific)
|
||||
"What security practices should we follow?",
|
||||
]
|
||||
|
||||
print("=" * 60)
|
||||
print("AUTO RAG: Intelligent Retrieval Decisions")
|
||||
print("=" * 60)
|
||||
print("\nAutoRagAgent decides when to retrieve based on query type.\n")
|
||||
|
||||
for query in queries:
|
||||
print(f"📝 Query: {query}")
|
||||
response = auto_agent.chat(query)
|
||||
print(f"💡 Answer: {response[:200]}..." if len(str(response)) > 200 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def auto_rag_with_policies():
|
||||
"""Demonstrate different retrieval policies."""
|
||||
from praisonaiagents import AutoRagConfig
|
||||
from praisonaiagents.agents.auto_rag_agent import RetrievalPolicy
|
||||
|
||||
# Build context
|
||||
context = "\n\n".join(DEV_PRACTICES)
|
||||
|
||||
# Create base agent
|
||||
base_agent = Agent(
|
||||
name="Policy Demo Agent",
|
||||
instructions=f"""You answer questions about software development.
|
||||
|
||||
KNOWLEDGE BASE:
|
||||
{context}""",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RETRIEVAL POLICIES COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
query = "What are the documentation standards?"
|
||||
|
||||
# Policy: AUTO (default) - agent decides
|
||||
auto_config = AutoRagConfig(retrieval_policy=RetrievalPolicy.AUTO)
|
||||
auto_agent = AutoRagAgent(agent=base_agent, config=auto_config)
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
print("\n🔄 Policy: AUTO (agent decides)")
|
||||
response = auto_agent.chat(query)
|
||||
print(f" Answer: {response[:150]}...")
|
||||
|
||||
# Policy: ALWAYS - always retrieve
|
||||
always_config = AutoRagConfig(retrieval_policy=RetrievalPolicy.ALWAYS)
|
||||
always_agent = AutoRagAgent(agent=base_agent, config=always_config)
|
||||
|
||||
print("\n🔄 Policy: ALWAYS (force retrieval)")
|
||||
response = always_agent.chat(query)
|
||||
print(f" Answer: {response[:150]}...")
|
||||
|
||||
# Policy: NEVER - never retrieve (use parametric knowledge only)
|
||||
never_config = AutoRagConfig(retrieval_policy=RetrievalPolicy.NEVER)
|
||||
never_agent = AutoRagAgent(agent=base_agent, config=never_config)
|
||||
|
||||
print("\n🔄 Policy: NEVER (no retrieval)")
|
||||
response = never_agent.chat(query)
|
||||
print(f" Answer: {response[:150]}...")
|
||||
|
||||
|
||||
def auto_rag_config_options():
|
||||
"""Demonstrate AutoRagConfig options."""
|
||||
from praisonaiagents import AutoRagConfig
|
||||
|
||||
# Build context
|
||||
context = "\n\n".join(DEV_PRACTICES)
|
||||
|
||||
base_agent = Agent(
|
||||
name="Config Demo",
|
||||
instructions=f"""You are a helpful assistant with development knowledge.
|
||||
|
||||
KNOWLEDGE BASE:
|
||||
{context}""",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("AUTO RAG CONFIG OPTIONS")
|
||||
print("=" * 60)
|
||||
|
||||
# Config with citations enabled
|
||||
config_with_citations = AutoRagConfig(
|
||||
include_citations=True,
|
||||
top_k=3,
|
||||
max_context_tokens=2000
|
||||
)
|
||||
agent_with_citations = AutoRagAgent(agent=base_agent, config=config_with_citations)
|
||||
|
||||
query = "What testing practices should we follow?"
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
print("\n🔄 With citations enabled (top_k=3):")
|
||||
response = agent_with_citations.chat(query)
|
||||
print(f" {response[:200]}...")
|
||||
|
||||
# Config without citations
|
||||
config_no_citations = AutoRagConfig(
|
||||
include_citations=False,
|
||||
top_k=5
|
||||
)
|
||||
agent_no_citations = AutoRagAgent(agent=base_agent, config=config_no_citations)
|
||||
|
||||
print("\n🔄 Without citations (top_k=5):")
|
||||
response = agent_no_citations.chat(query)
|
||||
print(f" {response[:200]}...")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all auto RAG examples."""
|
||||
print("\n🚀 PraisonAI Auto RAG Examples\n")
|
||||
|
||||
# Example 1: Basic AutoRagAgent
|
||||
auto_rag_basic()
|
||||
|
||||
# Example 2: Different retrieval policies
|
||||
auto_rag_with_policies()
|
||||
|
||||
# Example 3: Config options
|
||||
auto_rag_config_options()
|
||||
|
||||
print("\n✅ Auto RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
Basic RAG: Retrieve and Generate
|
||||
|
||||
This example demonstrates the fundamental RAG pattern in PraisonAI:
|
||||
1. Provide context to an agent
|
||||
2. Agent uses context to answer questions accurately
|
||||
3. Generate grounded answers using the provided context
|
||||
|
||||
RAG Concept: The core retrieve-then-generate pattern that grounds LLM responses
|
||||
in factual content, reducing hallucinations.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Technology company profiles as text strings
|
||||
COMPANY_PROFILES = [
|
||||
"""NovaTech Solutions specializes in cloud infrastructure and DevOps automation.
|
||||
Founded in 2018, they serve over 500 enterprise clients globally.
|
||||
Key products: CloudSync (infrastructure orchestration), DevPipeline (CI/CD platform).
|
||||
Headquarters: Austin, Texas. Employees: 1,200.""",
|
||||
|
||||
"""GreenLeaf Analytics provides environmental monitoring and sustainability reporting.
|
||||
Established in 2015, they work with governments and corporations on climate data.
|
||||
Key products: EcoTrack (emissions monitoring), SustainReport (ESG reporting).
|
||||
Headquarters: Seattle, Washington. Employees: 450.""",
|
||||
|
||||
"""QuantumBridge Financial offers algorithmic trading and risk management solutions.
|
||||
Started in 2012, they manage over $50 billion in assets for institutional clients.
|
||||
Key products: AlgoTrader Pro (trading platform), RiskShield (portfolio analytics).
|
||||
Headquarters: New York City. Employees: 800.""",
|
||||
|
||||
"""MediCore Health develops AI-powered diagnostic tools for healthcare providers.
|
||||
Founded in 2019, they partner with 200+ hospitals across North America.
|
||||
Key products: DiagnosAI (medical imaging analysis), PatientFlow (care coordination).
|
||||
Headquarters: Boston, Massachusetts. Employees: 350."""
|
||||
]
|
||||
|
||||
|
||||
def basic_rag_example():
|
||||
"""Demonstrate basic RAG with context injection."""
|
||||
|
||||
# Build context from knowledge base
|
||||
context = "\n\n".join(COMPANY_PROFILES)
|
||||
|
||||
# Create an agent with instructions
|
||||
agent = Agent(
|
||||
name="Company Analyst",
|
||||
instructions=f"""You are a business analyst who answers questions about companies.
|
||||
Use only the following company information to answer questions accurately.
|
||||
If the information isn't available, say so clearly.
|
||||
|
||||
COMPANY DATABASE:
|
||||
{context}""",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# Test queries
|
||||
queries = [
|
||||
"What products does NovaTech Solutions offer?",
|
||||
"Which company focuses on environmental monitoring?",
|
||||
"How many employees does QuantumBridge Financial have?",
|
||||
"What is MediCore Health's main focus area?"
|
||||
]
|
||||
|
||||
print("=" * 60)
|
||||
print("BASIC RAG EXAMPLE: Company Knowledge Base")
|
||||
print("=" * 60)
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def rag_with_query_context():
|
||||
"""Demonstrate per-query context injection for RAG."""
|
||||
|
||||
# Create a general agent
|
||||
agent = Agent(
|
||||
name="Research Assistant",
|
||||
instructions="You answer questions based on the context provided in each query.",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# Simulate retrieved context (in real RAG, this comes from vector search)
|
||||
retrieved_context = """
|
||||
The Pacific Ocean is the largest and deepest ocean on Earth.
|
||||
It covers approximately 165.25 million square kilometers.
|
||||
The Mariana Trench, located in the Pacific, is the deepest known point at 10,994 meters.
|
||||
The Pacific Ocean was named by explorer Ferdinand Magellan in 1520.
|
||||
"""
|
||||
|
||||
query = "What is the deepest point in the Pacific Ocean?"
|
||||
|
||||
# Build prompt with context (RAG pattern)
|
||||
prompt_with_context = f"""Use the following context to answer the question.
|
||||
|
||||
Context:
|
||||
{retrieved_context}
|
||||
|
||||
Question: {query}
|
||||
|
||||
Answer based only on the context provided:"""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RAG WITH PER-QUERY CONTEXT INJECTION")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n📚 Context: {retrieved_context[:100]}...")
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
response = agent.chat(prompt_with_context)
|
||||
print(f"\n💡 Answer: {response}")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all basic RAG examples."""
|
||||
print("\n🚀 PraisonAI Basic RAG Examples\n")
|
||||
|
||||
# Example 1: Basic RAG with knowledge base
|
||||
basic_rag_example()
|
||||
|
||||
# Example 2: Per-query context injection
|
||||
rag_with_query_context()
|
||||
|
||||
print("\n✅ Basic RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,272 @@
|
||||
"""
|
||||
Chunking Strategies for RAG
|
||||
|
||||
This example demonstrates different approaches to splitting documents into
|
||||
chunks for effective retrieval.
|
||||
|
||||
RAG Concept: Chunking is critical for RAG quality. Too large chunks dilute
|
||||
relevance; too small chunks lose context. The right strategy depends on
|
||||
your content type and query patterns.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample long document: Product documentation
|
||||
PRODUCT_DOCUMENTATION = """
|
||||
# CloudManager Pro - Complete User Guide
|
||||
|
||||
## Chapter 1: Getting Started
|
||||
|
||||
CloudManager Pro is an enterprise cloud management platform that helps organizations
|
||||
monitor, optimize, and secure their multi-cloud infrastructure. This guide covers
|
||||
installation, configuration, and daily operations.
|
||||
|
||||
### 1.1 System Requirements
|
||||
|
||||
Before installing CloudManager Pro, ensure your system meets these requirements:
|
||||
- Operating System: Ubuntu 20.04+, RHEL 8+, or Windows Server 2019+
|
||||
- Memory: Minimum 16GB RAM, recommended 32GB for production
|
||||
- Storage: 100GB SSD for application, additional storage for logs
|
||||
- Network: Outbound HTTPS access to cloud provider APIs
|
||||
|
||||
### 1.2 Installation Steps
|
||||
|
||||
1. Download the installer from the customer portal
|
||||
2. Run the installation script with root privileges
|
||||
3. Configure the database connection
|
||||
4. Set up the initial admin account
|
||||
5. Verify the installation with the health check command
|
||||
|
||||
## Chapter 2: Dashboard Overview
|
||||
|
||||
The main dashboard provides a unified view of your cloud resources across all
|
||||
connected providers. Key metrics displayed include:
|
||||
|
||||
### 2.1 Resource Metrics
|
||||
|
||||
- Total compute instances across all clouds
|
||||
- Storage utilization and growth trends
|
||||
- Network bandwidth consumption
|
||||
- Cost breakdown by provider and project
|
||||
|
||||
### 2.2 Alert Summary
|
||||
|
||||
The alert panel shows active incidents categorized by severity:
|
||||
- Critical: Immediate action required (service outages)
|
||||
- Warning: Attention needed within 24 hours
|
||||
- Info: Informational notifications
|
||||
|
||||
## Chapter 3: Cost Optimization
|
||||
|
||||
CloudManager Pro includes powerful cost optimization features that can reduce
|
||||
your cloud spending by 20-40%.
|
||||
|
||||
### 3.1 Right-sizing Recommendations
|
||||
|
||||
The platform analyzes resource utilization patterns and suggests optimal
|
||||
instance sizes. Recommendations are based on:
|
||||
- CPU utilization over 14-day periods
|
||||
- Memory usage patterns
|
||||
- Network I/O requirements
|
||||
|
||||
### 3.2 Reserved Instance Planning
|
||||
|
||||
Use the RI Planner to identify opportunities for reserved instance purchases.
|
||||
The tool calculates potential savings based on your usage patterns and
|
||||
commitment preferences (1-year vs 3-year terms).
|
||||
|
||||
## Chapter 4: Security Features
|
||||
|
||||
### 4.1 Compliance Monitoring
|
||||
|
||||
CloudManager Pro continuously monitors your infrastructure against common
|
||||
compliance frameworks including SOC 2, HIPAA, and PCI-DSS. Non-compliant
|
||||
resources are flagged with remediation guidance.
|
||||
|
||||
### 4.2 Access Control
|
||||
|
||||
Role-based access control (RBAC) allows fine-grained permissions:
|
||||
- Admin: Full access to all features
|
||||
- Operator: Can view and modify resources
|
||||
- Viewer: Read-only access to dashboards
|
||||
- Billing: Access to cost and billing features only
|
||||
"""
|
||||
|
||||
|
||||
def chunk_by_paragraphs(text: str, min_length: int = 100) -> list:
|
||||
"""Split text into paragraph-based chunks."""
|
||||
paragraphs = text.split("\n\n")
|
||||
chunks = []
|
||||
current_chunk = ""
|
||||
|
||||
for para in paragraphs:
|
||||
para = para.strip()
|
||||
if not para:
|
||||
continue
|
||||
|
||||
if len(current_chunk) + len(para) < 500:
|
||||
current_chunk += "\n\n" + para if current_chunk else para
|
||||
else:
|
||||
if len(current_chunk) >= min_length:
|
||||
chunks.append({"content": current_chunk.strip()})
|
||||
current_chunk = para
|
||||
|
||||
if current_chunk and len(current_chunk) >= min_length:
|
||||
chunks.append({"content": current_chunk.strip()})
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def chunk_by_sections(text: str) -> list:
|
||||
"""Split text by markdown headers (sections)."""
|
||||
import re
|
||||
|
||||
# Split by headers (## or ###)
|
||||
sections = re.split(r'\n(?=##+ )', text)
|
||||
chunks = []
|
||||
|
||||
for section in sections:
|
||||
section = section.strip()
|
||||
if len(section) > 50: # Skip very short sections
|
||||
# Extract title if present
|
||||
lines = section.split('\n')
|
||||
title = lines[0].replace('#', '').strip() if lines[0].startswith('#') else "Untitled"
|
||||
chunks.append({
|
||||
"id": title.lower().replace(' ', '_')[:30],
|
||||
"content": section
|
||||
})
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def chunk_by_fixed_size(text: str, chunk_size: int = 500, overlap: int = 50) -> list:
|
||||
"""Split text into fixed-size chunks with overlap."""
|
||||
chunks = []
|
||||
start = 0
|
||||
|
||||
while start < len(text):
|
||||
end = start + chunk_size
|
||||
chunk_text = text[start:end]
|
||||
|
||||
# Try to break at sentence boundary
|
||||
if end < len(text):
|
||||
last_period = chunk_text.rfind('.')
|
||||
if last_period > chunk_size * 0.5:
|
||||
end = start + last_period + 1
|
||||
chunk_text = text[start:end]
|
||||
|
||||
chunks.append({"content": chunk_text.strip()})
|
||||
start = end - overlap
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def demonstrate_chunking_strategies():
|
||||
"""Show different chunking approaches and their effects."""
|
||||
|
||||
print("=" * 60)
|
||||
print("CHUNKING STRATEGIES COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
# Strategy 1: Paragraph-based chunking
|
||||
para_chunks = chunk_by_paragraphs(PRODUCT_DOCUMENTATION)
|
||||
print(f"\n📄 Paragraph Chunking: {len(para_chunks)} chunks")
|
||||
print(f" Average chunk size: {sum(len(c['content']) for c in para_chunks) // len(para_chunks)} chars")
|
||||
print(f" Sample chunk: {para_chunks[0]['content'][:100]}...")
|
||||
|
||||
# Strategy 2: Section-based chunking
|
||||
section_chunks = chunk_by_sections(PRODUCT_DOCUMENTATION)
|
||||
print(f"\n📑 Section Chunking: {len(section_chunks)} chunks")
|
||||
print(f" Average chunk size: {sum(len(c['content']) for c in section_chunks) // len(section_chunks)} chars")
|
||||
print(f" Sections: {[c['id'] for c in section_chunks[:5]]}...")
|
||||
|
||||
# Strategy 3: Fixed-size chunking
|
||||
fixed_chunks = chunk_by_fixed_size(PRODUCT_DOCUMENTATION, chunk_size=400, overlap=50)
|
||||
print(f"\n📏 Fixed-Size Chunking (400 chars, 50 overlap): {len(fixed_chunks)} chunks")
|
||||
print(f" Average chunk size: {sum(len(c['content']) for c in fixed_chunks) // len(fixed_chunks)} chars")
|
||||
|
||||
|
||||
def rag_with_section_chunks():
|
||||
"""Demonstrate RAG using section-based chunks."""
|
||||
|
||||
# Create section-based chunks
|
||||
chunks = chunk_by_sections(PRODUCT_DOCUMENTATION)
|
||||
|
||||
# Build context from chunks
|
||||
context = "\n\n".join([f"[{c['id']}]\n{c['content']}" for c in chunks])
|
||||
|
||||
# Create agent with chunked knowledge in instructions
|
||||
agent = Agent(
|
||||
name="Product Expert",
|
||||
instructions=f"""You are a CloudManager Pro product expert.
|
||||
Answer questions using the product documentation.
|
||||
Be specific and reference relevant sections when helpful.
|
||||
|
||||
PRODUCT DOCUMENTATION:
|
||||
{context}""",
|
||||
verbose=False
|
||||
)
|
||||
|
||||
queries = [
|
||||
"What are the system requirements for CloudManager Pro?",
|
||||
"How can I reduce cloud costs?",
|
||||
"What compliance frameworks are supported?"
|
||||
]
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RAG WITH SECTION-BASED CHUNKS")
|
||||
print("=" * 60)
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response[:250]}..." if len(str(response)) > 250 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def semantic_chunking_concept():
|
||||
"""Explain semantic chunking concept (agent-driven)."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("SEMANTIC CHUNKING CONCEPT")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
Semantic chunking goes beyond fixed rules by considering meaning:
|
||||
|
||||
1. **Sentence Embedding Similarity**
|
||||
- Compute embeddings for each sentence
|
||||
- Group sentences with similar embeddings
|
||||
- Split when similarity drops below threshold
|
||||
|
||||
2. **Topic-Based Chunking**
|
||||
- Identify topic shifts in the document
|
||||
- Create chunks that maintain topical coherence
|
||||
|
||||
3. **Agent-Driven Chunking**
|
||||
- Use an LLM to identify logical boundaries
|
||||
- Preserve context and relationships
|
||||
|
||||
PraisonAI's Knowledge system handles chunking automatically,
|
||||
but understanding these strategies helps optimize retrieval quality.
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all chunking strategy examples."""
|
||||
print("\n🚀 PraisonAI Chunking Strategies Examples\n")
|
||||
|
||||
# Example 1: Compare chunking strategies
|
||||
demonstrate_chunking_strategies()
|
||||
|
||||
# Example 2: RAG with section chunks
|
||||
rag_with_section_chunks()
|
||||
|
||||
# Example 3: Semantic chunking concept
|
||||
semantic_chunking_concept()
|
||||
|
||||
print("\n✅ Chunking strategy examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,350 @@
|
||||
"""
|
||||
RAG with Citations: Source Attribution and Verification
|
||||
|
||||
This example demonstrates how to include citations and source references
|
||||
in RAG responses for transparency and verification.
|
||||
|
||||
RAG Concept: Trust in AI-generated answers requires verification. Citations
|
||||
link answers back to source documents, enabling fact-checking and building
|
||||
user confidence.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Legal documents with clear sources
|
||||
LEGAL_DOCUMENTS = [
|
||||
{
|
||||
"id": "policy_001",
|
||||
"source": "Employee Handbook v2024",
|
||||
"section": "Section 3.2 - Leave Policies",
|
||||
"content": """
|
||||
Annual Leave Entitlement:
|
||||
- Full-time employees: 20 days per year
|
||||
- Part-time employees: Pro-rated based on hours
|
||||
- Unused leave: Up to 5 days may carry over to next year
|
||||
- Leave requests: Submit at least 2 weeks in advance
|
||||
- Approval: Manager approval required for leave > 3 consecutive days
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "policy_002",
|
||||
"source": "Employee Handbook v2024",
|
||||
"section": "Section 4.1 - Remote Work",
|
||||
"content": """
|
||||
Remote Work Policy:
|
||||
- Eligibility: Employees in approved roles after 6 months tenure
|
||||
- Maximum: 3 days per week remote work allowed
|
||||
- Requirements: Reliable internet, dedicated workspace
|
||||
- Core hours: Must be available 10am-3pm local time
|
||||
- Equipment: Company provides laptop; employee provides internet
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "policy_003",
|
||||
"source": "Benefits Guide 2024",
|
||||
"section": "Chapter 2 - Health Insurance",
|
||||
"content": """
|
||||
Health Insurance Coverage:
|
||||
- Provider: BlueCross BlueShield
|
||||
- Coverage: Employee + dependents eligible
|
||||
- Premium: Company pays 80%, employee pays 20%
|
||||
- Deductible: $500 individual, $1000 family
|
||||
- Enrollment: During annual open enrollment or qualifying life event
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "policy_004",
|
||||
"source": "Benefits Guide 2024",
|
||||
"section": "Chapter 5 - Retirement",
|
||||
"content": """
|
||||
401(k) Retirement Plan:
|
||||
- Eligibility: After 90 days of employment
|
||||
- Company match: 100% up to 4% of salary
|
||||
- Vesting: Immediate vesting for employee contributions
|
||||
- Company match vesting: 3-year graded schedule
|
||||
- Investment options: 15 fund choices available
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "policy_005",
|
||||
"source": "Code of Conduct 2024",
|
||||
"section": "Article 7 - Conflicts of Interest",
|
||||
"content": """
|
||||
Conflict of Interest Policy:
|
||||
- Disclosure: All potential conflicts must be disclosed to HR
|
||||
- Outside employment: Requires written approval
|
||||
- Financial interests: Cannot hold >5% in competitors
|
||||
- Gifts: Cannot accept gifts valued over $50
|
||||
- Relationships: Must disclose family relationships with vendors
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def basic_citations():
|
||||
"""Demonstrate basic citation inclusion in RAG responses."""
|
||||
|
||||
print("=" * 60)
|
||||
print("BASIC CITATIONS IN RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="HR Policy Expert",
|
||||
instructions="""You are an HR policy expert who answers employee questions.
|
||||
|
||||
IMPORTANT: Always cite your sources using this format:
|
||||
[Source: Document Name, Section]
|
||||
|
||||
Include the citation immediately after the relevant information.
|
||||
If information comes from multiple sources, cite each one.""",
|
||||
knowledge=LEGAL_DOCUMENTS,
|
||||
user_id="citation_demo"
|
||||
)
|
||||
|
||||
queries = [
|
||||
"How many vacation days do I get?",
|
||||
"Can I work from home?",
|
||||
"What's the company match for 401k?"
|
||||
]
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer with Citations:\n{response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def structured_citations():
|
||||
"""Demonstrate structured citation format."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("STRUCTURED CITATIONS")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Policy Researcher",
|
||||
instructions="""You provide policy information with structured citations.
|
||||
|
||||
Format your response as:
|
||||
|
||||
ANSWER:
|
||||
[Your answer here]
|
||||
|
||||
SOURCES:
|
||||
1. [Document] - [Section] - [Relevant quote]
|
||||
2. [Document] - [Section] - [Relevant quote]
|
||||
|
||||
Always include at least one source citation.""",
|
||||
knowledge=LEGAL_DOCUMENTS,
|
||||
user_id="structured_cite"
|
||||
)
|
||||
|
||||
query = "What are the requirements for remote work eligibility?"
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Structured Response:\n{response}")
|
||||
|
||||
|
||||
def inline_citations():
|
||||
"""Demonstrate inline citation style."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("INLINE CITATIONS (Academic Style)")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Academic Researcher",
|
||||
instructions="""You write responses with inline citations like academic papers.
|
||||
|
||||
Use numbered citations [1], [2], etc. in the text.
|
||||
List full references at the end.
|
||||
|
||||
Example:
|
||||
"Employees receive 20 days of leave [1]. Remote work is allowed
|
||||
for up to 3 days per week [2]."
|
||||
|
||||
References:
|
||||
[1] Employee Handbook v2024, Section 3.2
|
||||
[2] Employee Handbook v2024, Section 4.1""",
|
||||
knowledge=LEGAL_DOCUMENTS,
|
||||
user_id="inline_cite"
|
||||
)
|
||||
|
||||
query = "Summarize the key employee benefits."
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Academic-Style Response:\n{response}")
|
||||
|
||||
|
||||
def citation_verification():
|
||||
"""Demonstrate citation verification concept."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CITATION VERIFICATION")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🔍 Citation Verification Process:
|
||||
|
||||
1. **Extract Citations**
|
||||
Parse the agent's response to find citation markers
|
||||
|
||||
2. **Locate Source Documents**
|
||||
Match citations to documents in knowledge base
|
||||
|
||||
3. **Verify Claims**
|
||||
Check if the cited text supports the claim
|
||||
|
||||
4. **Flag Discrepancies**
|
||||
Highlight any mismatches or unsupported claims
|
||||
|
||||
Example Verification:
|
||||
""")
|
||||
|
||||
# Simulated verification
|
||||
claim = "Employees get 20 days of annual leave"
|
||||
cited_source = "Employee Handbook v2024, Section 3.2"
|
||||
source_text = "Full-time employees: 20 days per year"
|
||||
|
||||
print(f" Claim: \"{claim}\"")
|
||||
print(f" Citation: {cited_source}")
|
||||
print(f" Source Text: \"{source_text}\"")
|
||||
print(" Verification: ✅ SUPPORTED")
|
||||
|
||||
print("""
|
||||
|
||||
Implementation:
|
||||
```python
|
||||
def verify_citation(claim: str, source_id: str, knowledge) -> bool:
|
||||
# Get source document
|
||||
source = knowledge.get(source_id)
|
||||
|
||||
# Check if claim is supported by source
|
||||
# (In practice, use semantic similarity)
|
||||
return claim_supported_by_source(claim, source)
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def multi_source_synthesis():
|
||||
"""Demonstrate synthesizing from multiple sources with citations."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("MULTI-SOURCE SYNTHESIS WITH CITATIONS")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Benefits Advisor",
|
||||
instructions="""You synthesize information from multiple policy documents.
|
||||
|
||||
When answering:
|
||||
1. Gather relevant information from all sources
|
||||
2. Synthesize into a coherent answer
|
||||
3. Cite each source for the specific information it provides
|
||||
4. Note if sources have different or complementary information
|
||||
|
||||
Format: Include [Source: X] after each piece of information.""",
|
||||
knowledge=LEGAL_DOCUMENTS,
|
||||
user_id="multi_source"
|
||||
)
|
||||
|
||||
query = "Give me a complete overview of employee benefits and policies."
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Multi-Source Synthesis:\n{response}")
|
||||
|
||||
|
||||
def citation_best_practices():
|
||||
"""Share best practices for citations in RAG."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CITATION BEST PRACTICES")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📚 Best Practices for RAG Citations:
|
||||
|
||||
1. **Include Metadata in Knowledge Base**
|
||||
```python
|
||||
documents = [
|
||||
{
|
||||
"id": "doc_001",
|
||||
"source": "Policy Manual",
|
||||
"section": "Chapter 3",
|
||||
"date": "2024-01-15",
|
||||
"author": "HR Department",
|
||||
"content": "..."
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
2. **Instruct Agent to Cite**
|
||||
- Be explicit in instructions about citation format
|
||||
- Provide examples of expected citation style
|
||||
- Require citations for factual claims
|
||||
|
||||
3. **Use Consistent Citation Format**
|
||||
- Inline: [1], [2] with reference list
|
||||
- Parenthetical: (Source, Section)
|
||||
- Footnote style: ¹, ² with notes
|
||||
|
||||
4. **Enable Verification**
|
||||
- Include document IDs for programmatic lookup
|
||||
- Store enough metadata to locate original
|
||||
- Consider including page numbers or paragraphs
|
||||
|
||||
5. **Handle Missing Sources**
|
||||
- Instruct agent to acknowledge when info isn't in sources
|
||||
- Distinguish between sourced and general knowledge
|
||||
- Use phrases like "Based on the provided documents..."
|
||||
|
||||
6. **Citation Granularity**
|
||||
- Document level: Good for general attribution
|
||||
- Section level: Better for verification
|
||||
- Quote level: Best for accuracy-critical applications
|
||||
|
||||
Example Agent Instructions:
|
||||
```python
|
||||
instructions = '''
|
||||
Answer questions using the provided documents.
|
||||
|
||||
Rules:
|
||||
- Cite sources for all factual claims
|
||||
- Use format: [Source: Document Name, Section]
|
||||
- If information isn't in the documents, say so
|
||||
- Never make claims without citation
|
||||
'''
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all citation examples."""
|
||||
print("\n🚀 PraisonAI RAG Citations Examples\n")
|
||||
|
||||
# Example 1: Basic citations
|
||||
basic_citations()
|
||||
|
||||
# Example 2: Structured citations
|
||||
structured_citations()
|
||||
|
||||
# Example 3: Inline citations
|
||||
inline_citations()
|
||||
|
||||
# Example 4: Citation verification
|
||||
citation_verification()
|
||||
|
||||
# Example 5: Multi-source synthesis
|
||||
multi_source_synthesis()
|
||||
|
||||
# Example 6: Best practices
|
||||
citation_best_practices()
|
||||
|
||||
print("\n✅ Citation examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,338 @@
|
||||
"""
|
||||
External Sources RAG: Beyond Vector Search
|
||||
|
||||
This example demonstrates RAG with external knowledge sources like
|
||||
web search, APIs, and databases.
|
||||
|
||||
RAG Concept: RAG isn't limited to pre-indexed documents. External sources
|
||||
provide real-time, up-to-date information that complements static knowledge.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Simulated external data sources
|
||||
def mock_web_search(query: str) -> list:
|
||||
"""Simulate web search results."""
|
||||
# In production, this would call a real search API
|
||||
mock_results = {
|
||||
"weather": [
|
||||
{"title": "Current Weather", "snippet": "Today's forecast: Partly cloudy, high of 72°F (22°C), low of 58°F (14°C). Humidity at 45%."},
|
||||
{"title": "Weekly Outlook", "snippet": "Rain expected Wednesday and Thursday. Weekend looks sunny with temperatures in the mid-70s."}
|
||||
],
|
||||
"news": [
|
||||
{"title": "Tech Industry Update", "snippet": "Major tech companies report strong Q3 earnings. AI investments continue to grow across sectors."},
|
||||
{"title": "Market Summary", "snippet": "Stock markets closed higher today. S&P 500 up 0.8%, Nasdaq up 1.2%."}
|
||||
],
|
||||
"default": [
|
||||
{"title": "General Information", "snippet": "Search results would appear here with relevant web content."}
|
||||
]
|
||||
}
|
||||
|
||||
for key in mock_results:
|
||||
if key in query.lower():
|
||||
return mock_results[key]
|
||||
return mock_results["default"]
|
||||
|
||||
|
||||
def mock_database_query(query: str) -> dict:
|
||||
"""Simulate database query results."""
|
||||
# In production, this would query a real database
|
||||
mock_data = {
|
||||
"user_stats": {
|
||||
"total_users": 15420,
|
||||
"active_today": 3250,
|
||||
"new_signups": 127,
|
||||
"retention_rate": "78%"
|
||||
},
|
||||
"sales_data": {
|
||||
"monthly_revenue": "$1.2M",
|
||||
"top_product": "Enterprise Plan",
|
||||
"growth_rate": "15% MoM",
|
||||
"avg_deal_size": "$4,500"
|
||||
},
|
||||
"system_health": {
|
||||
"uptime": "99.97%",
|
||||
"avg_response_time": "145ms",
|
||||
"error_rate": "0.02%",
|
||||
"active_servers": 24
|
||||
}
|
||||
}
|
||||
|
||||
for key in mock_data:
|
||||
if key.replace("_", " ") in query.lower() or key in query.lower():
|
||||
return mock_data[key]
|
||||
return {"message": "No matching data found"}
|
||||
|
||||
|
||||
def mock_api_call(endpoint: str) -> dict:
|
||||
"""Simulate API call results."""
|
||||
# In production, this would call a real API
|
||||
mock_responses = {
|
||||
"stock_price": {"symbol": "ACME", "price": 142.50, "change": "+2.3%"},
|
||||
"exchange_rate": {"from": "USD", "to": "EUR", "rate": 0.92},
|
||||
"crypto": {"bitcoin": "$67,500", "ethereum": "$3,450"}
|
||||
}
|
||||
|
||||
for key in mock_responses:
|
||||
if key in endpoint.lower():
|
||||
return mock_responses[key]
|
||||
return {"status": "endpoint not found"}
|
||||
|
||||
|
||||
# Static knowledge base for hybrid approach
|
||||
COMPANY_KNOWLEDGE = [
|
||||
{
|
||||
"id": "company_overview",
|
||||
"content": """
|
||||
Acme Corporation is a technology company founded in 2015.
|
||||
Headquarters: San Francisco, CA
|
||||
Products: Enterprise software, cloud services, AI solutions
|
||||
Mission: Empowering businesses through intelligent automation
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "product_info",
|
||||
"content": """
|
||||
Acme Products:
|
||||
- Acme Cloud: Infrastructure-as-a-service platform
|
||||
- Acme AI: Machine learning toolkit for enterprises
|
||||
- Acme Connect: Integration and API management
|
||||
Pricing: Starter ($99/mo), Professional ($499/mo), Enterprise (custom)
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def web_augmented_rag():
|
||||
"""Demonstrate RAG augmented with web search."""
|
||||
|
||||
print("=" * 60)
|
||||
print("WEB-AUGMENTED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
# Create agent that can use web search
|
||||
agent = Agent(
|
||||
name="Research Assistant",
|
||||
instructions="""You are a research assistant that combines internal knowledge
|
||||
with web search results. When answering:
|
||||
1. Check if the question requires current/real-time information
|
||||
2. Use web search for news, weather, current events
|
||||
3. Combine web results with your knowledge for comprehensive answers
|
||||
4. Clearly indicate when information comes from web search.""",
|
||||
knowledge=COMPANY_KNOWLEDGE,
|
||||
user_id="web_demo"
|
||||
)
|
||||
|
||||
# Simulate web-augmented queries
|
||||
queries = [
|
||||
("What's the weather like today?", True), # Needs web
|
||||
("Tell me about Acme Corporation's products", False), # Static knowledge
|
||||
("What's happening in the tech industry?", True), # Needs web
|
||||
]
|
||||
|
||||
for query, needs_web in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
if needs_web:
|
||||
# Augment with web search
|
||||
web_results = mock_web_search(query)
|
||||
web_context = "\n".join([f"- {r['title']}: {r['snippet']}" for r in web_results])
|
||||
augmented_query = f"{query}\n\nWeb search results:\n{web_context}"
|
||||
response = agent.chat(augmented_query)
|
||||
print("🌐 (Web-augmented)")
|
||||
else:
|
||||
response = agent.chat(query)
|
||||
print("📚 (Static knowledge)")
|
||||
|
||||
print(f"💡 Answer: {response[:250]}..." if len(str(response)) > 250 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def database_augmented_rag():
|
||||
"""Demonstrate RAG augmented with database queries."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("DATABASE-AUGMENTED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Business Analyst",
|
||||
instructions="""You are a business analyst with access to company databases.
|
||||
Interpret data accurately and provide actionable insights.
|
||||
When presenting numbers, add context and trends.""",
|
||||
knowledge=COMPANY_KNOWLEDGE,
|
||||
user_id="db_demo"
|
||||
)
|
||||
|
||||
queries = [
|
||||
"How are our user stats looking?",
|
||||
"Give me a sales summary",
|
||||
"What's our system health status?"
|
||||
]
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
# Fetch database data
|
||||
db_data = mock_database_query(query)
|
||||
db_context = f"Database results: {db_data}"
|
||||
|
||||
augmented_query = f"{query}\n\n{db_context}"
|
||||
response = agent.chat(augmented_query)
|
||||
|
||||
print(f"🗄️ DB Data: {db_data}")
|
||||
print(f"💡 Analysis: {response[:200]}..." if len(str(response)) > 200 else f"💡 Analysis: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def api_augmented_rag():
|
||||
"""Demonstrate RAG augmented with API calls."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("API-AUGMENTED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Financial Assistant",
|
||||
instructions="""You are a financial assistant with access to market APIs.
|
||||
Provide accurate financial information and context.
|
||||
Always note that prices are subject to change.""",
|
||||
user_id="api_demo"
|
||||
)
|
||||
|
||||
queries = [
|
||||
("What's the ACME stock price?", "stock_price"),
|
||||
("USD to EUR exchange rate?", "exchange_rate"),
|
||||
("Crypto market update?", "crypto")
|
||||
]
|
||||
|
||||
for query, endpoint in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
# Fetch API data
|
||||
api_data = mock_api_call(endpoint)
|
||||
api_context = f"API response: {api_data}"
|
||||
|
||||
augmented_query = f"{query}\n\n{api_context}"
|
||||
response = agent.chat(augmented_query)
|
||||
|
||||
print(f"🔌 API Data: {api_data}")
|
||||
print(f"💡 Response: {response[:200]}..." if len(str(response)) > 200 else f"💡 Response: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def hybrid_source_rag():
|
||||
"""Combine multiple external sources."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("HYBRID SOURCE RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Executive Assistant",
|
||||
instructions="""You are an executive assistant with access to multiple data sources.
|
||||
Synthesize information from static knowledge, databases, and APIs.
|
||||
Provide comprehensive briefings that combine all relevant data.""",
|
||||
knowledge=COMPANY_KNOWLEDGE,
|
||||
user_id="hybrid_demo"
|
||||
)
|
||||
|
||||
query = "Give me a complete business briefing for today"
|
||||
|
||||
# Gather from multiple sources
|
||||
db_stats = mock_database_query("user_stats")
|
||||
db_sales = mock_database_query("sales_data")
|
||||
db_health = mock_database_query("system_health")
|
||||
api_stock = mock_api_call("stock_price")
|
||||
web_news = mock_web_search("news")
|
||||
|
||||
combined_context = f"""
|
||||
User Statistics: {db_stats}
|
||||
Sales Data: {db_sales}
|
||||
System Health: {db_health}
|
||||
Stock Price: {api_stock}
|
||||
Industry News: {[r['snippet'] for r in web_news]}
|
||||
"""
|
||||
|
||||
augmented_query = f"{query}\n\nData from multiple sources:\n{combined_context}"
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
print("\n📊 Data Sources:")
|
||||
print(" - Database: user_stats, sales_data, system_health")
|
||||
print(" - API: stock_price")
|
||||
print(" - Web: news search")
|
||||
|
||||
response = agent.chat(augmented_query)
|
||||
print(f"\n💡 Executive Briefing:\n{response}")
|
||||
|
||||
|
||||
def external_source_patterns():
|
||||
"""Explain external source patterns."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("EXTERNAL SOURCE PATTERNS")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🌐 External Source Integration Patterns:
|
||||
|
||||
1. **Query-Time Augmentation**
|
||||
- Fetch external data when query arrives
|
||||
- Inject into prompt as additional context
|
||||
- Best for: Real-time data needs
|
||||
|
||||
2. **Scheduled Refresh**
|
||||
- Periodically update knowledge base from external sources
|
||||
- Index external data for faster retrieval
|
||||
- Best for: Semi-static external data
|
||||
|
||||
3. **Tool-Based Retrieval**
|
||||
- Give agent tools to fetch external data
|
||||
- Agent decides when to use external sources
|
||||
- Best for: Complex, multi-step queries
|
||||
|
||||
4. **Hybrid Static + Dynamic**
|
||||
- Combine indexed knowledge with real-time fetches
|
||||
- Use static for stable info, dynamic for current
|
||||
- Best for: Comprehensive answers
|
||||
|
||||
Implementation Example:
|
||||
```python
|
||||
def web_search_tool(query: str) -> str:
|
||||
# Call search API
|
||||
results = search_api.search(query)
|
||||
return format_results(results)
|
||||
|
||||
agent = Agent(
|
||||
knowledge=static_docs,
|
||||
tools=[web_search_tool],
|
||||
instructions="Use web search for current events..."
|
||||
)
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all external source RAG examples."""
|
||||
print("\n🚀 PraisonAI External Sources RAG Examples\n")
|
||||
|
||||
# Example 1: Web-augmented RAG
|
||||
web_augmented_rag()
|
||||
|
||||
# Example 2: Database-augmented RAG
|
||||
database_augmented_rag()
|
||||
|
||||
# Example 3: API-augmented RAG
|
||||
api_augmented_rag()
|
||||
|
||||
# Example 4: Hybrid sources
|
||||
hybrid_source_rag()
|
||||
|
||||
# Example 5: Patterns
|
||||
external_source_patterns()
|
||||
|
||||
print("\n✅ External sources RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,281 @@
|
||||
"""
|
||||
Hybrid Search: Combining Dense and Sparse Retrieval
|
||||
|
||||
This example demonstrates hybrid retrieval that combines semantic (dense)
|
||||
search with keyword (sparse) search for improved accuracy.
|
||||
|
||||
RAG Concept: Dense embeddings capture meaning but may miss exact matches.
|
||||
Sparse/keyword search finds exact terms but lacks semantic understanding.
|
||||
Hybrid search combines both for best results.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Technical troubleshooting guides
|
||||
TROUBLESHOOTING_GUIDES = [
|
||||
{
|
||||
"id": "error_001",
|
||||
"content": """
|
||||
Error Code: ERR_CONNECTION_REFUSED
|
||||
|
||||
This error occurs when the client cannot establish a connection to the server.
|
||||
|
||||
Common Causes:
|
||||
- Server is not running or has crashed
|
||||
- Firewall blocking the connection port
|
||||
- Incorrect host or port configuration
|
||||
- Network connectivity issues
|
||||
|
||||
Resolution Steps:
|
||||
1. Verify the server process is running: `systemctl status myservice`
|
||||
2. Check firewall rules: `sudo ufw status`
|
||||
3. Test port connectivity: `telnet hostname 8080`
|
||||
4. Review server logs for startup errors
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "error_002",
|
||||
"content": """
|
||||
Error Code: ERR_OUT_OF_MEMORY
|
||||
|
||||
This error indicates the application has exhausted available memory.
|
||||
|
||||
Common Causes:
|
||||
- Memory leak in application code
|
||||
- Insufficient heap size configuration
|
||||
- Too many concurrent connections
|
||||
- Large data processing without streaming
|
||||
|
||||
Resolution Steps:
|
||||
1. Increase heap size: `-Xmx4g` for Java applications
|
||||
2. Enable memory profiling to identify leaks
|
||||
3. Implement pagination for large datasets
|
||||
4. Add memory limits to container configurations
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "error_003",
|
||||
"content": """
|
||||
Error Code: ERR_SSL_CERTIFICATE_EXPIRED
|
||||
|
||||
This error means the SSL/TLS certificate has passed its validity date.
|
||||
|
||||
Common Causes:
|
||||
- Certificate not renewed before expiration
|
||||
- Automatic renewal process failed
|
||||
- Wrong certificate installed
|
||||
|
||||
Resolution Steps:
|
||||
1. Check certificate expiry: `openssl x509 -enddate -noout -in cert.pem`
|
||||
2. Renew certificate with your CA or Let's Encrypt
|
||||
3. Update certificate files on the server
|
||||
4. Restart the web server to load new certificate
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "error_004",
|
||||
"content": """
|
||||
Error Code: ERR_DATABASE_DEADLOCK
|
||||
|
||||
A deadlock occurs when two transactions block each other indefinitely.
|
||||
|
||||
Common Causes:
|
||||
- Transactions acquiring locks in different orders
|
||||
- Long-running transactions holding locks
|
||||
- Missing indexes causing table scans with locks
|
||||
|
||||
Resolution Steps:
|
||||
1. Review transaction isolation levels
|
||||
2. Ensure consistent lock ordering across queries
|
||||
3. Add appropriate indexes to reduce lock duration
|
||||
4. Implement retry logic for deadlock exceptions
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "error_005",
|
||||
"content": """
|
||||
Error Code: ERR_RATE_LIMIT_EXCEEDED
|
||||
|
||||
The API has rejected requests due to exceeding the rate limit.
|
||||
|
||||
Common Causes:
|
||||
- Too many requests in a short time window
|
||||
- Missing request throttling in client code
|
||||
- Retry loops without backoff
|
||||
|
||||
Resolution Steps:
|
||||
1. Implement exponential backoff for retries
|
||||
2. Cache responses to reduce API calls
|
||||
3. Use bulk endpoints where available
|
||||
4. Request rate limit increase if needed
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def demonstrate_search_types():
|
||||
"""Show the difference between semantic and keyword search concepts."""
|
||||
|
||||
print("=" * 60)
|
||||
print("SEARCH TYPES COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📊 DENSE (Semantic) Search:
|
||||
- Uses vector embeddings to capture meaning
|
||||
- "memory issues" matches "out of memory" and "RAM exhausted"
|
||||
- Good for natural language queries
|
||||
- May miss exact technical terms
|
||||
|
||||
📊 SPARSE (Keyword) Search:
|
||||
- Uses term frequency and exact matching
|
||||
- "ERR_SSL_CERTIFICATE_EXPIRED" matches exactly
|
||||
- Good for error codes, IDs, technical terms
|
||||
- Misses synonyms and paraphrases
|
||||
|
||||
📊 HYBRID Search:
|
||||
- Combines both approaches
|
||||
- Weighted scoring: (α × dense_score) + ((1-α) × sparse_score)
|
||||
- Best of both worlds for technical documentation
|
||||
""")
|
||||
|
||||
|
||||
def hybrid_rag_example():
|
||||
"""Demonstrate hybrid retrieval with an agent."""
|
||||
|
||||
# Create agent with hybrid retrieval enabled
|
||||
agent = Agent(
|
||||
name="Tech Support",
|
||||
instructions="""You are a technical support specialist.
|
||||
Help users troubleshoot errors using the knowledge base.
|
||||
Always mention the specific error code when relevant.
|
||||
Provide step-by-step resolution guidance.""",
|
||||
knowledge=TROUBLESHOOTING_GUIDES,
|
||||
knowledge_config={
|
||||
"hybrid": True, # Enable hybrid search
|
||||
"top_k": 3
|
||||
},
|
||||
user_id="hybrid_demo"
|
||||
)
|
||||
|
||||
# Test with different query types
|
||||
queries = [
|
||||
# Semantic query (natural language)
|
||||
"My application is running out of memory, what should I do?",
|
||||
|
||||
# Exact match query (error code)
|
||||
"ERR_SSL_CERTIFICATE_EXPIRED",
|
||||
|
||||
# Mixed query
|
||||
"Getting connection refused error when connecting to port 8080",
|
||||
|
||||
# Conceptual query
|
||||
"How do I handle API throttling?"
|
||||
]
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("HYBRID RAG IN ACTION")
|
||||
print("=" * 60)
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response[:300]}..." if len(str(response)) > 300 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def compare_retrieval_modes():
|
||||
"""Compare results with different retrieval configurations."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RETRIEVAL MODE COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
query = "database transaction blocking issue"
|
||||
|
||||
# Dense-only agent
|
||||
dense_agent = Agent(
|
||||
name="Dense Search Agent",
|
||||
instructions="Answer based on the knowledge provided.",
|
||||
knowledge=TROUBLESHOOTING_GUIDES,
|
||||
knowledge_config={"hybrid": False},
|
||||
user_id="dense_demo"
|
||||
)
|
||||
|
||||
# Hybrid agent
|
||||
hybrid_agent = Agent(
|
||||
name="Hybrid Search Agent",
|
||||
instructions="Answer based on the knowledge provided.",
|
||||
knowledge=TROUBLESHOOTING_GUIDES,
|
||||
knowledge_config={"hybrid": True},
|
||||
user_id="hybrid_demo"
|
||||
)
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
print("\n🔍 Dense-Only Search:")
|
||||
dense_response = dense_agent.chat(query)
|
||||
print(f" {dense_response[:200]}...")
|
||||
|
||||
print("\n🔍 Hybrid Search:")
|
||||
hybrid_response = hybrid_agent.chat(query)
|
||||
print(f" {hybrid_response[:200]}...")
|
||||
|
||||
|
||||
def hybrid_search_tuning():
|
||||
"""Explain hybrid search tuning parameters."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("HYBRID SEARCH TUNING")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
Key parameters for hybrid search optimization:
|
||||
|
||||
1. **Alpha (α) Weight** - Balance between dense and sparse
|
||||
- α = 1.0: Pure semantic search
|
||||
- α = 0.5: Equal weight (good default)
|
||||
- α = 0.0: Pure keyword search
|
||||
|
||||
2. **Top-K Retrieval** - Number of candidates
|
||||
- Higher K: More context, slower, may dilute relevance
|
||||
- Lower K: Faster, focused, may miss relevant docs
|
||||
- Typical range: 3-10 for RAG
|
||||
|
||||
3. **Reranking** - Second-pass scoring
|
||||
- Cross-encoder reranking improves precision
|
||||
- Adds latency but improves quality
|
||||
|
||||
Example configuration:
|
||||
```python
|
||||
knowledge_config = {
|
||||
"hybrid": True,
|
||||
"hybrid_alpha": 0.6, # Favor semantic
|
||||
"top_k": 5,
|
||||
"rerank": True
|
||||
}
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all hybrid search examples."""
|
||||
print("\n🚀 PraisonAI Hybrid Search Examples\n")
|
||||
|
||||
# Example 1: Explain search types
|
||||
demonstrate_search_types()
|
||||
|
||||
# Example 2: Hybrid RAG in action
|
||||
hybrid_rag_example()
|
||||
|
||||
# Example 3: Compare retrieval modes
|
||||
compare_retrieval_modes()
|
||||
|
||||
# Example 4: Tuning guidance
|
||||
hybrid_search_tuning()
|
||||
|
||||
print("\n✅ Hybrid search examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,288 @@
|
||||
"""
|
||||
Graph-Assisted RAG: Relationship-Aware Retrieval
|
||||
|
||||
This example demonstrates how graph structures can enhance RAG by
|
||||
capturing relationships between entities and enabling path-based retrieval.
|
||||
|
||||
RAG Concept: Traditional vector search finds similar documents but misses
|
||||
relationships. Graph-assisted RAG understands connections between entities,
|
||||
enabling multi-hop reasoning and relationship-aware answers.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Interconnected entities (simulating a knowledge graph)
|
||||
ENTITY_KNOWLEDGE = [
|
||||
{
|
||||
"id": "person_einstein",
|
||||
"type": "person",
|
||||
"content": """
|
||||
Albert Einstein (1879-1955) was a theoretical physicist who developed
|
||||
the theory of relativity. He received the Nobel Prize in Physics in 1921
|
||||
for his explanation of the photoelectric effect. Einstein worked at the
|
||||
Institute for Advanced Study in Princeton from 1933 until his death.
|
||||
Key relationships: Collaborated with Niels Bohr, influenced by Max Planck,
|
||||
mentored by Heinrich Weber.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "person_bohr",
|
||||
"type": "person",
|
||||
"content": """
|
||||
Niels Bohr (1885-1962) was a Danish physicist who made foundational
|
||||
contributions to understanding atomic structure and quantum theory.
|
||||
He received the Nobel Prize in Physics in 1922. Bohr founded the
|
||||
Institute of Theoretical Physics in Copenhagen.
|
||||
Key relationships: Debated with Einstein on quantum mechanics,
|
||||
mentored Werner Heisenberg, influenced by Ernest Rutherford.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "person_heisenberg",
|
||||
"type": "person",
|
||||
"content": """
|
||||
Werner Heisenberg (1901-1976) was a German physicist and pioneer of
|
||||
quantum mechanics. He formulated the uncertainty principle in 1927.
|
||||
He received the Nobel Prize in Physics in 1932.
|
||||
Key relationships: Student of Niels Bohr, collaborated with Max Born,
|
||||
worked with Wolfgang Pauli.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "concept_relativity",
|
||||
"type": "concept",
|
||||
"content": """
|
||||
Theory of Relativity: A framework developed by Albert Einstein consisting
|
||||
of special relativity (1905) and general relativity (1915). Special
|
||||
relativity deals with objects moving at constant speeds, introducing
|
||||
E=mc². General relativity describes gravity as the curvature of spacetime.
|
||||
Related concepts: Spacetime, time dilation, gravitational waves.
|
||||
Applications: GPS satellites, particle accelerators, cosmology.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "concept_quantum",
|
||||
"type": "concept",
|
||||
"content": """
|
||||
Quantum Mechanics: A fundamental theory describing nature at atomic scales.
|
||||
Key principles include wave-particle duality, superposition, and entanglement.
|
||||
Developed through contributions from Planck, Bohr, Heisenberg, Schrödinger.
|
||||
Related concepts: Uncertainty principle, quantum tunneling, wave function.
|
||||
Applications: Semiconductors, lasers, MRI machines, quantum computing.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "institution_princeton",
|
||||
"type": "institution",
|
||||
"content": """
|
||||
Institute for Advanced Study (Princeton): A private research center
|
||||
founded in 1930. Notable members include Albert Einstein, John von Neumann,
|
||||
Kurt Gödel, and Robert Oppenheimer. The institute focuses on theoretical
|
||||
research in mathematics, natural sciences, and humanities.
|
||||
Location: Princeton, New Jersey, USA.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "institution_copenhagen",
|
||||
"type": "institution",
|
||||
"content": """
|
||||
Niels Bohr Institute (Copenhagen): Founded in 1921 by Niels Bohr as the
|
||||
Institute of Theoretical Physics. It became a world center for quantum
|
||||
mechanics research. Notable visitors included Heisenberg, Pauli, and Dirac.
|
||||
The "Copenhagen interpretation" of quantum mechanics originated here.
|
||||
Location: Copenhagen, Denmark.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "event_solvay",
|
||||
"type": "event",
|
||||
"content": """
|
||||
Solvay Conferences: Series of physics conferences held in Brussels since 1911.
|
||||
The 1927 Fifth Solvay Conference is famous for the Bohr-Einstein debates
|
||||
on quantum mechanics. Attendees included Einstein, Bohr, Heisenberg,
|
||||
Schrödinger, Dirac, and Marie Curie. These conferences shaped modern physics.
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def graph_aware_rag():
|
||||
"""Demonstrate relationship-aware retrieval."""
|
||||
|
||||
# Create agent with entity knowledge
|
||||
agent = Agent(
|
||||
name="Physics Historian",
|
||||
instructions="""You are a physics historian who understands the relationships
|
||||
between scientists, concepts, and institutions. When answering:
|
||||
1. Trace connections between entities
|
||||
2. Explain how ideas and people influenced each other
|
||||
3. Reference specific relationships mentioned in the knowledge base
|
||||
4. Build a narrative that shows the interconnected nature of physics history.""",
|
||||
knowledge=ENTITY_KNOWLEDGE,
|
||||
user_id="graph_demo"
|
||||
)
|
||||
|
||||
# Relationship-focused queries
|
||||
queries = [
|
||||
"How were Einstein and Bohr connected?",
|
||||
"Trace the lineage of quantum mechanics through its key figures.",
|
||||
"What institutions were central to 20th century physics?",
|
||||
"How did the Solvay Conferences shape physics?"
|
||||
]
|
||||
|
||||
print("=" * 60)
|
||||
print("GRAPH-ASSISTED RAG: Physics History Network")
|
||||
print("=" * 60)
|
||||
print("\nKnowledge graph entities:")
|
||||
for entity in ENTITY_KNOWLEDGE:
|
||||
print(f" - [{entity['type']}] {entity['id']}")
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response[:350]}..." if len(str(response)) > 350 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def multi_hop_reasoning():
|
||||
"""Demonstrate multi-hop reasoning across entities."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("MULTI-HOP REASONING")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Connection Finder",
|
||||
instructions="""You excel at finding indirect connections between entities.
|
||||
When asked about relationships, trace the path through intermediate entities.
|
||||
Format your answer to show the chain of connections.""",
|
||||
knowledge=ENTITY_KNOWLEDGE,
|
||||
user_id="multihop_demo"
|
||||
)
|
||||
|
||||
# Multi-hop query requiring traversal
|
||||
query = """
|
||||
What is the connection between the uncertainty principle and Princeton?
|
||||
(Hint: trace through people and their relationships)
|
||||
"""
|
||||
|
||||
print(f"\n📝 Multi-hop Query: {query.strip()}")
|
||||
print("\n🔗 Expected path: Uncertainty Principle → Heisenberg → Bohr → Einstein → Princeton")
|
||||
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Answer:\n{response}")
|
||||
|
||||
|
||||
def entity_type_filtering():
|
||||
"""Show filtering by entity type."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("ENTITY TYPE FILTERING")
|
||||
print("=" * 60)
|
||||
|
||||
# Filter to only people
|
||||
people_only = [e for e in ENTITY_KNOWLEDGE if e['type'] == 'person']
|
||||
|
||||
people_agent = Agent(
|
||||
name="Biographer",
|
||||
instructions="You are a biographer focused on scientists' lives and relationships.",
|
||||
knowledge=people_only,
|
||||
user_id="people_demo"
|
||||
)
|
||||
|
||||
# Filter to only concepts
|
||||
concepts_only = [e for e in ENTITY_KNOWLEDGE if e['type'] == 'concept']
|
||||
|
||||
concept_agent = Agent(
|
||||
name="Concept Explainer",
|
||||
instructions="You explain scientific concepts and their relationships.",
|
||||
knowledge=concepts_only,
|
||||
user_id="concept_demo"
|
||||
)
|
||||
|
||||
query = "Tell me about the major developments in early 20th century physics."
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
print("\n👤 People-focused view:")
|
||||
response1 = people_agent.chat(query)
|
||||
print(f" {response1[:250]}...")
|
||||
|
||||
print("\n💡 Concept-focused view:")
|
||||
response2 = concept_agent.chat(query)
|
||||
print(f" {response2[:250]}...")
|
||||
|
||||
|
||||
def graph_rag_patterns():
|
||||
"""Explain graph RAG patterns."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("GRAPH RAG PATTERNS")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📊 Graph-Assisted RAG Patterns:
|
||||
|
||||
1. **Entity-Centric Retrieval**
|
||||
- Store entities with their relationships
|
||||
- Query retrieves entity + connected entities
|
||||
- Good for: "Tell me about X and its connections"
|
||||
|
||||
2. **Path-Based Retrieval**
|
||||
- Find paths between two entities
|
||||
- Retrieve all nodes along the path
|
||||
- Good for: "How is X related to Y?"
|
||||
|
||||
3. **Neighborhood Expansion**
|
||||
- Start with query-matched entities
|
||||
- Expand to include 1-hop or 2-hop neighbors
|
||||
- Good for: Comprehensive context
|
||||
|
||||
4. **Subgraph Extraction**
|
||||
- Extract relevant subgraph for query
|
||||
- Include nodes and edges as context
|
||||
- Good for: Complex multi-entity questions
|
||||
|
||||
Implementation in PraisonAI:
|
||||
```python
|
||||
# Structure knowledge as entities with relationships
|
||||
entities = [
|
||||
{
|
||||
"id": "entity_1",
|
||||
"type": "person",
|
||||
"content": "...",
|
||||
"relationships": ["entity_2", "entity_3"]
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
# Agent retrieves related entities automatically
|
||||
agent = Agent(
|
||||
knowledge=entities,
|
||||
instructions="Trace relationships between entities..."
|
||||
)
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all graph-assisted RAG examples."""
|
||||
print("\n🚀 PraisonAI Graph-Assisted RAG Examples\n")
|
||||
|
||||
# Example 1: Graph-aware retrieval
|
||||
graph_aware_rag()
|
||||
|
||||
# Example 2: Multi-hop reasoning
|
||||
multi_hop_reasoning()
|
||||
|
||||
# Example 3: Entity type filtering
|
||||
entity_type_filtering()
|
||||
|
||||
# Example 4: Graph RAG patterns
|
||||
graph_rag_patterns()
|
||||
|
||||
print("\n✅ Graph-assisted RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,266 @@
|
||||
"""
|
||||
Multi-Document RAG: Synthesizing Information Across Sources
|
||||
|
||||
This example demonstrates retrieving and synthesizing information from
|
||||
multiple documents to answer complex questions.
|
||||
|
||||
RAG Concept: Real-world questions often require combining information
|
||||
from multiple sources. Multi-document RAG retrieves relevant chunks
|
||||
from different documents and synthesizes a coherent answer.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Multiple documents on different topics
|
||||
CLIMATE_REPORT = {
|
||||
"id": "climate_2024",
|
||||
"source": "Global Climate Report 2024",
|
||||
"content": """
|
||||
Global Climate Report 2024 - Executive Summary
|
||||
|
||||
Global temperatures in 2024 reached 1.45°C above pre-industrial levels,
|
||||
making it the warmest year on record. Key findings:
|
||||
|
||||
- Arctic sea ice extent hit a new minimum in September
|
||||
- Ocean heat content reached unprecedented levels
|
||||
- Extreme weather events increased by 23% compared to the decade average
|
||||
- CO2 concentrations reached 422 ppm, up from 417 ppm in 2023
|
||||
|
||||
Regional impacts varied significantly. Europe experienced its hottest
|
||||
summer, while parts of South America faced severe drought conditions.
|
||||
The economic cost of climate-related disasters exceeded $380 billion.
|
||||
"""
|
||||
}
|
||||
|
||||
ENERGY_REPORT = {
|
||||
"id": "energy_2024",
|
||||
"source": "World Energy Outlook 2024",
|
||||
"content": """
|
||||
World Energy Outlook 2024 - Key Highlights
|
||||
|
||||
Renewable energy capacity grew by 35% in 2024, led by solar and wind.
|
||||
Major developments:
|
||||
|
||||
- Solar installations reached 1.5 TW cumulative capacity globally
|
||||
- Wind power generated 12% of global electricity
|
||||
- Battery storage capacity doubled to 500 GWh
|
||||
- Electric vehicle sales reached 20 million units
|
||||
|
||||
Despite progress, fossil fuels still account for 75% of primary energy.
|
||||
Investment in clean energy reached $1.8 trillion, but $4 trillion annually
|
||||
is needed to meet net-zero targets by 2050.
|
||||
"""
|
||||
}
|
||||
|
||||
POLICY_REPORT = {
|
||||
"id": "policy_2024",
|
||||
"source": "Climate Policy Tracker 2024",
|
||||
"content": """
|
||||
Climate Policy Tracker 2024 - Global Progress
|
||||
|
||||
145 countries have now committed to net-zero emissions targets.
|
||||
Policy developments:
|
||||
|
||||
- EU Carbon Border Adjustment Mechanism fully implemented
|
||||
- US Inflation Reduction Act drove $300B in clean energy investment
|
||||
- China pledged to peak emissions before 2030
|
||||
- India launched National Green Hydrogen Mission
|
||||
|
||||
Carbon pricing now covers 23% of global emissions, up from 20% in 2023.
|
||||
Average carbon price increased to $45/ton. However, only 4% of emissions
|
||||
are priced at levels consistent with Paris Agreement goals ($100+/ton).
|
||||
"""
|
||||
}
|
||||
|
||||
TECH_REPORT = {
|
||||
"id": "tech_2024",
|
||||
"source": "Climate Technology Review 2024",
|
||||
"content": """
|
||||
Climate Technology Review 2024 - Breakthrough Innovations
|
||||
|
||||
Several climate technologies reached commercial viability in 2024:
|
||||
|
||||
- Direct Air Capture: Costs fell to $400/ton CO2, down from $600 in 2022
|
||||
- Green Hydrogen: Production costs reached $3/kg in optimal locations
|
||||
- Solid-state Batteries: Energy density improved 40%, enabling longer EV range
|
||||
- Perovskite Solar Cells: Efficiency reached 33%, approaching silicon limits
|
||||
|
||||
Emerging technologies to watch:
|
||||
- Enhanced geothermal systems showing promise in non-volcanic regions
|
||||
- Ocean-based carbon removal gaining investment interest
|
||||
- AI-optimized grid management reducing curtailment by 15%
|
||||
"""
|
||||
}
|
||||
|
||||
# Combine all documents
|
||||
ALL_DOCUMENTS = [CLIMATE_REPORT, ENERGY_REPORT, POLICY_REPORT, TECH_REPORT]
|
||||
|
||||
|
||||
def multi_document_rag():
|
||||
"""Demonstrate RAG across multiple documents."""
|
||||
|
||||
# Create agent with multi-document knowledge
|
||||
agent = Agent(
|
||||
name="Climate Analyst",
|
||||
instructions="""You are a climate and energy analyst.
|
||||
Synthesize information from multiple reports to answer questions.
|
||||
When information comes from different sources, mention which reports
|
||||
you're drawing from. Provide comprehensive, well-rounded answers.""",
|
||||
knowledge=ALL_DOCUMENTS,
|
||||
user_id="multi_doc_demo"
|
||||
)
|
||||
|
||||
# Questions that require multi-document synthesis
|
||||
queries = [
|
||||
"What is the current state of global climate and what are we doing about it?",
|
||||
"How is renewable energy progressing and what policies support it?",
|
||||
"What technologies are emerging to address climate change?",
|
||||
"Compare the economic investment in clean energy with climate damage costs."
|
||||
]
|
||||
|
||||
print("=" * 60)
|
||||
print("MULTI-DOCUMENT RAG: Climate Analysis")
|
||||
print("=" * 60)
|
||||
print(f"\nKnowledge base: {len(ALL_DOCUMENTS)} documents")
|
||||
print(f"Sources: {[d['source'] for d in ALL_DOCUMENTS]}")
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response[:400]}..." if len(str(response)) > 400 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def cross_reference_example():
|
||||
"""Show how agents can cross-reference information."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CROSS-REFERENCING ACROSS DOCUMENTS")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Cross-Reference Analyst",
|
||||
instructions="""You are an analyst who excels at finding connections
|
||||
between different reports. When answering:
|
||||
1. Identify relevant information from each source
|
||||
2. Note any contradictions or complementary data
|
||||
3. Synthesize a coherent narrative
|
||||
4. Cite your sources explicitly.""",
|
||||
knowledge=ALL_DOCUMENTS,
|
||||
user_id="cross_ref_demo"
|
||||
)
|
||||
|
||||
query = """
|
||||
Analyze the relationship between renewable energy growth (from Energy Report),
|
||||
policy support (from Policy Report), and technology costs (from Tech Report).
|
||||
How do these factors reinforce each other?
|
||||
"""
|
||||
|
||||
print(f"\n📝 Complex Query: {query.strip()}")
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Synthesized Answer:\n{response}")
|
||||
|
||||
|
||||
def document_filtering():
|
||||
"""Demonstrate filtering documents by metadata."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("DOCUMENT FILTERING BY SOURCE")
|
||||
print("=" * 60)
|
||||
|
||||
# Create agents focused on specific document types
|
||||
policy_agent = Agent(
|
||||
name="Policy Expert",
|
||||
instructions="You are a policy expert. Focus only on policy-related information.",
|
||||
knowledge=[POLICY_REPORT],
|
||||
user_id="policy_expert"
|
||||
)
|
||||
|
||||
tech_agent = Agent(
|
||||
name="Technology Expert",
|
||||
instructions="You are a technology expert. Focus on technical innovations.",
|
||||
knowledge=[TECH_REPORT],
|
||||
user_id="tech_expert"
|
||||
)
|
||||
|
||||
query = "What progress has been made in addressing climate change?"
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
print("\n🏛️ Policy Expert's View:")
|
||||
policy_response = policy_agent.chat(query)
|
||||
print(f" {policy_response[:250]}...")
|
||||
|
||||
print("\n🔬 Technology Expert's View:")
|
||||
tech_response = tech_agent.chat(query)
|
||||
print(f" {tech_response[:250]}...")
|
||||
|
||||
print("\n💡 Note: Different document subsets yield different perspectives!")
|
||||
|
||||
|
||||
def multi_document_best_practices():
|
||||
"""Share best practices for multi-document RAG."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("MULTI-DOCUMENT RAG BEST PRACTICES")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📚 Best Practices for Multi-Document RAG:
|
||||
|
||||
1. **Document Metadata**
|
||||
- Include source, date, author in each document
|
||||
- Enables filtering and citation
|
||||
|
||||
2. **Consistent Chunking**
|
||||
- Use similar chunk sizes across documents
|
||||
- Maintains balanced retrieval
|
||||
|
||||
3. **Source Diversity**
|
||||
- Retrieve from multiple sources when possible
|
||||
- Reduces single-source bias
|
||||
|
||||
4. **Conflict Resolution**
|
||||
- Instruct agent how to handle contradictions
|
||||
- Prefer newer sources or authoritative ones
|
||||
|
||||
5. **Citation Requirements**
|
||||
- Ask agent to cite sources in responses
|
||||
- Enables verification and trust
|
||||
|
||||
Example configuration:
|
||||
```python
|
||||
documents = [
|
||||
{"id": "doc1", "source": "Report A", "date": "2024-01", "content": "..."},
|
||||
{"id": "doc2", "source": "Report B", "date": "2024-06", "content": "..."},
|
||||
]
|
||||
|
||||
agent = Agent(
|
||||
knowledge=documents,
|
||||
instructions="Always cite which report your information comes from."
|
||||
)
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all multi-document RAG examples."""
|
||||
print("\n🚀 PraisonAI Multi-Document RAG Examples\n")
|
||||
|
||||
# Example 1: Basic multi-document RAG
|
||||
multi_document_rag()
|
||||
|
||||
# Example 2: Cross-referencing
|
||||
cross_reference_example()
|
||||
|
||||
# Example 3: Document filtering
|
||||
document_filtering()
|
||||
|
||||
# Example 4: Best practices
|
||||
multi_document_best_practices()
|
||||
|
||||
print("\n✅ Multi-document RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,255 @@
|
||||
"""
|
||||
Reranking: Improving Retrieval Precision
|
||||
|
||||
This example demonstrates reranking - a second-pass scoring mechanism
|
||||
that improves the quality of retrieved documents.
|
||||
|
||||
RAG Concept: Initial retrieval (first stage) prioritizes recall - finding
|
||||
all potentially relevant documents. Reranking (second stage) prioritizes
|
||||
precision - ordering results by true relevance to the query.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Research paper abstracts
|
||||
RESEARCH_PAPERS = [
|
||||
{
|
||||
"id": "paper_001",
|
||||
"content": """
|
||||
Title: Attention Mechanisms in Neural Machine Translation
|
||||
|
||||
Abstract: This paper introduces a novel attention mechanism for sequence-to-sequence
|
||||
models in machine translation. Unlike previous approaches that use a fixed-length
|
||||
context vector, our method allows the model to automatically search for parts of
|
||||
the source sentence relevant to predicting the target word. Experiments on
|
||||
English-to-French translation show significant improvements over baseline systems.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "paper_002",
|
||||
"content": """
|
||||
Title: Deep Residual Learning for Image Recognition
|
||||
|
||||
Abstract: We present a residual learning framework to ease the training of very deep
|
||||
neural networks. By reformulating layers as learning residual functions with
|
||||
reference to layer inputs, we show that these networks are easier to optimize
|
||||
and can gain accuracy from considerably increased depth. Our 152-layer residual
|
||||
network achieves state-of-the-art results on ImageNet classification.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "paper_003",
|
||||
"content": """
|
||||
Title: BERT: Pre-training of Deep Bidirectional Transformers
|
||||
|
||||
Abstract: We introduce BERT, a new language representation model designed to
|
||||
pre-train deep bidirectional representations from unlabeled text. Unlike previous
|
||||
models, BERT is designed to jointly condition on both left and right context.
|
||||
The pre-trained BERT model can be fine-tuned with just one additional output
|
||||
layer for a wide range of NLP tasks.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "paper_004",
|
||||
"content": """
|
||||
Title: Generative Adversarial Networks
|
||||
|
||||
Abstract: We propose a new framework for estimating generative models via an
|
||||
adversarial process. We simultaneously train two models: a generative model G
|
||||
that captures the data distribution, and a discriminative model D that estimates
|
||||
the probability that a sample came from the training data rather than G.
|
||||
This framework can generate realistic images from random noise.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "paper_005",
|
||||
"content": """
|
||||
Title: Transformer Architecture for Language Understanding
|
||||
|
||||
Abstract: We propose the Transformer, a model architecture eschewing recurrence
|
||||
and instead relying entirely on an attention mechanism to draw global dependencies
|
||||
between input and output. The Transformer allows for significantly more
|
||||
parallelization and achieves new state of the art in translation quality.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "paper_006",
|
||||
"content": """
|
||||
Title: Word Embeddings and Semantic Similarity
|
||||
|
||||
Abstract: This paper explores methods for learning distributed representations
|
||||
of words that capture semantic relationships. We show that word vectors trained
|
||||
on large corpora encode meaningful syntactic and semantic regularities. These
|
||||
representations enable analogical reasoning: king - man + woman = queen.
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def explain_reranking():
|
||||
"""Explain the reranking concept."""
|
||||
|
||||
print("=" * 60)
|
||||
print("RERANKING EXPLAINED")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🔄 Two-Stage Retrieval Pipeline:
|
||||
|
||||
Stage 1: Initial Retrieval (Bi-encoder)
|
||||
├── Fast approximate matching
|
||||
├── Retrieves top-K candidates (e.g., 20-100)
|
||||
├── Uses pre-computed embeddings
|
||||
└── Optimizes for RECALL (find all relevant docs)
|
||||
|
||||
Stage 2: Reranking (Cross-encoder)
|
||||
├── Slower but more accurate scoring
|
||||
├── Reorders the top-K candidates
|
||||
├── Computes query-document interaction
|
||||
└── Optimizes for PRECISION (best docs first)
|
||||
|
||||
Why Reranking Matters:
|
||||
- Bi-encoders encode query and docs separately (fast but approximate)
|
||||
- Cross-encoders process query+doc together (slow but accurate)
|
||||
- Reranking gives you the best of both worlds
|
||||
""")
|
||||
|
||||
|
||||
def rag_with_reranking():
|
||||
"""Demonstrate RAG with reranking enabled."""
|
||||
|
||||
# Create agent with reranking
|
||||
agent = Agent(
|
||||
name="Research Assistant",
|
||||
instructions="""You are a research paper expert.
|
||||
Answer questions about machine learning research.
|
||||
Reference specific papers when relevant.
|
||||
Be precise about technical details.""",
|
||||
knowledge=RESEARCH_PAPERS,
|
||||
knowledge_config={
|
||||
"rerank": True,
|
||||
"top_k": 5
|
||||
},
|
||||
user_id="rerank_demo"
|
||||
)
|
||||
|
||||
queries = [
|
||||
"Which paper introduced the attention mechanism for translation?",
|
||||
"Tell me about pre-training language models",
|
||||
"What architecture avoids recurrence entirely?"
|
||||
]
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RAG WITH RERANKING")
|
||||
print("=" * 60)
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 Answer: {response[:300]}..." if len(str(response)) > 300 else f"💡 Answer: {response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def compare_with_without_reranking():
|
||||
"""Compare retrieval quality with and without reranking."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RERANKING IMPACT COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
query = "How do transformers handle long-range dependencies?"
|
||||
|
||||
# Without reranking
|
||||
agent_no_rerank = Agent(
|
||||
name="No Rerank Agent",
|
||||
instructions="Answer based on the research papers provided.",
|
||||
knowledge=RESEARCH_PAPERS,
|
||||
knowledge_config={"rerank": False, "top_k": 3},
|
||||
user_id="no_rerank"
|
||||
)
|
||||
|
||||
# With reranking
|
||||
agent_with_rerank = Agent(
|
||||
name="Rerank Agent",
|
||||
instructions="Answer based on the research papers provided.",
|
||||
knowledge=RESEARCH_PAPERS,
|
||||
knowledge_config={"rerank": True, "top_k": 3},
|
||||
user_id="with_rerank"
|
||||
)
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
|
||||
print("\n🔍 Without Reranking:")
|
||||
response1 = agent_no_rerank.chat(query)
|
||||
print(f" {response1[:250]}...")
|
||||
|
||||
print("\n🔍 With Reranking:")
|
||||
response2 = agent_with_rerank.chat(query)
|
||||
print(f" {response2[:250]}...")
|
||||
|
||||
|
||||
def reranking_configuration():
|
||||
"""Show reranking configuration options."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RERANKING CONFIGURATION")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
Configuration options for reranking in PraisonAI:
|
||||
|
||||
```python
|
||||
agent = Agent(
|
||||
name="My Agent",
|
||||
knowledge=documents,
|
||||
knowledge_config={
|
||||
# Enable reranking
|
||||
"rerank": True,
|
||||
|
||||
# Number of candidates for initial retrieval
|
||||
"top_k": 10,
|
||||
|
||||
# Number of results after reranking
|
||||
"rerank_top_k": 3,
|
||||
|
||||
# Reranker model (optional)
|
||||
"rerank_model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
Performance Considerations:
|
||||
┌─────────────────┬──────────────┬─────────────┐
|
||||
│ Configuration │ Latency │ Quality │
|
||||
├─────────────────┼──────────────┼─────────────┤
|
||||
│ No reranking │ ~50ms │ Good │
|
||||
│ Light reranker │ ~100ms │ Better │
|
||||
│ Heavy reranker │ ~200ms │ Best │
|
||||
└─────────────────┴──────────────┴─────────────┘
|
||||
|
||||
Tip: Use reranking for precision-critical applications
|
||||
where quality matters more than latency.
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all reranking examples."""
|
||||
print("\n🚀 PraisonAI Reranking Examples\n")
|
||||
|
||||
# Example 1: Explain reranking
|
||||
explain_reranking()
|
||||
|
||||
# Example 2: RAG with reranking
|
||||
rag_with_reranking()
|
||||
|
||||
# Example 3: Compare with/without reranking
|
||||
compare_with_without_reranking()
|
||||
|
||||
# Example 4: Configuration options
|
||||
reranking_configuration()
|
||||
|
||||
print("\n✅ Reranking examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,382 @@
|
||||
"""
|
||||
Retrieval Policies: Agent-Decided Retrieval Strategies
|
||||
|
||||
This example demonstrates how agents can dynamically decide retrieval
|
||||
strategies based on query characteristics.
|
||||
|
||||
RAG Concept: Not all queries are equal. Some need deep retrieval, others
|
||||
need broad coverage, and some don't need retrieval at all. Smart policies
|
||||
optimize both quality and efficiency.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, AutoRagAgent, AutoRagConfig
|
||||
|
||||
# Sample knowledge base: IT support documentation
|
||||
IT_SUPPORT_DOCS = [
|
||||
{
|
||||
"id": "password_reset",
|
||||
"category": "account",
|
||||
"priority": "high",
|
||||
"content": """
|
||||
Password Reset Procedure:
|
||||
1. Go to the self-service portal at https://portal.company.com
|
||||
2. Click "Forgot Password"
|
||||
3. Enter your employee ID and email
|
||||
4. Check your email for the reset link (valid for 1 hour)
|
||||
5. Create a new password meeting complexity requirements
|
||||
|
||||
If you don't receive the email, check spam or contact IT helpdesk.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "vpn_setup",
|
||||
"category": "network",
|
||||
"priority": "medium",
|
||||
"content": """
|
||||
VPN Setup Guide:
|
||||
1. Download the VPN client from the software center
|
||||
2. Install with default settings
|
||||
3. Launch and enter your credentials
|
||||
4. Select the nearest server location
|
||||
5. Click Connect
|
||||
|
||||
Troubleshooting: If connection fails, check firewall settings
|
||||
and ensure port 443 is open.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "email_config",
|
||||
"category": "communication",
|
||||
"priority": "medium",
|
||||
"content": """
|
||||
Email Configuration:
|
||||
- Server: mail.company.com
|
||||
- Port: 993 (IMAP) / 587 (SMTP)
|
||||
- Security: SSL/TLS required
|
||||
- Username: your full email address
|
||||
|
||||
Mobile setup: Use the Company Portal app for automatic configuration.
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "printer_setup",
|
||||
"category": "hardware",
|
||||
"priority": "low",
|
||||
"content": """
|
||||
Printer Setup:
|
||||
1. Open Settings > Printers & Scanners
|
||||
2. Click Add Printer
|
||||
3. Select your floor's printer from the list
|
||||
4. Install drivers if prompted
|
||||
|
||||
Printer naming convention: FLOOR-WING-NUMBER (e.g., 3-EAST-01)
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "software_request",
|
||||
"category": "software",
|
||||
"priority": "medium",
|
||||
"content": """
|
||||
Software Request Process:
|
||||
1. Submit request through ServiceNow
|
||||
2. Manager approval required for licensed software
|
||||
3. IT reviews for security compliance
|
||||
4. Approved software deployed within 48 hours
|
||||
|
||||
Pre-approved software can be installed directly from Software Center.
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def query_classification():
|
||||
"""Demonstrate query classification for retrieval decisions."""
|
||||
|
||||
print("=" * 60)
|
||||
print("QUERY CLASSIFICATION FOR RETRIEVAL")
|
||||
print("=" * 60)
|
||||
|
||||
# Different query types
|
||||
queries = [
|
||||
("How do I reset my password?", "procedural", True),
|
||||
("Hello!", "greeting", False),
|
||||
("What is 2 + 2?", "general_knowledge", False),
|
||||
("VPN connection keeps failing", "troubleshooting", True),
|
||||
("Thanks for your help", "closing", False),
|
||||
("How do I request new software?", "procedural", True),
|
||||
]
|
||||
|
||||
print("\n📊 Query Classification Examples:\n")
|
||||
print(f"{'Query':<40} {'Type':<20} {'Needs RAG':<10}")
|
||||
print("-" * 70)
|
||||
|
||||
for query, query_type, needs_rag in queries:
|
||||
rag_indicator = "✅ Yes" if needs_rag else "❌ No"
|
||||
print(f"{query:<40} {query_type:<20} {rag_indicator:<10}")
|
||||
|
||||
print("\n💡 Smart agents classify queries to decide retrieval strategy.")
|
||||
|
||||
|
||||
def policy_based_retrieval():
|
||||
"""Demonstrate different retrieval policies."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RETRIEVAL POLICY COMPARISON")
|
||||
print("=" * 60)
|
||||
|
||||
base_agent = Agent(
|
||||
name="IT Support",
|
||||
instructions="You are an IT support agent. Help users with technical issues.",
|
||||
knowledge=IT_SUPPORT_DOCS,
|
||||
user_id="policy_demo"
|
||||
)
|
||||
|
||||
query = "How do I set up VPN?"
|
||||
|
||||
policies = [
|
||||
("auto", "Agent decides based on query analysis"),
|
||||
("always", "Always retrieve, even for simple queries"),
|
||||
("never", "Never retrieve, use only parametric knowledge"),
|
||||
]
|
||||
|
||||
print(f"\n📝 Query: {query}\n")
|
||||
|
||||
for policy, description in policies:
|
||||
config = AutoRagConfig(policy=policy)
|
||||
auto_agent = AutoRagAgent(agent=base_agent, config=config)
|
||||
|
||||
print(f"🔄 Policy: {policy.upper()}")
|
||||
print(f" Description: {description}")
|
||||
|
||||
response = auto_agent.chat(query, user_id="policy_demo")
|
||||
print(f" Response: {response[:150]}...")
|
||||
print()
|
||||
|
||||
|
||||
def adaptive_top_k():
|
||||
"""Demonstrate adaptive top-k based on query complexity."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("ADAPTIVE TOP-K RETRIEVAL")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📊 Adaptive Top-K Strategy:
|
||||
|
||||
Query Complexity → Top-K Value
|
||||
─────────────────────────────────
|
||||
Simple (single topic) → k=1-2
|
||||
Moderate (related topics) → k=3-5
|
||||
Complex (multi-topic) → k=5-10
|
||||
|
||||
Examples:
|
||||
""")
|
||||
|
||||
queries = [
|
||||
("Reset password", 2, "Simple - single procedure"),
|
||||
("VPN not working, also need email setup", 5, "Moderate - two topics"),
|
||||
("New employee setup: email, VPN, printer, software", 8, "Complex - multiple topics"),
|
||||
]
|
||||
|
||||
for query, suggested_k, complexity in queries:
|
||||
print(f" Query: \"{query}\"")
|
||||
print(f" Complexity: {complexity}")
|
||||
print(f" Suggested top_k: {suggested_k}")
|
||||
print()
|
||||
|
||||
|
||||
def priority_based_retrieval():
|
||||
"""Demonstrate priority-based document retrieval."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("PRIORITY-BASED RETRIEVAL")
|
||||
print("=" * 60)
|
||||
|
||||
# Group documents by priority
|
||||
high_priority = [d for d in IT_SUPPORT_DOCS if d.get('priority') == 'high']
|
||||
all_docs = IT_SUPPORT_DOCS
|
||||
|
||||
print("\n📊 Document Priorities in Knowledge Base:")
|
||||
for doc in IT_SUPPORT_DOCS:
|
||||
print(f" [{doc['priority'].upper():^6}] {doc['id']}")
|
||||
|
||||
# High-priority agent (for urgent issues)
|
||||
urgent_agent = Agent(
|
||||
name="Urgent Support",
|
||||
instructions="Handle urgent IT issues. Focus on critical procedures.",
|
||||
knowledge=high_priority,
|
||||
user_id="urgent_demo"
|
||||
)
|
||||
|
||||
# Full knowledge agent
|
||||
full_agent = Agent(
|
||||
name="Full Support",
|
||||
instructions="Handle all IT support requests.",
|
||||
knowledge=all_docs,
|
||||
user_id="full_demo"
|
||||
)
|
||||
|
||||
query = "I'm locked out of my account!"
|
||||
|
||||
print(f"\n📝 Urgent Query: {query}")
|
||||
|
||||
print("\n🚨 Urgent Support Agent (high-priority docs only):")
|
||||
response1 = urgent_agent.chat(query)
|
||||
print(f" {response1[:200]}...")
|
||||
|
||||
print("\n📚 Full Support Agent (all docs):")
|
||||
response2 = full_agent.chat(query)
|
||||
print(f" {response2[:200]}...")
|
||||
|
||||
|
||||
def context_aware_retrieval():
|
||||
"""Demonstrate context-aware retrieval strategies."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CONTEXT-AWARE RETRIEVAL")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🎯 Context-Aware Retrieval Strategies:
|
||||
|
||||
1. **User Role Context**
|
||||
- New employee → Onboarding docs first
|
||||
- IT admin → Technical docs
|
||||
- Executive → Summary docs
|
||||
|
||||
2. **Time Context**
|
||||
- Business hours → Standard support
|
||||
- After hours → Emergency procedures only
|
||||
|
||||
3. **Conversation Context**
|
||||
- First message → Broad retrieval
|
||||
- Follow-up → Narrow to topic
|
||||
|
||||
4. **Device Context**
|
||||
- Mobile → Mobile-specific guides
|
||||
- Desktop → Full documentation
|
||||
|
||||
Implementation:
|
||||
```python
|
||||
def get_retrieval_config(context: dict) -> dict:
|
||||
if context.get("user_role") == "new_employee":
|
||||
return {"filter": {"category": "onboarding"}, "top_k": 5}
|
||||
elif context.get("is_followup"):
|
||||
return {"top_k": 2} # Narrow focus
|
||||
else:
|
||||
return {"top_k": 3} # Default
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def fallback_strategies():
|
||||
"""Demonstrate fallback strategies when retrieval fails."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RETRIEVAL FALLBACK STRATEGIES")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🔄 Fallback Strategy Chain:
|
||||
|
||||
1. Primary: Vector search in knowledge base
|
||||
↓ (if no relevant results)
|
||||
2. Secondary: Expand search with synonyms
|
||||
↓ (if still no results)
|
||||
3. Tertiary: Search external sources
|
||||
↓ (if still no results)
|
||||
4. Final: Acknowledge limitation, offer alternatives
|
||||
|
||||
Example Implementation:
|
||||
```python
|
||||
def retrieve_with_fallback(query: str, knowledge) -> str:
|
||||
# Primary retrieval
|
||||
results = knowledge.search(query, top_k=3)
|
||||
|
||||
if results and results[0].score > 0.7:
|
||||
return format_context(results)
|
||||
|
||||
# Fallback: Expand query
|
||||
expanded = expand_query(query) # Add synonyms
|
||||
results = knowledge.search(expanded, top_k=5)
|
||||
|
||||
if results and results[0].score > 0.5:
|
||||
return format_context(results)
|
||||
|
||||
# Final fallback
|
||||
return "I don't have specific information about that. " \\
|
||||
"Please contact IT helpdesk at ext. 1234."
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def retrieval_metrics():
|
||||
"""Explain retrieval quality metrics."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("RETRIEVAL QUALITY METRICS")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
📈 Key Metrics for Retrieval Policy Tuning:
|
||||
|
||||
┌─────────────────┬────────────────────────────────────────┐
|
||||
│ Metric │ Description │
|
||||
├─────────────────┼────────────────────────────────────────┤
|
||||
│ Precision@K │ % of retrieved docs that are relevant │
|
||||
│ Recall@K │ % of relevant docs that are retrieved │
|
||||
│ MRR │ Mean Reciprocal Rank of first hit │
|
||||
│ NDCG │ Normalized Discounted Cumulative Gain │
|
||||
│ Latency │ Time to retrieve (ms) │
|
||||
│ Token Usage │ Context tokens consumed │
|
||||
└─────────────────┴────────────────────────────────────────┘
|
||||
|
||||
Policy Optimization Goals:
|
||||
|
||||
- High Precision: Use smaller top_k, stricter thresholds
|
||||
- High Recall: Use larger top_k, relaxed thresholds
|
||||
- Low Latency: Use smaller top_k, skip reranking
|
||||
- Low Cost: Use smaller top_k, aggressive filtering
|
||||
|
||||
Balanced Default:
|
||||
```python
|
||||
config = {
|
||||
"top_k": 3,
|
||||
"score_threshold": 0.5,
|
||||
"rerank": True,
|
||||
"max_tokens": 2000
|
||||
}
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all retrieval policy examples."""
|
||||
print("\n🚀 PraisonAI Retrieval Policies Examples\n")
|
||||
|
||||
# Example 1: Query classification
|
||||
query_classification()
|
||||
|
||||
# Example 2: Policy comparison
|
||||
policy_based_retrieval()
|
||||
|
||||
# Example 3: Adaptive top-k
|
||||
adaptive_top_k()
|
||||
|
||||
# Example 4: Priority-based
|
||||
priority_based_retrieval()
|
||||
|
||||
# Example 5: Context-aware
|
||||
context_aware_retrieval()
|
||||
|
||||
# Example 6: Fallback strategies
|
||||
fallback_strategies()
|
||||
|
||||
# Example 7: Metrics
|
||||
retrieval_metrics()
|
||||
|
||||
print("\n✅ Retrieval policies examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,334 @@
|
||||
"""
|
||||
Structured Output RAG: Guided and Constrained Generation
|
||||
|
||||
This example demonstrates how to guide RAG output into specific formats
|
||||
and structures using Pydantic models and output constraints.
|
||||
|
||||
RAG Concept: Sometimes you need RAG answers in a specific format - JSON,
|
||||
tables, or structured data. Guided generation ensures the LLM output
|
||||
conforms to your schema.
|
||||
"""
|
||||
|
||||
from typing import List
|
||||
from pydantic import BaseModel, Field
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Sample knowledge base: Product catalog
|
||||
PRODUCT_CATALOG = [
|
||||
{
|
||||
"id": "prod_001",
|
||||
"content": """
|
||||
Product: UltraWidget Pro
|
||||
Category: Electronics
|
||||
Price: $299.99
|
||||
Rating: 4.5/5 (1,250 reviews)
|
||||
Features: Wireless connectivity, 12-hour battery, water-resistant
|
||||
Availability: In stock
|
||||
SKU: UW-PRO-2024
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "prod_002",
|
||||
"content": """
|
||||
Product: SmartHome Hub
|
||||
Category: Home Automation
|
||||
Price: $149.99
|
||||
Rating: 4.2/5 (890 reviews)
|
||||
Features: Voice control, 100+ device compatibility, energy monitoring
|
||||
Availability: In stock
|
||||
SKU: SH-HUB-2024
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "prod_003",
|
||||
"content": """
|
||||
Product: ErgoDesk Standing Desk
|
||||
Category: Furniture
|
||||
Price: $599.99
|
||||
Rating: 4.8/5 (2,100 reviews)
|
||||
Features: Electric height adjustment, memory presets, cable management
|
||||
Availability: Ships in 3-5 days
|
||||
SKU: ED-STAND-2024
|
||||
"""
|
||||
},
|
||||
{
|
||||
"id": "prod_004",
|
||||
"content": """
|
||||
Product: CloudSync Backup Drive
|
||||
Category: Storage
|
||||
Price: $199.99
|
||||
Rating: 4.6/5 (1,800 reviews)
|
||||
Features: 4TB capacity, automatic backup, encrypted storage
|
||||
Availability: In stock
|
||||
SKU: CS-4TB-2024
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
# Pydantic models for structured output
|
||||
class ProductInfo(BaseModel):
|
||||
"""Structured product information."""
|
||||
name: str = Field(description="Product name")
|
||||
price: float = Field(description="Price in USD")
|
||||
rating: float = Field(description="Rating out of 5")
|
||||
in_stock: bool = Field(description="Whether product is in stock")
|
||||
key_features: List[str] = Field(description="Top 3 features")
|
||||
|
||||
|
||||
class ProductComparison(BaseModel):
|
||||
"""Structured comparison of products."""
|
||||
products: List[str] = Field(description="Names of compared products")
|
||||
winner: str = Field(description="Recommended product")
|
||||
reason: str = Field(description="Why this product is recommended")
|
||||
price_range: str = Field(description="Price range of compared products")
|
||||
|
||||
|
||||
class SearchResult(BaseModel):
|
||||
"""Structured search result."""
|
||||
query: str = Field(description="Original search query")
|
||||
matches: List[str] = Field(description="Matching product names")
|
||||
total_found: int = Field(description="Number of matches")
|
||||
summary: str = Field(description="Brief summary of results")
|
||||
|
||||
|
||||
def json_output_rag():
|
||||
"""Demonstrate RAG with JSON-structured output."""
|
||||
|
||||
print("=" * 60)
|
||||
print("JSON-STRUCTURED RAG OUTPUT")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Product Assistant",
|
||||
instructions="""You are a product assistant that provides structured information.
|
||||
Always respond with valid JSON matching the requested format.
|
||||
Extract accurate information from the product catalog.""",
|
||||
knowledge=PRODUCT_CATALOG,
|
||||
user_id="json_demo"
|
||||
)
|
||||
|
||||
# Request structured product info
|
||||
query = """
|
||||
Tell me about the UltraWidget Pro. Respond in this JSON format:
|
||||
{
|
||||
"name": "product name",
|
||||
"price": 0.00,
|
||||
"rating": 0.0,
|
||||
"in_stock": true/false,
|
||||
"key_features": ["feature1", "feature2", "feature3"]
|
||||
}
|
||||
"""
|
||||
|
||||
print("\n📝 Query: Get structured product info")
|
||||
response = agent.chat(query)
|
||||
print("💡 Structured Response:\n" + str(response))
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def pydantic_guided_rag():
|
||||
"""Demonstrate RAG with Pydantic model guidance."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("PYDANTIC-GUIDED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
# Generate schema from Pydantic model
|
||||
schema = ProductComparison.model_json_schema()
|
||||
|
||||
agent = Agent(
|
||||
name="Product Comparator",
|
||||
instructions=f"""You compare products and provide structured recommendations.
|
||||
Your response MUST be valid JSON matching this schema:
|
||||
{schema}
|
||||
|
||||
Be objective and base recommendations on the product data.""",
|
||||
knowledge=PRODUCT_CATALOG,
|
||||
user_id="pydantic_demo"
|
||||
)
|
||||
|
||||
query = "Compare the UltraWidget Pro and SmartHome Hub. Which should I buy?"
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
print("📋 Expected Schema: ProductComparison")
|
||||
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Structured Comparison:\n{response}")
|
||||
|
||||
|
||||
def table_format_rag():
|
||||
"""Demonstrate RAG with table-formatted output."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("TABLE-FORMATTED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Catalog Browser",
|
||||
instructions="""You present product information in clean table format.
|
||||
Use markdown tables for structured data.
|
||||
Include relevant columns based on the query.""",
|
||||
knowledge=PRODUCT_CATALOG,
|
||||
user_id="table_demo"
|
||||
)
|
||||
|
||||
query = "Show me all products in a table with name, price, and rating."
|
||||
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"\n💡 Table Output:\n{response}")
|
||||
|
||||
|
||||
def list_format_rag():
|
||||
"""Demonstrate RAG with list-formatted output."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("LIST-FORMATTED RAG")
|
||||
print("=" * 60)
|
||||
|
||||
agent = Agent(
|
||||
name="Feature Lister",
|
||||
instructions="""You extract and present information as organized lists.
|
||||
Use bullet points and numbered lists appropriately.
|
||||
Group related items together.""",
|
||||
knowledge=PRODUCT_CATALOG,
|
||||
user_id="list_demo"
|
||||
)
|
||||
|
||||
queries = [
|
||||
"List all product features across the catalog",
|
||||
"What are the top-rated products? List them in order."
|
||||
]
|
||||
|
||||
for query in queries:
|
||||
print(f"\n📝 Query: {query}")
|
||||
response = agent.chat(query)
|
||||
print(f"💡 List Output:\n{response[:400]}..." if len(str(response)) > 400 else f"💡 List Output:\n{response}")
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def constrained_generation():
|
||||
"""Demonstrate constrained/guided generation patterns."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CONSTRAINED GENERATION PATTERNS")
|
||||
print("=" * 60)
|
||||
|
||||
print("""
|
||||
🎯 Constrained Generation Techniques:
|
||||
|
||||
1. **Schema Enforcement**
|
||||
- Provide JSON schema in instructions
|
||||
- Agent outputs valid JSON matching schema
|
||||
```python
|
||||
instructions = f"Respond with JSON matching: {schema}"
|
||||
```
|
||||
|
||||
2. **Format Templates**
|
||||
- Give explicit output templates
|
||||
- Agent fills in the blanks
|
||||
```python
|
||||
instructions = '''
|
||||
Respond in this format:
|
||||
PRODUCT: [name]
|
||||
PRICE: $[amount]
|
||||
VERDICT: [recommendation]
|
||||
'''
|
||||
```
|
||||
|
||||
3. **Pydantic Validation**
|
||||
- Parse agent output with Pydantic
|
||||
- Retry if validation fails
|
||||
```python
|
||||
response = agent.chat(query)
|
||||
try:
|
||||
result = ProductInfo.model_validate_json(response)
|
||||
except ValidationError:
|
||||
# Retry with clarification
|
||||
```
|
||||
|
||||
4. **Output Parsers**
|
||||
- Post-process agent output
|
||||
- Extract structured data from text
|
||||
```python
|
||||
def parse_product(text: str) -> dict:
|
||||
# Extract price, rating, etc.
|
||||
return structured_data
|
||||
```
|
||||
|
||||
5. **Few-Shot Examples**
|
||||
- Show examples of desired output format
|
||||
- Agent learns pattern from examples
|
||||
```python
|
||||
instructions = '''
|
||||
Example:
|
||||
Q: Tell me about Product X
|
||||
A: {"name": "Product X", "price": 99.99}
|
||||
|
||||
Now answer the user's question in the same format.
|
||||
'''
|
||||
```
|
||||
""")
|
||||
|
||||
|
||||
def multi_format_rag():
|
||||
"""Demonstrate switching between output formats."""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("MULTI-FORMAT RAG")
|
||||
print("=" * 60)
|
||||
|
||||
base_knowledge = PRODUCT_CATALOG
|
||||
query = "Tell me about the ErgoDesk Standing Desk"
|
||||
|
||||
formats = [
|
||||
("JSON", "Respond with a JSON object containing name, price, and features."),
|
||||
("Markdown", "Respond with a markdown-formatted product card with headers."),
|
||||
("Plain Text", "Respond with a brief, conversational product description."),
|
||||
("Bullet Points", "Respond with bullet points covering key product details.")
|
||||
]
|
||||
|
||||
print(f"\n📝 Base Query: {query}")
|
||||
print("\n🔄 Same query, different output formats:\n")
|
||||
|
||||
for format_name, format_instruction in formats:
|
||||
agent = Agent(
|
||||
name=f"{format_name} Agent",
|
||||
instructions=f"You are a product expert. {format_instruction}",
|
||||
knowledge=base_knowledge,
|
||||
user_id=f"format_{format_name.lower()}"
|
||||
)
|
||||
|
||||
response = agent.chat(query)
|
||||
print(f"📋 {format_name} Format:")
|
||||
print(f"{response[:250]}..." if len(str(response)) > 250 else response)
|
||||
print("-" * 40)
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all structured output RAG examples."""
|
||||
print("\n🚀 PraisonAI Structured Output RAG Examples\n")
|
||||
|
||||
# Example 1: JSON output
|
||||
json_output_rag()
|
||||
|
||||
# Example 2: Pydantic-guided
|
||||
pydantic_guided_rag()
|
||||
|
||||
# Example 3: Table format
|
||||
table_format_rag()
|
||||
|
||||
# Example 4: List format
|
||||
list_format_rag()
|
||||
|
||||
# Example 5: Constrained generation patterns
|
||||
constrained_generation()
|
||||
|
||||
# Example 6: Multi-format
|
||||
multi_format_rag()
|
||||
|
||||
print("\n✅ Structured output RAG examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,30 +1,151 @@
|
||||
"""
|
||||
Context module for PraisonAI Agents.
|
||||
Context Management Module for PraisonAI Agents.
|
||||
|
||||
This module provides context retrieval capabilities including:
|
||||
This module provides comprehensive context management capabilities:
|
||||
- Token estimation and budgeting
|
||||
- Context composition within limits
|
||||
- Optimization strategies (truncate, sliding window, summarize, prune, smart)
|
||||
- Real-time context monitoring
|
||||
- Multi-agent context isolation
|
||||
|
||||
Also includes:
|
||||
- Fast Context: Rapid parallel code search subagent
|
||||
"""
|
||||
|
||||
# Lazy imports to avoid impacting package load time
|
||||
def __getattr__(name):
|
||||
"""Lazy import to avoid impacting package performance."""
|
||||
if name == "FastContext":
|
||||
from praisonaiagents.context.fast.fast_context import FastContext
|
||||
return FastContext
|
||||
elif name == "FastContextResult":
|
||||
from praisonaiagents.context.fast.result import FastContextResult
|
||||
return FastContextResult
|
||||
elif name == "FileMatch":
|
||||
from praisonaiagents.context.fast.result import FileMatch
|
||||
return FileMatch
|
||||
elif name == "LineRange":
|
||||
from praisonaiagents.context.fast.result import LineRange
|
||||
return LineRange
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
Zero Performance Impact:
|
||||
- All imports are lazy loaded via __getattr__
|
||||
- Features only activate when explicitly enabled
|
||||
- No overhead when context management is not used
|
||||
|
||||
Usage:
|
||||
from praisonaiagents.context import (
|
||||
ContextBudgeter,
|
||||
ContextComposer,
|
||||
ContextMonitor,
|
||||
get_optimizer,
|
||||
)
|
||||
|
||||
# Create budgeter for model
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
budget = budgeter.allocate()
|
||||
|
||||
# Compose context within budget
|
||||
composer = ContextComposer(budget=budget)
|
||||
result = composer.compose(
|
||||
system_prompt="You are helpful",
|
||||
history=messages,
|
||||
tools=tool_schemas,
|
||||
)
|
||||
|
||||
# Monitor context (opt-in)
|
||||
monitor = ContextMonitor(enabled=True)
|
||||
monitor.snapshot(ledger=result.ledger, budget=budget, messages=result.messages)
|
||||
"""
|
||||
|
||||
__all__ = [
|
||||
# Models
|
||||
"ContextSegment",
|
||||
"ContextLedger",
|
||||
"BudgetAllocation",
|
||||
"ContextSnapshot",
|
||||
"OptimizerStrategy",
|
||||
"OptimizationResult",
|
||||
"MonitorConfig",
|
||||
"ContextConfig",
|
||||
# Token estimation
|
||||
"estimate_tokens_heuristic",
|
||||
"estimate_messages_tokens",
|
||||
"estimate_tool_schema_tokens",
|
||||
"TokenEstimatorImpl",
|
||||
"get_estimator",
|
||||
# Budgeting
|
||||
"ContextBudgeter",
|
||||
"get_model_limit",
|
||||
"get_output_reserve",
|
||||
# Ledger
|
||||
"ContextLedgerManager",
|
||||
"MultiAgentLedger",
|
||||
# Composition
|
||||
"ContextComposer",
|
||||
"ComposedContext",
|
||||
# Optimization
|
||||
"BaseOptimizer",
|
||||
"TruncateOptimizer",
|
||||
"SlidingWindowOptimizer",
|
||||
"PruneToolsOptimizer",
|
||||
"NonDestructiveOptimizer",
|
||||
"SummarizeOptimizer",
|
||||
"SmartOptimizer",
|
||||
"get_optimizer",
|
||||
"get_effective_history",
|
||||
# Monitoring
|
||||
"ContextMonitor",
|
||||
"MultiAgentMonitor",
|
||||
"redact_sensitive",
|
||||
"format_human_snapshot",
|
||||
"format_json_snapshot",
|
||||
# Fast Context (legacy)
|
||||
"FastContext",
|
||||
"FastContextResult",
|
||||
"FileMatch",
|
||||
"LineRange",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str):
|
||||
"""Lazy load module components to avoid import overhead."""
|
||||
# Models
|
||||
if name in ("ContextSegment", "ContextLedger", "BudgetAllocation",
|
||||
"ContextSnapshot", "OptimizerStrategy", "OptimizationResult",
|
||||
"MonitorConfig", "ContextConfig"):
|
||||
from . import models
|
||||
return getattr(models, name)
|
||||
|
||||
# Token estimation
|
||||
if name in ("estimate_tokens_heuristic", "estimate_messages_tokens",
|
||||
"estimate_tool_schema_tokens", "TokenEstimatorImpl", "get_estimator"):
|
||||
from . import tokens
|
||||
return getattr(tokens, name)
|
||||
|
||||
# Budgeting
|
||||
if name in ("ContextBudgeter", "get_model_limit", "get_output_reserve"):
|
||||
from . import budgeter
|
||||
return getattr(budgeter, name)
|
||||
|
||||
# Ledger
|
||||
if name in ("ContextLedgerManager", "MultiAgentLedger"):
|
||||
from . import ledger
|
||||
return getattr(ledger, name)
|
||||
|
||||
# Composition
|
||||
if name in ("ContextComposer", "ComposedContext"):
|
||||
from . import composer
|
||||
return getattr(composer, name)
|
||||
|
||||
# Optimization
|
||||
if name in ("BaseOptimizer", "TruncateOptimizer", "SlidingWindowOptimizer",
|
||||
"PruneToolsOptimizer", "NonDestructiveOptimizer", "SummarizeOptimizer",
|
||||
"SmartOptimizer", "get_optimizer", "get_effective_history"):
|
||||
from . import optimizer
|
||||
return getattr(optimizer, name)
|
||||
|
||||
# Monitoring
|
||||
if name in ("ContextMonitor", "MultiAgentMonitor", "redact_sensitive",
|
||||
"format_human_snapshot", "format_json_snapshot"):
|
||||
from . import monitor
|
||||
return getattr(monitor, name)
|
||||
|
||||
# Fast Context (legacy)
|
||||
if name == "FastContext":
|
||||
from praisonaiagents.context.fast.fast_context import FastContext
|
||||
return FastContext
|
||||
if name == "FastContextResult":
|
||||
from praisonaiagents.context.fast.result import FastContextResult
|
||||
return FastContextResult
|
||||
if name == "FileMatch":
|
||||
from praisonaiagents.context.fast.result import FileMatch
|
||||
return FileMatch
|
||||
if name == "LineRange":
|
||||
from praisonaiagents.context.fast.result import LineRange
|
||||
return LineRange
|
||||
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
@@ -0,0 +1,229 @@
|
||||
"""
|
||||
Context Budgeter for PraisonAI Agents.
|
||||
|
||||
Allocates token budgets across context segments based on model limits.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
from .models import BudgetAllocation
|
||||
|
||||
|
||||
# Model token limits (context window sizes)
|
||||
MODEL_LIMITS: Dict[str, int] = {
|
||||
# OpenAI
|
||||
"gpt-4o": 128000,
|
||||
"gpt-4o-mini": 128000,
|
||||
"gpt-4-turbo": 128000,
|
||||
"gpt-4": 8192,
|
||||
"gpt-3.5-turbo": 16385,
|
||||
"gpt-3.5-turbo-16k": 16385,
|
||||
# Anthropic
|
||||
"claude-3-5-sonnet": 200000,
|
||||
"claude-3-5-haiku": 200000,
|
||||
"claude-3-opus": 200000,
|
||||
"claude-3-sonnet": 200000,
|
||||
"claude-3-haiku": 200000,
|
||||
# Google
|
||||
"gemini-2.0-flash": 1048576,
|
||||
"gemini-1.5-pro": 2097152,
|
||||
"gemini-1.5-flash": 1048576,
|
||||
# Defaults
|
||||
"default": 128000,
|
||||
}
|
||||
|
||||
# Default output reserves by model family
|
||||
OUTPUT_RESERVES: Dict[str, int] = {
|
||||
"gpt-4o": 16384,
|
||||
"gpt-4o-mini": 16384,
|
||||
"gpt-4-turbo": 4096,
|
||||
"gpt-4": 4096,
|
||||
"gpt-3.5-turbo": 4096,
|
||||
"claude-3": 8192,
|
||||
"gemini": 8192,
|
||||
"default": 8000,
|
||||
}
|
||||
|
||||
|
||||
def get_model_limit(model: str) -> int:
|
||||
"""
|
||||
Get context window limit for a model.
|
||||
|
||||
Args:
|
||||
model: Model name or identifier
|
||||
|
||||
Returns:
|
||||
Context window size in tokens
|
||||
"""
|
||||
# Exact match
|
||||
if model in MODEL_LIMITS:
|
||||
return MODEL_LIMITS[model]
|
||||
|
||||
# Partial match (e.g., "gpt-4o-2024-05-13" matches "gpt-4o")
|
||||
model_lower = model.lower()
|
||||
for key, limit in MODEL_LIMITS.items():
|
||||
if key in model_lower or model_lower.startswith(key):
|
||||
return limit
|
||||
|
||||
return MODEL_LIMITS["default"]
|
||||
|
||||
|
||||
def get_output_reserve(model: str) -> int:
|
||||
"""
|
||||
Get recommended output token reserve for a model.
|
||||
|
||||
Args:
|
||||
model: Model name
|
||||
|
||||
Returns:
|
||||
Output reserve in tokens
|
||||
"""
|
||||
model_lower = model.lower()
|
||||
|
||||
for key, reserve in OUTPUT_RESERVES.items():
|
||||
if key in model_lower:
|
||||
return reserve
|
||||
|
||||
return OUTPUT_RESERVES["default"]
|
||||
|
||||
|
||||
class ContextBudgeter:
|
||||
"""
|
||||
Allocates token budgets across context segments.
|
||||
|
||||
Ensures context fits within model limits while reserving
|
||||
space for output generation.
|
||||
|
||||
Example:
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
allocation = budgeter.allocate()
|
||||
print(f"History budget: {allocation.history_budget}")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: str = "gpt-4o-mini",
|
||||
model_limit: Optional[int] = None,
|
||||
output_reserve: Optional[int] = None,
|
||||
# Segment budget overrides (absolute tokens)
|
||||
system_prompt_budget: int = 2000,
|
||||
rules_budget: int = 500,
|
||||
skills_budget: int = 500,
|
||||
memory_budget: int = 1000,
|
||||
tools_schema_budget: int = 2000,
|
||||
tool_outputs_budget: int = 20000,
|
||||
buffer_budget: int = 1000,
|
||||
# Ratio-based allocation (if set, overrides fixed budgets)
|
||||
history_ratio: Optional[float] = None,
|
||||
):
|
||||
"""
|
||||
Initialize budgeter.
|
||||
|
||||
Args:
|
||||
model: Model name for limit lookup
|
||||
model_limit: Override model limit
|
||||
output_reserve: Override output reserve
|
||||
system_prompt_budget: Budget for system prompt
|
||||
rules_budget: Budget for rules
|
||||
skills_budget: Budget for skills
|
||||
memory_budget: Budget for memory
|
||||
tools_schema_budget: Budget for tool schemas
|
||||
tool_outputs_budget: Budget for tool outputs
|
||||
buffer_budget: Safety buffer
|
||||
history_ratio: If set, allocate this ratio of usable for history
|
||||
"""
|
||||
self.model = model
|
||||
self.model_limit = model_limit or get_model_limit(model)
|
||||
self.output_reserve = output_reserve or get_output_reserve(model)
|
||||
|
||||
self.system_prompt_budget = system_prompt_budget
|
||||
self.rules_budget = rules_budget
|
||||
self.skills_budget = skills_budget
|
||||
self.memory_budget = memory_budget
|
||||
self.tools_schema_budget = tools_schema_budget
|
||||
self.tool_outputs_budget = tool_outputs_budget
|
||||
self.buffer_budget = buffer_budget
|
||||
self.history_ratio = history_ratio
|
||||
|
||||
@property
|
||||
def usable(self) -> int:
|
||||
"""Usable tokens after output reserve."""
|
||||
return self.model_limit - self.output_reserve
|
||||
|
||||
def allocate(self) -> BudgetAllocation:
|
||||
"""
|
||||
Create a budget allocation.
|
||||
|
||||
Returns:
|
||||
BudgetAllocation with segment budgets
|
||||
"""
|
||||
allocation = BudgetAllocation(
|
||||
model_limit=self.model_limit,
|
||||
output_reserve=self.output_reserve,
|
||||
system_prompt=self.system_prompt_budget,
|
||||
rules=self.rules_budget,
|
||||
skills=self.skills_budget,
|
||||
memory=self.memory_budget,
|
||||
tools_schema=self.tools_schema_budget,
|
||||
tool_outputs=self.tool_outputs_budget,
|
||||
buffer=self.buffer_budget,
|
||||
history=-1, # Dynamic
|
||||
)
|
||||
|
||||
# If history_ratio is set, compute history budget from ratio
|
||||
if self.history_ratio is not None:
|
||||
allocation.history = int(self.usable * self.history_ratio)
|
||||
|
||||
return allocation
|
||||
|
||||
def check_overflow(
|
||||
self,
|
||||
current_tokens: int,
|
||||
threshold: float = 0.8
|
||||
) -> bool:
|
||||
"""
|
||||
Check if current tokens exceed threshold.
|
||||
|
||||
Args:
|
||||
current_tokens: Current total tokens
|
||||
threshold: Threshold as fraction of usable (0.0-1.0)
|
||||
|
||||
Returns:
|
||||
True if overflow threshold exceeded
|
||||
"""
|
||||
return current_tokens > (self.usable * threshold)
|
||||
|
||||
def get_remaining(self, current_tokens: int) -> int:
|
||||
"""
|
||||
Get remaining tokens before hitting usable limit.
|
||||
|
||||
Args:
|
||||
current_tokens: Current total tokens
|
||||
|
||||
Returns:
|
||||
Remaining tokens (can be negative if over limit)
|
||||
"""
|
||||
return self.usable - current_tokens
|
||||
|
||||
def get_utilization(self, current_tokens: int) -> float:
|
||||
"""
|
||||
Get current utilization as fraction.
|
||||
|
||||
Args:
|
||||
current_tokens: Current total tokens
|
||||
|
||||
Returns:
|
||||
Utilization (0.0-1.0+)
|
||||
"""
|
||||
if self.usable == 0:
|
||||
return 0.0
|
||||
return current_tokens / self.usable
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert budgeter state to dictionary."""
|
||||
return {
|
||||
"model": self.model,
|
||||
"model_limit": self.model_limit,
|
||||
"output_reserve": self.output_reserve,
|
||||
"usable": self.usable,
|
||||
"allocation": self.allocate().to_dict(),
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
"""
|
||||
Context Composer for PraisonAI Agents.
|
||||
|
||||
Assembles context from segments within budget constraints.
|
||||
"""
|
||||
|
||||
from typing import Dict, List, Any, Optional
|
||||
from .models import (
|
||||
ContextLedger, BudgetAllocation, ContextSegment, ContextSnapshot
|
||||
)
|
||||
from .tokens import estimate_tokens_heuristic, estimate_messages_tokens
|
||||
from .ledger import ContextLedgerManager
|
||||
|
||||
|
||||
class ContextComposer:
|
||||
"""
|
||||
Composes context from segments within budget constraints.
|
||||
|
||||
Assembles system prompt, rules, skills, memory, tools, and history
|
||||
while respecting token budgets and applying trimming as needed.
|
||||
|
||||
Example:
|
||||
composer = ContextComposer(budget=budgeter.allocate())
|
||||
|
||||
result = composer.compose(
|
||||
system_prompt="You are a helpful assistant",
|
||||
history=messages,
|
||||
tools=tool_schemas,
|
||||
)
|
||||
|
||||
# Use composed messages
|
||||
response = llm.chat(result.messages)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
budget: Optional[BudgetAllocation] = None,
|
||||
preserve_tool_pairs: bool = True,
|
||||
max_tool_output_tokens: int = 10000,
|
||||
keep_recent_turns: int = 5,
|
||||
):
|
||||
"""
|
||||
Initialize composer.
|
||||
|
||||
Args:
|
||||
budget: Budget allocation (uses defaults if None)
|
||||
preserve_tool_pairs: Keep tool_call/tool_result pairs together
|
||||
max_tool_output_tokens: Max tokens per tool output
|
||||
keep_recent_turns: Minimum recent turns to keep
|
||||
"""
|
||||
self.budget = budget or BudgetAllocation()
|
||||
self.preserve_tool_pairs = preserve_tool_pairs
|
||||
self.max_tool_output_tokens = max_tool_output_tokens
|
||||
self.keep_recent_turns = keep_recent_turns
|
||||
|
||||
self._ledger = ContextLedgerManager()
|
||||
self._ledger.set_budget(self.budget)
|
||||
|
||||
def compose(
|
||||
self,
|
||||
system_prompt: str = "",
|
||||
rules: str = "",
|
||||
skills: str = "",
|
||||
memory: str = "",
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
history: Optional[List[Dict[str, Any]]] = None,
|
||||
agent_id: Optional[str] = None,
|
||||
) -> "ComposedContext":
|
||||
"""
|
||||
Compose context from segments.
|
||||
|
||||
Args:
|
||||
system_prompt: System prompt content
|
||||
rules: Rules content
|
||||
skills: Skills content
|
||||
memory: Memory content
|
||||
tools: Tool schemas
|
||||
history: Conversation history
|
||||
agent_id: Agent identifier
|
||||
|
||||
Returns:
|
||||
ComposedContext with messages and ledger
|
||||
"""
|
||||
self._ledger.reset()
|
||||
tools = tools or []
|
||||
history = history or []
|
||||
|
||||
# Track fixed segments
|
||||
self._ledger.track_system_prompt(system_prompt)
|
||||
self._ledger.track_rules(rules)
|
||||
self._ledger.track_skills(skills)
|
||||
self._ledger.track_memory(memory)
|
||||
self._ledger.track_tools(tools)
|
||||
|
||||
# Build combined system message
|
||||
system_parts = []
|
||||
if system_prompt:
|
||||
system_parts.append(system_prompt)
|
||||
if rules:
|
||||
system_parts.append(f"\n## Rules\n{rules}")
|
||||
if skills:
|
||||
system_parts.append(f"\n## Skills\n{skills}")
|
||||
if memory:
|
||||
system_parts.append(f"\n## Memory\n{memory}")
|
||||
|
||||
combined_system = "\n".join(system_parts)
|
||||
|
||||
# Process history with budget awareness
|
||||
processed_history = self._process_history(history)
|
||||
|
||||
# Build final messages
|
||||
messages = []
|
||||
if combined_system:
|
||||
messages.append({"role": "system", "content": combined_system})
|
||||
messages.extend(processed_history)
|
||||
|
||||
# Track final history tokens
|
||||
history_tokens = estimate_messages_tokens(processed_history)
|
||||
self._ledger.track_segment(
|
||||
ContextSegment.HISTORY,
|
||||
history_tokens,
|
||||
len(processed_history)
|
||||
)
|
||||
|
||||
# Check for overflow
|
||||
total = self._ledger.get_total()
|
||||
overflow = total > self.budget.usable
|
||||
|
||||
return ComposedContext(
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
ledger=self._ledger.get_ledger(),
|
||||
total_tokens=total,
|
||||
overflow=overflow,
|
||||
warnings=self._ledger.get_warnings(),
|
||||
agent_id=agent_id,
|
||||
)
|
||||
|
||||
def _process_history(
|
||||
self,
|
||||
history: List[Dict[str, Any]]
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Process history with budget constraints.
|
||||
|
||||
Applies:
|
||||
- Tool output truncation
|
||||
- Sliding window if over budget
|
||||
- Tool pair preservation
|
||||
"""
|
||||
if not history:
|
||||
return []
|
||||
|
||||
# First pass: truncate large tool outputs
|
||||
processed = []
|
||||
for msg in history:
|
||||
processed_msg = self._truncate_tool_output(msg)
|
||||
processed.append(processed_msg)
|
||||
|
||||
# Check if within budget
|
||||
history_budget = self.budget.history_budget
|
||||
current_tokens = estimate_messages_tokens(processed)
|
||||
|
||||
if current_tokens <= history_budget:
|
||||
return processed
|
||||
|
||||
# Apply sliding window
|
||||
return self._apply_sliding_window(processed, history_budget)
|
||||
|
||||
def _truncate_tool_output(self, msg: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Truncate tool output if too large."""
|
||||
if msg.get("role") != "tool" and not msg.get("tool_call_id"):
|
||||
return msg
|
||||
|
||||
content = msg.get("content", "")
|
||||
if not isinstance(content, str):
|
||||
return msg
|
||||
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
if tokens <= self.max_tool_output_tokens:
|
||||
return msg
|
||||
|
||||
# Truncate to max tokens (rough: 4 chars per token)
|
||||
max_chars = self.max_tool_output_tokens * 4
|
||||
truncated = content[:max_chars] + "\n...[output truncated]..."
|
||||
|
||||
result = msg.copy()
|
||||
result["content"] = truncated
|
||||
result["_truncated"] = True
|
||||
return result
|
||||
|
||||
def _apply_sliding_window(
|
||||
self,
|
||||
history: List[Dict[str, Any]],
|
||||
budget: int
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Apply sliding window to fit within budget."""
|
||||
if not history:
|
||||
return []
|
||||
|
||||
# Always keep at least recent turns
|
||||
min_keep = self.keep_recent_turns * 2 # user + assistant per turn
|
||||
|
||||
# Start from end, add messages until budget exceeded
|
||||
result = []
|
||||
current_tokens = 0
|
||||
|
||||
# Track tool call IDs to preserve pairs
|
||||
tool_call_ids = set()
|
||||
|
||||
for msg in reversed(history):
|
||||
msg_tokens = estimate_tokens_heuristic(str(msg.get("content", "")))
|
||||
|
||||
if current_tokens + msg_tokens > budget and len(result) >= min_keep:
|
||||
break
|
||||
|
||||
result.insert(0, msg)
|
||||
current_tokens += msg_tokens
|
||||
|
||||
# Track tool calls for pair preservation
|
||||
if self.preserve_tool_pairs:
|
||||
if "tool_calls" in msg:
|
||||
for tc in msg.get("tool_calls", []):
|
||||
if isinstance(tc, dict) and "id" in tc:
|
||||
tool_call_ids.add(tc["id"])
|
||||
if msg.get("tool_call_id"):
|
||||
tool_call_ids.add(msg["tool_call_id"])
|
||||
|
||||
# Ensure tool pairs are complete
|
||||
if self.preserve_tool_pairs:
|
||||
result = self._ensure_tool_pairs(result, history, tool_call_ids)
|
||||
|
||||
return result
|
||||
|
||||
def _ensure_tool_pairs(
|
||||
self,
|
||||
result: List[Dict[str, Any]],
|
||||
full_history: List[Dict[str, Any]],
|
||||
tool_call_ids: set
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Ensure tool_call/tool_result pairs are complete."""
|
||||
# Find tool calls in result
|
||||
result_call_ids = set()
|
||||
result_response_ids = set()
|
||||
|
||||
for msg in result:
|
||||
if "tool_calls" in msg:
|
||||
for tc in msg.get("tool_calls", []):
|
||||
if isinstance(tc, dict) and "id" in tc:
|
||||
result_call_ids.add(tc["id"])
|
||||
if msg.get("tool_call_id"):
|
||||
result_response_ids.add(msg["tool_call_id"])
|
||||
|
||||
# Find missing pairs
|
||||
missing_calls = result_response_ids - result_call_ids
|
||||
missing_responses = result_call_ids - result_response_ids
|
||||
|
||||
if not missing_calls and not missing_responses:
|
||||
return result
|
||||
|
||||
# Add missing messages from full history
|
||||
for msg in full_history:
|
||||
# Add missing tool calls
|
||||
if "tool_calls" in msg:
|
||||
for tc in msg.get("tool_calls", []):
|
||||
if isinstance(tc, dict) and tc.get("id") in missing_calls:
|
||||
if msg not in result:
|
||||
result.insert(0, msg)
|
||||
break
|
||||
|
||||
# Add missing tool responses
|
||||
if msg.get("tool_call_id") in missing_responses:
|
||||
if msg not in result:
|
||||
result.append(msg)
|
||||
|
||||
return result
|
||||
|
||||
def get_ledger(self) -> ContextLedger:
|
||||
"""Get current ledger."""
|
||||
return self._ledger.get_ledger()
|
||||
|
||||
def get_warnings(self) -> List[str]:
|
||||
"""Get current warnings."""
|
||||
return self._ledger.get_warnings()
|
||||
|
||||
|
||||
class ComposedContext:
|
||||
"""Result of context composition."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
tools: List[Dict[str, Any]],
|
||||
ledger: ContextLedger,
|
||||
total_tokens: int,
|
||||
overflow: bool,
|
||||
warnings: List[str],
|
||||
agent_id: Optional[str] = None,
|
||||
):
|
||||
self.messages = messages
|
||||
self.tools = tools
|
||||
self.ledger = ledger
|
||||
self.total_tokens = total_tokens
|
||||
self.overflow = overflow
|
||||
self.warnings = warnings
|
||||
self.agent_id = agent_id
|
||||
|
||||
def to_snapshot(
|
||||
self,
|
||||
session_id: str = "",
|
||||
model_name: str = "",
|
||||
budget: Optional[BudgetAllocation] = None,
|
||||
) -> ContextSnapshot:
|
||||
"""Convert to snapshot for monitoring."""
|
||||
return ContextSnapshot(
|
||||
session_id=session_id,
|
||||
agent_name=self.agent_id or "",
|
||||
model_name=model_name,
|
||||
budget=budget,
|
||||
ledger=self.ledger,
|
||||
utilization=self.total_tokens / budget.usable if budget else 0.0,
|
||||
history_content=self.messages,
|
||||
warnings=self.warnings,
|
||||
)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
return {
|
||||
"message_count": len(self.messages),
|
||||
"tool_count": len(self.tools),
|
||||
"total_tokens": self.total_tokens,
|
||||
"overflow": self.overflow,
|
||||
"warnings": self.warnings,
|
||||
"ledger": self.ledger.to_dict(),
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
"""
|
||||
Context Ledger for PraisonAI Agents.
|
||||
|
||||
Tracks token usage across context segments with per-agent and per-session support.
|
||||
"""
|
||||
|
||||
from typing import Dict, List, Any, Optional
|
||||
from dataclasses import dataclass
|
||||
from .models import ContextLedger, ContextSegment, BudgetAllocation
|
||||
from .tokens import estimate_messages_tokens, estimate_tool_schema_tokens
|
||||
|
||||
|
||||
@dataclass
|
||||
class SegmentStats:
|
||||
"""Statistics for a context segment."""
|
||||
tokens: int = 0
|
||||
count: int = 0 # Number of items (messages, tools, etc.)
|
||||
budget: int = 0
|
||||
|
||||
@property
|
||||
def utilization(self) -> float:
|
||||
"""Utilization as fraction of budget."""
|
||||
if self.budget == 0:
|
||||
return 0.0
|
||||
return self.tokens / self.budget
|
||||
|
||||
@property
|
||||
def over_budget(self) -> bool:
|
||||
"""Check if over budget."""
|
||||
return self.tokens > self.budget if self.budget > 0 else False
|
||||
|
||||
|
||||
class ContextLedgerManager:
|
||||
"""
|
||||
Manages context token accounting.
|
||||
|
||||
Tracks tokens per segment and provides utilization metrics.
|
||||
Supports multi-agent scenarios with per-agent ledgers.
|
||||
|
||||
Example:
|
||||
manager = ContextLedgerManager()
|
||||
manager.set_budget(budgeter.allocate())
|
||||
|
||||
# Track segments
|
||||
manager.track_system_prompt(system_prompt)
|
||||
manager.track_history(messages)
|
||||
manager.track_tools(tool_schemas)
|
||||
|
||||
# Get stats
|
||||
print(manager.get_utilization())
|
||||
print(manager.get_warnings())
|
||||
"""
|
||||
|
||||
def __init__(self, agent_id: Optional[str] = None):
|
||||
"""
|
||||
Initialize ledger manager.
|
||||
|
||||
Args:
|
||||
agent_id: Optional agent identifier for multi-agent tracking
|
||||
"""
|
||||
self.agent_id = agent_id
|
||||
self._ledger = ContextLedger()
|
||||
self._budget: Optional[BudgetAllocation] = None
|
||||
self._segment_details: Dict[str, SegmentStats] = {}
|
||||
|
||||
# Initialize segment stats
|
||||
for segment in ContextSegment:
|
||||
self._segment_details[segment.value] = SegmentStats()
|
||||
|
||||
def set_budget(self, budget: BudgetAllocation) -> None:
|
||||
"""Set the budget allocation."""
|
||||
self._budget = budget
|
||||
|
||||
# Update segment budgets
|
||||
for segment in ContextSegment:
|
||||
self._segment_details[segment.value].budget = budget.get_segment_budget(segment)
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Reset all token counts."""
|
||||
self._ledger = ContextLedger()
|
||||
for segment in ContextSegment:
|
||||
stats = self._segment_details[segment.value]
|
||||
stats.tokens = 0
|
||||
stats.count = 0
|
||||
|
||||
def track_segment(
|
||||
self,
|
||||
segment: ContextSegment,
|
||||
tokens: int,
|
||||
count: int = 1
|
||||
) -> None:
|
||||
"""
|
||||
Track tokens for a segment.
|
||||
|
||||
Args:
|
||||
segment: Context segment
|
||||
tokens: Token count
|
||||
count: Number of items
|
||||
"""
|
||||
self._ledger.set_segment(segment, tokens)
|
||||
stats = self._segment_details[segment.value]
|
||||
stats.tokens = tokens
|
||||
stats.count = count
|
||||
|
||||
def add_to_segment(
|
||||
self,
|
||||
segment: ContextSegment,
|
||||
tokens: int,
|
||||
count: int = 1
|
||||
) -> None:
|
||||
"""
|
||||
Add tokens to a segment.
|
||||
|
||||
Args:
|
||||
segment: Context segment
|
||||
tokens: Tokens to add
|
||||
count: Items to add
|
||||
"""
|
||||
self._ledger.add_segment(segment, tokens)
|
||||
stats = self._segment_details[segment.value]
|
||||
stats.tokens += tokens
|
||||
stats.count += count
|
||||
|
||||
def track_system_prompt(self, content: str) -> int:
|
||||
"""Track system prompt tokens."""
|
||||
from .tokens import estimate_tokens_heuristic
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
self.track_segment(ContextSegment.SYSTEM_PROMPT, tokens)
|
||||
return tokens
|
||||
|
||||
def track_rules(self, content: str) -> int:
|
||||
"""Track rules tokens."""
|
||||
from .tokens import estimate_tokens_heuristic
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
self.track_segment(ContextSegment.RULES, tokens)
|
||||
return tokens
|
||||
|
||||
def track_skills(self, content: str) -> int:
|
||||
"""Track skills tokens."""
|
||||
from .tokens import estimate_tokens_heuristic
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
self.track_segment(ContextSegment.SKILLS, tokens)
|
||||
return tokens
|
||||
|
||||
def track_memory(self, content: str) -> int:
|
||||
"""Track memory tokens."""
|
||||
from .tokens import estimate_tokens_heuristic
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
self.track_segment(ContextSegment.MEMORY, tokens)
|
||||
return tokens
|
||||
|
||||
def track_history(self, messages: List[Dict[str, Any]]) -> int:
|
||||
"""
|
||||
Track conversation history tokens.
|
||||
|
||||
Args:
|
||||
messages: List of message dicts
|
||||
|
||||
Returns:
|
||||
Total tokens
|
||||
"""
|
||||
tokens = estimate_messages_tokens(messages)
|
||||
self.track_segment(ContextSegment.HISTORY, tokens, len(messages))
|
||||
self._ledger.message_count = len(messages)
|
||||
|
||||
# Count turns (user messages)
|
||||
turns = sum(1 for m in messages if m.get("role") == "user")
|
||||
self._ledger.turn_count = turns
|
||||
|
||||
return tokens
|
||||
|
||||
def track_tools(self, tools: List[Dict[str, Any]]) -> int:
|
||||
"""
|
||||
Track tool schema tokens.
|
||||
|
||||
Args:
|
||||
tools: List of tool definitions
|
||||
|
||||
Returns:
|
||||
Total tokens
|
||||
"""
|
||||
tokens = estimate_tool_schema_tokens(tools)
|
||||
self.track_segment(ContextSegment.TOOLS_SCHEMA, tokens, len(tools))
|
||||
return tokens
|
||||
|
||||
def track_tool_output(self, content: str) -> int:
|
||||
"""
|
||||
Add a tool output to tracking.
|
||||
|
||||
Args:
|
||||
content: Tool output content
|
||||
|
||||
Returns:
|
||||
Tokens for this output
|
||||
"""
|
||||
from .tokens import estimate_tokens_heuristic
|
||||
tokens = estimate_tokens_heuristic(content)
|
||||
self.add_to_segment(ContextSegment.TOOL_OUTPUTS, tokens)
|
||||
self._ledger.tool_call_count += 1
|
||||
return tokens
|
||||
|
||||
def get_ledger(self) -> ContextLedger:
|
||||
"""Get current ledger."""
|
||||
return self._ledger.copy()
|
||||
|
||||
def get_total(self) -> int:
|
||||
"""Get total tokens across all segments."""
|
||||
return self._ledger.total
|
||||
|
||||
def get_utilization(self) -> float:
|
||||
"""Get overall utilization as fraction of usable budget."""
|
||||
if self._budget is None:
|
||||
return 0.0
|
||||
usable = self._budget.usable
|
||||
if usable == 0:
|
||||
return 0.0
|
||||
return self._ledger.total / usable
|
||||
|
||||
def get_segment_stats(self, segment: ContextSegment) -> SegmentStats:
|
||||
"""Get stats for a segment."""
|
||||
return self._segment_details[segment.value]
|
||||
|
||||
def get_all_stats(self) -> Dict[str, SegmentStats]:
|
||||
"""Get stats for all segments."""
|
||||
return self._segment_details.copy()
|
||||
|
||||
def get_warnings(self, threshold: float = 0.9) -> List[str]:
|
||||
"""
|
||||
Get warnings for segments near or over budget.
|
||||
|
||||
Args:
|
||||
threshold: Warning threshold (0.0-1.0)
|
||||
|
||||
Returns:
|
||||
List of warning messages
|
||||
"""
|
||||
warnings = []
|
||||
|
||||
for segment in ContextSegment:
|
||||
stats = self._segment_details[segment.value]
|
||||
if stats.budget > 0:
|
||||
if stats.over_budget:
|
||||
warnings.append(
|
||||
f"{segment.value}: OVER BUDGET ({stats.tokens:,} / {stats.budget:,} tokens)"
|
||||
)
|
||||
elif stats.utilization >= threshold:
|
||||
pct = int(stats.utilization * 100)
|
||||
warnings.append(
|
||||
f"{segment.value}: {pct}% of budget ({stats.tokens:,} / {stats.budget:,} tokens)"
|
||||
)
|
||||
|
||||
# Overall utilization warning
|
||||
if self._budget:
|
||||
overall_util = self.get_utilization()
|
||||
if overall_util >= threshold:
|
||||
pct = int(overall_util * 100)
|
||||
warnings.insert(0, f"Overall context: {pct}% of usable budget")
|
||||
|
||||
return warnings
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary for serialization."""
|
||||
return {
|
||||
"agent_id": self.agent_id,
|
||||
"ledger": self._ledger.to_dict(),
|
||||
"total": self._ledger.total,
|
||||
"utilization": self.get_utilization(),
|
||||
"segments": {
|
||||
name: {
|
||||
"tokens": stats.tokens,
|
||||
"count": stats.count,
|
||||
"budget": stats.budget,
|
||||
"utilization": stats.utilization,
|
||||
}
|
||||
for name, stats in self._segment_details.items()
|
||||
},
|
||||
"warnings": self.get_warnings(),
|
||||
}
|
||||
|
||||
|
||||
class MultiAgentLedger:
|
||||
"""
|
||||
Manages ledgers for multiple agents.
|
||||
|
||||
Provides per-agent tracking and combined views.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize multi-agent ledger."""
|
||||
self._agents: Dict[str, ContextLedgerManager] = {}
|
||||
self._shared_tokens: int = 0 # Tokens shared across agents
|
||||
|
||||
def get_agent_ledger(self, agent_id: str) -> ContextLedgerManager:
|
||||
"""Get or create ledger for an agent."""
|
||||
if agent_id not in self._agents:
|
||||
self._agents[agent_id] = ContextLedgerManager(agent_id)
|
||||
return self._agents[agent_id]
|
||||
|
||||
def set_shared_tokens(self, tokens: int) -> None:
|
||||
"""Set tokens shared across agents (e.g., team memory)."""
|
||||
self._shared_tokens = tokens
|
||||
|
||||
def get_combined_total(self) -> int:
|
||||
"""Get total tokens across all agents plus shared."""
|
||||
total = self._shared_tokens
|
||||
for ledger in self._agents.values():
|
||||
total += ledger.get_total()
|
||||
return total
|
||||
|
||||
def get_agent_ids(self) -> List[str]:
|
||||
"""Get list of tracked agent IDs."""
|
||||
return list(self._agents.keys())
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
return {
|
||||
"agents": {
|
||||
agent_id: ledger.to_dict()
|
||||
for agent_id, ledger in self._agents.items()
|
||||
},
|
||||
"shared_tokens": self._shared_tokens,
|
||||
"combined_total": self.get_combined_total(),
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
"""
|
||||
Context Management Data Models for PraisonAI Agents.
|
||||
|
||||
Provides dataclasses and protocols for context budgeting, composition,
|
||||
optimization, and monitoring.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from typing import Dict, List, Any, Optional, Literal, Protocol, runtime_checkable
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ContextSegment(str, Enum):
|
||||
"""Segments that contribute to context."""
|
||||
SYSTEM_PROMPT = "system_prompt"
|
||||
RULES = "rules"
|
||||
SKILLS = "skills"
|
||||
MEMORY = "memory"
|
||||
TOOLS_SCHEMA = "tools_schema"
|
||||
HISTORY = "history"
|
||||
TOOL_OUTPUTS = "tool_outputs"
|
||||
BUFFER = "buffer"
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContextLedger:
|
||||
"""
|
||||
Tracks token usage across context segments.
|
||||
|
||||
Provides per-segment token counts and total usage.
|
||||
"""
|
||||
system_prompt: int = 0
|
||||
rules: int = 0
|
||||
skills: int = 0
|
||||
memory: int = 0
|
||||
tools_schema: int = 0
|
||||
history: int = 0
|
||||
tool_outputs: int = 0
|
||||
buffer: int = 0
|
||||
|
||||
# Metadata
|
||||
turn_count: int = 0
|
||||
message_count: int = 0
|
||||
tool_call_count: int = 0
|
||||
|
||||
@property
|
||||
def total(self) -> int:
|
||||
"""Total tokens across all segments."""
|
||||
return (
|
||||
self.system_prompt +
|
||||
self.rules +
|
||||
self.skills +
|
||||
self.memory +
|
||||
self.tools_schema +
|
||||
self.history +
|
||||
self.tool_outputs +
|
||||
self.buffer
|
||||
)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
return asdict(self)
|
||||
|
||||
def get_segment(self, segment: ContextSegment) -> int:
|
||||
"""Get token count for a segment."""
|
||||
return getattr(self, segment.value, 0)
|
||||
|
||||
def set_segment(self, segment: ContextSegment, tokens: int) -> None:
|
||||
"""Set token count for a segment."""
|
||||
setattr(self, segment.value, tokens)
|
||||
|
||||
def add_segment(self, segment: ContextSegment, tokens: int) -> None:
|
||||
"""Add tokens to a segment."""
|
||||
current = self.get_segment(segment)
|
||||
self.set_segment(segment, current + tokens)
|
||||
|
||||
def copy(self) -> "ContextLedger":
|
||||
"""Create a copy of this ledger."""
|
||||
return ContextLedger(
|
||||
system_prompt=self.system_prompt,
|
||||
rules=self.rules,
|
||||
skills=self.skills,
|
||||
memory=self.memory,
|
||||
tools_schema=self.tools_schema,
|
||||
history=self.history,
|
||||
tool_outputs=self.tool_outputs,
|
||||
buffer=self.buffer,
|
||||
turn_count=self.turn_count,
|
||||
message_count=self.message_count,
|
||||
tool_call_count=self.tool_call_count,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BudgetAllocation:
|
||||
"""
|
||||
Token budget allocation across segments.
|
||||
|
||||
Specifies maximum tokens allowed per segment.
|
||||
"""
|
||||
model_limit: int = 128000
|
||||
output_reserve: int = 8000
|
||||
|
||||
# Segment budgets (absolute tokens or -1 for dynamic)
|
||||
system_prompt: int = 2000
|
||||
rules: int = 500
|
||||
skills: int = 500
|
||||
memory: int = 1000
|
||||
tools_schema: int = 2000
|
||||
history: int = -1 # Dynamic: fills remaining
|
||||
tool_outputs: int = 20000
|
||||
buffer: int = 1000
|
||||
|
||||
@property
|
||||
def usable(self) -> int:
|
||||
"""Usable tokens after output reserve."""
|
||||
return self.model_limit - self.output_reserve
|
||||
|
||||
@property
|
||||
def fixed_total(self) -> int:
|
||||
"""Total of fixed segment budgets."""
|
||||
total = 0
|
||||
for seg in [self.system_prompt, self.rules, self.skills,
|
||||
self.memory, self.tools_schema, self.tool_outputs, self.buffer]:
|
||||
if seg > 0:
|
||||
total += seg
|
||||
return total
|
||||
|
||||
@property
|
||||
def history_budget(self) -> int:
|
||||
"""Computed history budget (remainder after fixed segments)."""
|
||||
if self.history > 0:
|
||||
return self.history
|
||||
return max(0, self.usable - self.fixed_total)
|
||||
|
||||
def get_segment_budget(self, segment: ContextSegment) -> int:
|
||||
"""Get budget for a segment."""
|
||||
if segment == ContextSegment.HISTORY:
|
||||
return self.history_budget
|
||||
return getattr(self, segment.value, 0)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
result = asdict(self)
|
||||
result['usable'] = self.usable
|
||||
result['history_budget'] = self.history_budget
|
||||
return result
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContextSnapshot:
|
||||
"""
|
||||
A snapshot of the current context state.
|
||||
|
||||
Used for monitoring and debugging.
|
||||
"""
|
||||
timestamp: str = ""
|
||||
session_id: str = ""
|
||||
agent_name: str = ""
|
||||
model_name: str = ""
|
||||
|
||||
# Budget info
|
||||
budget: Optional[BudgetAllocation] = None
|
||||
|
||||
# Token accounting
|
||||
ledger: Optional[ContextLedger] = None
|
||||
|
||||
# Utilization
|
||||
utilization: float = 0.0
|
||||
|
||||
# Content segments (for dump)
|
||||
system_prompt_content: str = ""
|
||||
rules_content: str = ""
|
||||
skills_content: str = ""
|
||||
memory_content: str = ""
|
||||
tools_schema_content: str = ""
|
||||
history_content: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
# Warnings
|
||||
warnings: List[str] = field(default_factory=list)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary for JSON serialization."""
|
||||
return {
|
||||
"timestamp": self.timestamp,
|
||||
"session_id": self.session_id,
|
||||
"agent_name": self.agent_name,
|
||||
"model_name": self.model_name,
|
||||
"budget": self.budget.to_dict() if self.budget else None,
|
||||
"ledger": self.ledger.to_dict() if self.ledger else None,
|
||||
"utilization": self.utilization,
|
||||
"warnings": self.warnings,
|
||||
"history_turn_count": len(self.history_content),
|
||||
}
|
||||
|
||||
|
||||
class OptimizerStrategy(str, Enum):
|
||||
"""Context optimization strategies."""
|
||||
TRUNCATE = "truncate"
|
||||
SLIDING_WINDOW = "sliding_window"
|
||||
SUMMARIZE = "summarize"
|
||||
PRUNE_TOOLS = "prune_tools"
|
||||
NON_DESTRUCTIVE = "non_destructive"
|
||||
SMART = "smart"
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptimizationResult:
|
||||
"""Result of context optimization."""
|
||||
original_tokens: int = 0
|
||||
optimized_tokens: int = 0
|
||||
tokens_saved: int = 0
|
||||
strategy_used: OptimizerStrategy = OptimizerStrategy.SMART
|
||||
messages_removed: int = 0
|
||||
messages_tagged: int = 0
|
||||
tool_outputs_pruned: int = 0
|
||||
summary_added: bool = False
|
||||
|
||||
@property
|
||||
def reduction_percent(self) -> float:
|
||||
"""Percentage of tokens reduced."""
|
||||
if self.original_tokens == 0:
|
||||
return 0.0
|
||||
return (self.tokens_saved / self.original_tokens) * 100
|
||||
|
||||
|
||||
@dataclass
|
||||
class MonitorConfig:
|
||||
"""Configuration for context monitoring."""
|
||||
enabled: bool = False
|
||||
path: str = "./context.txt"
|
||||
format: Literal["human", "json"] = "human"
|
||||
frequency: Literal["turn", "tool_call", "manual", "overflow"] = "turn"
|
||||
redact_sensitive: bool = True
|
||||
multi_agent_files: bool = True
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
return asdict(self)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContextConfig:
|
||||
"""
|
||||
Complete context management configuration.
|
||||
|
||||
Merges settings from CLI flags, env vars, and config files.
|
||||
"""
|
||||
# Auto-compaction
|
||||
auto_compact: bool = True
|
||||
compact_threshold: float = 0.8 # Trigger at 80% of usable budget
|
||||
strategy: OptimizerStrategy = OptimizerStrategy.SMART
|
||||
|
||||
# Budget overrides
|
||||
output_reserve: int = 8000
|
||||
history_ratio: float = 0.6 # 60% of usable for history
|
||||
tool_output_max: int = 10000 # Max tokens per tool output
|
||||
|
||||
# Pruning
|
||||
prune_after_tokens: int = 40000
|
||||
protected_tools: List[str] = field(default_factory=list)
|
||||
|
||||
# Monitoring
|
||||
monitor: MonitorConfig = field(default_factory=MonitorConfig)
|
||||
|
||||
# Sliding window
|
||||
keep_recent_turns: int = 5
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
result = asdict(self)
|
||||
result['strategy'] = self.strategy.value
|
||||
return result
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class TokenEstimator(Protocol):
|
||||
"""Protocol for token estimation."""
|
||||
|
||||
def estimate(self, text: str) -> int:
|
||||
"""Estimate tokens for text."""
|
||||
...
|
||||
|
||||
def estimate_messages(self, messages: List[Dict[str, Any]]) -> int:
|
||||
"""Estimate tokens for a list of messages."""
|
||||
...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ContextOptimizer(Protocol):
|
||||
"""Protocol for context optimization strategies."""
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: ContextLedger,
|
||||
) -> tuple[List[Dict[str, Any]], OptimizationResult]:
|
||||
"""Optimize messages to fit within target tokens."""
|
||||
...
|
||||
@@ -0,0 +1,478 @@
|
||||
"""
|
||||
Context Monitor for PraisonAI Agents.
|
||||
|
||||
Writes runtime context to disk for inspection and debugging.
|
||||
"""
|
||||
|
||||
from typing import Dict, List, Any, Optional, Literal
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import json
|
||||
import re
|
||||
|
||||
from .models import (
|
||||
ContextSnapshot, ContextLedger, BudgetAllocation, MonitorConfig, ContextSegment
|
||||
)
|
||||
|
||||
|
||||
# Sensitive patterns for redaction
|
||||
SENSITIVE_PATTERNS = [
|
||||
r'sk-[a-zA-Z0-9]{20,}', # OpenAI API keys
|
||||
r'sk-proj-[a-zA-Z0-9_-]+', # OpenAI project keys
|
||||
r'AKIA[0-9A-Z]{16}', # AWS access keys
|
||||
r'[a-zA-Z0-9]{32,}', # Generic long tokens (conservative)
|
||||
r'password["\s:=]+["\']?[^"\'\s]+', # Password patterns
|
||||
r'secret["\s:=]+["\']?[^"\'\s]+', # Secret patterns
|
||||
r'token["\s:=]+["\']?[^"\'\s]+', # Token patterns
|
||||
r'api[_-]?key["\s:=]+["\']?[^"\'\s]+', # API key patterns
|
||||
]
|
||||
|
||||
|
||||
def redact_sensitive(text: str) -> str:
|
||||
"""
|
||||
Redact sensitive information from text.
|
||||
|
||||
Args:
|
||||
text: Text to redact
|
||||
|
||||
Returns:
|
||||
Redacted text
|
||||
"""
|
||||
result = text
|
||||
for pattern in SENSITIVE_PATTERNS:
|
||||
result = re.sub(pattern, '[REDACTED]', result, flags=re.IGNORECASE)
|
||||
return result
|
||||
|
||||
|
||||
def format_human_snapshot(snapshot: ContextSnapshot, redact: bool = True) -> str:
|
||||
"""
|
||||
Format snapshot as human-readable text.
|
||||
|
||||
Args:
|
||||
snapshot: Context snapshot
|
||||
redact: Whether to redact sensitive info
|
||||
|
||||
Returns:
|
||||
Formatted text
|
||||
"""
|
||||
lines = []
|
||||
|
||||
# Header
|
||||
lines.append("=" * 80)
|
||||
lines.append("PRAISONAI CONTEXT SNAPSHOT")
|
||||
lines.append("=" * 80)
|
||||
lines.append(f"Timestamp: {snapshot.timestamp}")
|
||||
lines.append(f"Session ID: {snapshot.session_id}")
|
||||
lines.append(f"Agent: {snapshot.agent_name}")
|
||||
lines.append(f"Model: {snapshot.model_name}")
|
||||
|
||||
if snapshot.budget:
|
||||
lines.append(f"Model Limit: {snapshot.budget.model_limit:,} tokens")
|
||||
lines.append(f"Output Reserve: {snapshot.budget.output_reserve:,} tokens")
|
||||
lines.append(f"Usable Budget: {snapshot.budget.usable:,} tokens")
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Token Ledger
|
||||
lines.append("-" * 80)
|
||||
lines.append("TOKEN LEDGER")
|
||||
lines.append("-" * 80)
|
||||
lines.append(f"{'Component':<20} | {'Tokens':>10} | {'Budget':>10} | {'% Used':>8}")
|
||||
lines.append("-" * 20 + "-+-" + "-" * 10 + "-+-" + "-" * 10 + "-+-" + "-" * 8)
|
||||
|
||||
if snapshot.ledger and snapshot.budget:
|
||||
segments = [
|
||||
(ContextSegment.SYSTEM_PROMPT, "System Prompt"),
|
||||
(ContextSegment.RULES, "Rules"),
|
||||
(ContextSegment.SKILLS, "Skills"),
|
||||
(ContextSegment.MEMORY, "Memory"),
|
||||
(ContextSegment.TOOLS_SCHEMA, "Tool Schemas"),
|
||||
(ContextSegment.HISTORY, "History"),
|
||||
(ContextSegment.TOOL_OUTPUTS, "Tool Outputs"),
|
||||
]
|
||||
|
||||
for seg, name in segments:
|
||||
tokens = snapshot.ledger.get_segment(seg)
|
||||
budget = snapshot.budget.get_segment_budget(seg)
|
||||
pct = (tokens / budget * 100) if budget > 0 else 0
|
||||
lines.append(f"{name:<20} | {tokens:>10,} | {budget:>10,} | {pct:>7.1f}%")
|
||||
|
||||
lines.append("-" * 20 + "-+-" + "-" * 10 + "-+-" + "-" * 10 + "-+-" + "-" * 8)
|
||||
lines.append(f"{'TOTAL':<20} | {snapshot.ledger.total:>10,} | {snapshot.budget.usable:>10,} | {snapshot.utilization * 100:>7.1f}%")
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Warnings
|
||||
if snapshot.warnings:
|
||||
lines.append("-" * 80)
|
||||
lines.append("WARNINGS")
|
||||
lines.append("-" * 80)
|
||||
for warning in snapshot.warnings:
|
||||
lines.append(f"⚠ {warning}")
|
||||
lines.append("")
|
||||
|
||||
# History summary
|
||||
if snapshot.history_content:
|
||||
lines.append("-" * 80)
|
||||
lines.append(f"CONVERSATION HISTORY ({len(snapshot.history_content)} messages)")
|
||||
lines.append("-" * 80)
|
||||
|
||||
for i, msg in enumerate(snapshot.history_content):
|
||||
role = msg.get("role", "unknown").upper()
|
||||
content = msg.get("content", "")
|
||||
|
||||
if isinstance(content, str):
|
||||
if redact:
|
||||
content = redact_sensitive(content)
|
||||
# Truncate for display
|
||||
if len(content) > 200:
|
||||
content = content[:200] + "..."
|
||||
lines.append(f"[{i+1}] {role}: {content}")
|
||||
else:
|
||||
lines.append(f"[{i+1}] {role}: [complex content]")
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Footer
|
||||
lines.append("=" * 80)
|
||||
lines.append("END CONTEXT SNAPSHOT")
|
||||
lines.append("=" * 80)
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def format_json_snapshot(snapshot: ContextSnapshot, redact: bool = True) -> str:
|
||||
"""
|
||||
Format snapshot as JSON.
|
||||
|
||||
Args:
|
||||
snapshot: Context snapshot
|
||||
redact: Whether to redact sensitive info
|
||||
|
||||
Returns:
|
||||
JSON string
|
||||
"""
|
||||
data = snapshot.to_dict()
|
||||
|
||||
# Add history with optional redaction
|
||||
if snapshot.history_content:
|
||||
history = []
|
||||
for msg in snapshot.history_content:
|
||||
msg_copy = msg.copy()
|
||||
content = msg_copy.get("content", "")
|
||||
if isinstance(content, str) and redact:
|
||||
msg_copy["content"] = redact_sensitive(content)
|
||||
history.append(msg_copy)
|
||||
data["history"] = history
|
||||
|
||||
return json.dumps(data, indent=2, default=str)
|
||||
|
||||
|
||||
class ContextMonitor:
|
||||
"""
|
||||
Monitors and writes context to disk.
|
||||
|
||||
Provides real-time visibility into context state for debugging
|
||||
and optimization.
|
||||
|
||||
Example:
|
||||
monitor = ContextMonitor(
|
||||
enabled=True,
|
||||
path="./context.txt",
|
||||
format="human",
|
||||
)
|
||||
|
||||
# Write snapshot
|
||||
monitor.snapshot(
|
||||
ledger=ledger,
|
||||
budget=budget,
|
||||
messages=messages,
|
||||
agent_name="Assistant",
|
||||
)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool = False,
|
||||
path: str = "./context.txt",
|
||||
format: Literal["human", "json"] = "human",
|
||||
frequency: Literal["turn", "tool_call", "manual", "overflow"] = "turn",
|
||||
redact_sensitive: bool = True,
|
||||
multi_agent_files: bool = True,
|
||||
):
|
||||
"""
|
||||
Initialize monitor.
|
||||
|
||||
Args:
|
||||
enabled: Whether monitoring is enabled
|
||||
path: Output file path
|
||||
format: Output format (human or json)
|
||||
frequency: Update frequency
|
||||
redact_sensitive: Redact sensitive info
|
||||
multi_agent_files: Create per-agent files
|
||||
"""
|
||||
self.enabled = enabled
|
||||
self.path = Path(path)
|
||||
self.format = format
|
||||
self.frequency = frequency
|
||||
self.redact = redact_sensitive
|
||||
self.multi_agent_files = multi_agent_files
|
||||
|
||||
self._last_snapshot: Optional[ContextSnapshot] = None
|
||||
self._turn_count = 0
|
||||
self._tool_call_count = 0
|
||||
|
||||
@classmethod
|
||||
def from_config(cls, config: MonitorConfig) -> "ContextMonitor":
|
||||
"""Create monitor from config."""
|
||||
return cls(
|
||||
enabled=config.enabled,
|
||||
path=config.path,
|
||||
format=config.format,
|
||||
frequency=config.frequency,
|
||||
redact_sensitive=config.redact_sensitive,
|
||||
multi_agent_files=config.multi_agent_files,
|
||||
)
|
||||
|
||||
def enable(self) -> None:
|
||||
"""Enable monitoring."""
|
||||
self.enabled = True
|
||||
|
||||
def disable(self) -> None:
|
||||
"""Disable monitoring."""
|
||||
self.enabled = False
|
||||
|
||||
def set_path(self, path: str) -> None:
|
||||
"""Set output path."""
|
||||
self.path = Path(path)
|
||||
|
||||
def set_format(self, format: Literal["human", "json"]) -> None:
|
||||
"""Set output format."""
|
||||
self.format = format
|
||||
|
||||
def set_frequency(self, frequency: Literal["turn", "tool_call", "manual", "overflow"]) -> None:
|
||||
"""Set update frequency."""
|
||||
self.frequency = frequency
|
||||
|
||||
def should_write(
|
||||
self,
|
||||
trigger: Literal["turn", "tool_call", "manual", "overflow"]
|
||||
) -> bool:
|
||||
"""Check if should write based on frequency."""
|
||||
if not self.enabled:
|
||||
return False
|
||||
|
||||
if trigger == "manual":
|
||||
return True
|
||||
|
||||
if self.frequency == "manual":
|
||||
return trigger == "manual"
|
||||
|
||||
if self.frequency == "overflow":
|
||||
return trigger == "overflow"
|
||||
|
||||
if self.frequency == "tool_call":
|
||||
return trigger in ("tool_call", "overflow")
|
||||
|
||||
# frequency == "turn"
|
||||
return trigger in ("turn", "overflow")
|
||||
|
||||
def snapshot(
|
||||
self,
|
||||
ledger: ContextLedger,
|
||||
budget: BudgetAllocation,
|
||||
messages: List[Dict[str, Any]],
|
||||
session_id: str = "",
|
||||
agent_name: str = "",
|
||||
model_name: str = "",
|
||||
trigger: Literal["turn", "tool_call", "manual", "overflow"] = "turn",
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
Create and write a context snapshot.
|
||||
|
||||
Args:
|
||||
ledger: Current token ledger
|
||||
budget: Budget allocation
|
||||
messages: Current messages
|
||||
session_id: Session identifier
|
||||
agent_name: Agent name
|
||||
model_name: Model name
|
||||
trigger: What triggered this snapshot
|
||||
|
||||
Returns:
|
||||
Path to written file, or None if not written
|
||||
"""
|
||||
if not self.should_write(trigger):
|
||||
return None
|
||||
|
||||
# Track counts
|
||||
if trigger == "turn":
|
||||
self._turn_count += 1
|
||||
elif trigger == "tool_call":
|
||||
self._tool_call_count += 1
|
||||
|
||||
# Create snapshot
|
||||
snapshot = ContextSnapshot(
|
||||
timestamp=datetime.utcnow().isoformat() + "Z",
|
||||
session_id=session_id,
|
||||
agent_name=agent_name,
|
||||
model_name=model_name,
|
||||
budget=budget,
|
||||
ledger=ledger,
|
||||
utilization=ledger.total / budget.usable if budget.usable > 0 else 0,
|
||||
history_content=messages,
|
||||
warnings=self._get_warnings(ledger, budget),
|
||||
)
|
||||
|
||||
self._last_snapshot = snapshot
|
||||
|
||||
# Determine output path
|
||||
output_path = self._get_output_path(agent_name)
|
||||
|
||||
# Format content
|
||||
if self.format == "json":
|
||||
content = format_json_snapshot(snapshot, self.redact)
|
||||
else:
|
||||
content = format_human_snapshot(snapshot, self.redact)
|
||||
|
||||
# Atomic write
|
||||
self._atomic_write(output_path, content)
|
||||
|
||||
return str(output_path)
|
||||
|
||||
def _get_output_path(self, agent_name: str = "") -> Path:
|
||||
"""Get output path, optionally per-agent."""
|
||||
if not self.multi_agent_files or not agent_name:
|
||||
return self.path
|
||||
|
||||
# Create per-agent path
|
||||
stem = self.path.stem
|
||||
suffix = self.path.suffix or (".json" if self.format == "json" else ".txt")
|
||||
|
||||
# Sanitize agent name
|
||||
safe_name = re.sub(r'[^\w\-]', '_', agent_name.lower())
|
||||
|
||||
return self.path.parent / f"{stem}_{safe_name}{suffix}"
|
||||
|
||||
def _get_warnings(
|
||||
self,
|
||||
ledger: ContextLedger,
|
||||
budget: BudgetAllocation
|
||||
) -> List[str]:
|
||||
"""Generate warnings based on current state."""
|
||||
warnings = []
|
||||
|
||||
utilization = ledger.total / budget.usable if budget.usable > 0 else 0
|
||||
|
||||
if utilization >= 1.0:
|
||||
warnings.append(f"CRITICAL: Context overflow! {utilization*100:.1f}% of usable budget")
|
||||
elif utilization >= 0.9:
|
||||
warnings.append(f"WARNING: Context at {utilization*100:.1f}% of usable budget")
|
||||
elif utilization >= 0.8:
|
||||
warnings.append(f"NOTICE: Context at {utilization*100:.1f}% of usable budget")
|
||||
|
||||
# Check individual segments
|
||||
if budget.tool_outputs > 0:
|
||||
tool_util = ledger.tool_outputs / budget.tool_outputs
|
||||
if tool_util >= 0.9:
|
||||
warnings.append(f"Tool outputs at {tool_util*100:.1f}% of budget")
|
||||
|
||||
return warnings
|
||||
|
||||
def _atomic_write(self, path: Path, content: str) -> None:
|
||||
"""Write content atomically using temp file + rename."""
|
||||
# Ensure parent directory exists
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Write to temp file
|
||||
temp_path = path.with_suffix(path.suffix + ".tmp")
|
||||
|
||||
try:
|
||||
temp_path.write_text(content, encoding="utf-8")
|
||||
# Atomic rename
|
||||
temp_path.rename(path)
|
||||
except Exception:
|
||||
# Clean up temp file on error
|
||||
if temp_path.exists():
|
||||
temp_path.unlink()
|
||||
raise
|
||||
|
||||
def get_last_snapshot(self) -> Optional[ContextSnapshot]:
|
||||
"""Get the last snapshot."""
|
||||
return self._last_snapshot
|
||||
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
"""Get monitor statistics."""
|
||||
return {
|
||||
"enabled": self.enabled,
|
||||
"path": str(self.path),
|
||||
"format": self.format,
|
||||
"frequency": self.frequency,
|
||||
"turn_count": self._turn_count,
|
||||
"tool_call_count": self._tool_call_count,
|
||||
"last_snapshot": self._last_snapshot.timestamp if self._last_snapshot else None,
|
||||
}
|
||||
|
||||
|
||||
class MultiAgentMonitor:
|
||||
"""
|
||||
Manages monitors for multiple agents.
|
||||
|
||||
Creates per-agent monitors and optional combined view.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_path: str = "./context",
|
||||
format: Literal["human", "json"] = "human",
|
||||
enabled: bool = False,
|
||||
redact_sensitive: bool = True,
|
||||
):
|
||||
"""
|
||||
Initialize multi-agent monitor.
|
||||
|
||||
Args:
|
||||
base_path: Base path for output files
|
||||
format: Output format
|
||||
enabled: Whether monitoring is enabled
|
||||
redact_sensitive: Redact sensitive info
|
||||
"""
|
||||
self.base_path = Path(base_path)
|
||||
self.format = format
|
||||
self.enabled = enabled
|
||||
self.redact = redact_sensitive
|
||||
|
||||
self._agents: Dict[str, ContextMonitor] = {}
|
||||
|
||||
def get_agent_monitor(self, agent_id: str) -> ContextMonitor:
|
||||
"""Get or create monitor for an agent."""
|
||||
if agent_id not in self._agents:
|
||||
suffix = ".json" if self.format == "json" else ".txt"
|
||||
safe_id = re.sub(r'[^\w\-]', '_', agent_id.lower())
|
||||
path = self.base_path / f"context_{safe_id}{suffix}"
|
||||
|
||||
self._agents[agent_id] = ContextMonitor(
|
||||
enabled=self.enabled,
|
||||
path=str(path),
|
||||
format=self.format,
|
||||
redact_sensitive=self.redact,
|
||||
multi_agent_files=False, # Already per-agent
|
||||
)
|
||||
|
||||
return self._agents[agent_id]
|
||||
|
||||
def enable_all(self) -> None:
|
||||
"""Enable all monitors."""
|
||||
self.enabled = True
|
||||
for monitor in self._agents.values():
|
||||
monitor.enable()
|
||||
|
||||
def disable_all(self) -> None:
|
||||
"""Disable all monitors."""
|
||||
self.enabled = False
|
||||
for monitor in self._agents.values():
|
||||
monitor.disable()
|
||||
|
||||
def get_agent_ids(self) -> List[str]:
|
||||
"""Get list of monitored agent IDs."""
|
||||
return list(self._agents.keys())
|
||||
@@ -0,0 +1,484 @@
|
||||
"""
|
||||
Context Optimizer for PraisonAI Agents.
|
||||
|
||||
Provides strategies for reducing context size when approaching limits.
|
||||
"""
|
||||
|
||||
from typing import Dict, List, Any, Optional
|
||||
from abc import ABC, abstractmethod
|
||||
from .models import ContextLedger, OptimizerStrategy, OptimizationResult
|
||||
from .tokens import estimate_messages_tokens
|
||||
|
||||
|
||||
class BaseOptimizer(ABC):
|
||||
"""Base class for optimization strategies."""
|
||||
|
||||
@abstractmethod
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
"""
|
||||
Optimize messages to fit within target tokens.
|
||||
|
||||
Args:
|
||||
messages: Messages to optimize
|
||||
target_tokens: Target token count
|
||||
ledger: Current token ledger
|
||||
|
||||
Returns:
|
||||
Tuple of (optimized_messages, OptimizationResult)
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class TruncateOptimizer(BaseOptimizer):
|
||||
"""Truncate oldest messages strategy."""
|
||||
|
||||
def __init__(self, preserve_system: bool = True, preserve_recent: int = 5):
|
||||
self.preserve_system = preserve_system
|
||||
self.preserve_recent = preserve_recent
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
if original_tokens <= target_tokens:
|
||||
return messages, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=original_tokens,
|
||||
tokens_saved=0,
|
||||
strategy_used=OptimizerStrategy.TRUNCATE,
|
||||
)
|
||||
|
||||
# Separate system and other messages
|
||||
system_msgs = []
|
||||
other_msgs = []
|
||||
|
||||
for msg in messages:
|
||||
if self.preserve_system and msg.get("role") == "system":
|
||||
system_msgs.append(msg)
|
||||
else:
|
||||
other_msgs.append(msg)
|
||||
|
||||
# Keep recent messages
|
||||
recent = other_msgs[-self.preserve_recent:] if other_msgs else []
|
||||
|
||||
result = system_msgs + recent
|
||||
|
||||
# Continue removing until under target
|
||||
while estimate_messages_tokens(result) > target_tokens and len(result) > 1:
|
||||
for i, msg in enumerate(result):
|
||||
if msg.get("role") != "system":
|
||||
result.pop(i)
|
||||
break
|
||||
|
||||
optimized_tokens = estimate_messages_tokens(result)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.TRUNCATE,
|
||||
messages_removed=len(messages) - len(result),
|
||||
)
|
||||
|
||||
|
||||
class SlidingWindowOptimizer(BaseOptimizer):
|
||||
"""Keep a sliding window of recent messages."""
|
||||
|
||||
def __init__(self, preserve_system: bool = True):
|
||||
self.preserve_system = preserve_system
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
if original_tokens <= target_tokens:
|
||||
return messages, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=original_tokens,
|
||||
tokens_saved=0,
|
||||
strategy_used=OptimizerStrategy.SLIDING_WINDOW,
|
||||
)
|
||||
|
||||
result = []
|
||||
|
||||
# Keep system messages
|
||||
for msg in messages:
|
||||
if self.preserve_system and msg.get("role") == "system":
|
||||
result.append(msg)
|
||||
|
||||
# Add messages from end until target reached
|
||||
non_system = [m for m in messages if m.get("role") != "system"]
|
||||
|
||||
for msg in reversed(non_system):
|
||||
test_result = result + [msg]
|
||||
if estimate_messages_tokens(test_result) <= target_tokens:
|
||||
result.insert(len([m for m in result if m.get("role") == "system"]), msg)
|
||||
else:
|
||||
break
|
||||
|
||||
# Ensure order
|
||||
system_msgs = [m for m in result if m.get("role") == "system"]
|
||||
other_msgs = [m for m in result if m.get("role") != "system"]
|
||||
result = system_msgs + other_msgs
|
||||
|
||||
optimized_tokens = estimate_messages_tokens(result)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.SLIDING_WINDOW,
|
||||
messages_removed=len(messages) - len(result),
|
||||
)
|
||||
|
||||
|
||||
class PruneToolsOptimizer(BaseOptimizer):
|
||||
"""Prune old tool outputs while keeping tool calls."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
preserve_recent: int = 5,
|
||||
max_output_chars: int = 500,
|
||||
protected_tools: Optional[List[str]] = None,
|
||||
):
|
||||
self.preserve_recent = preserve_recent
|
||||
self.max_output_chars = max_output_chars
|
||||
self.protected_tools = protected_tools or []
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
result = []
|
||||
pruned_count = 0
|
||||
|
||||
# Separate system and other messages
|
||||
system_msgs = [m for m in messages if m.get("role") == "system"]
|
||||
other_msgs = [m for m in messages if m.get("role") != "system"]
|
||||
|
||||
result.extend(system_msgs)
|
||||
|
||||
# Keep recent messages intact
|
||||
recent = other_msgs[-self.preserve_recent:]
|
||||
older = other_msgs[:-self.preserve_recent] if len(other_msgs) > self.preserve_recent else []
|
||||
|
||||
# Prune older tool outputs
|
||||
for msg in older:
|
||||
role = msg.get("role", "")
|
||||
content = msg.get("content", "")
|
||||
|
||||
# Check if this is a tool result
|
||||
if role == "tool" or msg.get("tool_call_id"):
|
||||
# Check if protected
|
||||
tool_name = msg.get("name", "")
|
||||
if tool_name in self.protected_tools:
|
||||
result.append(msg)
|
||||
continue
|
||||
|
||||
# Truncate if too long
|
||||
if isinstance(content, str) and len(content) > self.max_output_chars:
|
||||
pruned_msg = msg.copy()
|
||||
pruned_msg["content"] = content[:self.max_output_chars] + "\n...[output truncated]..."
|
||||
pruned_msg["_pruned"] = True
|
||||
result.append(pruned_msg)
|
||||
pruned_count += 1
|
||||
else:
|
||||
result.append(msg)
|
||||
else:
|
||||
result.append(msg)
|
||||
|
||||
result.extend(recent)
|
||||
|
||||
optimized_tokens = estimate_messages_tokens(result)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.PRUNE_TOOLS,
|
||||
tool_outputs_pruned=pruned_count,
|
||||
)
|
||||
|
||||
|
||||
class NonDestructiveOptimizer(BaseOptimizer):
|
||||
"""
|
||||
Tag messages for exclusion without deleting them.
|
||||
|
||||
Messages are tagged with _condense_parent and excluded from
|
||||
effective history, but can be restored on rewind.
|
||||
"""
|
||||
|
||||
def __init__(self, preserve_recent: int = 3):
|
||||
self.preserve_recent = preserve_recent
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
import uuid
|
||||
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
if original_tokens <= target_tokens:
|
||||
return messages, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=original_tokens,
|
||||
tokens_saved=0,
|
||||
strategy_used=OptimizerStrategy.NON_DESTRUCTIVE,
|
||||
)
|
||||
|
||||
condense_id = str(uuid.uuid4())
|
||||
|
||||
# Separate system and other messages
|
||||
system_msgs = [m for m in messages if m.get("role") == "system"]
|
||||
other_msgs = [m for m in messages if m.get("role") != "system"]
|
||||
|
||||
# Keep recent, tag older
|
||||
keep_start = max(0, len(other_msgs) - self.preserve_recent)
|
||||
|
||||
result = []
|
||||
tagged_count = 0
|
||||
|
||||
# Add system messages
|
||||
result.extend(system_msgs)
|
||||
|
||||
# Tag older messages
|
||||
for i, msg in enumerate(other_msgs):
|
||||
if i < keep_start:
|
||||
tagged_msg = msg.copy()
|
||||
if "_condense_parent" not in tagged_msg:
|
||||
tagged_msg["_condense_parent"] = condense_id
|
||||
tagged_count += 1
|
||||
result.append(tagged_msg)
|
||||
else:
|
||||
result.append(msg)
|
||||
|
||||
# Calculate effective tokens (excluding tagged)
|
||||
effective_msgs = [m for m in result if "_condense_parent" not in m]
|
||||
optimized_tokens = estimate_messages_tokens(effective_msgs)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.NON_DESTRUCTIVE,
|
||||
messages_tagged=tagged_count,
|
||||
)
|
||||
|
||||
|
||||
class SummarizeOptimizer(BaseOptimizer):
|
||||
"""
|
||||
Summarize older messages using LLM.
|
||||
|
||||
Note: This is a placeholder that creates a structured summary.
|
||||
Actual LLM summarization should be done at the agent level.
|
||||
"""
|
||||
|
||||
def __init__(self, preserve_recent: int = 5, max_summary_items: int = 10):
|
||||
self.preserve_recent = preserve_recent
|
||||
self.max_summary_items = max_summary_items
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
if original_tokens <= target_tokens:
|
||||
return messages, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=original_tokens,
|
||||
tokens_saved=0,
|
||||
strategy_used=OptimizerStrategy.SUMMARIZE,
|
||||
)
|
||||
|
||||
# Separate system and other messages
|
||||
system_msgs = [m for m in messages if m.get("role") == "system"]
|
||||
other_msgs = [m for m in messages if m.get("role") != "system"]
|
||||
|
||||
result = list(system_msgs)
|
||||
|
||||
# Keep recent messages
|
||||
recent = other_msgs[-self.preserve_recent:]
|
||||
older = other_msgs[:-self.preserve_recent] if len(other_msgs) > self.preserve_recent else []
|
||||
|
||||
if older:
|
||||
# Create summary
|
||||
summary_parts = []
|
||||
for msg in older[:self.max_summary_items]:
|
||||
role = msg.get("role", "unknown")
|
||||
content = msg.get("content", "")
|
||||
if isinstance(content, str) and content:
|
||||
summary_parts.append(f"[{role}]: {content[:150]}...")
|
||||
|
||||
if summary_parts:
|
||||
summary = (
|
||||
"[Previous conversation summary]\n"
|
||||
+ "\n".join(summary_parts)
|
||||
)
|
||||
result.append({
|
||||
"role": "system",
|
||||
"content": summary,
|
||||
"_summary": True,
|
||||
"_original_count": len(older),
|
||||
})
|
||||
|
||||
result.extend(recent)
|
||||
|
||||
optimized_tokens = estimate_messages_tokens(result)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.SUMMARIZE,
|
||||
messages_removed=len(older),
|
||||
summary_added=bool(older),
|
||||
)
|
||||
|
||||
|
||||
class SmartOptimizer(BaseOptimizer):
|
||||
"""
|
||||
Smart optimization combining multiple strategies.
|
||||
|
||||
Applies strategies in order:
|
||||
1. Prune tool outputs
|
||||
2. Sliding window
|
||||
3. Summarize if still over
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
preserve_recent: int = 5,
|
||||
protected_tools: Optional[List[str]] = None,
|
||||
):
|
||||
self.preserve_recent = preserve_recent
|
||||
self.protected_tools = protected_tools or []
|
||||
|
||||
self._prune = PruneToolsOptimizer(
|
||||
preserve_recent=preserve_recent,
|
||||
protected_tools=protected_tools,
|
||||
)
|
||||
self._window = SlidingWindowOptimizer()
|
||||
self._summarize = SummarizeOptimizer(preserve_recent=preserve_recent)
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
target_tokens: int,
|
||||
ledger: Optional[ContextLedger] = None,
|
||||
) -> tuple:
|
||||
original_tokens = estimate_messages_tokens(messages)
|
||||
|
||||
if original_tokens <= target_tokens:
|
||||
return messages, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=original_tokens,
|
||||
tokens_saved=0,
|
||||
strategy_used=OptimizerStrategy.SMART,
|
||||
)
|
||||
|
||||
# Step 1: Prune tool outputs
|
||||
result, prune_result = self._prune.optimize(messages, target_tokens, ledger)
|
||||
|
||||
if estimate_messages_tokens(result) <= target_tokens:
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=prune_result.optimized_tokens,
|
||||
tokens_saved=original_tokens - prune_result.optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.SMART,
|
||||
tool_outputs_pruned=prune_result.tool_outputs_pruned,
|
||||
)
|
||||
|
||||
# Step 2: Sliding window
|
||||
result, window_result = self._window.optimize(result, target_tokens, ledger)
|
||||
|
||||
if estimate_messages_tokens(result) <= target_tokens:
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=window_result.optimized_tokens,
|
||||
tokens_saved=original_tokens - window_result.optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.SMART,
|
||||
tool_outputs_pruned=prune_result.tool_outputs_pruned,
|
||||
messages_removed=window_result.messages_removed,
|
||||
)
|
||||
|
||||
# Step 3: Summarize
|
||||
result, summary_result = self._summarize.optimize(result, target_tokens, ledger)
|
||||
|
||||
optimized_tokens = estimate_messages_tokens(result)
|
||||
|
||||
return result, OptimizationResult(
|
||||
original_tokens=original_tokens,
|
||||
optimized_tokens=optimized_tokens,
|
||||
tokens_saved=original_tokens - optimized_tokens,
|
||||
strategy_used=OptimizerStrategy.SMART,
|
||||
tool_outputs_pruned=prune_result.tool_outputs_pruned,
|
||||
messages_removed=window_result.messages_removed + summary_result.messages_removed,
|
||||
summary_added=summary_result.summary_added,
|
||||
)
|
||||
|
||||
|
||||
# Strategy registry
|
||||
OPTIMIZER_REGISTRY: Dict[OptimizerStrategy, type] = {
|
||||
OptimizerStrategy.TRUNCATE: TruncateOptimizer,
|
||||
OptimizerStrategy.SLIDING_WINDOW: SlidingWindowOptimizer,
|
||||
OptimizerStrategy.PRUNE_TOOLS: PruneToolsOptimizer,
|
||||
OptimizerStrategy.NON_DESTRUCTIVE: NonDestructiveOptimizer,
|
||||
OptimizerStrategy.SUMMARIZE: SummarizeOptimizer,
|
||||
OptimizerStrategy.SMART: SmartOptimizer,
|
||||
}
|
||||
|
||||
|
||||
def get_optimizer(
|
||||
strategy: OptimizerStrategy,
|
||||
**kwargs
|
||||
) -> BaseOptimizer:
|
||||
"""
|
||||
Get an optimizer instance for a strategy.
|
||||
|
||||
Args:
|
||||
strategy: Optimization strategy
|
||||
**kwargs: Strategy-specific arguments
|
||||
|
||||
Returns:
|
||||
Optimizer instance
|
||||
"""
|
||||
optimizer_class = OPTIMIZER_REGISTRY.get(strategy, SmartOptimizer)
|
||||
return optimizer_class(**kwargs)
|
||||
|
||||
|
||||
def get_effective_history(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get effective history excluding tagged messages.
|
||||
|
||||
Filters out messages with _condense_parent tags.
|
||||
|
||||
Args:
|
||||
messages: Full message history
|
||||
|
||||
Returns:
|
||||
Effective messages for API calls
|
||||
"""
|
||||
return [m for m in messages if "_condense_parent" not in m]
|
||||
@@ -0,0 +1,238 @@
|
||||
"""
|
||||
Token Estimation for PraisonAI Agents.
|
||||
|
||||
Provides fast, offline token estimation with optional accurate tokenizer support.
|
||||
"""
|
||||
|
||||
from typing import List, Dict, Any, Optional
|
||||
import json
|
||||
|
||||
|
||||
# Token estimation constants (based on OpenAI tokenizer patterns)
|
||||
ASCII_TOKENS_PER_CHAR = 0.25 # ~4 chars per token for ASCII
|
||||
NON_ASCII_TOKENS_PER_CHAR = 1.3 # CJK and other scripts are denser
|
||||
|
||||
# Optional tokenizer cache
|
||||
_tokenizer_cache: Dict[str, Any] = {}
|
||||
|
||||
|
||||
def estimate_tokens_heuristic(text: str) -> int:
|
||||
"""
|
||||
Estimate token count using character-based heuristic.
|
||||
|
||||
Fast and works offline. Accuracy: ~85-95% for English text.
|
||||
|
||||
Args:
|
||||
text: Text to estimate tokens for
|
||||
|
||||
Returns:
|
||||
Estimated token count
|
||||
"""
|
||||
if not text:
|
||||
return 0
|
||||
|
||||
total = 0.0
|
||||
for char in text:
|
||||
code_point = ord(char)
|
||||
if code_point <= 127:
|
||||
total += ASCII_TOKENS_PER_CHAR
|
||||
else:
|
||||
total += NON_ASCII_TOKENS_PER_CHAR
|
||||
|
||||
return max(1, int(total))
|
||||
|
||||
|
||||
def estimate_tokens_accurate(text: str, model: str = "gpt-4") -> int:
|
||||
"""
|
||||
Estimate tokens using tiktoken if available, else fallback to heuristic.
|
||||
|
||||
Args:
|
||||
text: Text to estimate tokens for
|
||||
model: Model name for tokenizer selection
|
||||
|
||||
Returns:
|
||||
Token count
|
||||
"""
|
||||
try:
|
||||
import tiktoken
|
||||
|
||||
# Cache tokenizer
|
||||
if model not in _tokenizer_cache:
|
||||
try:
|
||||
_tokenizer_cache[model] = tiktoken.encoding_for_model(model)
|
||||
except KeyError:
|
||||
# Fallback to cl100k_base for unknown models
|
||||
_tokenizer_cache[model] = tiktoken.get_encoding("cl100k_base")
|
||||
|
||||
enc = _tokenizer_cache[model]
|
||||
return len(enc.encode(text))
|
||||
except ImportError:
|
||||
return estimate_tokens_heuristic(text)
|
||||
|
||||
|
||||
def estimate_message_tokens(message: Dict[str, Any], use_accurate: bool = False) -> int:
|
||||
"""
|
||||
Estimate tokens for a single message.
|
||||
|
||||
Handles various message formats including tool calls and multi-part content.
|
||||
|
||||
Args:
|
||||
message: Message dict with role and content
|
||||
use_accurate: Use tiktoken if available
|
||||
|
||||
Returns:
|
||||
Estimated token count
|
||||
"""
|
||||
estimate_fn = estimate_tokens_accurate if use_accurate else estimate_tokens_heuristic
|
||||
|
||||
total = 0
|
||||
|
||||
# Role overhead (~4 tokens for role markers)
|
||||
total += 4
|
||||
|
||||
# Content
|
||||
content = message.get("content", "")
|
||||
if isinstance(content, str):
|
||||
total += estimate_fn(content)
|
||||
elif isinstance(content, list):
|
||||
# Multi-part content (text, images, etc.)
|
||||
for part in content:
|
||||
if isinstance(part, dict):
|
||||
if "text" in part:
|
||||
total += estimate_fn(str(part["text"]))
|
||||
elif "type" in part:
|
||||
# Image or other media - estimate based on type
|
||||
if part.get("type") == "image_url":
|
||||
total += 85 # Base image token cost
|
||||
else:
|
||||
total += estimate_fn(json.dumps(part))
|
||||
else:
|
||||
total += estimate_fn(str(part))
|
||||
|
||||
# Tool calls
|
||||
if "tool_calls" in message:
|
||||
for tool_call in message.get("tool_calls", []):
|
||||
if isinstance(tool_call, dict):
|
||||
# Function name and arguments
|
||||
func = tool_call.get("function", {})
|
||||
total += estimate_fn(func.get("name", ""))
|
||||
total += estimate_fn(func.get("arguments", ""))
|
||||
total += 10 # Overhead for tool call structure
|
||||
|
||||
# Tool call ID (for tool responses)
|
||||
if "tool_call_id" in message:
|
||||
total += 10
|
||||
|
||||
# Name field
|
||||
if "name" in message:
|
||||
total += estimate_fn(message["name"])
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def estimate_messages_tokens(
|
||||
messages: List[Dict[str, Any]],
|
||||
use_accurate: bool = False
|
||||
) -> int:
|
||||
"""
|
||||
Estimate total tokens for a list of messages.
|
||||
|
||||
Args:
|
||||
messages: List of message dicts
|
||||
use_accurate: Use tiktoken if available
|
||||
|
||||
Returns:
|
||||
Total estimated token count
|
||||
"""
|
||||
if not messages:
|
||||
return 0
|
||||
|
||||
total = 0
|
||||
for msg in messages:
|
||||
total += estimate_message_tokens(msg, use_accurate)
|
||||
|
||||
# Add overhead for message array structure
|
||||
total += 3 # Array markers
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def estimate_tool_schema_tokens(
|
||||
tools: List[Dict[str, Any]],
|
||||
use_accurate: bool = False
|
||||
) -> int:
|
||||
"""
|
||||
Estimate tokens for tool/function schemas.
|
||||
|
||||
Args:
|
||||
tools: List of tool definitions
|
||||
use_accurate: Use tiktoken if available
|
||||
|
||||
Returns:
|
||||
Estimated token count
|
||||
"""
|
||||
if not tools:
|
||||
return 0
|
||||
|
||||
estimate_fn = estimate_tokens_accurate if use_accurate else estimate_tokens_heuristic
|
||||
|
||||
# Serialize tools to JSON and estimate
|
||||
try:
|
||||
tools_json = json.dumps(tools)
|
||||
return estimate_fn(tools_json)
|
||||
except (TypeError, ValueError):
|
||||
# Fallback: estimate per-tool
|
||||
total = 0
|
||||
for tool in tools:
|
||||
if isinstance(tool, dict):
|
||||
total += estimate_fn(json.dumps(tool))
|
||||
else:
|
||||
total += 100 # Default estimate per tool
|
||||
return total
|
||||
|
||||
|
||||
class TokenEstimatorImpl:
|
||||
"""
|
||||
Token estimator implementation.
|
||||
|
||||
Provides both heuristic and accurate estimation methods.
|
||||
"""
|
||||
|
||||
def __init__(self, use_accurate: bool = False, model: str = "gpt-4"):
|
||||
"""
|
||||
Initialize estimator.
|
||||
|
||||
Args:
|
||||
use_accurate: Use tiktoken if available
|
||||
model: Model name for tokenizer selection
|
||||
"""
|
||||
self.use_accurate = use_accurate
|
||||
self.model = model
|
||||
|
||||
def estimate(self, text: str) -> int:
|
||||
"""Estimate tokens for text."""
|
||||
if self.use_accurate:
|
||||
return estimate_tokens_accurate(text, self.model)
|
||||
return estimate_tokens_heuristic(text)
|
||||
|
||||
def estimate_messages(self, messages: List[Dict[str, Any]]) -> int:
|
||||
"""Estimate tokens for messages."""
|
||||
return estimate_messages_tokens(messages, self.use_accurate)
|
||||
|
||||
def estimate_tools(self, tools: List[Dict[str, Any]]) -> int:
|
||||
"""Estimate tokens for tool schemas."""
|
||||
return estimate_tool_schema_tokens(tools, self.use_accurate)
|
||||
|
||||
|
||||
# Default estimator instance
|
||||
_default_estimator: Optional[TokenEstimatorImpl] = None
|
||||
|
||||
|
||||
def get_estimator(use_accurate: bool = False, model: str = "gpt-4") -> TokenEstimatorImpl:
|
||||
"""Get or create a token estimator."""
|
||||
global _default_estimator
|
||||
|
||||
if _default_estimator is None or _default_estimator.use_accurate != use_accurate:
|
||||
_default_estimator = TokenEstimatorImpl(use_accurate, model)
|
||||
|
||||
return _default_estimator
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.6.8"
|
||||
version = "0.7.0"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
"""Unit tests for context management module."""
|
||||
@@ -0,0 +1,201 @@
|
||||
"""Tests for context budgeter module."""
|
||||
|
||||
|
||||
class TestContextBudgeter:
|
||||
"""Tests for ContextBudgeter class."""
|
||||
|
||||
def test_default_initialization(self):
|
||||
"""Test default budgeter initialization."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter()
|
||||
assert budgeter.model == "gpt-4o-mini"
|
||||
assert budgeter.model_limit > 0
|
||||
assert budgeter.output_reserve > 0
|
||||
assert budgeter.usable == budgeter.model_limit - budgeter.output_reserve
|
||||
|
||||
def test_model_limit_lookup(self):
|
||||
"""Test model limit lookup."""
|
||||
from praisonaiagents.context.budgeter import get_model_limit
|
||||
|
||||
assert get_model_limit("gpt-4o") == 128000
|
||||
assert get_model_limit("gpt-4") == 8192
|
||||
assert get_model_limit("claude-3-5-sonnet") == 200000
|
||||
assert get_model_limit("gemini-1.5-pro") == 2097152
|
||||
|
||||
def test_model_limit_partial_match(self):
|
||||
"""Test partial model name matching."""
|
||||
from praisonaiagents.context.budgeter import get_model_limit
|
||||
|
||||
# Should match gpt-4o
|
||||
assert get_model_limit("gpt-4o-2024-05-13") == 128000
|
||||
|
||||
def test_model_limit_unknown(self):
|
||||
"""Test unknown model uses default."""
|
||||
from praisonaiagents.context.budgeter import get_model_limit
|
||||
|
||||
limit = get_model_limit("unknown-model-xyz")
|
||||
assert limit == 128000 # Default
|
||||
|
||||
def test_output_reserve_lookup(self):
|
||||
"""Test output reserve lookup."""
|
||||
from praisonaiagents.context.budgeter import get_output_reserve
|
||||
|
||||
assert get_output_reserve("gpt-4o") == 16384
|
||||
assert get_output_reserve("claude-3-opus") == 8192
|
||||
|
||||
def test_allocate_returns_budget(self):
|
||||
"""Test allocate returns BudgetAllocation."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
from praisonaiagents.context.models import BudgetAllocation
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
allocation = budgeter.allocate()
|
||||
|
||||
assert isinstance(allocation, BudgetAllocation)
|
||||
assert allocation.model_limit == budgeter.model_limit
|
||||
assert allocation.output_reserve == budgeter.output_reserve
|
||||
|
||||
def test_allocate_segment_budgets(self):
|
||||
"""Test segment budgets in allocation."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(
|
||||
system_prompt_budget=3000,
|
||||
rules_budget=1000,
|
||||
)
|
||||
allocation = budgeter.allocate()
|
||||
|
||||
assert allocation.system_prompt == 3000
|
||||
assert allocation.rules == 1000
|
||||
|
||||
def test_history_budget_dynamic(self):
|
||||
"""Test history budget is computed dynamically."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
allocation = budgeter.allocate()
|
||||
|
||||
# History budget should be remainder after fixed segments
|
||||
assert allocation.history_budget > 0
|
||||
assert allocation.history_budget < allocation.usable
|
||||
|
||||
def test_history_ratio_override(self):
|
||||
"""Test history_ratio overrides dynamic calculation."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(
|
||||
model="gpt-4o",
|
||||
history_ratio=0.5,
|
||||
)
|
||||
allocation = budgeter.allocate()
|
||||
|
||||
expected = int(budgeter.usable * 0.5)
|
||||
assert allocation.history == expected
|
||||
|
||||
def test_check_overflow_under_threshold(self):
|
||||
"""Test overflow check when under threshold."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
|
||||
# 50% of usable should not trigger at 80% threshold
|
||||
current = int(budgeter.usable * 0.5)
|
||||
assert not budgeter.check_overflow(current, threshold=0.8)
|
||||
|
||||
def test_check_overflow_over_threshold(self):
|
||||
"""Test overflow check when over threshold."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
|
||||
# 90% of usable should trigger at 80% threshold
|
||||
current = int(budgeter.usable * 0.9)
|
||||
assert budgeter.check_overflow(current, threshold=0.8)
|
||||
|
||||
def test_get_remaining(self):
|
||||
"""Test remaining tokens calculation."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
|
||||
current = 50000
|
||||
remaining = budgeter.get_remaining(current)
|
||||
assert remaining == budgeter.usable - current
|
||||
|
||||
def test_get_utilization(self):
|
||||
"""Test utilization calculation."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
|
||||
current = budgeter.usable // 2
|
||||
util = budgeter.get_utilization(current)
|
||||
assert 0.49 <= util <= 0.51
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test dictionary conversion."""
|
||||
from praisonaiagents.context.budgeter import ContextBudgeter
|
||||
|
||||
budgeter = ContextBudgeter(model="gpt-4o")
|
||||
data = budgeter.to_dict()
|
||||
|
||||
assert "model" in data
|
||||
assert "model_limit" in data
|
||||
assert "usable" in data
|
||||
assert "allocation" in data
|
||||
|
||||
|
||||
class TestBudgetAllocation:
|
||||
"""Tests for BudgetAllocation dataclass."""
|
||||
|
||||
def test_usable_property(self):
|
||||
"""Test usable property calculation."""
|
||||
from praisonaiagents.context.models import BudgetAllocation
|
||||
|
||||
allocation = BudgetAllocation(
|
||||
model_limit=128000,
|
||||
output_reserve=8000,
|
||||
)
|
||||
assert allocation.usable == 120000
|
||||
|
||||
def test_fixed_total(self):
|
||||
"""Test fixed total calculation."""
|
||||
from praisonaiagents.context.models import BudgetAllocation
|
||||
|
||||
allocation = BudgetAllocation(
|
||||
system_prompt=2000,
|
||||
rules=500,
|
||||
skills=500,
|
||||
memory=1000,
|
||||
tools_schema=2000,
|
||||
tool_outputs=20000,
|
||||
buffer=1000,
|
||||
)
|
||||
|
||||
expected = 2000 + 500 + 500 + 1000 + 2000 + 20000 + 1000
|
||||
assert allocation.fixed_total == expected
|
||||
|
||||
def test_history_budget_computed(self):
|
||||
"""Test history budget is computed from remainder."""
|
||||
from praisonaiagents.context.models import BudgetAllocation
|
||||
|
||||
allocation = BudgetAllocation(
|
||||
model_limit=128000,
|
||||
output_reserve=8000,
|
||||
history=-1, # Dynamic
|
||||
)
|
||||
|
||||
expected = allocation.usable - allocation.fixed_total
|
||||
assert allocation.history_budget == expected
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test dictionary conversion."""
|
||||
from praisonaiagents.context.models import BudgetAllocation
|
||||
|
||||
allocation = BudgetAllocation()
|
||||
data = allocation.to_dict()
|
||||
|
||||
assert "model_limit" in data
|
||||
assert "usable" in data
|
||||
assert "history_budget" in data
|
||||
@@ -0,0 +1,284 @@
|
||||
"""Tests for context monitor module."""
|
||||
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestContextMonitor:
|
||||
"""Tests for ContextMonitor class."""
|
||||
|
||||
def test_monitor_disabled_by_default(self):
|
||||
"""Test monitor is disabled by default."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor()
|
||||
assert not monitor.enabled
|
||||
|
||||
def test_monitor_enable_disable(self):
|
||||
"""Test enable/disable methods."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor()
|
||||
|
||||
monitor.enable()
|
||||
assert monitor.enabled
|
||||
|
||||
monitor.disable()
|
||||
assert not monitor.enabled
|
||||
|
||||
def test_should_write_when_disabled(self):
|
||||
"""Test should_write returns False when disabled."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(enabled=False)
|
||||
assert not monitor.should_write("turn")
|
||||
assert not monitor.should_write("manual")
|
||||
|
||||
def test_should_write_frequency_turn(self):
|
||||
"""Test should_write with turn frequency."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(enabled=True, frequency="turn")
|
||||
|
||||
assert monitor.should_write("turn")
|
||||
assert monitor.should_write("overflow")
|
||||
assert not monitor.should_write("tool_call")
|
||||
|
||||
def test_should_write_frequency_tool_call(self):
|
||||
"""Test should_write with tool_call frequency."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(enabled=True, frequency="tool_call")
|
||||
|
||||
assert monitor.should_write("tool_call")
|
||||
assert monitor.should_write("overflow")
|
||||
|
||||
def test_should_write_frequency_manual(self):
|
||||
"""Test should_write with manual frequency."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(enabled=True, frequency="manual")
|
||||
|
||||
assert monitor.should_write("manual")
|
||||
assert not monitor.should_write("turn")
|
||||
|
||||
def test_snapshot_writes_file(self):
|
||||
"""Test snapshot writes file to disk."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
from praisonaiagents.context.models import ContextLedger, BudgetAllocation
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "context.txt")
|
||||
monitor = ContextMonitor(enabled=True, path=path)
|
||||
|
||||
ledger = ContextLedger(history=1000, system_prompt=500)
|
||||
budget = BudgetAllocation()
|
||||
|
||||
result_path = monitor.snapshot(
|
||||
ledger=ledger,
|
||||
budget=budget,
|
||||
messages=[{"role": "user", "content": "Hello"}],
|
||||
trigger="manual",
|
||||
)
|
||||
|
||||
assert result_path is not None
|
||||
assert os.path.exists(path)
|
||||
|
||||
content = Path(path).read_text()
|
||||
assert "PRAISONAI CONTEXT SNAPSHOT" in content
|
||||
|
||||
def test_snapshot_json_format(self):
|
||||
"""Test snapshot with JSON format."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
from praisonaiagents.context.models import ContextLedger, BudgetAllocation
|
||||
import json
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "context.json")
|
||||
monitor = ContextMonitor(enabled=True, path=path, format="json")
|
||||
|
||||
ledger = ContextLedger(history=1000)
|
||||
budget = BudgetAllocation()
|
||||
|
||||
monitor.snapshot(
|
||||
ledger=ledger,
|
||||
budget=budget,
|
||||
messages=[],
|
||||
trigger="manual",
|
||||
)
|
||||
|
||||
content = Path(path).read_text()
|
||||
data = json.loads(content)
|
||||
|
||||
assert "ledger" in data
|
||||
assert "timestamp" in data
|
||||
|
||||
def test_snapshot_not_written_when_disabled(self):
|
||||
"""Test snapshot not written when disabled."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
from praisonaiagents.context.models import ContextLedger, BudgetAllocation
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "context.txt")
|
||||
monitor = ContextMonitor(enabled=False, path=path)
|
||||
|
||||
result = monitor.snapshot(
|
||||
ledger=ContextLedger(),
|
||||
budget=BudgetAllocation(),
|
||||
messages=[],
|
||||
trigger="turn",
|
||||
)
|
||||
|
||||
assert result is None
|
||||
assert not os.path.exists(path)
|
||||
|
||||
def test_set_path(self):
|
||||
"""Test set_path method."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor()
|
||||
monitor.set_path("/new/path/context.txt")
|
||||
|
||||
assert str(monitor.path) == "/new/path/context.txt"
|
||||
|
||||
def test_set_format(self):
|
||||
"""Test set_format method."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(format="human")
|
||||
monitor.set_format("json")
|
||||
|
||||
assert monitor.format == "json"
|
||||
|
||||
def test_get_stats(self):
|
||||
"""Test get_stats method."""
|
||||
from praisonaiagents.context.monitor import ContextMonitor
|
||||
|
||||
monitor = ContextMonitor(enabled=True, path="./test.txt")
|
||||
stats = monitor.get_stats()
|
||||
|
||||
assert "enabled" in stats
|
||||
assert "path" in stats
|
||||
assert "format" in stats
|
||||
assert stats["enabled"] is True
|
||||
|
||||
|
||||
class TestRedaction:
|
||||
"""Tests for sensitive data redaction."""
|
||||
|
||||
def test_redact_api_key(self):
|
||||
"""Test API key redaction."""
|
||||
from praisonaiagents.context.monitor import redact_sensitive
|
||||
|
||||
text = "My API key is sk-proj-abc123def456ghi789jkl012mno345pqr678"
|
||||
result = redact_sensitive(text)
|
||||
|
||||
assert "sk-proj-" not in result
|
||||
assert "[REDACTED]" in result
|
||||
|
||||
def test_redact_password(self):
|
||||
"""Test password redaction."""
|
||||
from praisonaiagents.context.monitor import redact_sensitive
|
||||
|
||||
text = 'password: "mysecretpassword123"'
|
||||
result = redact_sensitive(text)
|
||||
|
||||
assert "mysecretpassword" not in result
|
||||
|
||||
def test_redact_preserves_normal_text(self):
|
||||
"""Test normal text is preserved."""
|
||||
from praisonaiagents.context.monitor import redact_sensitive
|
||||
|
||||
text = "Hello, how are you today?"
|
||||
result = redact_sensitive(text)
|
||||
|
||||
assert result == text
|
||||
|
||||
|
||||
class TestFormatting:
|
||||
"""Tests for snapshot formatting."""
|
||||
|
||||
def test_format_human_snapshot(self):
|
||||
"""Test human-readable format."""
|
||||
from praisonaiagents.context.monitor import format_human_snapshot
|
||||
from praisonaiagents.context.models import ContextSnapshot, ContextLedger, BudgetAllocation
|
||||
|
||||
snapshot = ContextSnapshot(
|
||||
timestamp="2025-01-07T12:00:00Z",
|
||||
session_id="test-session",
|
||||
agent_name="TestAgent",
|
||||
model_name="gpt-4o",
|
||||
budget=BudgetAllocation(),
|
||||
ledger=ContextLedger(history=5000, system_prompt=1000),
|
||||
utilization=0.5,
|
||||
warnings=["Test warning"],
|
||||
)
|
||||
|
||||
result = format_human_snapshot(snapshot)
|
||||
|
||||
assert "PRAISONAI CONTEXT SNAPSHOT" in result
|
||||
assert "TestAgent" in result
|
||||
assert "gpt-4o" in result
|
||||
assert "Test warning" in result
|
||||
|
||||
def test_format_json_snapshot(self):
|
||||
"""Test JSON format."""
|
||||
from praisonaiagents.context.monitor import format_json_snapshot
|
||||
from praisonaiagents.context.models import ContextSnapshot, ContextLedger, BudgetAllocation
|
||||
import json
|
||||
|
||||
snapshot = ContextSnapshot(
|
||||
timestamp="2025-01-07T12:00:00Z",
|
||||
session_id="test-session",
|
||||
ledger=ContextLedger(),
|
||||
budget=BudgetAllocation(),
|
||||
)
|
||||
|
||||
result = format_json_snapshot(snapshot)
|
||||
data = json.loads(result)
|
||||
|
||||
assert data["timestamp"] == "2025-01-07T12:00:00Z"
|
||||
assert data["session_id"] == "test-session"
|
||||
|
||||
|
||||
class TestMultiAgentMonitor:
|
||||
"""Tests for MultiAgentMonitor class."""
|
||||
|
||||
def test_get_agent_monitor(self):
|
||||
"""Test getting agent-specific monitor."""
|
||||
from praisonaiagents.context.monitor import MultiAgentMonitor
|
||||
|
||||
multi = MultiAgentMonitor(base_path="./context")
|
||||
|
||||
monitor1 = multi.get_agent_monitor("agent1")
|
||||
monitor2 = multi.get_agent_monitor("agent2")
|
||||
|
||||
assert monitor1 is not monitor2
|
||||
assert "agent1" in str(monitor1.path)
|
||||
assert "agent2" in str(monitor2.path)
|
||||
|
||||
def test_enable_all(self):
|
||||
"""Test enabling all monitors."""
|
||||
from praisonaiagents.context.monitor import MultiAgentMonitor
|
||||
|
||||
multi = MultiAgentMonitor()
|
||||
multi.get_agent_monitor("agent1")
|
||||
multi.get_agent_monitor("agent2")
|
||||
|
||||
multi.enable_all()
|
||||
|
||||
assert multi.enabled
|
||||
|
||||
def test_get_agent_ids(self):
|
||||
"""Test getting list of agent IDs."""
|
||||
from praisonaiagents.context.monitor import MultiAgentMonitor
|
||||
|
||||
multi = MultiAgentMonitor()
|
||||
multi.get_agent_monitor("agent1")
|
||||
multi.get_agent_monitor("agent2")
|
||||
|
||||
ids = multi.get_agent_ids()
|
||||
|
||||
assert "agent1" in ids
|
||||
assert "agent2" in ids
|
||||
@@ -0,0 +1,208 @@
|
||||
"""Tests for context optimizer module."""
|
||||
|
||||
|
||||
class TestOptimizers:
|
||||
"""Tests for optimizer strategies."""
|
||||
|
||||
def test_truncate_optimizer_no_change_needed(self):
|
||||
"""Test truncate when under limit."""
|
||||
from praisonaiagents.context.optimizer import TruncateOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"role": "assistant", "content": "Hi"},
|
||||
]
|
||||
|
||||
optimizer = TruncateOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=10000)
|
||||
|
||||
assert len(result) == 2
|
||||
assert stats.tokens_saved == 0
|
||||
|
||||
def test_truncate_optimizer_removes_old(self):
|
||||
"""Test truncate removes old messages."""
|
||||
from praisonaiagents.context.optimizer import TruncateOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": "You are helpful"},
|
||||
{"role": "user", "content": "a" * 1000},
|
||||
{"role": "assistant", "content": "b" * 1000},
|
||||
{"role": "user", "content": "c" * 1000},
|
||||
{"role": "assistant", "content": "d" * 1000},
|
||||
]
|
||||
|
||||
optimizer = TruncateOptimizer(preserve_recent=2)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=500)
|
||||
|
||||
# Should keep system + recent
|
||||
assert len(result) < len(messages)
|
||||
assert stats.messages_removed > 0
|
||||
|
||||
def test_truncate_preserves_system(self):
|
||||
"""Test truncate preserves system messages."""
|
||||
from praisonaiagents.context.optimizer import TruncateOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": "Important system prompt"},
|
||||
{"role": "user", "content": "a" * 1000},
|
||||
{"role": "assistant", "content": "b" * 1000},
|
||||
]
|
||||
|
||||
optimizer = TruncateOptimizer(preserve_system=True)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=100)
|
||||
|
||||
# System message should be preserved
|
||||
system_msgs = [m for m in result if m.get("role") == "system"]
|
||||
assert len(system_msgs) >= 1
|
||||
|
||||
def test_sliding_window_optimizer(self):
|
||||
"""Test sliding window keeps recent."""
|
||||
from praisonaiagents.context.optimizer import SlidingWindowOptimizer
|
||||
|
||||
# Create messages with enough content to exceed target
|
||||
messages = [
|
||||
{"role": "user", "content": f"Message {i} " * 50}
|
||||
for i in range(10)
|
||||
]
|
||||
|
||||
optimizer = SlidingWindowOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=100)
|
||||
|
||||
# Should keep most recent messages (fewer than original)
|
||||
assert len(result) <= len(messages)
|
||||
|
||||
def test_prune_tools_optimizer(self):
|
||||
"""Test prune tools truncates old outputs."""
|
||||
from praisonaiagents.context.optimizer import PruneToolsOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Get weather"},
|
||||
{"role": "assistant", "content": "", "tool_calls": [{"id": "1"}]},
|
||||
{"role": "tool", "tool_call_id": "1", "content": "x" * 2000},
|
||||
{"role": "user", "content": "Thanks"},
|
||||
{"role": "assistant", "content": "You're welcome"},
|
||||
]
|
||||
|
||||
optimizer = PruneToolsOptimizer(preserve_recent=2, max_output_chars=100)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=10000)
|
||||
|
||||
# Tool output should be truncated
|
||||
tool_msg = [m for m in result if m.get("role") == "tool"][0]
|
||||
assert len(tool_msg["content"]) < 2000
|
||||
|
||||
def test_prune_tools_protects_recent(self):
|
||||
"""Test prune tools protects recent outputs."""
|
||||
from praisonaiagents.context.optimizer import PruneToolsOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Get weather"},
|
||||
{"role": "tool", "tool_call_id": "1", "content": "x" * 2000},
|
||||
]
|
||||
|
||||
optimizer = PruneToolsOptimizer(preserve_recent=5)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=10000)
|
||||
|
||||
# Recent should not be truncated
|
||||
tool_msg = [m for m in result if m.get("role") == "tool"][0]
|
||||
assert len(tool_msg["content"]) == 2000
|
||||
|
||||
def test_non_destructive_optimizer(self):
|
||||
"""Test non-destructive tags messages."""
|
||||
from praisonaiagents.context.optimizer import NonDestructiveOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": f"Message {i}"}
|
||||
for i in range(10)
|
||||
]
|
||||
|
||||
optimizer = NonDestructiveOptimizer(preserve_recent=3)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=50)
|
||||
|
||||
# All messages should still be present
|
||||
assert len(result) == len(messages)
|
||||
|
||||
# Older messages should be tagged
|
||||
tagged = [m for m in result if "_condense_parent" in m]
|
||||
assert len(tagged) > 0
|
||||
assert stats.messages_tagged > 0
|
||||
|
||||
def test_summarize_optimizer(self):
|
||||
"""Test summarize creates summary."""
|
||||
from praisonaiagents.context.optimizer import SummarizeOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": f"Long message {i} " * 50}
|
||||
for i in range(10)
|
||||
]
|
||||
|
||||
optimizer = SummarizeOptimizer(preserve_recent=3)
|
||||
result, stats = optimizer.optimize(messages, target_tokens=100)
|
||||
|
||||
# Should have summary message
|
||||
assert stats.summary_added
|
||||
summary_msgs = [m for m in result if m.get("_summary")]
|
||||
assert len(summary_msgs) >= 1
|
||||
|
||||
def test_smart_optimizer_combines_strategies(self):
|
||||
"""Test smart optimizer applies multiple strategies."""
|
||||
from praisonaiagents.context.optimizer import SmartOptimizer
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Get data"},
|
||||
{"role": "tool", "tool_call_id": "1", "content": "x" * 5000},
|
||||
]
|
||||
messages.extend([
|
||||
{"role": "user", "content": f"Message {i} " * 100}
|
||||
for i in range(10)
|
||||
])
|
||||
|
||||
optimizer = SmartOptimizer()
|
||||
result, stats = optimizer.optimize(messages, target_tokens=500)
|
||||
|
||||
assert stats.optimized_tokens < stats.original_tokens
|
||||
|
||||
def test_get_optimizer_factory(self):
|
||||
"""Test get_optimizer factory function."""
|
||||
from praisonaiagents.context.optimizer import get_optimizer
|
||||
from praisonaiagents.context.models import OptimizerStrategy
|
||||
|
||||
for strategy in OptimizerStrategy:
|
||||
optimizer = get_optimizer(strategy)
|
||||
assert optimizer is not None
|
||||
|
||||
def test_get_effective_history(self):
|
||||
"""Test get_effective_history filters tagged."""
|
||||
from praisonaiagents.context.optimizer import get_effective_history
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "Old", "_condense_parent": "abc"},
|
||||
{"role": "user", "content": "Recent"},
|
||||
]
|
||||
|
||||
effective = get_effective_history(messages)
|
||||
|
||||
assert len(effective) == 1
|
||||
assert effective[0]["content"] == "Recent"
|
||||
|
||||
|
||||
class TestOptimizationResult:
|
||||
"""Tests for OptimizationResult dataclass."""
|
||||
|
||||
def test_reduction_percent(self):
|
||||
"""Test reduction percentage calculation."""
|
||||
from praisonaiagents.context.models import OptimizationResult
|
||||
|
||||
result = OptimizationResult(
|
||||
original_tokens=1000,
|
||||
optimized_tokens=600,
|
||||
tokens_saved=400,
|
||||
)
|
||||
|
||||
assert result.reduction_percent == 40.0
|
||||
|
||||
def test_reduction_percent_zero_original(self):
|
||||
"""Test reduction with zero original."""
|
||||
from praisonaiagents.context.models import OptimizationResult
|
||||
|
||||
result = OptimizationResult(original_tokens=0)
|
||||
assert result.reduction_percent == 0.0
|
||||
@@ -0,0 +1,184 @@
|
||||
"""Tests for token estimation module."""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestTokenEstimation:
|
||||
"""Tests for token estimation functions."""
|
||||
|
||||
def test_estimate_tokens_heuristic_empty(self):
|
||||
"""Test empty string returns 0."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
assert estimate_tokens_heuristic("") == 0
|
||||
|
||||
def test_estimate_tokens_heuristic_ascii(self):
|
||||
"""Test ASCII text estimation."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
# ~4 chars per token for ASCII
|
||||
text = "Hello world" # 11 chars
|
||||
tokens = estimate_tokens_heuristic(text)
|
||||
assert 2 <= tokens <= 4 # Should be ~2-3 tokens
|
||||
|
||||
def test_estimate_tokens_heuristic_long_text(self):
|
||||
"""Test longer text estimation."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
text = "a" * 400 # 400 ASCII chars
|
||||
tokens = estimate_tokens_heuristic(text)
|
||||
assert 90 <= tokens <= 110 # Should be ~100 tokens
|
||||
|
||||
def test_estimate_tokens_heuristic_non_ascii(self):
|
||||
"""Test non-ASCII text has higher token count."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
ascii_text = "hello"
|
||||
non_ascii_text = "你好世界" # Chinese
|
||||
|
||||
ascii_tokens = estimate_tokens_heuristic(ascii_text)
|
||||
non_ascii_tokens = estimate_tokens_heuristic(non_ascii_text)
|
||||
|
||||
# Non-ASCII should have higher token density
|
||||
assert non_ascii_tokens > ascii_tokens
|
||||
|
||||
def test_estimate_message_tokens_simple(self):
|
||||
"""Test simple message token estimation."""
|
||||
from praisonaiagents.context.tokens import estimate_message_tokens
|
||||
|
||||
msg = {"role": "user", "content": "Hello world"}
|
||||
tokens = estimate_message_tokens(msg)
|
||||
|
||||
# Should include role overhead + content
|
||||
assert tokens > 0
|
||||
assert tokens < 20
|
||||
|
||||
def test_estimate_message_tokens_empty_content(self):
|
||||
"""Test message with empty content."""
|
||||
from praisonaiagents.context.tokens import estimate_message_tokens
|
||||
|
||||
msg = {"role": "user", "content": ""}
|
||||
tokens = estimate_message_tokens(msg)
|
||||
|
||||
# Should have role overhead
|
||||
assert tokens >= 4
|
||||
|
||||
def test_estimate_message_tokens_multipart(self):
|
||||
"""Test multipart content message."""
|
||||
from praisonaiagents.context.tokens import estimate_message_tokens
|
||||
|
||||
msg = {
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "text": "Hello"},
|
||||
{"type": "text", "text": "World"},
|
||||
]
|
||||
}
|
||||
tokens = estimate_message_tokens(msg)
|
||||
assert tokens > 0
|
||||
|
||||
def test_estimate_message_tokens_tool_call(self):
|
||||
"""Test message with tool calls."""
|
||||
from praisonaiagents.context.tokens import estimate_message_tokens
|
||||
|
||||
msg = {
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_123",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"arguments": '{"location": "NYC"}'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
tokens = estimate_message_tokens(msg)
|
||||
assert tokens > 10 # Should include tool call overhead
|
||||
|
||||
def test_estimate_messages_tokens_list(self):
|
||||
"""Test estimating tokens for message list."""
|
||||
from praisonaiagents.context.tokens import estimate_messages_tokens
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": "You are helpful"},
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"role": "assistant", "content": "Hi there!"},
|
||||
]
|
||||
tokens = estimate_messages_tokens(messages)
|
||||
assert tokens > 0
|
||||
assert tokens < 100
|
||||
|
||||
def test_estimate_messages_tokens_empty(self):
|
||||
"""Test empty message list."""
|
||||
from praisonaiagents.context.tokens import estimate_messages_tokens
|
||||
assert estimate_messages_tokens([]) == 0
|
||||
|
||||
def test_estimate_tool_schema_tokens(self):
|
||||
"""Test tool schema token estimation."""
|
||||
from praisonaiagents.context.tokens import estimate_tool_schema_tokens
|
||||
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get weather for a location",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {"type": "string"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
tokens = estimate_tool_schema_tokens(tools)
|
||||
assert tokens > 0
|
||||
|
||||
def test_estimate_tool_schema_tokens_empty(self):
|
||||
"""Test empty tool list."""
|
||||
from praisonaiagents.context.tokens import estimate_tool_schema_tokens
|
||||
assert estimate_tool_schema_tokens([]) == 0
|
||||
|
||||
def test_token_estimator_impl(self):
|
||||
"""Test TokenEstimatorImpl class."""
|
||||
from praisonaiagents.context.tokens import TokenEstimatorImpl
|
||||
|
||||
estimator = TokenEstimatorImpl(use_accurate=False)
|
||||
|
||||
assert estimator.estimate("Hello") > 0
|
||||
assert estimator.estimate_messages([{"role": "user", "content": "Hi"}]) > 0
|
||||
assert estimator.estimate_tools([]) == 0
|
||||
|
||||
def test_get_estimator(self):
|
||||
"""Test get_estimator factory."""
|
||||
from praisonaiagents.context.tokens import get_estimator
|
||||
|
||||
estimator = get_estimator(use_accurate=False)
|
||||
assert estimator is not None
|
||||
assert estimator.estimate("test") > 0
|
||||
|
||||
|
||||
class TestTokenEstimationAccuracy:
|
||||
"""Tests for estimation accuracy."""
|
||||
|
||||
def test_consistency(self):
|
||||
"""Test that estimation is consistent."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
|
||||
text = "The quick brown fox jumps over the lazy dog"
|
||||
|
||||
# Should return same value each time
|
||||
result1 = estimate_tokens_heuristic(text)
|
||||
result2 = estimate_tokens_heuristic(text)
|
||||
assert result1 == result2
|
||||
|
||||
def test_proportional_scaling(self):
|
||||
"""Test that longer text has more tokens."""
|
||||
from praisonaiagents.context.tokens import estimate_tokens_heuristic
|
||||
|
||||
short = "Hello"
|
||||
long = "Hello " * 100
|
||||
|
||||
short_tokens = estimate_tokens_heuristic(short)
|
||||
long_tokens = estimate_tokens_heuristic(long)
|
||||
|
||||
assert long_tokens > short_tokens * 50
|
||||
Generated
+1
-1
@@ -2422,7 +2422,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.6.8"
|
||||
version = "0.7.0"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
|
||||
@@ -3,8 +3,8 @@ class Praisonai < Formula
|
||||
|
||||
desc "AI tools for various AI applications"
|
||||
homepage "https://github.com/MervinPraison/PraisonAI"
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v3.1.8.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v3.1.8.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v3.2.0.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v3.2.0.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -27,6 +27,8 @@ def chat_main(
|
||||
workspace: Optional[str] = typer.Option(None, "--workspace", "-w", help="Workspace directory"),
|
||||
no_acp: bool = typer.Option(False, "--no-acp", help="Disable ACP tools"),
|
||||
no_lsp: bool = typer.Option(False, "--no-lsp", help="Disable LSP tools"),
|
||||
profile: bool = typer.Option(False, "--profile", help="Enable CLI profiling (timing breakdown)"),
|
||||
profile_deep: bool = typer.Option(False, "--profile-deep", help="Enable deep profiling (cProfile stats, higher overhead)"),
|
||||
):
|
||||
"""
|
||||
Start terminal-native interactive chat mode.
|
||||
@@ -42,6 +44,7 @@ def chat_main(
|
||||
praisonai chat --model gpt-4o --memory
|
||||
praisonai chat --continue # Resume last session
|
||||
praisonai chat "Summarize this" --file README.md
|
||||
praisonai chat "What is 2+2?" --profile
|
||||
"""
|
||||
import asyncio
|
||||
import os
|
||||
@@ -50,6 +53,21 @@ def chat_main(
|
||||
if workspace:
|
||||
os.environ["PRAISONAI_WORKSPACE"] = workspace
|
||||
|
||||
# Handle profiling for single prompt mode
|
||||
if prompt and (profile or profile_deep):
|
||||
_run_profiled_chat(
|
||||
prompt=prompt,
|
||||
model=model,
|
||||
verbose=verbose,
|
||||
profile_deep=profile_deep,
|
||||
)
|
||||
return
|
||||
|
||||
# Warn if profiling requested without prompt (REPL mode doesn't support profiling)
|
||||
if (profile or profile_deep) and not prompt:
|
||||
typer.echo("⚠️ Profiling is only supported for single prompt mode.", err=True)
|
||||
typer.echo(" Use: praisonai chat \"your prompt\" --profile", err=True)
|
||||
|
||||
# Try InteractiveCore first (preferred terminal-native implementation)
|
||||
try:
|
||||
from praisonai.cli.interactive import InteractiveCore, InteractiveConfig
|
||||
@@ -98,6 +116,64 @@ def chat_main(
|
||||
)
|
||||
|
||||
|
||||
def _run_profiled_chat(
|
||||
prompt: str,
|
||||
model: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
profile_deep: bool = False,
|
||||
):
|
||||
"""Run chat with profiling enabled."""
|
||||
from praisonai.cli.features.cli_profiler import (
|
||||
CLIProfileConfig,
|
||||
CLIProfiler,
|
||||
)
|
||||
|
||||
config = CLIProfileConfig(enabled=True, deep=profile_deep)
|
||||
profiler = CLIProfiler(config)
|
||||
|
||||
if profile_deep:
|
||||
typer.echo("⚠️ Deep profiling enabled - this adds significant overhead", err=True)
|
||||
|
||||
profiler.start()
|
||||
|
||||
# Import phase
|
||||
profiler.mark_import_start()
|
||||
try:
|
||||
from praisonaiagents import Agent
|
||||
except ImportError:
|
||||
typer.echo("Error: praisonaiagents not installed", err=True)
|
||||
raise typer.Exit(1)
|
||||
profiler.mark_import_end()
|
||||
|
||||
# Agent initialization phase
|
||||
profiler.mark_init_start()
|
||||
agent_config = {
|
||||
"name": "ChatAgent",
|
||||
"role": "Assistant",
|
||||
"goal": "Help the user",
|
||||
"verbose": verbose,
|
||||
}
|
||||
if model:
|
||||
agent_config["llm"] = model
|
||||
|
||||
agent = Agent(**agent_config)
|
||||
profiler.mark_init_end()
|
||||
|
||||
# Execution phase
|
||||
profiler.mark_exec_start()
|
||||
response = agent.start(prompt)
|
||||
profiler.mark_exec_end()
|
||||
|
||||
profiler.stop()
|
||||
|
||||
# Print response
|
||||
if response:
|
||||
print(response)
|
||||
|
||||
# Print profiling report
|
||||
profiler.print_report()
|
||||
|
||||
|
||||
def _run_legacy_terminal_chat(
|
||||
prompt: Optional[str] = None,
|
||||
model: Optional[str] = None,
|
||||
|
||||
@@ -25,6 +25,8 @@ def code_main(
|
||||
no_lsp: bool = typer.Option(False, "--no-lsp", help="Disable LSP tools (code intelligence)"),
|
||||
session_id: Optional[str] = typer.Option(None, "--session", "-s", help="Session ID to resume"),
|
||||
continue_session: bool = typer.Option(False, "--continue", "-c", help="Continue last session"),
|
||||
profile: bool = typer.Option(False, "--profile", help="Enable CLI profiling (timing breakdown)"),
|
||||
profile_deep: bool = typer.Option(False, "--profile-deep", help="Enable deep profiling (cProfile stats, higher overhead)"),
|
||||
):
|
||||
"""
|
||||
Start terminal-native code assistant mode.
|
||||
@@ -39,6 +41,7 @@ def code_main(
|
||||
praisonai code "Refactor this function"
|
||||
praisonai code --model gpt-4o --workspace ./src
|
||||
praisonai code "Fix the bug" --file main.py
|
||||
praisonai code "What is 2+2?" --profile
|
||||
"""
|
||||
import os
|
||||
import argparse
|
||||
@@ -50,6 +53,21 @@ def code_main(
|
||||
# Enable code-specific environment
|
||||
os.environ["PRAISONAI_CODE_MODE"] = "true"
|
||||
|
||||
# Handle profiling for single prompt mode
|
||||
if prompt and (profile or profile_deep):
|
||||
_run_profiled_code(
|
||||
prompt=prompt,
|
||||
model=model,
|
||||
verbose=verbose,
|
||||
profile_deep=profile_deep,
|
||||
)
|
||||
return
|
||||
|
||||
# Warn if profiling requested without prompt (REPL mode doesn't support profiling)
|
||||
if (profile or profile_deep) and not prompt:
|
||||
typer.echo("⚠️ Profiling is only supported for single prompt mode.", err=True)
|
||||
typer.echo(" Use: praisonai code \"your prompt\" --profile", err=True)
|
||||
|
||||
# Build args namespace for _start_interactive_mode
|
||||
args = argparse.Namespace()
|
||||
args.llm = model
|
||||
@@ -70,3 +88,61 @@ def code_main(
|
||||
else:
|
||||
# Interactive REPL mode - use _start_interactive_mode
|
||||
praison._start_interactive_mode(args)
|
||||
|
||||
|
||||
def _run_profiled_code(
|
||||
prompt: str,
|
||||
model: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
profile_deep: bool = False,
|
||||
):
|
||||
"""Run code assistant with profiling enabled."""
|
||||
from praisonai.cli.features.cli_profiler import (
|
||||
CLIProfileConfig,
|
||||
CLIProfiler,
|
||||
)
|
||||
|
||||
config = CLIProfileConfig(enabled=True, deep=profile_deep)
|
||||
profiler = CLIProfiler(config)
|
||||
|
||||
if profile_deep:
|
||||
typer.echo("⚠️ Deep profiling enabled - this adds significant overhead", err=True)
|
||||
|
||||
profiler.start()
|
||||
|
||||
# Import phase
|
||||
profiler.mark_import_start()
|
||||
try:
|
||||
from praisonaiagents import Agent
|
||||
except ImportError:
|
||||
typer.echo("Error: praisonaiagents not installed", err=True)
|
||||
raise typer.Exit(1)
|
||||
profiler.mark_import_end()
|
||||
|
||||
# Agent initialization phase
|
||||
profiler.mark_init_start()
|
||||
agent_config = {
|
||||
"name": "CodeAgent",
|
||||
"role": "Code Assistant",
|
||||
"goal": "Help with coding tasks",
|
||||
"verbose": verbose,
|
||||
}
|
||||
if model:
|
||||
agent_config["llm"] = model
|
||||
|
||||
agent = Agent(**agent_config)
|
||||
profiler.mark_init_end()
|
||||
|
||||
# Execution phase
|
||||
profiler.mark_exec_start()
|
||||
response = agent.start(prompt)
|
||||
profiler.mark_exec_end()
|
||||
|
||||
profiler.stop()
|
||||
|
||||
# Print response
|
||||
if response:
|
||||
print(response)
|
||||
|
||||
# Print profiling report
|
||||
profiler.print_report()
|
||||
@@ -27,6 +27,8 @@ def run_main(
|
||||
memory: bool = typer.Option(False, "--memory", help="Enable memory"),
|
||||
tools: Optional[str] = typer.Option(None, "--tools", "-t", help="Tools file path"),
|
||||
max_tokens: int = typer.Option(16000, "--max-tokens", help="Maximum output tokens"),
|
||||
profile: bool = typer.Option(False, "--profile", help="Enable CLI profiling (timing breakdown)"),
|
||||
profile_deep: bool = typer.Option(False, "--profile-deep", help="Enable deep profiling (cProfile stats, higher overhead)"),
|
||||
):
|
||||
"""
|
||||
Run agents from a file or prompt.
|
||||
@@ -35,9 +37,10 @@ def run_main(
|
||||
praisonai run agents.yaml
|
||||
praisonai run "What is the weather?"
|
||||
praisonai run agents.yaml --interactive
|
||||
praisonai run "What is 2+2?" --profile
|
||||
"""
|
||||
output = get_output_controller()
|
||||
context = get_current_context()
|
||||
_ = get_current_context() # Initialize context
|
||||
|
||||
if not target:
|
||||
output.print_panel(
|
||||
@@ -71,6 +74,27 @@ def run_main(
|
||||
import os
|
||||
is_file = os.path.exists(target) and (target.endswith('.yaml') or target.endswith('.yml'))
|
||||
|
||||
# Handle profiling
|
||||
if profile or profile_deep:
|
||||
if is_file:
|
||||
# Profiling for YAML file execution
|
||||
_run_from_file_profiled(
|
||||
target,
|
||||
model=model,
|
||||
framework=framework,
|
||||
verbose=verbose,
|
||||
profile_deep=profile_deep,
|
||||
)
|
||||
else:
|
||||
# Profiling for direct prompt
|
||||
_run_prompt_profiled(
|
||||
target,
|
||||
model=model,
|
||||
verbose=verbose,
|
||||
profile_deep=profile_deep,
|
||||
)
|
||||
return
|
||||
|
||||
if is_file:
|
||||
# Run from file
|
||||
_run_from_file(
|
||||
@@ -226,3 +250,116 @@ def _run_prompt(
|
||||
output.emit_error(message=str(e))
|
||||
output.print_error(str(e))
|
||||
raise typer.Exit(1)
|
||||
|
||||
|
||||
def _run_from_file_profiled(
|
||||
file_path: str,
|
||||
model: Optional[str] = None,
|
||||
framework: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
profile_deep: bool = False,
|
||||
):
|
||||
"""Run agents from a YAML file with profiling enabled."""
|
||||
from praisonai.cli.features.cli_profiler import (
|
||||
CLIProfileConfig,
|
||||
CLIProfiler,
|
||||
)
|
||||
|
||||
config = CLIProfileConfig(enabled=True, deep=profile_deep)
|
||||
profiler = CLIProfiler(config)
|
||||
|
||||
if profile_deep:
|
||||
typer.echo("⚠️ Deep profiling enabled - this adds significant overhead", err=True)
|
||||
|
||||
profiler.start()
|
||||
|
||||
# Import phase
|
||||
profiler.mark_import_start()
|
||||
try:
|
||||
from praisonai.cli.main import PraisonAI
|
||||
except ImportError as e:
|
||||
typer.echo(f"Error: {e}", err=True)
|
||||
raise typer.Exit(1)
|
||||
profiler.mark_import_end()
|
||||
|
||||
# Agent initialization phase
|
||||
profiler.mark_init_start()
|
||||
praison = PraisonAI(
|
||||
agent_file=file_path,
|
||||
framework=framework or "praisonai",
|
||||
)
|
||||
if model:
|
||||
praison.config_list[0]['model'] = model
|
||||
profiler.mark_init_end()
|
||||
|
||||
# Execution phase
|
||||
profiler.mark_exec_start()
|
||||
result = praison.run()
|
||||
profiler.mark_exec_end()
|
||||
|
||||
profiler.stop()
|
||||
|
||||
# Print result
|
||||
if result:
|
||||
print(result)
|
||||
|
||||
# Print profiling report
|
||||
profiler.print_report()
|
||||
|
||||
|
||||
def _run_prompt_profiled(
|
||||
prompt: str,
|
||||
model: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
profile_deep: bool = False,
|
||||
):
|
||||
"""Run a direct prompt with profiling enabled."""
|
||||
from praisonai.cli.features.cli_profiler import (
|
||||
CLIProfileConfig,
|
||||
CLIProfiler,
|
||||
)
|
||||
|
||||
config = CLIProfileConfig(enabled=True, deep=profile_deep)
|
||||
profiler = CLIProfiler(config)
|
||||
|
||||
if profile_deep:
|
||||
typer.echo("⚠️ Deep profiling enabled - this adds significant overhead", err=True)
|
||||
|
||||
profiler.start()
|
||||
|
||||
# Import phase
|
||||
profiler.mark_import_start()
|
||||
try:
|
||||
from praisonaiagents import Agent
|
||||
except ImportError:
|
||||
typer.echo("Error: praisonaiagents not installed", err=True)
|
||||
raise typer.Exit(1)
|
||||
profiler.mark_import_end()
|
||||
|
||||
# Agent initialization phase
|
||||
profiler.mark_init_start()
|
||||
agent_config = {
|
||||
"name": "RunAgent",
|
||||
"role": "Assistant",
|
||||
"goal": "Complete the task",
|
||||
"verbose": verbose,
|
||||
}
|
||||
if model:
|
||||
agent_config["llm"] = model
|
||||
|
||||
agent = Agent(**agent_config)
|
||||
profiler.mark_init_end()
|
||||
|
||||
# Execution phase
|
||||
profiler.mark_exec_start()
|
||||
response = agent.start(prompt)
|
||||
profiler.mark_exec_end()
|
||||
|
||||
profiler.stop()
|
||||
|
||||
# Print response
|
||||
if response:
|
||||
print(response)
|
||||
|
||||
# Print profiling report
|
||||
profiler.print_report()
|
||||
@@ -0,0 +1,335 @@
|
||||
"""
|
||||
CLI Profiler - Unified profiling for terminal-native CLI commands.
|
||||
|
||||
Provides --profile and --profile-deep flags for eligible commands:
|
||||
- praisonai "prompt"
|
||||
- praisonai chat
|
||||
- praisonai chat "prompt"
|
||||
- praisonai code
|
||||
- praisonai code "prompt"
|
||||
- praisonai run agents.yaml
|
||||
|
||||
NOT supported for:
|
||||
- praisonai ui ... (browser-based)
|
||||
- praisonai tui (Textual TUI)
|
||||
- praisonai call (long-running server)
|
||||
"""
|
||||
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import typer
|
||||
|
||||
|
||||
# Commands that support profiling
|
||||
PROFILING_ELIGIBLE_COMMANDS = frozenset({
|
||||
"chat",
|
||||
"code",
|
||||
"run",
|
||||
# Direct prompt is handled separately in main.py
|
||||
})
|
||||
|
||||
# Commands that explicitly reject profiling
|
||||
PROFILING_INELIGIBLE_COMMANDS = frozenset({
|
||||
"ui",
|
||||
"tui",
|
||||
"call",
|
||||
"realtime",
|
||||
"serve",
|
||||
"schedule",
|
||||
"acp",
|
||||
"mcp",
|
||||
"lsp",
|
||||
})
|
||||
|
||||
|
||||
@dataclass
|
||||
class CLIProfileConfig:
|
||||
"""Configuration for CLI profiling."""
|
||||
enabled: bool = False
|
||||
deep: bool = False
|
||||
output_format: str = "text" # text or json
|
||||
save_path: Optional[str] = None
|
||||
|
||||
@classmethod
|
||||
def from_flags(cls, profile: bool = False, profile_deep: bool = False) -> 'CLIProfileConfig':
|
||||
"""Create config from CLI flags."""
|
||||
return cls(
|
||||
enabled=profile or profile_deep,
|
||||
deep=profile_deep,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CLIProfileResult:
|
||||
"""Result from CLI profiling."""
|
||||
import_time_ms: float = 0.0
|
||||
agent_init_time_ms: float = 0.0
|
||||
execution_time_ms: float = 0.0
|
||||
total_time_ms: float = 0.0
|
||||
response: str = ""
|
||||
|
||||
# Deep profiling data (only if deep=True)
|
||||
function_stats: Optional[list] = None
|
||||
call_graph: Optional[dict] = None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary."""
|
||||
result = {
|
||||
"import_time_ms": self.import_time_ms,
|
||||
"agent_init_time_ms": self.agent_init_time_ms,
|
||||
"execution_time_ms": self.execution_time_ms,
|
||||
"total_time_ms": self.total_time_ms,
|
||||
}
|
||||
if self.function_stats:
|
||||
result["function_stats"] = self.function_stats
|
||||
if self.call_graph:
|
||||
result["call_graph"] = self.call_graph
|
||||
return result
|
||||
|
||||
|
||||
def print_profile_report(result: CLIProfileResult, config: CLIProfileConfig):
|
||||
"""Print profiling report to console."""
|
||||
try:
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich.panel import Panel
|
||||
console = Console()
|
||||
except ImportError:
|
||||
# Fallback to simple output
|
||||
print("\n─── Profiling ───")
|
||||
print(f"Import: {result.import_time_ms:.1f}ms")
|
||||
print(f"Agent setup: {result.agent_init_time_ms:.1f}ms")
|
||||
print(f"Execution: {result.execution_time_ms:.1f}ms")
|
||||
print(f"Total: {result.total_time_ms:.1f}ms")
|
||||
return
|
||||
|
||||
# Create timing table
|
||||
table = Table(show_header=False, box=None, padding=(0, 1))
|
||||
table.add_column("Phase", style="cyan")
|
||||
table.add_column("Time", style="green", justify="right")
|
||||
|
||||
table.add_row("Import", f"{result.import_time_ms:.1f}ms")
|
||||
table.add_row("Agent setup", f"{result.agent_init_time_ms:.1f}ms")
|
||||
table.add_row("Execution", f"{result.execution_time_ms:.1f}ms")
|
||||
table.add_row("─" * 12, "─" * 10)
|
||||
table.add_row("Total", f"{result.total_time_ms:.1f}ms")
|
||||
|
||||
console.print("\n")
|
||||
console.print(Panel(table, title="[bold]Profiling[/bold]", border_style="dim"))
|
||||
|
||||
# Deep profiling output
|
||||
if config.deep and result.function_stats:
|
||||
console.print("\n[bold yellow]⚠️ Deep profiling enabled - overhead included in measurements[/bold yellow]")
|
||||
|
||||
func_table = Table(title="Top Functions by Cumulative Time")
|
||||
func_table.add_column("Function", style="cyan")
|
||||
func_table.add_column("Calls", justify="right")
|
||||
func_table.add_column("Cumul (ms)", justify="right", style="green")
|
||||
|
||||
for func in result.function_stats[:15]:
|
||||
name = func.get("name", "")[:40]
|
||||
calls = func.get("calls", 0)
|
||||
cumtime = func.get("cumulative_time_ms", 0)
|
||||
func_table.add_row(name, str(calls), f"{cumtime:.2f}")
|
||||
|
||||
console.print(func_table)
|
||||
|
||||
|
||||
def reject_profiling_for_command(command_name: str):
|
||||
"""Raise error if profiling is attempted on ineligible command."""
|
||||
if command_name in PROFILING_INELIGIBLE_COMMANDS:
|
||||
raise typer.BadParameter(
|
||||
f"Profiling is not supported for '{command_name}' command.\n"
|
||||
f"Profiling is only available for terminal-native execution commands:\n"
|
||||
f" praisonai \"prompt\" --profile\n"
|
||||
f" praisonai chat \"prompt\" --profile\n"
|
||||
f" praisonai code \"prompt\" --profile\n"
|
||||
f" praisonai run agents.yaml --profile"
|
||||
)
|
||||
|
||||
|
||||
class CLIProfiler:
|
||||
"""
|
||||
CLI Profiler for wrapping command execution.
|
||||
|
||||
Usage:
|
||||
profiler = CLIProfiler(config)
|
||||
with profiler.profile():
|
||||
# execution code
|
||||
profiler.print_report()
|
||||
"""
|
||||
|
||||
def __init__(self, config: CLIProfileConfig):
|
||||
self.config = config
|
||||
self._result = CLIProfileResult()
|
||||
self._start_time: float = 0.0
|
||||
self._cprofile = None
|
||||
|
||||
# Phase timestamps
|
||||
self._import_start: float = 0.0
|
||||
self._import_end: float = 0.0
|
||||
self._init_start: float = 0.0
|
||||
self._init_end: float = 0.0
|
||||
self._exec_start: float = 0.0
|
||||
self._exec_end: float = 0.0
|
||||
|
||||
def start(self):
|
||||
"""Start profiling."""
|
||||
if not self.config.enabled:
|
||||
return
|
||||
|
||||
self._start_time = time.perf_counter()
|
||||
|
||||
if self.config.deep:
|
||||
import cProfile
|
||||
self._cprofile = cProfile.Profile()
|
||||
self._cprofile.enable()
|
||||
|
||||
def stop(self):
|
||||
"""Stop profiling and compute results."""
|
||||
if not self.config.enabled:
|
||||
return
|
||||
|
||||
end_time = time.perf_counter()
|
||||
|
||||
if self._cprofile:
|
||||
self._cprofile.disable()
|
||||
self._extract_cprofile_stats()
|
||||
|
||||
self._result.total_time_ms = (end_time - self._start_time) * 1000
|
||||
|
||||
def mark_import_start(self):
|
||||
"""Mark start of import phase."""
|
||||
if self.config.enabled:
|
||||
self._import_start = time.perf_counter()
|
||||
|
||||
def mark_import_end(self):
|
||||
"""Mark end of import phase."""
|
||||
if self.config.enabled:
|
||||
self._import_end = time.perf_counter()
|
||||
self._result.import_time_ms = (self._import_end - self._import_start) * 1000
|
||||
|
||||
def mark_init_start(self):
|
||||
"""Mark start of agent initialization."""
|
||||
if self.config.enabled:
|
||||
self._init_start = time.perf_counter()
|
||||
|
||||
def mark_init_end(self):
|
||||
"""Mark end of agent initialization."""
|
||||
if self.config.enabled:
|
||||
self._init_end = time.perf_counter()
|
||||
self._result.agent_init_time_ms = (self._init_end - self._init_start) * 1000
|
||||
|
||||
def mark_exec_start(self):
|
||||
"""Mark start of execution."""
|
||||
if self.config.enabled:
|
||||
self._exec_start = time.perf_counter()
|
||||
|
||||
def mark_exec_end(self):
|
||||
"""Mark end of execution."""
|
||||
if self.config.enabled:
|
||||
self._exec_end = time.perf_counter()
|
||||
self._result.execution_time_ms = (self._exec_end - self._exec_start) * 1000
|
||||
|
||||
def set_response(self, response: str):
|
||||
"""Set the response for the profile result."""
|
||||
self._result.response = response
|
||||
|
||||
def _extract_cprofile_stats(self):
|
||||
"""Extract statistics from cProfile."""
|
||||
if not self._cprofile:
|
||||
return
|
||||
|
||||
import pstats
|
||||
|
||||
stats = pstats.Stats(self._cprofile)
|
||||
stats.sort_stats("cumulative")
|
||||
|
||||
function_stats = []
|
||||
for (filename, line, name), (cc, nc, tt, ct, callers) in stats.stats.items():
|
||||
if len(function_stats) >= 100:
|
||||
break
|
||||
function_stats.append({
|
||||
"name": name,
|
||||
"file": filename,
|
||||
"line": line,
|
||||
"calls": nc,
|
||||
"total_time_ms": tt * 1000,
|
||||
"cumulative_time_ms": ct * 1000,
|
||||
})
|
||||
|
||||
function_stats.sort(key=lambda x: x["cumulative_time_ms"], reverse=True)
|
||||
self._result.function_stats = function_stats[:30]
|
||||
|
||||
def get_result(self) -> CLIProfileResult:
|
||||
"""Get the profiling result."""
|
||||
return self._result
|
||||
|
||||
def print_report(self):
|
||||
"""Print the profiling report."""
|
||||
if self.config.enabled:
|
||||
print_profile_report(self._result, self.config)
|
||||
|
||||
|
||||
def run_with_profiling(
|
||||
prompt: str,
|
||||
config: CLIProfileConfig,
|
||||
model: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
tools: Optional[str] = None,
|
||||
) -> tuple:
|
||||
"""
|
||||
Run a prompt with profiling enabled.
|
||||
|
||||
Returns:
|
||||
Tuple of (response, CLIProfileResult)
|
||||
"""
|
||||
profiler = CLIProfiler(config)
|
||||
profiler.start()
|
||||
|
||||
# Import phase
|
||||
profiler.mark_import_start()
|
||||
try:
|
||||
from praisonaiagents import Agent
|
||||
except ImportError:
|
||||
raise ImportError("praisonaiagents not installed. Install with: pip install praisonaiagents")
|
||||
profiler.mark_import_end()
|
||||
|
||||
# Agent initialization phase
|
||||
profiler.mark_init_start()
|
||||
agent_config = {
|
||||
"name": "ProfiledAgent",
|
||||
"role": "Assistant",
|
||||
"goal": "Complete the task",
|
||||
"verbose": verbose,
|
||||
}
|
||||
if model:
|
||||
agent_config["llm"] = model
|
||||
|
||||
agent = Agent(**agent_config)
|
||||
profiler.mark_init_end()
|
||||
|
||||
# Execution phase
|
||||
profiler.mark_exec_start()
|
||||
response = agent.start(prompt)
|
||||
profiler.mark_exec_end()
|
||||
|
||||
profiler.stop()
|
||||
profiler.set_response(str(response) if response else "")
|
||||
|
||||
return response, profiler.get_result()
|
||||
|
||||
|
||||
# Export all
|
||||
__all__ = [
|
||||
"CLIProfileConfig",
|
||||
"CLIProfileResult",
|
||||
"CLIProfiler",
|
||||
"PROFILING_ELIGIBLE_COMMANDS",
|
||||
"PROFILING_INELIGIBLE_COMMANDS",
|
||||
"print_profile_report",
|
||||
"reject_profiling_for_command",
|
||||
"run_with_profiling",
|
||||
]
|
||||
@@ -0,0 +1,525 @@
|
||||
"""
|
||||
Context Management CLI Feature for PraisonAI.
|
||||
|
||||
Provides CLI commands and integration for context budgeting,
|
||||
optimization, and monitoring.
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Dict, List, Any, Optional, Literal
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContextManagerConfig:
|
||||
"""Configuration for context management."""
|
||||
# Auto-compaction
|
||||
auto_compact: bool = True
|
||||
compact_threshold: float = 0.8
|
||||
strategy: str = "smart"
|
||||
|
||||
# Output reserve
|
||||
output_reserve: int = 8000
|
||||
|
||||
# Monitoring
|
||||
monitor_enabled: bool = False
|
||||
monitor_path: str = "./context.txt"
|
||||
monitor_format: Literal["human", "json"] = "human"
|
||||
monitor_frequency: Literal["turn", "tool_call", "manual", "overflow"] = "turn"
|
||||
redact_sensitive: bool = True
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "ContextManagerConfig":
|
||||
"""Load config from environment variables."""
|
||||
return cls(
|
||||
auto_compact=os.getenv("PRAISONAI_CONTEXT_AUTO_COMPACT", "true").lower() == "true",
|
||||
compact_threshold=float(os.getenv("PRAISONAI_CONTEXT_THRESHOLD", "0.8")),
|
||||
strategy=os.getenv("PRAISONAI_CONTEXT_STRATEGY", "smart"),
|
||||
output_reserve=int(os.getenv("PRAISONAI_CONTEXT_OUTPUT_RESERVE", "8000")),
|
||||
monitor_enabled=os.getenv("PRAISONAI_CONTEXT_MONITOR", "false").lower() == "true",
|
||||
monitor_path=os.getenv("PRAISONAI_CONTEXT_MONITOR_PATH", "./context.txt"),
|
||||
monitor_format=os.getenv("PRAISONAI_CONTEXT_MONITOR_FORMAT", "human"),
|
||||
monitor_frequency=os.getenv("PRAISONAI_CONTEXT_MONITOR_FREQUENCY", "turn"),
|
||||
redact_sensitive=os.getenv("PRAISONAI_CONTEXT_REDACT", "true").lower() == "true",
|
||||
)
|
||||
|
||||
def merge_cli_args(
|
||||
self,
|
||||
auto_compact: Optional[bool] = None,
|
||||
strategy: Optional[str] = None,
|
||||
threshold: Optional[float] = None,
|
||||
monitor: Optional[bool] = None,
|
||||
monitor_path: Optional[str] = None,
|
||||
monitor_format: Optional[str] = None,
|
||||
monitor_frequency: Optional[str] = None,
|
||||
redact: Optional[bool] = None,
|
||||
output_reserve: Optional[int] = None,
|
||||
) -> "ContextManagerConfig":
|
||||
"""Merge CLI arguments (highest precedence)."""
|
||||
return ContextManagerConfig(
|
||||
auto_compact=auto_compact if auto_compact is not None else self.auto_compact,
|
||||
compact_threshold=threshold if threshold is not None else self.compact_threshold,
|
||||
strategy=strategy if strategy is not None else self.strategy,
|
||||
output_reserve=output_reserve if output_reserve is not None else self.output_reserve,
|
||||
monitor_enabled=monitor if monitor is not None else self.monitor_enabled,
|
||||
monitor_path=monitor_path if monitor_path is not None else self.monitor_path,
|
||||
monitor_format=monitor_format if monitor_format is not None else self.monitor_format,
|
||||
monitor_frequency=monitor_frequency if monitor_frequency is not None else self.monitor_frequency,
|
||||
redact_sensitive=redact if redact is not None else self.redact_sensitive,
|
||||
)
|
||||
|
||||
|
||||
class ContextManagerHandler:
|
||||
"""
|
||||
Handles context management for interactive CLI sessions.
|
||||
|
||||
Integrates budgeting, composition, optimization, and monitoring.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: Optional[ContextManagerConfig] = None,
|
||||
model: str = "gpt-4o-mini",
|
||||
session_id: str = "",
|
||||
agent_name: str = "Assistant",
|
||||
):
|
||||
"""
|
||||
Initialize context manager.
|
||||
|
||||
Args:
|
||||
config: Context management configuration
|
||||
model: Model name for budget calculation
|
||||
session_id: Session identifier
|
||||
agent_name: Agent name for monitoring
|
||||
"""
|
||||
self.config = config or ContextManagerConfig.from_env()
|
||||
self.model = model
|
||||
self.session_id = session_id
|
||||
self.agent_name = agent_name
|
||||
|
||||
# Lazy-loaded components
|
||||
self._budgeter = None
|
||||
self._composer = None
|
||||
self._monitor = None
|
||||
self._ledger = None
|
||||
|
||||
@property
|
||||
def budgeter(self):
|
||||
"""Get or create budgeter."""
|
||||
if self._budgeter is None:
|
||||
from praisonaiagents.context import ContextBudgeter
|
||||
self._budgeter = ContextBudgeter(
|
||||
model=self.model,
|
||||
output_reserve=self.config.output_reserve,
|
||||
)
|
||||
return self._budgeter
|
||||
|
||||
@property
|
||||
def composer(self):
|
||||
"""Get or create composer."""
|
||||
if self._composer is None:
|
||||
from praisonaiagents.context import ContextComposer
|
||||
self._composer = ContextComposer(
|
||||
budget=self.budgeter.allocate(),
|
||||
)
|
||||
return self._composer
|
||||
|
||||
@property
|
||||
def monitor(self):
|
||||
"""Get or create monitor."""
|
||||
if self._monitor is None:
|
||||
from praisonaiagents.context import ContextMonitor
|
||||
self._monitor = ContextMonitor(
|
||||
enabled=self.config.monitor_enabled,
|
||||
path=self.config.monitor_path,
|
||||
format=self.config.monitor_format,
|
||||
frequency=self.config.monitor_frequency,
|
||||
redact_sensitive=self.config.redact_sensitive,
|
||||
)
|
||||
return self._monitor
|
||||
|
||||
@property
|
||||
def ledger(self):
|
||||
"""Get or create ledger manager."""
|
||||
if self._ledger is None:
|
||||
from praisonaiagents.context import ContextLedgerManager
|
||||
self._ledger = ContextLedgerManager(agent_id=self.agent_name)
|
||||
self._ledger.set_budget(self.budgeter.allocate())
|
||||
return self._ledger
|
||||
|
||||
def set_model(self, model: str) -> None:
|
||||
"""Update model and reset budgeter."""
|
||||
self.model = model
|
||||
self._budgeter = None
|
||||
self._composer = None
|
||||
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
"""Get current context statistics."""
|
||||
budget = self.budgeter.allocate()
|
||||
ledger_data = self.ledger.to_dict()
|
||||
|
||||
return {
|
||||
"model": self.model,
|
||||
"model_limit": budget.model_limit,
|
||||
"output_reserve": budget.output_reserve,
|
||||
"usable": budget.usable,
|
||||
"total_tokens": self.ledger.get_total(),
|
||||
"utilization": self.ledger.get_utilization(),
|
||||
"turn_count": self.ledger.get_ledger().turn_count,
|
||||
"message_count": self.ledger.get_ledger().message_count,
|
||||
"segments": ledger_data.get("segments", {}),
|
||||
"warnings": self.ledger.get_warnings(),
|
||||
"auto_compact": self.config.auto_compact,
|
||||
"monitor_enabled": self.config.monitor_enabled,
|
||||
}
|
||||
|
||||
def track_history(self, messages: List[Dict[str, Any]]) -> int:
|
||||
"""Track conversation history tokens."""
|
||||
return self.ledger.track_history(messages)
|
||||
|
||||
def track_system_prompt(self, content: str) -> int:
|
||||
"""Track system prompt tokens."""
|
||||
return self.ledger.track_system_prompt(content)
|
||||
|
||||
def track_tools(self, tools: List[Dict[str, Any]]) -> int:
|
||||
"""Track tool schema tokens."""
|
||||
return self.ledger.track_tools(tools)
|
||||
|
||||
def check_overflow(self) -> bool:
|
||||
"""Check if context is approaching limit."""
|
||||
return self.budgeter.check_overflow(
|
||||
self.ledger.get_total(),
|
||||
threshold=self.config.compact_threshold,
|
||||
)
|
||||
|
||||
def should_auto_compact(self) -> bool:
|
||||
"""Check if auto-compaction should trigger."""
|
||||
return self.config.auto_compact and self.check_overflow()
|
||||
|
||||
def optimize(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
strategy: Optional[str] = None,
|
||||
) -> tuple:
|
||||
"""
|
||||
Optimize messages using configured strategy.
|
||||
|
||||
Args:
|
||||
messages: Messages to optimize
|
||||
strategy: Override strategy
|
||||
|
||||
Returns:
|
||||
Tuple of (optimized_messages, result)
|
||||
"""
|
||||
from praisonaiagents.context import get_optimizer, OptimizerStrategy
|
||||
|
||||
strategy_name = strategy or self.config.strategy
|
||||
try:
|
||||
strategy_enum = OptimizerStrategy(strategy_name)
|
||||
except ValueError:
|
||||
strategy_enum = OptimizerStrategy.SMART
|
||||
|
||||
optimizer = get_optimizer(strategy_enum)
|
||||
target = int(self.budgeter.usable * self.config.compact_threshold)
|
||||
|
||||
return optimizer.optimize(messages, target, self.ledger.get_ledger())
|
||||
|
||||
def write_snapshot(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
trigger: str = "manual",
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
Write context snapshot to disk.
|
||||
|
||||
Args:
|
||||
messages: Current messages
|
||||
trigger: What triggered the snapshot
|
||||
|
||||
Returns:
|
||||
Path to written file, or None
|
||||
"""
|
||||
return self.monitor.snapshot(
|
||||
ledger=self.ledger.get_ledger(),
|
||||
budget=self.budgeter.allocate(),
|
||||
messages=messages,
|
||||
session_id=self.session_id,
|
||||
agent_name=self.agent_name,
|
||||
model_name=self.model,
|
||||
trigger=trigger,
|
||||
)
|
||||
|
||||
def enable_monitor(self) -> None:
|
||||
"""Enable context monitoring."""
|
||||
self.config.monitor_enabled = True
|
||||
self.monitor.enable()
|
||||
|
||||
def disable_monitor(self) -> None:
|
||||
"""Disable context monitoring."""
|
||||
self.config.monitor_enabled = False
|
||||
self.monitor.disable()
|
||||
|
||||
def set_monitor_path(self, path: str) -> None:
|
||||
"""Set monitor output path."""
|
||||
self.config.monitor_path = path
|
||||
self.monitor.set_path(path)
|
||||
|
||||
def set_monitor_format(self, format: str) -> None:
|
||||
"""Set monitor output format."""
|
||||
self.config.monitor_format = format
|
||||
self.monitor.set_format(format)
|
||||
|
||||
def set_monitor_frequency(self, frequency: str) -> None:
|
||||
"""Set monitor update frequency."""
|
||||
self.config.monitor_frequency = frequency
|
||||
self.monitor.set_frequency(frequency)
|
||||
|
||||
|
||||
def handle_context_command(
|
||||
console,
|
||||
args: str,
|
||||
session_state: Dict[str, Any],
|
||||
context_manager: Optional[ContextManagerHandler] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Handle /context command and subcommands.
|
||||
|
||||
Usage:
|
||||
/context - Show context stats
|
||||
/context show - Show summary + budgets
|
||||
/context stats - Token ledger table
|
||||
/context budget - Budget allocation details
|
||||
/context dump - Write snapshot now
|
||||
/context on - Enable monitoring
|
||||
/context off - Disable monitoring
|
||||
/context path <path> - Set snapshot path
|
||||
/context format <fmt> - Set format (human/json)
|
||||
/context frequency <f>- Set frequency
|
||||
/context compact - Trigger optimization
|
||||
"""
|
||||
args = args.strip().lower() if args else ""
|
||||
parts = args.split(maxsplit=1)
|
||||
subcommand = parts[0] if parts else ""
|
||||
subargs = parts[1] if len(parts) > 1 else ""
|
||||
|
||||
# Get or create context manager
|
||||
if context_manager is None:
|
||||
context_manager = session_state.get("context_manager")
|
||||
if context_manager is None:
|
||||
context_manager = ContextManagerHandler(
|
||||
model=session_state.get("current_model", "gpt-4o-mini"),
|
||||
session_id=session_state.get("session_id", ""),
|
||||
)
|
||||
session_state["context_manager"] = context_manager
|
||||
|
||||
# Update history tracking
|
||||
history = session_state.get("conversation_history", [])
|
||||
context_manager.track_history(history)
|
||||
|
||||
if subcommand in ("", "show"):
|
||||
_show_context_summary(console, context_manager)
|
||||
elif subcommand == "stats":
|
||||
_show_context_stats(console, context_manager)
|
||||
elif subcommand == "budget":
|
||||
_show_budget_details(console, context_manager)
|
||||
elif subcommand == "dump":
|
||||
_dump_context(console, context_manager, history)
|
||||
elif subcommand == "on":
|
||||
context_manager.enable_monitor()
|
||||
console.print("[green]✓ Context monitoring enabled[/green]")
|
||||
console.print(f"[dim]Output: {context_manager.config.monitor_path}[/dim]")
|
||||
elif subcommand == "off":
|
||||
context_manager.disable_monitor()
|
||||
console.print("[yellow]Context monitoring disabled[/yellow]")
|
||||
elif subcommand == "path":
|
||||
if subargs:
|
||||
context_manager.set_monitor_path(subargs)
|
||||
console.print(f"[green]✓ Monitor path set to: {subargs}[/green]")
|
||||
else:
|
||||
console.print(f"[cyan]Current path: {context_manager.config.monitor_path}[/cyan]")
|
||||
elif subcommand == "format":
|
||||
if subargs in ("human", "json"):
|
||||
context_manager.set_monitor_format(subargs)
|
||||
console.print(f"[green]✓ Monitor format set to: {subargs}[/green]")
|
||||
else:
|
||||
console.print("[yellow]Format must be 'human' or 'json'[/yellow]")
|
||||
elif subcommand == "frequency":
|
||||
valid = ("turn", "tool_call", "manual", "overflow")
|
||||
if subargs in valid:
|
||||
context_manager.set_monitor_frequency(subargs)
|
||||
console.print(f"[green]✓ Monitor frequency set to: {subargs}[/green]")
|
||||
else:
|
||||
console.print(f"[yellow]Frequency must be one of: {', '.join(valid)}[/yellow]")
|
||||
elif subcommand == "compact":
|
||||
_compact_context(console, context_manager, session_state)
|
||||
else:
|
||||
console.print("[yellow]Unknown subcommand. Use: show, stats, budget, dump, on, off, path, format, frequency, compact[/yellow]")
|
||||
|
||||
|
||||
def _show_context_summary(console, manager: ContextManagerHandler) -> None:
|
||||
"""Show context summary."""
|
||||
stats = manager.get_stats()
|
||||
|
||||
console.print("\n[bold cyan]Context Summary[/bold cyan]")
|
||||
console.print(f" Model: {stats['model']}")
|
||||
console.print(f" Model Limit: {stats['model_limit']:,} tokens")
|
||||
console.print(f" Output Reserve: {stats['output_reserve']:,} tokens")
|
||||
console.print(f" Usable Budget: {stats['usable']:,} tokens")
|
||||
console.print(f" Current Usage: {stats['total_tokens']:,} tokens ({stats['utilization']*100:.1f}%)")
|
||||
console.print(f" Turns: {stats['turn_count']}")
|
||||
console.print(f" Messages: {stats['message_count']}")
|
||||
console.print(f" Auto-Compact: {'enabled' if stats['auto_compact'] else 'disabled'}")
|
||||
console.print(f" Monitoring: {'enabled' if stats['monitor_enabled'] else 'disabled'}")
|
||||
|
||||
if stats['warnings']:
|
||||
console.print("\n[yellow]Warnings:[/yellow]")
|
||||
for warning in stats['warnings']:
|
||||
console.print(f" ⚠ {warning}")
|
||||
console.print("")
|
||||
|
||||
|
||||
def _show_context_stats(console, manager: ContextManagerHandler) -> None:
|
||||
"""Show detailed token stats."""
|
||||
stats = manager.get_stats()
|
||||
segments = stats.get("segments", {})
|
||||
|
||||
console.print("\n[bold cyan]Token Ledger[/bold cyan]")
|
||||
console.print(f"{'Segment':<20} {'Tokens':>10} {'Budget':>10} {'Used':>8}")
|
||||
console.print("-" * 50)
|
||||
|
||||
for name, data in segments.items():
|
||||
tokens = data.get("tokens", 0)
|
||||
budget = data.get("budget", 0)
|
||||
util = data.get("utilization", 0) * 100
|
||||
console.print(f"{name:<20} {tokens:>10,} {budget:>10,} {util:>7.1f}%")
|
||||
|
||||
console.print("-" * 50)
|
||||
console.print(f"{'TOTAL':<20} {stats['total_tokens']:>10,} {stats['usable']:>10,} {stats['utilization']*100:>7.1f}%")
|
||||
console.print("")
|
||||
|
||||
|
||||
def _show_budget_details(console, manager: ContextManagerHandler) -> None:
|
||||
"""Show budget allocation details."""
|
||||
budget = manager.budgeter.allocate()
|
||||
|
||||
console.print("\n[bold cyan]Budget Allocation[/bold cyan]")
|
||||
console.print(f" Model Limit: {budget.model_limit:,}")
|
||||
console.print(f" Output Reserve: {budget.output_reserve:,}")
|
||||
console.print(f" Usable: {budget.usable:,}")
|
||||
console.print("")
|
||||
console.print(" Segment Budgets:")
|
||||
console.print(f" System Prompt: {budget.system_prompt:,}")
|
||||
console.print(f" Rules: {budget.rules:,}")
|
||||
console.print(f" Skills: {budget.skills:,}")
|
||||
console.print(f" Memory: {budget.memory:,}")
|
||||
console.print(f" Tool Schemas: {budget.tools_schema:,}")
|
||||
console.print(f" Tool Outputs: {budget.tool_outputs:,}")
|
||||
console.print(f" History: {budget.history_budget:,}")
|
||||
console.print(f" Buffer: {budget.buffer:,}")
|
||||
console.print("")
|
||||
|
||||
|
||||
def _dump_context(console, manager: ContextManagerHandler, messages: List[Dict[str, Any]]) -> None:
|
||||
"""Dump context snapshot to file."""
|
||||
# Temporarily enable if needed
|
||||
was_enabled = manager.config.monitor_enabled
|
||||
if not was_enabled:
|
||||
manager.enable_monitor()
|
||||
|
||||
path = manager.write_snapshot(messages, trigger="manual")
|
||||
|
||||
if not was_enabled:
|
||||
manager.disable_monitor()
|
||||
|
||||
if path:
|
||||
console.print(f"[green]✓ Context snapshot written to: {path}[/green]")
|
||||
else:
|
||||
console.print("[yellow]Failed to write context snapshot[/yellow]")
|
||||
|
||||
|
||||
def _compact_context(console, manager: ContextManagerHandler, session_state: Dict[str, Any]) -> None:
|
||||
"""Trigger context compaction."""
|
||||
history = session_state.get("conversation_history", [])
|
||||
|
||||
if len(history) < 4:
|
||||
console.print("[yellow]Not enough history to compact (need at least 4 messages)[/yellow]")
|
||||
return
|
||||
|
||||
console.print("[dim]Optimizing context...[/dim]")
|
||||
|
||||
optimized, result = manager.optimize(history)
|
||||
|
||||
if result.tokens_saved > 0:
|
||||
session_state["conversation_history"] = optimized
|
||||
console.print(f"[green]✓ Optimized: {result.original_tokens:,} → {result.optimized_tokens:,} tokens[/green]")
|
||||
console.print(f"[dim]Saved {result.tokens_saved:,} tokens ({result.reduction_percent:.1f}%)[/dim]")
|
||||
console.print(f"[dim]Strategy: {result.strategy_used.value}[/dim]")
|
||||
else:
|
||||
console.print("[yellow]No optimization needed[/yellow]")
|
||||
|
||||
|
||||
# CLI argument definitions for argparse integration
|
||||
CONTEXT_CLI_ARGS = [
|
||||
{
|
||||
"flags": ["--context-auto-compact", "--no-context-auto-compact"],
|
||||
"dest": "context_auto_compact",
|
||||
"action": "store_true",
|
||||
"default": None,
|
||||
"help": "Enable/disable auto-compaction",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-strategy"],
|
||||
"dest": "context_strategy",
|
||||
"choices": ["smart", "truncate", "sliding_window", "summarize", "prune_tools"],
|
||||
"default": None,
|
||||
"help": "Context optimization strategy",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-threshold"],
|
||||
"dest": "context_threshold",
|
||||
"type": float,
|
||||
"default": None,
|
||||
"help": "Auto-compact threshold (0.0-1.0)",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-monitor"],
|
||||
"dest": "context_monitor",
|
||||
"action": "store_true",
|
||||
"default": None,
|
||||
"help": "Enable context monitoring",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-monitor-path"],
|
||||
"dest": "context_monitor_path",
|
||||
"default": None,
|
||||
"help": "Context monitor output path",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-monitor-format"],
|
||||
"dest": "context_monitor_format",
|
||||
"choices": ["human", "json"],
|
||||
"default": None,
|
||||
"help": "Context monitor output format",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-monitor-frequency"],
|
||||
"dest": "context_monitor_frequency",
|
||||
"choices": ["turn", "tool_call", "manual", "overflow"],
|
||||
"default": None,
|
||||
"help": "Context monitor update frequency",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-redact", "--no-context-redact"],
|
||||
"dest": "context_redact",
|
||||
"action": "store_true",
|
||||
"default": None,
|
||||
"help": "Enable/disable sensitive data redaction",
|
||||
},
|
||||
{
|
||||
"flags": ["--context-output-reserve"],
|
||||
"dest": "context_output_reserve",
|
||||
"type": int,
|
||||
"default": None,
|
||||
"help": "Output token reserve",
|
||||
},
|
||||
]
|
||||
@@ -5181,6 +5181,9 @@ Now, {final_instruction.lower()}:"""
|
||||
elif cmd == "compact":
|
||||
self._handle_compact_command(console, session_state)
|
||||
continue
|
||||
elif cmd == "context":
|
||||
self._handle_context_command(console, cmd_args, session_state)
|
||||
continue
|
||||
elif cmd == "undo":
|
||||
self._handle_undo_command(console, session_state)
|
||||
continue
|
||||
@@ -5611,6 +5614,43 @@ Provide a concise summary (max 200 words):"""
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error compacting: {e}[/red]")
|
||||
|
||||
def _handle_context_command(self, console, args, session_state):
|
||||
"""
|
||||
Handle /context command - manage context budgeting and monitoring.
|
||||
|
||||
Usage:
|
||||
- /context - Show context stats
|
||||
- /context show - Show summary + budgets
|
||||
- /context stats - Token ledger table
|
||||
- /context budget - Budget allocation details
|
||||
- /context dump - Write snapshot now
|
||||
- /context on - Enable monitoring
|
||||
- /context off - Disable monitoring
|
||||
- /context path <path> - Set snapshot path
|
||||
- /context format <fmt> - Set format (human/json)
|
||||
- /context frequency <f>- Set frequency
|
||||
- /context compact - Trigger optimization
|
||||
"""
|
||||
try:
|
||||
from praisonai.cli.features.context_manager import (
|
||||
handle_context_command,
|
||||
ContextManagerHandler,
|
||||
)
|
||||
|
||||
# Get or create context manager
|
||||
context_manager = session_state.get("context_manager")
|
||||
if context_manager is None:
|
||||
context_manager = ContextManagerHandler(
|
||||
model=session_state.get("current_model", "gpt-4o-mini"),
|
||||
session_id=session_state.get("session_id", ""),
|
||||
)
|
||||
session_state["context_manager"] = context_manager
|
||||
|
||||
handle_context_command(console, args, session_state, context_manager)
|
||||
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error handling context command: {e}[/red]")
|
||||
|
||||
def _handle_undo_command(self, console, session_state):
|
||||
"""
|
||||
Handle /undo command - undo the last response.
|
||||
|
||||
@@ -57,7 +57,7 @@ class CloudDeployer:
|
||||
file.write("FROM python:3.11-slim\n")
|
||||
file.write("WORKDIR /app\n")
|
||||
file.write("COPY . .\n")
|
||||
file.write("RUN pip install flask praisonai==3.1.8 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==3.2.0 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
__version__ = "3.1.8"
|
||||
__version__ = "3.2.0"
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.6.8",
|
||||
"praisonaiagents>=0.7.0",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
|
||||
Generated
+4
-4
@@ -4276,7 +4276,7 @@ requires-dist = [
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.1.0" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen-v4'", specifier = ">=0.1.0" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.1.0" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.6.8" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.7.0" },
|
||||
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = "==0.2.29" },
|
||||
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
|
||||
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
|
||||
@@ -4324,7 +4324,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.6.8"
|
||||
version = "0.7.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
@@ -4333,9 +4333,9 @@ dependencies = [
|
||||
{ name = "pydantic" },
|
||||
{ name = "rich" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a8/43/eba942ff2c03f066aa6a5ffaa0c0a3e636104306d53186d270a644bf4879/praisonaiagents-0.6.8.tar.gz", hash = "sha256:67e2a58a90339be644dde52138c8661538e5e092120b24424d21609c49fec4b5", size = 844876, upload-time = "2026-01-07T15:02:37.524Z" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/6c/14/cb92a1e8ab1e210e78338bfed30d617ad10ad96452a0b0bed85dacf992eb/praisonaiagents-0.7.0.tar.gz", hash = "sha256:e393749cc9da921e98cf47843675066f60adc9fa8ba2a0748add77a1e2effa8d", size = 861098, upload-time = "2026-01-07T19:36:49.455Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/74/33/e699299ce0952a8036b3d72bc66bd14dbd7459cfb3ce5447e197d7c64b24/praisonaiagents-0.6.8-py3-none-any.whl", hash = "sha256:d1eea97cd397d09e4e7b43c9ce86b67ec6d352046587662363510c5b1cc54d3f", size = 802217, upload-time = "2026-01-07T15:02:35.44Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/41/bf/ce802a9f899131c8ff525b669f3754cf23d39a8e612a2aa1d18401a3569a/praisonaiagents-0.7.0-py3-none-any.whl", hash = "sha256:a1cd9514e95dc54cd214548a72c734ba89131e58bb2b6a34c43f0e1ac36e72eb", size = 822619, upload-time = "2026-01-07T19:36:47.174Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in new issue
Block a user