bittensor.dendrite#
Attributes#
Classes#
The Dendrite class represents the abstracted implementation of a network client module. |
|
The Dendrite class represents the abstracted implementation of a network client module. |
Functions#
|
Module Contents#
- class bittensor.dendrite.DendriteMixin(wallet=None)[source]#
The Dendrite class represents the abstracted implementation of a network client module.
In the brain analogy, dendrites receive signals from other neurons (in this case, network servers or axons), and the Dendrite class here is designed to send requests to those endpoint to recieve inputs.
This class includes a wallet or keypair used for signing messages, and methods for making HTTP requests to the network servers. It also provides functionalities such as logging network requests and processing server responses.
- Parameters:
- __repr__()[source]#
Returns a string representation of the Dendrite object, acting as a fallback for __str__().
- Return type:
- query(self, *args, **kwargs) bittensor.Synapse | List[bittensor.Synapse] [source]#
Makes synchronous requests to one or multiple target Axons and returns responses.
- Return type:
List[Union[AsyncGenerator[Any, Any], bittensor.Synapse, bittensor.StreamingSynapse]]
- forward(self, axons, synapse=bittensor.Synapse(), timeout=12, deserialize=True, run_async=True, streaming=False) bittensor.Synapse [source]#
Asynchronously sends requests to one or multiple Axons and collates their responses.
- Parameters:
axons (Union[List[Union[bittensor.AxonInfo, bittensor.axon]], Union[bittensor.AxonInfo, bittensor.axon]])
synapse (bittensor.Synapse)
timeout (float)
deserialize (bool)
run_async (bool)
streaming (bool)
- Return type:
List[Union[AsyncGenerator[Any, Any], bittensor.Synapse, bittensor.StreamingSynapse]]
- call(self, target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True) bittensor.Synapse [source]#
Asynchronously sends a request to a specified Axon and processes the response.
- Parameters:
target_axon (Union[bittensor.AxonInfo, bittensor.axon])
synapse (bittensor.Synapse)
timeout (float)
deserialize (bool)
- Return type:
- call_stream(self, target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True) AsyncGenerator[bittensor.Synapse, None] [source]#
Sends a request to a specified Axon and yields an AsyncGenerator that contains streaming response chunks before finally yielding the filled Synapse as the final element.
- Parameters:
target_axon (Union[bittensor.AxonInfo, bittensor.axon])
synapse (bittensor.StreamingSynapse)
timeout (float)
deserialize (bool)
- Return type:
AsyncGenerator[Any, Any]
- preprocess_synapse_for_request(self, target_axon_info, synapse, timeout=12.0) bittensor.Synapse [source]#
Preprocesses the synapse for making a request, including building headers and signing.
- Parameters:
target_axon_info (bittensor.AxonInfo)
synapse (bittensor.Synapse)
timeout (float)
- Return type:
- process_server_response(self, server_response, json_response, local_synapse)[source]#
Processes the server response, updates the local synapse state, and merges headers.
- Parameters:
server_response (aiohttp.ClientResponse)
json_response (dict)
local_synapse (bittensor.Synapse)
Note
When working with async aiohttp client sessions, it is recommended to use a context manager.
Example with a context manager:
>>> aysnc with dendrite(wallet = bittensor.wallet()) as d: >>> print(d) >>> d( <axon> ) # ping axon >>> d( [<axons>] ) # ping multiple >>> d( bittensor.axon(), bittensor.Synapse )
However, you are able to safely call
dendrite.query()
without a context manager in a synchronous setting.Example without a context manager:
>>> d = dendrite(wallet = bittensor.wallet() ) >>> print(d) >>> d( <axon> ) # ping axon >>> d( [<axons>] ) # ping multiple >>> d( bittensor.axon(), bittensor.Synapse )
Initializes the Dendrite object, setting up essential properties.
- Parameters:
wallet (Optional[Union['bittensor.wallet', 'bittensor.keypair']], optional) – The user’s wallet or keypair used for signing messages. Defaults to
None
, in which case a newbittensor.wallet().hotkey()
is generated and used.
- uuid#
- external_ip#
- keypair#
- property session: aiohttp.ClientSession#
An asynchronous property that provides access to the internal aiohttp client session.
This property ensures the management of HTTP connections in an efficient way. It lazily initializes the aiohttp.ClientSession on its first use. The session is then reused for subsequent HTTP requests, offering performance benefits by reusing underlying connections.
This is used internally by the dendrite when querying axons, and should not be used directly unless absolutely necessary for your application.
- Returns:
The active aiohttp client session instance. If no session exists, a new one is created and returned. This session is used for asynchronous HTTP requests within the dendrite, adhering to the async nature of the network interactions in the Bittensor framework.
- Return type:
aiohttp.ClientSession
Example usage:
import bittensor as bt # Import bittensor wallet = bt.wallet( ... ) # Initialize a wallet dendrite = bt.dendrite( wallet ) # Initialize a dendrite instance with the wallet async with (await dendrite.session).post( # Use the session to make an HTTP POST request url, # URL to send the request to headers={...}, # Headers dict to be sent with the request json={...}, # JSON body data to be sent with the request timeout=10, # Timeout duration in seconds ) as response: json_response = await response.json() # Extract the JSON response from the server
- close_session()[source]#
Closes the internal aiohttp client session synchronously.
This method ensures the proper closure and cleanup of the aiohttp client session, releasing any resources like open connections and internal buffers. It is crucial for preventing resource leakage and should be called when the dendrite instance is no longer in use, especially in synchronous contexts.
Note
This method utilizes asyncio’s event loop to close the session asynchronously from a synchronous context. It is advisable to use this method only when asynchronous context management is not feasible.
- Usage:
When finished with dendrite in a synchronous context
dendrite_instance.close_session()
.
- async aclose_session()[source]#
Asynchronously closes the internal aiohttp client session.
This method is the asynchronous counterpart to the
close_session()
method. It should be used in asynchronous contexts to ensure that the aiohttp client session is closed properly. The method releases resources associated with the session, such as open connections and internal buffers, which is essential for resource management in asynchronous applications.- Usage:
When finished with dendrite in an asynchronous context await
dendrite_instance.aclose_session()
.
Example:
async with dendrite_instance: # Operations using dendrite pass # The session will be closed automatically after the above block
- _get_endpoint_url(target_axon, request_name)[source]#
Constructs the endpoint URL for a network request to a target axon.
This internal method generates the full HTTP URL for sending a request to the specified axon. The URL includes the IP address and port of the target axon, along with the specific request name. It differentiates between requests to the local system (using ‘0.0.0.0’) and external systems.
- Parameters:
target_axon – The target axon object containing IP and port information.
request_name – The specific name of the request being made.
- Returns:
A string representing the complete HTTP URL for the request.
- Return type:
- log_exception(exception)[source]#
Logs an exception with a unique identifier.
This method generates a unique UUID for the error, extracts the error type, and logs the error message using Bittensor’s logging system.
- Parameters:
exception (Exception) – The exception object to be logged.
- Returns:
None
- process_error_message(synapse, request_name, exception)[source]#
Handles exceptions that occur during network requests, updating the synapse with appropriate status codes and messages.
This method interprets different types of exceptions and sets the corresponding status code and message in the synapse object. It covers common network errors such as connection issues and timeouts.
- Parameters:
synapse (Union[bittensor.Synapse, bittensor.StreamingSynapse]) – The synapse object associated with the request.
request_name (str) – The name of the request during which the exception occurred.
exception (Exception) – The exception object caught during the request.
- Returns:
The updated synapse object with the error status code and message.
- Return type:
Note
This method updates the synapse object in-place.
- _log_outgoing_request(synapse)[source]#
Logs information about outgoing requests for debugging purposes.
This internal method logs key details about each outgoing request, including the size of the request, the name of the synapse, the axon’s details, and a success indicator. This information is crucial for monitoring and debugging network activity within the Bittensor network.
To turn on debug messages, set the environment variable BITTENSOR_DEBUG to
1
, or call the bittensor debug method like so:import bittensor bittensor.debug()
- Parameters:
synapse – The synapse object representing the request being sent.
- _log_incoming_response(synapse)[source]#
Logs information about incoming responses for debugging and monitoring.
Similar to
_log_outgoing_request()
, this method logs essential details of the incoming responses, including the size of the response, synapse name, axon details, status code, and status message. This logging is vital for troubleshooting and understanding the network interactions in Bittensor.- Parameters:
synapse – The synapse object representing the received response.
- query(*args, **kwargs)[source]#
Makes a synchronous request to multiple target Axons and returns the server responses.
Cleanup is automatically handled and sessions are closed upon completed requests.
- Parameters:
axons (Union[List[Union['bittensor.AxonInfo', 'bittensor.axon']], Union['bittensor.AxonInfo', 'bittensor.axon']]) – The list of target Axon information.
synapse (bittensor.Synapse, optional) – The Synapse object. Defaults to
bittensor.Synapse()
.timeout (float, optional) – The request timeout duration in seconds. Defaults to
12.0
seconds.
- Returns:
If a single target axon is provided, returns the response from that axon. If multiple target axons are provided, returns a list of responses from all target axons.
- Return type:
Union[bittensor.Synapse, List[bittensor.Synapse]]
- async forward(axons, synapse=bittensor.Synapse(), timeout=12, deserialize=True, run_async=True, streaming=False)[source]#
Asynchronously sends requests to one or multiple Axons and collates their responses.
This function acts as a bridge for sending multiple requests concurrently or sequentially based on the provided parameters. It checks the type of the target Axons, preprocesses the requests, and then sends them off. After getting the responses, it processes and collates them into a unified format.
When querying an Axon that sends a single response, this function returns a Synapse object containing the response data. If multiple Axons are queried, a list of Synapse objects is returned, each containing the response from the corresponding Axon.
For example:
>>> ... >>> wallet = bittensor.wallet() # Initialize a wallet >>> synapse = bittensor.Synapse(...) # Create a synapse object that contains query data >>> dendrte = bittensor.dendrite(wallet = wallet) # Initialize a dendrite instance >>> axons = metagraph.axons # Create a list of axons to query >>> responses = await dendrite(axons, synapse) # Send the query to all axons and await the responses
When querying an Axon that sends back data in chunks using the Dendrite, this function returns an AsyncGenerator that yields each chunk as it is received. The generator can be iterated over to process each chunk individually.
For example:
>>> ... >>> dendrte = bittensor.dendrite(wallet = wallet) >>> async for chunk in dendrite.forward(axons, synapse, timeout, deserialize, run_async, streaming): >>> # Process each chunk here >>> print(chunk)
- Parameters:
axons (Union[List[Union['bittensor.AxonInfo', 'bittensor.axon']], Union['bittensor.AxonInfo', 'bittensor.axon']]) – The target Axons to send requests to. Can be a single Axon or a list of Axons.
synapse (bittensor.Synapse, optional) – The Synapse object encapsulating the data. Defaults to a new
bittensor.Synapse()
instance.timeout (float, optional) – Maximum duration to wait for a response from an Axon in seconds. Defaults to
12.0
.deserialize (bool, optional) – Determines if the received response should be deserialized. Defaults to
True
.run_async (bool, optional) – If
True
, sends requests concurrently. Otherwise, sends requests sequentially. Defaults toTrue
.streaming (bool, optional) – Indicates if the response is expected to be in streaming format. Defaults to
False
.
- Returns:
If a single Axon is targeted, returns its response. If multiple Axons are targeted, returns a list of their responses.
- Return type:
Union[AsyncGenerator, bittensor.Synapse, List[bittensor.Synapse]]
- async call(target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True)[source]#
Asynchronously sends a request to a specified Axon and processes the response.
This function establishes a connection with a specified Axon, sends the encapsulated data through the Synapse object, waits for a response, processes it, and then returns the updated Synapse object.
- Parameters:
target_axon (Union['bittensor.AxonInfo', 'bittensor.axon']) – The target Axon to send the request to.
synapse (bittensor.Synapse, optional) – The Synapse object encapsulating the data. Defaults to a new
bittensor.Synapse()
instance.timeout (float, optional) – Maximum duration to wait for a response from the Axon in seconds. Defaults to
12.0
.deserialize (bool, optional) – Determines if the received response should be deserialized. Defaults to
True
.
- Returns:
The Synapse object, updated with the response data from the Axon.
- Return type:
- async call_stream(target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True)[source]#
Sends a request to a specified Axon and yields streaming responses.
Similar to
call
, but designed for scenarios where the Axon sends back data in multiple chunks or streams. The function yields each chunk as it is received. This is useful for processing large responses piece by piece without waiting for the entire data to be transmitted.- Parameters:
target_axon (Union['bittensor.AxonInfo', 'bittensor.axon']) – The target Axon to send the request to.
synapse (bittensor.Synapse, optional) – The Synapse object encapsulating the data. Defaults to a new
bittensor.Synapse()
instance.timeout (float, optional) – Maximum duration to wait for a response (or a chunk of the response) from the Axon in seconds. Defaults to
12.0
.deserialize (bool, optional) – Determines if each received chunk should be deserialized. Defaults to
True
.
- Yields:
object – Each yielded object contains a chunk of the arbitrary response data from the Axon. bittensor.Synapse: After the AsyncGenerator has been exhausted, yields the final filled Synapse.
- Return type:
AsyncGenerator[Any, Any]
- preprocess_synapse_for_request(target_axon_info, synapse, timeout=12.0)[source]#
Preprocesses the synapse for making a request. This includes building headers for Dendrite and Axon and signing the request.
- Parameters:
target_axon_info (bittensor.AxonInfo) – The target axon information.
synapse (bittensor.Synapse) – The synapse object to be preprocessed.
timeout (float, optional) – The request timeout duration in seconds. Defaults to
12.0
seconds.
- Returns:
The preprocessed synapse.
- Return type:
- process_server_response(server_response, json_response, local_synapse)[source]#
Processes the server response, updates the local synapse state with the server’s state and merges headers set by the server.
- Parameters:
server_response (object) –
The aiohttp response object from the server.
json_response (dict) – The parsed JSON response from the server.
local_synapse (bittensor.Synapse) – The local synapse object to be updated.
- Raises:
None – But errors in attribute setting are silently ignored.
- __str__()[source]#
Returns a string representation of the Dendrite object.
- Returns:
The string representation of the Dendrite object in the format
dendrite(<user_wallet_address>)()
.- Return type:
- __repr__()[source]#
Returns a string representation of the Dendrite object, acting as a fallback for
__str__()
.- Returns:
The string representation of the Dendrite object in the format
dendrite(<user_wallet_address>)()
.- Return type:
- async __aenter__()[source]#
Asynchronous context manager entry method.
Enables the use of the
async with
statement with the Dendrite instance. When entering the context, the current instance of the class is returned, making it accessible within the asynchronous context.- Returns:
The current instance of the Dendrite class.
- Return type:
Dendrite
Usage:
async with Dendrite() as dendrite: await dendrite.some_async_method()
- async __aexit__(exc_type, exc_value, traceback)[source]#
Asynchronous context manager exit method.
Ensures proper cleanup when exiting the
async with
context. This method will close the aiohttp client session asynchronously, releasing any tied resources.- Parameters:
exc_type (Type[BaseException], optional) – The type of exception that was raised.
exc_value (BaseException, optional) – The instance of exception that was raised.
traceback (TracebackType, optional) – A traceback object encapsulating the call stack at the point where the exception was raised.
Usage:
async with bt.dendrite( wallet ) as dendrite: await dendrite.some_async_method()
Note
This automatically closes the session by calling
__aexit__()
after the context closes.
- __del__()[source]#
Dendrite destructor.
This method is invoked when the Dendrite instance is about to be destroyed. The destructor ensures that the aiohttp client session is closed before the instance is fully destroyed, releasing any remaining resources.
Note
Relying on the destructor for cleanup can be unpredictable. It is recommended to explicitly close sessions using the provided methods or the
async with
context manager.Usage:
dendrite = Dendrite() # ... some operations ... del dendrite # This will implicitly invoke the __del__ method and close the session.
- class bittensor.dendrite.dendrite(wallet=None)[source]#
Bases:
DendriteMixin
,BaseModel
The Dendrite class represents the abstracted implementation of a network client module.
In the brain analogy, dendrites receive signals from other neurons (in this case, network servers or axons), and the Dendrite class here is designed to send requests to those endpoint to recieve inputs.
This class includes a wallet or keypair used for signing messages, and methods for making HTTP requests to the network servers. It also provides functionalities such as logging network requests and processing server responses.
- Parameters:
- __repr__()#
Returns a string representation of the Dendrite object, acting as a fallback for __str__().
- Return type:
- query(self, *args, **kwargs) bittensor.Synapse | List[bittensor.Synapse] #
Makes synchronous requests to one or multiple target Axons and returns responses.
- Return type:
List[Union[AsyncGenerator[Any, Any], bittensor.Synapse, bittensor.StreamingSynapse]]
- forward(self, axons, synapse=bittensor.Synapse(), timeout=12, deserialize=True, run_async=True, streaming=False) bittensor.Synapse #
Asynchronously sends requests to one or multiple Axons and collates their responses.
- Parameters:
axons (Union[List[Union[bittensor.AxonInfo, bittensor.axon]], Union[bittensor.AxonInfo, bittensor.axon]])
synapse (bittensor.Synapse)
timeout (float)
deserialize (bool)
run_async (bool)
streaming (bool)
- Return type:
List[Union[AsyncGenerator[Any, Any], bittensor.Synapse, bittensor.StreamingSynapse]]
- call(self, target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True) bittensor.Synapse #
Asynchronously sends a request to a specified Axon and processes the response.
- Parameters:
target_axon (Union[bittensor.AxonInfo, bittensor.axon])
synapse (bittensor.Synapse)
timeout (float)
deserialize (bool)
- Return type:
- call_stream(self, target_axon, synapse=bittensor.Synapse(), timeout=12.0, deserialize=True) AsyncGenerator[bittensor.Synapse, None] #
Sends a request to a specified Axon and yields an AsyncGenerator that contains streaming response chunks before finally yielding the filled Synapse as the final element.
- Parameters:
target_axon (Union[bittensor.AxonInfo, bittensor.axon])
synapse (bittensor.StreamingSynapse)
timeout (float)
deserialize (bool)
- Return type:
AsyncGenerator[Any, Any]
- preprocess_synapse_for_request(self, target_axon_info, synapse, timeout=12.0) bittensor.Synapse #
Preprocesses the synapse for making a request, including building headers and signing.
- Parameters:
target_axon_info (bittensor.AxonInfo)
synapse (bittensor.Synapse)
timeout (float)
- Return type:
- process_server_response(self, server_response, json_response, local_synapse)#
Processes the server response, updates the local synapse state, and merges headers.
- Parameters:
server_response (aiohttp.ClientResponse)
json_response (dict)
local_synapse (bittensor.Synapse)
- close_session(self)#
Synchronously closes the internal aiohttp client session.
- aclose_session(self)#
Asynchronously closes the internal aiohttp client session.
Note
When working with async aiohttp client sessions, it is recommended to use a context manager.
Example with a context manager:
>>> aysnc with dendrite(wallet = bittensor.wallet()) as d: >>> print(d) >>> d( <axon> ) # ping axon >>> d( [<axons>] ) # ping multiple >>> d( bittensor.axon(), bittensor.Synapse )
However, you are able to safely call
dendrite.query()
without a context manager in a synchronous setting.Example without a context manager:
>>> d = dendrite(wallet = bittensor.wallet() ) >>> print(d) >>> d( <axon> ) # ping axon >>> d( [<axons>] ) # ping multiple >>> d( bittensor.axon(), bittensor.Synapse )
Initializes the Dendrite object, setting up essential properties.
- Parameters:
wallet (Optional[Union['bittensor.wallet', 'bittensor.keypair']], optional) – The user’s wallet or keypair used for signing messages. Defaults to
None
, in which case a newbittensor.wallet().hotkey()
is generated and used.