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

Merge v2.5.2 main into pre-staging #292

Merged
merged 39 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
19a052a
initial push on openai miner
bkb2135 Jun 6, 2024
037ebfc
deprecates langchain and hf miner, adds openai miner
bkb2135 Jun 6, 2024
0d7a576
brings back pm2 autorun command
p-ferreira Jun 11, 2024
a138f4f
Merge pull request #258 from macrocosm-os/main
p-ferreira Jun 11, 2024
33819af
Prevent multi-turn on translation and sentiment
bkb2135 Jun 11, 2024
a91bbf0
updates gitignore
p-ferreira Jun 12, 2024
306394b
bumps bittensor version
p-ferreira Jun 12, 2024
2685ab9
adds block to logged wandb data
p-ferreira Jun 12, 2024
2a3cbd5
updates version
p-ferreira Jun 12, 2024
9b4adff
Merge pull request #262 from macrocosm-os/features/disable-multiturn-…
bkb2135 Jun 12, 2024
231e211
Add Unit Tests
bkb2135 Jun 12, 2024
8d616cc
Improve multi-turn prompting
bkb2135 Jun 13, 2024
3a3fab3
Reiterate need for pronouns
bkb2135 Jun 13, 2024
3576010
Stress importance of pronouns
bkb2135 Jun 13, 2024
5447c11
Merge pull request #266 from macrocosm-os/features/improve-multi-turn-qa
bkb2135 Jun 13, 2024
49da096
SN1-12
bkb2135 Jun 13, 2024
523e00a
drops block from log data
p-ferreira Jun 14, 2024
6ea3919
Merge pull request #259 from macrocosm-os/hotfix/autorun-readme
p-ferreira Jun 14, 2024
54c6f89
Merge pull request #261 from macrocosm-os/features/bittensor-upgrade
p-ferreira Jun 14, 2024
dae21ce
Merge pull request #263 from macrocosm-os/SN1-5-Push-PR-to-reduce-tas…
p-ferreira Jun 14, 2024
d1bac1c
Merge pull request #264 from macrocosm-os/features/improve-multi-turn-qa
p-ferreira Jun 14, 2024
52207ea
Merge pull request #267 from macrocosm-os/SN1-12-Multi-turn-Task-Inte…
p-ferreira Jun 14, 2024
85ba572
Merge pull request #256 from macrocosm-os/features/miner-template
p-ferreira Jun 14, 2024
629e014
Switch last_update_block to local var instead of metagraph attribute
bkb2135 Jun 17, 2024
7114080
Update README.md
bkb2135 Jun 18, 2024
a4b6364
Merge pull request #272 from macrocosm-os/hotfix/update-readme
p-ferreira Jun 18, 2024
cc981f0
Merge pull request #271 from macrocosm-os/hotfix/reduce-miner-resync
bkb2135 Jun 18, 2024
8ad5953
Merge pull request #269 from macrocosm-os/staging
p-ferreira Jun 18, 2024
4813601
update logging to use python rather than loguru
minopeef Jun 24, 2024
39c487c
Reduce timout to 15 seconds
bkb2135 Jun 24, 2024
a3cafea
Merge pull request #277 from macrocosm-os/hotfix/reduce-timeout-to-15…
bkb2135 Jun 24, 2024
0722eda
Merge pull request #274 from minopeef/issues/200
bkb2135 Jun 24, 2024
3556e97
Change event logging
bkb2135 Jun 24, 2024
03e77dc
Remove loguru requirement
bkb2135 Jun 24, 2024
18ed24e
Increment Version Number
bkb2135 Jun 24, 2024
8f25144
Merge pull request #280 from macrocosm-os/hotfix/fix-logging
bkb2135 Jun 24, 2024
f3340d6
Merge pull request #279 from macrocosm-os/pre-staging
bkb2135 Jun 24, 2024
91e88b7
Merge pull request #281 from macrocosm-os/staging
p-ferreira Jun 25, 2024
00d67a4
v2.5.2 (#287)
bkb2135 Jul 16, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ cython_debug/
#.idea/

testing/
data/*
plots/*
notebooks/*
core
app.config.js
wandb
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ git clone https://github.com/opentensor/prompting.git
cd prompting
bash install.sh
```
If you are running a miner, you will also need to uninstall uvloop.
```bash
pip uninstall uvloop -y
```

</div>

Expand Down Expand Up @@ -70,22 +74,30 @@ python <SCRIPT_PATH>
```

where `SCRIPT_PATH` is either:
1. neurons/miners/huggingface/miner.py
2. neurons/miners/openai/miner.py
3. neurons/validator.py
1. neurons/miners/openai/miner.py
2. neurons/validator.py

For ease of use, you can run the scripts as well with PM2. Installation of PM2 is:
**On Linux**:
```bash
sudo apt update && sudo apt install jq && sudo apt install npm && sudo npm install pm2 -g && pm2 update
```

Example of running a Llama3 miner:
Example of running an Openai miner on Main:

```bash
pm2 start neurons/miners/huggingface/miner.py --interpreter python3 --name llama3_miner -- --netuid 1 --subtensor.network finney --wallet.name my_wallet --wallet.hotkey m1 --neuron.model_id casperhansen/llama-3-70b-instruct-awq --neuron.load_in_4bit True --axon.port 21988 --logging.debug
pm2 start neurons/miners/openai/miner.py --interpreter python --name openai_miner -- --netuid 1 --subtensor.network finney --wallet.name my_wallet --wallet.hotkey my_hotkey --neuron.model_id gpt-3.5-turbo-1106 --axon.port 8091
```

## Running with autoupdate

You can run the validator in auto-update mode by using pm2 along with the `run.sh` bash script. This command will initiate two pm2 processes: one for auto-update monitoring, named **s1_validator_update**, and another for running the validator itself, named **s1_validator_main_process**.
```bash
pm2 start run.sh --name s1_validator_autoupdate -- --wallet.name <your-wallet-name> --wallet.hotkey <your-wallet-hot-key>
```

> Note: this is not an end solution, major releases or changes in requirements will still require you to manually restart the processes. Regularly monitor the health of your validator to ensure optimal performance.

# Testnet
We highly recommend that you run your miners on testnet before deploying on main. This is give you an opportunity to debug your systems, and ensure that you will not lose valuable immunity time. The SN1 testnet is **netuid 61**.

Expand All @@ -94,7 +106,7 @@ In order to run on testnet, you will need to go through the same hotkey registra
To run:

```bash
pm2 start neurons/miners/huggingface/miner.py --interpreter python3 --name llama3_miner -- --netuid 61 --subtensor.network test --wallet.name my_test_wallet --wallet.hotkey m1 --neuron.model_id casperhansen/llama-3-70b-instruct-awq --neuron.load_in_4bit True --axon.port 21988 --logging.debug
pm2 start neurons/miners/openai/miner.py --interpreter python3 --name openai_miner -- --netuid 61 --subtensor.network test --wallet.name my_test_wallet --wallet.hotkey my_test_hotkey --neuron.model_id gpt-3.5-turbo-1106 --axon.port 8091
```

# Limitations
Expand Down
9 changes: 7 additions & 2 deletions neurons/miners/huggingface/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import time
import bittensor as bt
from prompting.miners import HuggingFaceMiner
from deprecated import deprecated


# This is the main function, which runs the miner.
if __name__ == "__main__":
@deprecated(version="2.4.1+", reason="Class is deprecated, use openai miner for reference on example miner.")
def main():
with HuggingFaceMiner() as miner:
while True:
miner.log_status()
Expand All @@ -29,3 +30,7 @@
if miner.should_exit:
bt.logging.warning("Ending miner...")
break


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion prompting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# DEALINGS IN THE SOFTWARE.

# Define the version of the template module.
__version__ = "2.4.2"
__version__ = "2.5.2"
version_split = __version__.split(".")
__spec_version__ = (
(10000 * int(version_split[0]))
Expand Down
5 changes: 3 additions & 2 deletions prompting/base/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ def run(self):
self.axon.start()

bt.logging.info(f"Miner starting at block: {self.block}")

last_update_block = 0
# This loop maintains the miner's operations until intentionally stopped.
try:
while not self.should_exit:
while (
self.block - self.metagraph.last_update[self.uid]
self.block - last_update_block
< self.config.neuron.epoch_length
):
# Wait before checking again.
Expand All @@ -121,6 +121,7 @@ def run(self):

# Sync metagraph and potentially set weights.
self.sync()
last_update_block = self.block
self.step += 1

# If someone intentionally stops the miner, it'll safely terminate operations.
Expand Down
41 changes: 26 additions & 15 deletions prompting/base/prompting_miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from prompting.protocol import StreamPromptingSynapse
from prompting.base.miner import BaseStreamMinerNeuron
from datetime import datetime

from typing import List, Dict
dbobrenko marked this conversation as resolved.
Show resolved Hide resolved

class BaseStreamPromptingMiner(BaseStreamMinerNeuron):
"""
Expand Down Expand Up @@ -159,27 +159,38 @@ def init_wandb(self):

def log_event(
self,
synapse: StreamPromptingSynapse,
timing: float,
prompt: str,
completion: str,
system_prompt: str,
messages,
accumulated_chunks: List[str] = [],
accumulated_chunks_timings: List[float] = [],
extra_info: dict = {},
):
if not getattr(self, "wandb_run", None):
self.init_wandb()


dendrite_uid = self.metagraph.hotkeys.index(synapse.dendrite.hotkey)
step_log = {
"epoch_time": timing,
# "block": self.last_epoch_block,
"prompt": prompt,
"completion": completion,
"system_prompt": system_prompt,
"uid": self.metagraph.hotkeys.index(self.wallet.hotkey.ss58_address),
"stake": self.metagraph.S[self.uid].item(),
"trust": self.metagraph.T[self.uid].item(),
"incentive": self.metagraph.I[self.uid].item(),
"consensus": self.metagraph.C[self.uid].item(),
"dividends": self.metagraph.D[self.uid].item(),
# TODO: add block to logs in the future in a way that doesn't impact performance
# "block": self.block,
"messages": messages,
"accumulated_chunks": accumulated_chunks,
"accumulated_chunks_timings": accumulated_chunks_timings,
"validator_uid": dendrite_uid,
"validator_ip": synapse.dendrite.ip,
"validator_coldkey": self.metagraph.coldkeys[dendrite_uid],
"validator_hotkey": self.metagraph.hotkeys[dendrite_uid],
"validator_stake": self.metagraph.S[dendrite_uid].item(),
"validator_trust": self.metagraph.T[dendrite_uid].item(),
"validator_incentive": self.metagraph.I[dendrite_uid].item(),
"validator_consensus": self.metagraph.C[dendrite_uid].item(),
"validator_dividends": self.metagraph.D[dendrite_uid].item(),
"miner_stake": self.metagraph.S[self.uid].item(),
"miner_trust": self.metagraph.T[self.uid].item(),
"miner_incentive": self.metagraph.I[self.uid].item(),
"miner_consensus": self.metagraph.C[self.uid].item(),
"miner_dividends": self.metagraph.D[self.uid].item(),
**extra_info,
}

Expand Down
5 changes: 5 additions & 0 deletions prompting/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
from prompting.utils.uids import get_random_uids
from dataclasses import dataclass

SINGLE_TURN_TASKS = ['sentiment', 'translation']

@async_log
async def generate_reference(agent):
loop = asyncio.get_running_loop()
Expand Down Expand Up @@ -321,6 +323,9 @@ async def forward(self):
if random.random()<0.5 or turn>=1:
break

if task.name in SINGLE_TURN_TASKS:
break

history = '\n'.join([f"{role}: {message}" for role, message in zip(roles, messages)])

# Use PREVIOUS task context
Expand Down
3 changes: 1 addition & 2 deletions prompting/miners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
from .phrase import PhraseMiner

# Real miners
from .hf_miner import HuggingFaceMiner
from .openai_miner import OpenAIMiner
from .openai_miner import OpenAIMiner
3 changes: 2 additions & 1 deletion prompting/miners/hf_miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

# import base miner class which takes care of most of the boilerplate
from prompting.base.prompting_miner import BaseStreamPromptingMiner
from deprecated import deprecated


@deprecated(version="2.4.1+", reason="Class is deprecated, use openai miner for reference on example miner.")
class HuggingFaceMiner(BaseStreamPromptingMiner):
"""
Base miner which runs zephyr (https://huggingface.co/HuggingFaceH4/zephyr-7b-beta)
Expand Down
169 changes: 169 additions & 0 deletions prompting/miners/langchain_miner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# The MIT License (MIT)
dbobrenko marked this conversation as resolved.
Show resolved Hide resolved
# Copyright © 2024 Yuma Rao

# 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 time
import os
import bittensor as bt
import argparse
from starlette.types import Send
from functools import partial
from typing import Dict, Awaitable

# Bittensor Miner Template:
from prompting.base.prompting_miner import BaseStreamPromptingMiner
from prompting.protocol import StreamPromptingSynapse

# import base miner class which takes care of most of the boilerplate

from prompting.miners.utils import OpenAIUtils

from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.chat_models import ChatOpenAI
from dotenv import load_dotenv, find_dotenv
from langchain_core.runnables.base import RunnableSequence
from deprecated import deprecated

@deprecated(version="2.4.1+", reason="Class is deprecated, use openai miner for reference on example miner.")
class LangchainMiner(BaseStreamPromptingMiner, OpenAIUtils):
"""Langchain-based miner which uses OpenAI's API as the LLM.
This miner does not use any tools or external APIs when processing requests - it relies entirely on the models' own representation and world model. In some cases, this can produce lower quality results.
You should also install the dependencies for this miner, which can be found in the requirements.txt file in this directory.
"""

@classmethod
def add_args(cls, parser: argparse.ArgumentParser):
"""
Adds OpenAI-specific arguments to the command line parser.
"""
super().add_args(parser)

def __init__(self, config=None):
super().__init__(config=config)

bt.logging.info(f"Initializing with model {self.config.neuron.model_id}...")

if self.config.wandb.on:
self.identity_tags = ("openai_miner",) + (self.config.neuron.model_id,)

_ = load_dotenv(find_dotenv())
api_key = os.environ.get("OPENAI_API_KEY")

# Set openai key and other args
self.model = ChatOpenAI(
api_key=api_key,
model_name=self.config.neuron.model_id,
max_tokens=self.config.neuron.max_tokens,
temperature=self.config.neuron.temperature,
)

self.system_prompt = self.config.neuron.system_prompt
self.accumulated_total_tokens = 0
self.accumulated_prompt_tokens = 0
self.accumulated_completion_tokens = 0
self.accumulated_total_cost = 0

def forward(self, synapse: StreamPromptingSynapse) -> Awaitable:
async def _forward(
self,
message: str,
init_time: float,
timeout_threshold: float,
chain: RunnableSequence,
chain_formatter: Dict[str, str],
send: Send,
):
buffer = []
temp_completion = "" # for wandb logging
timeout_reached = False

try:
# Langchain built in streaming. 'astream' also available for async
for token in chain.stream(chain_formatter):
buffer.append(token)

if time.time() - init_time > timeout_threshold:
bt.logging.debug(f"⏰ Timeout reached, stopping streaming")
timeout_reached = True
break

if len(buffer) == self.config.neuron.streaming_batch_size:
joined_buffer = "".join(buffer)
temp_completion += joined_buffer
bt.logging.debug(f"Streamed tokens: {joined_buffer}")

await send(
{
"type": "http.response.body",
"body": joined_buffer.encode("utf-8"),
"more_body": True,
}
)
buffer = []

if (
buffer and not timeout_reached
): # Don't send the last buffer of data if timeout.
joined_buffer = "".join(buffer)
await send(
{
"type": "http.response.body",
"body": joined_buffer.encode("utf-8"),
"more_body": False,
}
)

except Exception as e:
bt.logging.error(f"Error in forward: {e}")
if self.config.neuron.stop_on_forward_exception:
self.should_exit = True

finally:
synapse_latency = time.time() - init_time
if self.config.wandb.on:
self.log_event(
timing=synapse_latency,
prompt=message,
completion=temp_completion,
system_prompt=self.system_prompt,
)

bt.logging.debug(f"📧 Message received, forwarding synapse: {synapse}")

prompt = ChatPromptTemplate.from_messages(
[("system", self.system_prompt), ("user", "{input}")]
)
chain = prompt | self.model | StrOutputParser()

role = synapse.roles[-1]
message = synapse.messages[-1]

chain_formatter = {"role": role, "input": message}

init_time = time.time()
timeout_threshold = synapse.timeout

token_streamer = partial(
_forward,
self,
message,
init_time,
timeout_threshold,
chain,
chain_formatter,
)
return synapse.create_streaming_response(token_streamer)
Loading
Loading