Spaces:
Sleeping
Sleeping
Remove old docs and update README
Browse files
README.md
CHANGED
|
@@ -1,32 +1,125 @@
|
|
| 1 |
-
# chattensor-backend
|
| 2 |
-
Backend for Chattensor app
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
## Install
|
| 10 |
-
Create a new python environment and install the dependencies with the command.
|
| 11 |
|
| 12 |
(First time only)
|
| 13 |
```bash
|
|
|
|
|
|
|
| 14 |
pip install -r requirements.txt
|
| 15 |
```
|
| 16 |
|
|
|
|
|
|
|
| 17 |
> Note: Currently the prompting library is only installable on machines with cuda devices (NVIDIA-GPU).
|
| 18 |
|
| 19 |
## Run
|
| 20 |
|
| 21 |
-
First activate the virtual environment and then run the following command to start the server
|
| 22 |
|
| 23 |
```bash
|
| 24 |
source env/bin/activate
|
| 25 |
```
|
| 26 |
|
| 27 |
-
Run
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
```bash
|
| 30 |
-
|
| 31 |
```
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
<picture>
|
| 3 |
+
<source srcset="./assets/macrocosmos-white.png" media="(prefers-color-scheme: dark)">
|
| 4 |
+
<img src="macrocosmos-white.png">
|
| 5 |
+
</picture>
|
| 6 |
|
| 7 |
+
<picture>
|
| 8 |
+
<source srcset="./assets/macrocosmos-black.png" media="(prefers-color-scheme: light)">
|
| 9 |
+
<img src="macrocosmos-black.png">
|
| 10 |
+
</picture>
|
| 11 |
|
| 12 |
|
| 13 |
+
<br/>
|
| 14 |
+
<br/>
|
| 15 |
+
<br/>
|
| 16 |
+
|
| 17 |
+
# Subnet 1 API
|
| 18 |
+
> Note: This project is still in development and is not yet ready for production use.
|
| 19 |
+
|
| 20 |
+
The official REST API for Bittensor's flagship subnet 1 ([prompting](https://github.com/opentensor/prompting)), built by [Macrocosmos](https://macrocosmos.ai).
|
| 21 |
+
|
| 22 |
+
Subnet 1 is an decentralized open source network containing around 1000 highly capable LLM agents. These agents are capable of performing a wide range of tasks, from simple math problems to complex natural language processing tasks. As subnet 1 is constantly evolving, its capabilities are always expanding. Our goal is to provide a world-class inference engine, to be used by developers and researchers alike.
|
| 23 |
+
|
| 24 |
+
This API is designed to power applications and facilitate the interaction between subnets by providing a simple and easy-to-use interface for developers which enables:
|
| 25 |
+
1. **Conversation**: Chatting with the network (streaming and non-streaming)
|
| 26 |
+
2. **Data cleaning**: Filtering empty and otherwise useless responses
|
| 27 |
+
3. **Advanced inference**: Providing enhanced responses using SOTA ensembling techniques (WIP)
|
| 28 |
+
|
| 29 |
+
Validators can use this API to interact with the network and perform various tasks.
|
| 30 |
+
To run an API server, you will need a bittensor wallet which is registered as a validator the relevant subnet (1@mainnet or 61@testnet).
|
| 31 |
+
|
| 32 |
+
NOTE: At present, miners are choosing not to stream their responses to the network. This means that the server will not be able to provide a streamed response to the client until the miner has finished processing the request. This is a temporary measure and will be resolved in the future.
|
| 33 |
+
|
| 34 |
+
## How it works
|
| 35 |
+
The API server is a RESTful API that provides endpoints for interacting with the network. It is a simple [wrapper](./validators/sn1_validator_wrapper.py) around your subnet 1 validator, which makes use of the dendrite to make queries.
|
| 36 |
|
| 37 |
## Install
|
| 38 |
+
Create a new python environment and install the dependencies with the command.
|
| 39 |
|
| 40 |
(First time only)
|
| 41 |
```bash
|
| 42 |
+
python3.10 -m venv env
|
| 43 |
+
source env/bin/activate
|
| 44 |
pip install -r requirements.txt
|
| 45 |
```
|
| 46 |
|
| 47 |
+
> Note: This project requires python >=3.10.
|
| 48 |
+
|
| 49 |
> Note: Currently the prompting library is only installable on machines with cuda devices (NVIDIA-GPU).
|
| 50 |
|
| 51 |
## Run
|
| 52 |
|
| 53 |
+
First activate the virtual environment and then run the following command to start the server:
|
| 54 |
|
| 55 |
```bash
|
| 56 |
source env/bin/activate
|
| 57 |
```
|
| 58 |
|
| 59 |
+
Run an API server on subnet 1 with the following command:
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
EXPECTED_ACCESS_KEY=<ACCESS_KEY> python server.py --wallet.name <WALLET_NAME> --wallet.hotkey <WALLET_HOTKEY> --netuid <NETUID> --neuron.model_id mock --neuron.tasks math --neuron.task_p 1 --neuron.device cpu
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
The command ensures that no GPU memory is used by the server, and that the large models used by the incentive mechanism are not loaded.
|
| 66 |
+
|
| 67 |
+
> Note: This command is subject to change as the project evolves.
|
| 68 |
+
|
| 69 |
+
We recommend that you run the server using a process manager like PM2. This will ensure that the server is always running and will restart if it crashes.
|
| 70 |
+
|
| 71 |
+
```bash
|
| 72 |
+
EXPECTED_ACCESS_KEY=<ACCESS_KEY> pm2 start server.py --interpreter python3 --name sn1-api -- --wallet.name <WALLET_NAME> --wallet.hotkey <WALLET_HOTKEY> --netuid <NETUID> --neuron.model_id mock --neuron.tasks math --neuron.task_p 1 --neuron.device cpu
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## API Usage
|
| 76 |
+
At present, the API provides two endpoints: `/chat` (live) and `/echo` (test).
|
| 77 |
+
|
| 78 |
+
`/chat` is used to chat with the network and receive a response. The endpoint requires a JSON payload with the following fields:
|
| 79 |
+
- `k: int`: The number of responses to return
|
| 80 |
+
- `timeout: float`: The time in seconds to wait for a response
|
| 81 |
+
- `roles: List[str]`: The roles of the agents to query
|
| 82 |
+
- `messages: List[str]`: The messages to send to the network
|
| 83 |
+
- `prefer: str`: The preferred response to use as the default view. Should be one of `{'longest', 'shortest'}`
|
| 84 |
+
|
| 85 |
+
Responses from the `/chat` endpoint are streamed back to the client as they are received from the network. Upon completion, the server will return a JSON response with the following fields:
|
| 86 |
+
- `streamed_chunks: List[str]`: The streamed responses from the network
|
| 87 |
+
- `streamed_chunks_timings: List[float]`: The time taken to receive each streamed response
|
| 88 |
+
- `synapse: StreamPromptingSynapse`: The synapse used to query the network. This contains full context and metadata about the query.
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
## Testing
|
| 92 |
+
|
| 93 |
+
To test the API locally, you can use the following curl command:
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
curl --no-buffer -X POST http://0.0.0.0:10000/chat/ -H "api_key: <ACCESS_KEY>" -d '{"k": 5, "timeout": 15, "roles": ["user"], "messages": ["What is today's date?"]}'
|
| 97 |
+
"""
|
| 98 |
+
```
|
| 99 |
+
> Note: Use the `--no-buffer` flag to ensure that the response is streamed back to the client.
|
| 100 |
+
|
| 101 |
+
After verifying that the server is responding to requests locally, you can test the server on a remote machine.
|
| 102 |
+
|
| 103 |
+
### Troubleshooting
|
| 104 |
+
|
| 105 |
+
If you do not receive a response from the server, check that the server is running and that the port is open on the server. You can open the port using the following commands:
|
| 106 |
|
| 107 |
```bash
|
| 108 |
+
sudo ufw allow 10000/tcp
|
| 109 |
```
|
| 110 |
+
|
| 111 |
+
---
|
| 112 |
+
|
| 113 |
+
## Contributing
|
| 114 |
+
If you would like to contribute to the project, please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.
|
| 115 |
+
|
| 116 |
+
You can find out more about the project by visiting the [Macrocosmos website](https://macrocosmos.ai) or by joining us in our social channels:
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+

|
| 120 |
+
[](https://substack.com/@macrocosmosai)
|
| 121 |
+
[](https://twitter.com/MacrocosmosAI)
|
| 122 |
+
[](https://twitter.com/MacrocosmosAI)
|
| 123 |
+
[](www.linkedin.com/in/MacrocosmosAI)
|
| 124 |
+
[](https://opensource.org/licenses/MIT)
|
| 125 |
+
|
server.py
CHANGED
|
@@ -5,50 +5,6 @@ from aiohttp import web
|
|
| 5 |
from validators import S1ValidatorAPI, QueryValidatorParams, ValidatorAPI
|
| 6 |
from middlewares import api_key_middleware, json_parsing_middleware
|
| 7 |
|
| 8 |
-
"""
|
| 9 |
-
# Test chat endpoint with curl
|
| 10 |
-
```
|
| 11 |
-
//Local:
|
| 12 |
-
curl --no-buffer -X POST http://0.0.0.0:10000/chat/ -H "api_key: hey-michal" -d '{"k": 5, "timeout": 15, "roles": ["user"], "messages": ["on what exact date did the 21st century begin??"]}'
|
| 13 |
-
|
| 14 |
-
//Global:
|
| 15 |
-
curl --no-buffer -X POST http://129.146.127.82:10000/chat/ -H "api_key: hey-michal" -d '{"k": 5, "timeout": 15, "roles": ["user"], "messages": ["on what exact date did the 21st century begin??"]}'
|
| 16 |
-
curl --no-buffer -X POST http://129.146.127.82:10000/chat/ -H "api_key: hey-michal" -d '{"k": 5, "timeout": 15, "roles": ["user"], "messages": ["What is the meaning of life??"]}'
|
| 17 |
-
|
| 18 |
-
# echo stream test endpoint
|
| 19 |
-
//Local
|
| 20 |
-
curl --no-buffer -X POST http://0.0.0.0:10000/echo/ -H "api_key: hey-michal" -d '{"k": 3, "timeout": 0.2, "roles": ["user"], "messages": ["i need to tell you something important but first"]}'
|
| 21 |
-
|
| 22 |
-
//Global
|
| 23 |
-
curl --no-buffer -X POST http://129.146.127.82:10000/echo/ -H "api_key: hey-michal" -d '{"k": 3, "timeout": 0.2, "roles": ["user"], "messages": ["i need to tell you something important but first"]}'
|
| 24 |
-
```
|
| 25 |
-
|
| 26 |
-
TROUBLESHOOT
|
| 27 |
-
check if port is open
|
| 28 |
-
```
|
| 29 |
-
sudo ufw allow 10000/tcp
|
| 30 |
-
sudo ufw allow 10000/tcp
|
| 31 |
-
```
|
| 32 |
-
|
| 33 |
-
---
|
| 34 |
-
|
| 35 |
-
# Run Chattensor
|
| 36 |
-
|
| 37 |
-
## With vanilla python:
|
| 38 |
-
python server.py --neuron.model_id mock --wallet.name sn1 --wallet.hotkey v1 --netuid 1 --neuron.tasks math --neuron.task_p 1 --neuron.device cpu --subtensor.network local
|
| 39 |
-
|
| 40 |
-
## With PM2:
|
| 41 |
-
```
|
| 42 |
-
EXPECTED_ACCESS_KEY="hey-michal" pm2 start app.py --interpreter python3 --name app -- --neuron.model_id mock --wallet.name sn1 --wallet.hotkey v1 --netuid 1 --neuron.tasks math --neuron.task_p 1 --neuron.device cpu
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
basic testing
|
| 46 |
-
```
|
| 47 |
-
EXPECTED_ACCESS_KEY="hey-michal" python app.py --neuron.model_id mock --wallet.name sn1 --wallet.hotkey v1 --netuid 1 --neuron.tasks math --neuron.task_p 1 --neuron.device cpu
|
| 48 |
-
```
|
| 49 |
-
add --mock to test the echo stream
|
| 50 |
-
"""
|
| 51 |
-
|
| 52 |
|
| 53 |
async def chat(request: web.Request) -> web.StreamResponse:
|
| 54 |
"""
|
|
|
|
| 5 |
from validators import S1ValidatorAPI, QueryValidatorParams, ValidatorAPI
|
| 6 |
from middlewares import api_key_middleware, json_parsing_middleware
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
async def chat(request: web.Request) -> web.StreamResponse:
|
| 10 |
"""
|