generated from opentensor/bittensor-subnet-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge v2.5.2 main into pre-staging (#292)
* initial push on openai miner * deprecates langchain and hf miner, adds openai miner * brings back pm2 autorun command * Prevent multi-turn on translation and sentiment * updates gitignore * bumps bittensor version * adds block to logged wandb data * updates version * Add Unit Tests * Improve multi-turn prompting * Reiterate need for pronouns * Stress importance of pronouns * SN1-12 * drops block from log data * Switch last_update_block to local var instead of metagraph attribute * Update README.md * update logging to use python rather than loguru refs #200 * Reduce timout to 15 seconds * Change event logging * Remove loguru requirement * Increment Version Number * v2.5.2 (#287) * Point to macrocosmos entity * Adjust project name * Increase spec version to 2.5.2 --------- Co-authored-by: bkb2135 <bkb2135@columbia.edu> Co-authored-by: p-ferreira <38992619+p-ferreira@users.noreply.github.com> Co-authored-by: p-ferreira <pe_drojunior@hotmail.com> Co-authored-by: bkb2135 <98138173+bkb2135@users.noreply.github.com> Co-authored-by: Chengzhe <chengzhe8919@gmail.com>
- Loading branch information
1 parent
66af972
commit c3fffcd
Showing
16 changed files
with
325 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,9 @@ cython_debug/ | |
#.idea/ | ||
|
||
testing/ | ||
data/* | ||
plots/* | ||
notebooks/* | ||
core | ||
app.config.js | ||
wandb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
# The MIT License (MIT) | ||
# 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) |
Oops, something went wrong.