Create Agent
Create intelligent AI agents with memory and conversation capabilities using Python.Overview
The Agent class combines Chat, Memory, and Storage capabilities into a unified interface for building sophisticated AI applications. Agents maintain conversation context, store persistent data, and can be extended with custom tools and behaviors.Constructor
Parameters
AgentConfig
required
Configuration object for the agent
ZGComputeBroker
required
The compute broker for connecting to 0G network
ZGStorageClient
required
The storage client for persistent memory
AgentConfig
Convenience Function
create_agent()
Create a pre-configured agent with default settings.config(Dict[str, Any]): Agent configuration dictionary
name(str): Agent nameprovider_address(str): 0G network provider addressmemory_bucket(str): Storage bucket for memoryprivate_key(str): Private key for blockchain operationsrpc_url(str, optional): Custom RPC endpointindexer_rpc(str, optional): Custom indexer endpointkv_rpc(str, optional): Custom KV storage endpointmax_ephemeral_messages(int, optional): Max conversation historytemperature(float, optional): Response randomness (0.0-2.0)max_tokens(int, optional): Maximum response length
Examples
Core Methods
init()
Initialize the agent and test connections.ask()
Send a simple question to the agent.input_text(str): The question or message to send
str - The agent’s response
Example:
chat_with_context()
Have a conversation with full context and memory.input_text(str): The message to send
str - The agent’s response with full context
Example:
stream_chat()
Stream responses in real-time.input_text(str): The message to sendon_chunk(Callable): Function to handle each response chunk
str - Complete response after streaming
Example:
System Prompt Management
set_system_prompt()
Set the agent’s system prompt to define behavior.save_system_prompt()
Save the current system prompt to persistent memory.Memory Methods
remember()
Store data in persistent memory.recall()
Retrieve data from persistent memory.forget()
Remove data from persistent memory.Conversation Management
save_conversation()
Save current conversation to persistent storage.str - The conversation ID
load_conversation()
Load a previously saved conversation.clear_conversation()
Clear current conversation from memory.Configuration Methods
set_temperature()
Adjust response creativity.set_max_tokens()
Set maximum response length.Introspection Methods
get_stats()
Get agent statistics and current state.get_service_info()
Get information about the connected AI service.Error Handling
Best Practices
- Always Initialize: Call
await agent.init()before using the agent - Set System Prompts: Define clear behavior with system prompts
- Handle Errors: Implement proper error handling for network issues
- Manage Memory: Use conversation management for long sessions
- Save Important Data: Store critical information in persistent memory
- Monitor Usage: Check agent stats periodically for performance insights