Skip to main content

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.
Parameters:
  • config (Dict[str, Any]): Agent configuration dictionary
Configuration Options:
  • name (str): Agent name
  • provider_address (str): 0G network provider address
  • memory_bucket (str): Storage bucket for memory
  • private_key (str): Private key for blockchain operations
  • rpc_url (str, optional): Custom RPC endpoint
  • indexer_rpc (str, optional): Custom indexer endpoint
  • kv_rpc (str, optional): Custom KV storage endpoint
  • max_ephemeral_messages (int, optional): Max conversation history
  • temperature (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.
Example:

ask()

Send a simple question to the agent.
Parameters:
  • input_text (str): The question or message to send
Returns: str - The agent’s response Example:

chat_with_context()

Have a conversation with full context and memory.
Parameters:
  • input_text (str): The message to send
Returns: str - The agent’s response with full context Example:

stream_chat()

Stream responses in real-time.
Parameters:
  • input_text (str): The message to send
  • on_chunk (Callable): Function to handle each response chunk
Returns: str - Complete response after streaming Example:

System Prompt Management

set_system_prompt()

Set the agent’s system prompt to define behavior.
Example:

save_system_prompt()

Save the current system prompt to persistent memory.
Example:

Memory Methods

remember()

Store data in persistent memory.

recall()

Retrieve data from persistent memory.

forget()

Remove data from persistent memory.
Example:

Conversation Management

save_conversation()

Save current conversation to persistent storage.
Returns: str - The conversation ID

load_conversation()

Load a previously saved conversation.

clear_conversation()

Clear current conversation from memory.
Example:

Configuration Methods

set_temperature()

Adjust response creativity.

set_max_tokens()

Set maximum response length.
Example:

Introspection Methods

get_stats()

Get agent statistics and current state.
Returns: Dictionary with agent statistics

get_service_info()

Get information about the connected AI service.
Example:

Error Handling

Best Practices

  1. Always Initialize: Call await agent.init() before using the agent
  2. Set System Prompts: Define clear behavior with system prompts
  3. Handle Errors: Implement proper error handling for network issues
  4. Manage Memory: Use conversation management for long sessions
  5. Save Important Data: Store critical information in persistent memory
  6. Monitor Usage: Check agent stats periodically for performance insights

Next Steps