File size: 3,193 Bytes
82b3169
32e1e2e
82b3169
 
ab063cf
32e1e2e
82b3169
 
7ea7f29
82b3169
7ea7f29
82b3169
7ea7f29
 
82b3169
 
 
 
 
 
 
728d41d
7ea7f29
 
 
 
 
 
 
 
 
82b3169
 
 
728d41d
 
 
48f02b6
728d41d
 
82b3169
fdc8fdb
 
7ea7f29
82b3169
 
fdc8fdb
 
1e9def1
ab063cf
fdc8fdb
 
1e9def1
728d41d
82b3169
48f02b6
7ea7f29
 
32e1e2e
ab063cf
82b3169
ab063cf
 
fdc8fdb
 
 
 
 
 
 
a415c67
82b3169
fdc8fdb
a415c67
 
 
fdc8fdb
 
82b3169
 
 
 
ab063cf
 
fdc8fdb
 
 
82b3169
 
 
ab063cf
82b3169
 
 
fdc8fdb
82b3169
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import asyncio
import utils
import bittensor as bt
from aiohttp import web
from validators import S1ValidatorAPI, QueryValidatorParams, ValidatorAPI
from middlewares import api_key_middleware, json_parsing_middleware

"""
# Test chat endpoint with curl
```
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??"]}'

# echo stream test endpoint
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"]}'
```

TROUBLESHOOT
check if port is open
```
sudo ufw allow 10000/tcp
sudo ufw allow 10000/tcp
```

---

# Run Chattensor

## With vanilla python:
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

## With PM2:
```
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
```

basic testing
```
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
```
add --mock to test the echo stream
"""


async def chat(request: web.Request) -> web.StreamResponse:
    """
    Chat endpoint for the validator.
    """
    params = QueryValidatorParams.from_request(request)

    # Access the validator from the application context
    validator: ValidatorAPI = request.app["validator"]

    response = await validator.query_validator(params)
    return response


async def echo_stream(request: web.Request) -> web.StreamResponse:    
    return await utils.echo_stream(request)


class ValidatorApplication(web.Application):
    def __init__(self, validator_instance=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self["validator"] = (
            validator_instance if validator_instance else S1ValidatorAPI()
        )

        # Add middlewares to application
        self.add_routes([web.post("/chat/", chat), web.post("/echo/", echo_stream)])
        self.setup_middlewares()
        # TODO: Enable rewarding and other features

    def setup_middlewares(self):
        self.middlewares.append(json_parsing_middleware)
        self.middlewares.append(api_key_middleware)


def main(run_aio_app=True, test=False) -> None:
    loop = asyncio.get_event_loop()
    port = 10000
    if run_aio_app:
        # Instantiate the application with the actual validator
        bt.logging.info("Starting validator application.")
        validator_app = ValidatorApplication()
        bt.logging.success(f"Validator app initialized successfully", validator_app)

        try:
            web.run_app(validator_app, port=port, loop=loop)
        except KeyboardInterrupt:
            print("Keyboard interrupt detected. Exiting validator.")
        finally:
            pass


if __name__ == "__main__":
    main()