Skip to main content

Create Chat

Create a new chat instance to interact with AI models using Python.

Constructor

Parameters

ZGComputeBroker
required
The compute broker instance for connecting to the 0G network
str
required
The provider address on the 0G network for the AI model
float
default:"0.7"
Controls randomness in responses (0.0-2.0)
int
default:"1000"
Maximum number of tokens in the response

Response

Returns a Chat instance that can be used to send messages and stream responses.

Example

Available Models

The 0G AI SDK connects to models running on the 0G decentralized compute network:

Methods

ask()

Send a simple question to the AI model.
Parameters:
  • question (str): The question to ask the AI
  • system_prompt (Optional[str]): Optional system prompt to set context
Returns: str - The AI’s response Example:

chat_completion()

Send a structured chat completion request.
Parameters:
  • messages (List[ChatMessage]): List of chat messages
Returns: ChatCompletionResponse - Structured response with usage info Example:

stream_chat_completion()

Stream responses for real-time chat.
Parameters:
  • messages (List[ChatMessage]): List of chat messages
  • on_chunk (callable): Callback function for each chunk
Returns: str - Complete response text Example:

get_service_info()

Get information about the AI service.
Returns: ServiceMetadata - Service information including endpoint and model Example:

Configuration Methods

set_temperature()

Update the temperature setting.
Parameters:
  • temperature (float): New temperature value (0.0-2.0)
Example:

set_max_tokens()

Update the maximum tokens setting.
Parameters:
  • max_tokens (int): New maximum tokens value
Example:

get_config()

Get current configuration.
Returns: Dict[str, Any] - Current configuration settings Example:

Error Handling

The constructor and methods will raise exceptions if:
  • Invalid provider address is provided
  • Network connection fails
  • Invalid configuration parameters are passed

Next Steps