Skip to content

Commit

Permalink
adding sonet support
Browse files Browse the repository at this point in the history
  • Loading branch information
PsicoThePato committed Jun 27, 2024
1 parent 25e8a2b commit 60095e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/synthia/miner/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Config:

class OpenrouterSettings(BaseSettings):
api_key: str
model: str = "anthropic/claude-3-opus"
model: str = "anthropic/claude-3.5-sonnet"
max_tokens: int = 3000
temperature: float = 0.5

Expand Down
32 changes: 16 additions & 16 deletions src/synthia/miner/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def _treat_response(self, message: Any):
or message_dict["stop_reason"] != "end_turn"
):
return (
None,
None,
f"Could not generate an answer. Stop reason {message_dict['stop_reason']}"
)
)

blocks = message_dict["content"]
answer = "".join([block["text"] for block in blocks])
Expand All @@ -73,37 +73,37 @@ def model(self) -> str:


class OpenrouterModule(BaseLLM):

module_map: dict[str, str] = {
"claude-3-opus-20240229": "anthropic/claude-3-opus",
"anthropic/claude-3-opus": "anthropic/claude-3-opus",
"anthropic/claude-3.5-sonnet": "anthropic/claude-3.5-sonnet",
}

def __init__(self, settings: OpenrouterSettings | None = None) -> None:
super().__init__()
self.settings = settings or OpenrouterSettings() # type: ignore
self.settings = settings or OpenrouterSettings() # type: ignore
self._max_tokens = self.settings.max_tokens
if self.settings.model not in self.module_map:
raise ValueError(
f"Model {self.settings.model} not supported on Openrouter"
)

)

@property
def max_tokens(self) -> int:
return self._max_tokens

@property
def model(self) -> str:
model_name = self.module_name_mapping(self.settings.model)
return model_name
def module_name_mapping(self, model_name: str) -> str:

def module_name_mapping(self, model_name: str) -> str:
return self.module_map[model_name]


def prompt(self, user_prompt: str, system_prompt: str | None = None):
context_prompt = system_prompt or self.get_context_prompt(self.max_tokens)
context_prompt = system_prompt or self.get_context_prompt(
self.max_tokens)
model = self.model
prompt = {
"model": model,
Expand All @@ -114,11 +114,11 @@ def prompt(self, user_prompt: str, system_prompt: str | None = None):
}
key = self.settings.api_key
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {key}",
},
data=json.dumps(prompt)
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {key}",
},
data=json.dumps(prompt)
)

json_response: dict[Any, Any] = response.json()
Expand Down
29 changes: 16 additions & 13 deletions src/synthia/miner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
from communex.module._rate_limiters.limiters import StakeLimiterParams # type: ignore
import uvicorn

from synthia.miner.anthropic import OpenrouterModule, AnthropicModule
Expand All @@ -26,19 +26,20 @@ def stake_to_ratio(stake: int, multiplier: int = 1) -> float:
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
# 20 * 10 ** -1 request per 4000 * 10 ** -1 second
case _ if stake < to_nano(500_000):
return base_ratio * multiplier
case _:
return mult_2(base_ratio) * multiplier # 30 * 10 ** -1 requests per 4000 * 10 ** -1 second

# 30 * 10 ** -1 requests per 4000 * 10 ** -1 second
return mult_2(base_ratio) * multiplier


def provider_callback(value: str):
Expand All @@ -50,41 +51,43 @@ def provider_callback(value: str):
)
return value


@app.command('serve-miner')
def serve(
commune_key: Annotated[
str,
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
keypair = classic_load_key(commune_key) # type: ignore
match provider_enumerated:
case ClaudeProviders.ANTHROPIC:
module = AnthropicModule()
case ClaudeProviders.OPENROUTER:
module = OpenrouterModule()

stake_limiter = StakeLimiterParams(
epoch=800,
epoch=800,
cache_age=600,
get_refill_per_epoch=stake_to_ratio,
)
server = ModuleServer(
module, keypair, subnets_whitelist=[3], limiter = stake_limiter
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)
typer.run(serve)
8 changes: 4 additions & 4 deletions src/synthia/validator/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@


class ValidatorSettings(BaseSettings):
api_key: str # anthropic api key
api_key: str # anthropic api key
# == Text generation ==
model: str = "claude-3-opus-20240229"
model: str = "anthropic/claude-3.5-sonnet"
temperature: float = 0.2
max_tokens: int = 1000

# == Scoring ==
# sleep time between each iteration
# (we are aiming at 50 block subnet tempo, with 8 second block time)
iteration_interval: int = 1920
max_allowed_weights: int = 420 # this is a global parameter of the maximum weights that a validator can set
#  this is a global parameter of the maximum weights that a validator can set
max_allowed_weights: int = 420
hf_uploader_ss58: str = "5EX6ixabe8fiWHySw4SYaJAkaHLKeqSJ3rv7so2FrLC2cfGV"

class Config:
env_prefix = "ANTHROPIC_"
env_file = "env/config.env"
extra = "ignore"

7 changes: 4 additions & 3 deletions src/synthia/validator/text_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def set_weights(
client = CommuneClient(get_node_url())
client.vote(key=key, uids=uids, weights=weights, netuid=netuid)


def cut_to_max_allowed_weights(
score_dict: dict[int, float], settings: ValidatorSettings | None = None
) -> dict[int, float]:
Expand Down Expand Up @@ -211,7 +212,7 @@ def __init__(
if not embedder:
embedder = OpenAIEmbedder(OpenAISettings()) # type: ignore
self.embedder = embedder
self.val_model = "claude-3-opus-20240229"
self.val_model = "anthropic/claude-3.5-sonnet"
self.upload_client = ModuleClient("5.161.229.89", 80, self.key)
self.call_timeout = call_timeout
self.provider = provider
Expand Down Expand Up @@ -403,8 +404,8 @@ async def validate_step(
miner_answers = [
answer for answer in miner_answers if (
not isinstance(answer, BaseException)
)
]
)
]

for uid, miner_response in zip(modules_info.keys(), miner_answers):
miner_answer, val_info = miner_response
Expand Down

0 comments on commit 60095e2

Please sign in to comment.