praval.core.agent๏
Core Agent class for the Praval framework.
The Agent class provides a simple, composable interface for LLM-based conversations with support for multiple providers, tools, and state persistence.
Classes
|
A simple, composable LLM agent. |
|
Configuration for Agent behavior and LLM parameters. |
- class praval.core.agent.AgentConfig(provider=None, temperature=0.7, max_tokens=1000, system_message=None)[source]๏
Bases:
objectConfiguration for Agent behavior and LLM parameters.
- class praval.core.agent.Agent(name, provider=None, persist_state=False, system_message=None, config=None, memory_enabled=False, memory_config=None, knowledge_base=None, max_history=100, hitl_enabled=False, hitl_db_path=None)[source]๏
Bases:
objectA simple, composable LLM agent.
The Agent class provides the core functionality for LLM-based conversations with support for multiple providers, conversation history, tools, and state persistence.
Examples
Basic usage: >>> agent = Agent(โassistantโ) >>> response = agent.chat(โHello!โ)
With persistence: >>> agent = Agent(โmy_agentโ, persist_state=True) >>> agent.chat(โRemember this conversationโ)
With tools: >>> agent = Agent(โcalculatorโ) >>> @agent.tool >>> def add(x: int, y: int) -> int: โฆ return x + y
- Parameters:
- __init__(name, provider=None, persist_state=False, system_message=None, config=None, memory_enabled=False, memory_config=None, knowledge_base=None, max_history=100, hitl_enabled=False, hitl_db_path=None)[source]๏
Initialize a new Agent.
- Parameters:
name (
str) โ Unique identifier for this agentprovider (
Optional[str]) โ LLM provider to use (openai, anthropic, cohere)persist_state (
bool) โ Whether to persist conversation statesystem_message (
Optional[str]) โ System message to set agent behaviorconfig (
Optional[Dict[str,Any]]) โ Additional configuration parametersmemory_enabled (
bool) โ Whether to enable vector memory capabilitiesmemory_config (
Optional[Dict[str,Any]]) โ Configuration for memory systemknowledge_base (
Optional[str]) โ Path to knowledge base files to auto-indexmax_history (
Optional[int]) โ Max conversation turns to retain (None for unbounded)hitl_enabled (bool)
hitl_db_path (str | None)
- Raises:
ValueError โ If name is empty or configuration is invalid
ProviderError โ If provider setup fails
- chat(message)[source]๏
Send a message to the agent and get a response.
- Parameters:
message (
Optional[str]) โ User message to send to the agent- Return type:
- Returns:
Agentโs response as a string
- Raises:
ValueError โ If message is empty or None
PravalError โ If response generation fails
- get_pending_interventions(run_id=None, limit=100)[source]๏
Get pending interventions filtered to this agent.
- approve_intervention(intervention_id, *, reviewer='human', edited_args=None)[source]๏
Approve or edit-approve an intervention for this agent.
- reject_intervention(intervention_id, *, reason, reviewer='human')[source]๏
Reject an intervention for this agent.
- tool(func)[source]๏
Decorator to register a function as a tool for the agent.
- Parameters:
func (
Callable) โ Function to register as a tool- Return type:
- Returns:
The original function (unchanged)
- Raises:
ValueError โ If function lacks proper type hints
- send_knowledge(to_agent, knowledge, channel='main')[source]๏
Send knowledge to another agent through the reef.
- broadcast_knowledge(knowledge, channel='main')[source]๏
Broadcast knowledge to all agents in the reef.
- request_knowledge(from_agent, request, timeout=30)[source]๏
Request knowledge from another agent with timeout.
- on_spore_received(spore)[source]๏
Handle received spores from the reef.
This is a default implementation that can be overridden by subclasses for custom spore handling.
- Parameters:
spore โ The received Spore object
- Return type:
- property spore_handler: Callable | None๏
Get the current spore handler for this agent.
- Returns:
The custom spore handler function, or None if not set
- close()[source]๏
Release all resources held by the agent.
This method: - Unsubscribes from all reef channels - Shuts down the memory system - Clears conversation history
Safe to call multiple times. After calling close(), the agent should not be used for further operations.
Example:
agent = Agent("assistant") try: response = agent.chat("Hello") finally: agent.close() # Or use as context manager: with Agent("assistant") as agent: response = agent.chat("Hello")
- Return type:
- create_knowledge_reference(content, importance=0.8)[source]๏
Create knowledge references for lightweight spores