Skip to content

Commit

Permalink
moved miner instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
PsicoThePato committed May 20, 2024
1 parent d88ac29 commit 4dcd58d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
19 changes: 0 additions & 19 deletions src/synthia/miner/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import requests
from anthropic import Anthropic
from anthropic._types import NotGiven
from communex.key import generate_keypair # type: ignore
from communex.module.module import Module, endpoint # type: ignore
from keylimiter import TokenBucketLimiter

from ..utils import log # Import the log function from utils
from ._config import ( # Import the AnthropicSettings class from config
AnthropicSettings, OpenrouterSettings)
from .BaseLLM import BaseLLM
Expand Down Expand Up @@ -131,18 +127,3 @@ def prompt(self, user_prompt: str, system_prompt: str | None = None):
if finish_reason != "end_turn":
return None, f"Could not get a complete answer: {finish_reason}"
return answer["message"]["content"], ""


if __name__ == "__main__":
import uvicorn
from communex.module.server import ModuleServer # type: ignore
key = generate_keypair()
log(f"Running module with key {key.ss58_address}")
claude = OpenrouterModule()
refill_rate = 1/400
bucket = TokenBucketLimiter(15, refill_rate)
server = ModuleServer(
claude, key, ip_limiter=bucket, subnets_whitelist=None
)
app = server.get_fastapi_app()
uvicorn.run(app, host="127.0.0.1", port=8000)
86 changes: 86 additions & 0 deletions src/synthia/miner/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from typing import Annotated, Optional
from enum import Enum

import typer
from communex.compat.key import classic_load_key
from communex.module.server import ModuleServer
from communex.balance import to_nano
from communex.module._rate_limiters.limiters import StakeLimiterParams # type: ignore
import uvicorn

from synthia.miner.anthropic import OpenrouterModule, AnthropicModule


class ClaudeProviders(Enum):
ANTHROPIC = "anthropic"
OPENROUTER = "openrouter"


app = typer.Typer()


def stake_to_ratio(stake: int, multiplier: int = 1) -> float:
max_ratio = 4
base_ratio = 13
if multiplier <= 1/max_ratio:
raise ValueError(
f"Given multiplier {multiplier} would set 0 tokens for all stakes"
)

def mult_2(x: int) -> int:
return x * 2

# 10x engineer switch case (btw, this actually optimizes)
match stake:
case _ if stake < to_nano(10_000):
return 0
case _ if stake < to_nano(500_000): # 20 * 10 ** -1 request per 4000 * 10 ** -1 second
return base_ratio * multiplier
case _:
return mult_2(base_ratio) * multiplier # 30 * 10 ** -1 requests per 4000 * 10 ** -1 second



def provider_callback(value: str):
value = value.lower()
allowed_providers = ["anthropic", "openrouter"]
if value not in allowed_providers:
raise typer.BadParameter(
f"Invalid provider. Allowed providers are: {', '.join(allowed_providers)}"
)
return value

@app.command('serve-miner')
def serve(
commune_key: Annotated[
str,
typer.Argument(
help="Name of the key present in `~/.commune/key`"
)
],
provider: Optional[str] = typer.Option(
default="anthropic", callback=provider_callback
),
ip: Optional[str] = None,
port: Optional[int] = None,

):
provider_enumerated = ClaudeProviders(provider)
keypair = classic_load_key(commune_key) # type: ignore
match provider_enumerated:
case ClaudeProviders.ANTHROPIC:
module = AnthropicModule()
case ClaudeProviders.OPENROUTER:
module = OpenrouterModule()

stake_limiter = StakeLimiterParams(get_refill_rate=stake_to_ratio)
server = ModuleServer(
module, keypair, subnets_whitelist=[3], limiter = stake_limiter
)
miner_app = server.get_fastapi_app()
host = ip or "127.0.0.1"
port_ = port or 8000
uvicorn.run(miner_app, host=host, port=port_)

if __name__ == "__main__":
typer.run(serve)

0 comments on commit 4dcd58d

Please sign in to comment.