Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bittensor shared request layer #1698

Merged
merged 14 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,45 @@ y
⠏ 📡 Setting root weights on test ...
```

## Bittensor Subnets API

This guide provides instructions on how to extend the Bittensor Subnets API, a powerful interface for interacting with the Bittensor network across subnets. The Bittensor Subnets API facilitates querying across any subnet that has exposed API endpoints to unlock utility of the Bittensor decentralized network.

The Bittensor Subnets API consists of abstract classes and a registry system to dynamically handle API interactions. It allows developers to implement custom logic for storing and retrieving data, while also providing a straightforward way for end users to interact with these functionalities.

### Core Components

- **APIRegistry**: A central registry that manages API handlers. It allows for dynamic retrieval of handlers based on keys.
- **SubnetsAPI (Abstract Base Class)**: Defines the structure for API implementations, including methods for querying the network and processing responses.
- **StoreUserAPI & RetrieveUserAPI**: Concrete implementations of the `SubnetsAPI` for storing and retrieving user data.

### Implementing Custom Subnet APIs

To implement your own subclasses of `bittensor.SubnetsAPI` to integrate an API into your subnet.

1. **Inherit from `SubnetsAPI`**: Your class should inherit from the `SubnetsAPI` abstract base class.

2. **Implement Required Methods**: Implement the `prepare_synapse` and `process_responses` abstract methods with your custom logic.

That's it! For example:

```python
import bittensor

class CustomSubnetAPI(bittensor.SubnetsAPI):
def __init__(self, wallet: "bittensor.wallet"):
super().__init__(wallet)
# Custom initialization here

def prepare_synapse(self, *args, **kwargs):
# Custom synapse preparation logic
pass

def process_responses(self, responses):
# Custom response processing logic
pass
```

## Release
The release manager should follow the instructions of the [RELEASE_GUIDELINES.md](./RELEASE_GUIDELINES.md) document.

Expand Down
2 changes: 2 additions & 0 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def debug(on: bool = True):
from .mock.subtensor_mock import MockSubtensor as MockSubtensor
from .mock.wallet_mock import MockWallet as MockWallet

from .subnets import SubnetsAPI as SubnetsAPI

configs = [
axon.config(),
subtensor.config(),
Expand Down
78 changes: 78 additions & 0 deletions bittensor/subnets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# The MIT License (MIT)
# Copyright © 2021 Yuma Rao
# Copyright © 2023 Opentensor Foundation
# Copyright © 2023 Opentensor Technologies Inc

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.

# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import bittensor as bt
from abc import ABC, abstractmethod
from typing import Any, List, Union, Optional


class SubnetsAPI(ABC):
def __init__(self, wallet: "bt.wallet"):
self.wallet = wallet
self.dendrite = bt.dendrite(wallet=wallet)

async def __call__(self, *args, **kwargs):
return await self.query_api(*args, **kwargs)

@abstractmethod
def prepare_synapse(self, *args, **kwargs) -> Any:
"""
Prepare the synapse-specific payload.
"""
...

@abstractmethod
def process_responses(self, responses: List[Union["bt.Synapse", Any]]) -> Any:
"""
Process the responses from the network.
"""
...

async def query_api(
self,
axons: Union[bt.axon, List[bt.axon]],
deserialize: Optional[bool] = False,
timeout: Optional[int] = 12,
n: Optional[float] = 0.1,
uid: Optional[int] = None,
**kwargs: Optional[Any],
) -> Any:
"""
Queries the API nodes of a subnet using the given synapse and bespoke query function.

Args:
axons (Union[bt.axon, List[bt.axon]]): The list of axon(s) to query.
deserialize (bool, optional): Whether to deserialize the responses. Defaults to False.
timeout (int, optional): The timeout in seconds for the query. Defaults to 12.
n (float, optional): The fraction of top nodes to consider based on stake. Defaults to 0.1.
uid (int, optional): The specific UID of the API node to query. Defaults to None.
**kwargs: Keyword arguments for the prepare_synapse_fn.

Returns:
Any: The result of the process_responses_fn.
"""
synapse = self.prepare_synapse(**kwargs)
bt.logging.debug(f"Quering valdidator axons with synapse {synapse.name}...")
responses = await self.dendrite(
axons=axons,
synapse=synapse,
deserialize=deserialize,
timeout=timeout,
)
return self.process_responses(responses)