Skip to content

Commit

Permalink
Merge pull request #85 from Datura-ai/Update-Bittensor
Browse files Browse the repository at this point in the history
add capacities
  • Loading branch information
surcyf123 authored Oct 2, 2024
2 parents 23f15a1 + cb3ffb6 commit 6288f3e
Show file tree
Hide file tree
Showing 29 changed files with 1,060 additions and 480 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ wandb/
validators/.ipynb_checkpoints/
**/validator.ipynb
**/.env
**/Cortex.t.egg-info
**/Cortex.t.egg-info
**/test.ipynb
.env
**/server/**/*.py
cache.db
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ Before starting make sure update your system and have pm2 installed to run the s

```bash
apt update -y && apt-get install git -y && apt install python3-pip -y

```

Download the repository, navigate to the folder and then create virtual env and install the necessary requirements with the following chained command.
Expand All @@ -217,9 +218,11 @@ After installing it, copy `env.example` to `.env` and substitute
all env vars with values appropriate for your accounts.

## Mining

# step1.
go to cortext/constants.py and change bandwidth_to_model value as per limit of api.
currently we support only 3 models: "gpt-4o", "claude-3-5-sonnet-20240620", "llama-3.1-70b-versatile".
so don't add more than that.
You can launch your miners via python3 using the following command.

```bash
bash start_miner.sh
```
Expand Down
Binary file added cache.db
Binary file not shown.
7 changes: 4 additions & 3 deletions cortext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


# version must stay on line 22
__version__ = "4.0.10"
__version__ = "4.0.11"
version_split = __version__.split(".")
__spec_version__ = (
(1000 * int(version_split[0]))
Expand Down Expand Up @@ -52,8 +52,6 @@
IMAGE_BLACKLIST_STAKE = 5000
EMBEDDING_BLACKLIST_STAKE = 5000
ISALIVE_BLACKLIST_STAKE = min(PROMPT_BLACKLIST_STAKE, IMAGE_BLACKLIST_STAKE, EMBEDDING_BLACKLIST_STAKE)
MIN_REQUEST_PERIOD = 2
MAX_REQUESTS = 20
# must have the test_key whitelisted to avoid a global blacklist
testnet_key = ["5EhEZN6soubtKJm8RN7ANx9FGZ2JezxBUFxr45cdsHtDp3Uk"]
test_key = ["5DcRHcCwD33YsHfj4PX5j2evWLniR1wSWeNmpf5RXaspQT6t"]
Expand Down Expand Up @@ -3769,3 +3767,6 @@


ALL_SYNAPSE_TYPE = Union[StreamPrompting, Embeddings, ImageResponse, IsAlive]

REDIS_RESULT_STREAM = 'result_stream'
REDIS_RESULT = 'result'
87 changes: 87 additions & 0 deletions cortext/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
TEXT_MODEL = "gpt-4-turbo-2024-04-09"
TEXT_PROVIDER = "OpenAI"
TEXT_MAX_TOKENS = 4096
TEXT_TEMPERATURE = 0.001
TEXT_WEIGHT = 1
TEXT_TOP_P = 0.01
TEXT_TOP_K = 1
VISION_MODELS = ["gpt-4o", "claude-3-opus-20240229", "anthropic.claude-3-sonnet-20240229-v1:0",
"claude-3-5-sonnet-20240620"]
TEXT_VALI_MODELS_WEIGHTS = {
# from https://openai.com/api/pricing/
"OpenAI": {
"gpt-4o": 15.00,
# "gpt-3.5-turbo": 2.00,
# "o1-preview": 60.00,
# "o1-mini": 12.00,
},
# from https://ai.google.dev/pricing
# "Gemini": {
# "gemini-1.5-flash": 0.30,
# "gemini-1.5-pro": 10.50,
# },
#
"Anthropic": {
"claude-3-5-sonnet-20240620": 15.00,
# "claude-3-opus-20240229": 75,
# "claude-3-haiku-20240307": 1.25,
},
# model IDs from https://console.groq.com/docs/tool-use?hss_channel=tw-842860575289819136
# prices not available yet, default to bedrock pricing
# free tier: 30 rpm
"Groq": {
# "gemma2-9b-it": 0.22,
# "llama-3.1-8b-instant": 0.22,
"llama-3.1-70b-versatile": .99,
# "llama-3.1-405b-reasoning": 16,
# "mixtral-8x7b-32768": 0.7,
},
# from https://aws.amazon.com/bedrock/pricing/
# model IDs from https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns
"Bedrock": {
# "mistral.mixtral-8x7b-instruct-v0:1": 0.7,
# "mistral.mistral-large-2402-v1:0": 24,
# "meta.llama3-1-8b-instruct-v1:0": 0.22,
# "meta.llama3-1-70b-instruct-v1:0": 0.99,
# "meta.llama3-1-405b-instruct-v1:0": 16,
}
}

bandwidth_to_model = {
"OpenAI": {
"gpt-4o": 2,
# "gpt-3.5-turbo": 1,
# "o1-preview": 1,
# "o1-mini": 1,
},
# from https://ai.google.dev/pricing
# "Gemini": {
# "gemini-1.5-flash": 1,
# "gemini-1.5-pro": 1,
# },
#
"Anthropic": {
"claude-3-5-sonnet-20240620": 2,
# "claude-3-opus-20240229": 1,
# "claude-3-haiku-20240307": 1,
},
# model IDs from https://console.groq.com/docs/tool-use?hss_channel=tw-842860575289819136
# prices not available yet, default to bedrock pricing
# free tier: 30 rpm
"Groq": {
# "gemma2-9b-it": 1,
# "llama-3.1-8b-instant": 1,
"llama-3.1-70b-versatile": 1,
# "llama-3.1-405b-reasoning": 16,
# "mixtral-8x7b-32768": 1,
},
# from https://aws.amazon.com/bedrock/pricing/
# model IDs from https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns
# "Bedrock": {
# "mistral.mixtral-8x7b-instruct-v0:1": 1,
# "mistral.mistral-large-2402-v1:0": 1,
# "meta.llama3-1-8b-instruct-v1:0": 1,
# "meta.llama3-1-70b-instruct-v1:0": 1,
# "meta.llama3-1-405b-instruct-v1:0": 16,
# }
}
25 changes: 20 additions & 5 deletions cortext/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class IsAlive(bt.Synapse):


class Bandwidth(bt.Synapse):
bandwidth_rpm: Optional[Dict[str, int]] = None
bandwidth_rpm: Optional[Dict[str, dict]] = None


class ImageResponse(bt.Synapse):
""" A class to represent the response for an image-related request. """
Expand Down Expand Up @@ -124,6 +125,15 @@ class ImageResponse(bt.Synapse):
description="A list of fields required for the hash."
)

process_time: int = pydantic.Field(
default=9999,
title="process time",
description="processed time of querying dendrite.",
)
task_id: str = pydantic.Field(
default="9999"
)

def deserialize(self) -> Optional[Dict]:
""" Deserialize the completion data of the image response. """
return self.completion
Expand Down Expand Up @@ -286,6 +296,14 @@ class StreamPrompting(bt.StreamingSynapse):
title="streaming",
description="whether to stream the output",
)
deserialize_flag: bool = pydantic.Field(
default=True
)
task_id: str = pydantic.Field(
default="9999",
title="task_id",
description="task id of the request from this syanpse."
)

async def process_streaming_response(self, response: StreamingResponse) -> AsyncIterator[str]:
if self.completion is None:
Expand All @@ -297,9 +315,6 @@ async def process_streaming_response(self, response: StreamingResponse) -> Async
self.completion += token
yield tokens

def deserialize(self) -> str:
return self.completion

def extract_response_json(self, response: StreamingResponse) -> dict:
headers = {
k.decode("utf-8"): v.decode("utf-8")
Expand Down Expand Up @@ -332,4 +347,4 @@ def extract_info(prefix: str) -> dict[str, str]:
"timeout": self.timeout,
"streaming": self.streaming,
"uid": self.uid,
}
}
14 changes: 8 additions & 6 deletions cortext/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from transformers import logging as hf_logging
hf_logging.set_verbosity_error()

hf_logging.set_verbosity_error()

import re
import io
Expand All @@ -37,10 +37,13 @@
from sklearn.feature_extraction.text import TfidfVectorizer
from transformers import CLIPProcessor, CLIPModel


# ==== TEXT ====

def calculate_text_similarity(text1: str, text2: str):
try:
text1 = str(text1).lower()
text2 = str(text2).lower()
# Initialize the TF-IDF Vectorizer
vectorizer = TfidfVectorizer()

Expand All @@ -50,12 +53,12 @@ def calculate_text_similarity(text1: str, text2: str):
# Calculate the Cosine Similarity
similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]

bt.logging.debug(f"Similarity: {similarity}")
return similarity
except Exception as e:
bt.logging.error(f"Error in calculate_text_similarity: {traceback.format_exc()}")
raise


async def api_score(api_answer: str, response: str, weight: float, temperature: float, provider: str) -> float:
try:
if api_answer is None or response is None:
Expand Down Expand Up @@ -153,6 +156,7 @@ def calculate_image_similarity(image, description, max_length: int = 77):
# Calculate cosine similarity
return torch.cosine_similarity(image_embedding, text_embedding, dim=1).item()


async def dalle_score(uid, url, desired_size, description, weight, similarity_threshold=0.21) -> float:
"""Calculate the image score based on similarity and size asynchronously."""

Expand Down Expand Up @@ -191,11 +195,11 @@ async def dalle_score(uid, url, desired_size, description, weight, similarity_th
return 0



# IMAGES ---- DETERMINISTIC

async def deterministic_score(uid: int, syn, weight: float):
vali_b64s = await utils.call_stability(syn.messages, syn.seed, syn.steps, syn.cfg_scale, syn.width, syn.height, syn.samples, syn.sampler)
vali_b64s = await utils.call_stability(syn.messages, syn.seed, syn.steps, syn.cfg_scale, syn.width, syn.height,
syn.samples, syn.sampler)

for miner_b64, vali_b64 in zip(syn.completion["b64s"], vali_b64s):
if miner_b64[:50] != vali_b64[:50]:
Expand All @@ -206,15 +210,13 @@ async def deterministic_score(uid: int, syn, weight: float):
return weight



# ==== Embeddings =====

async def embeddings_score(openai_answer: list, response: list, weight: float, threshold: float = .95) -> float:
if len(openai_answer) != len(response):
bt.logging.info("The number of embeddings in openai_answer and response do not match.")
return 0


# Calculate similarity for each pair of embeddings
similarities = []
for oa_emb, resp_emb in zip(openai_answer, response):
Expand Down
20 changes: 10 additions & 10 deletions cortext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async def get_item_from_list(items, vision):
async with list_update_lock:
items = state[category][item_type]

bt.logging.debug(f"Queue for {list_type}: {len(items) if items else 0} items")
bt.logging.trace(f"Queue for {list_type}: {len(items) if items else 0} items")

item = await get_item_from_list(items, vision)

Expand All @@ -319,7 +319,7 @@ async def get_item_from_list(items, vision):
items = await get_items(category, item_type, theme)
bt.logging.trace(f"Items generated: {items}")
state[category][item_type] = items
bt.logging.debug(f"Fetched new list for {list_type}, containing {len(items)} items")
bt.logging.trace(f"Fetched new list for {list_type}, containing {len(items)} items")

item = await get_item_from_list(items, vision)

Expand Down Expand Up @@ -457,8 +457,8 @@ def extract_python_list(text: str):

async def call_openai(messages, temperature, model, seed=1234, max_tokens=2048, top_p=1):
for _ in range(2):
bt.logging.debug(
f"Calling Openai. Temperature = {temperature}, Model = {model}, Seed = {seed}, Messages = {messages}"
bt.logging.trace(
f"Calling Openai to get answer. Temperature = {temperature}, Model = {model}, Seed = {seed}, Messages = {messages}"
)
try:
message = messages[0]
Expand Down Expand Up @@ -503,7 +503,7 @@ async def call_openai(messages, temperature, model, seed=1234, max_tokens=2048,


async def call_gemini(messages, temperature, model, max_tokens, top_p, top_k):
bt.logging.debug(f"Calling Gemini. Temperature = {temperature}, Model = {model}, Messages = {messages}")
bt.logging.trace(f"Calling Gemini. Temperature = {temperature}, Model = {model}, Messages = {messages}")
try:
model = genai.GenerativeModel(model)
response = model.generate_content(
Expand Down Expand Up @@ -554,7 +554,7 @@ async def call_gemini(messages, temperature, model, max_tokens, top_p, top_k):

async def call_anthropic_bedrock(prompt, temperature, model, max_tokens=2048, top_p=1, top_k=10000):
try:
bt.logging.debug(
bt.logging.trace(
f"Calling Bedrock via Anthropic. Model = {model}, Prompt = {prompt}, Temperature = {temperature}, Max Tokens = {max_tokens}"
)
completion = await anthropic_bedrock_client.completions.create(
Expand Down Expand Up @@ -610,7 +610,7 @@ async def generate_messages_to_claude(messages):

async def call_anthropic(messages, temperature, model, max_tokens, top_p, top_k):
try:
bt.logging.info(
bt.logging.trace(
f"calling Anthropic for {messages} with temperature: {temperature}, model: {model}, max_tokens: {max_tokens}, top_p: {top_p}, top_k: {top_k}"
)
filtered_messages, system_prompt = await generate_messages_to_claude(messages)
Expand All @@ -633,7 +633,7 @@ async def call_anthropic(messages, temperature, model, max_tokens, top_p, top_k)

async def call_groq(messages, temperature, model, max_tokens, top_p, seed):
try:
bt.logging.info(
bt.logging.trace(
f"calling groq for {messages} with temperature: {temperature}, model: {model}, max_tokens: {max_tokens}, top_p: {top_p}"
)

Expand All @@ -655,7 +655,7 @@ async def call_groq(messages, temperature, model, max_tokens, top_p, seed):

async def call_bedrock(messages, temperature, model, max_tokens, top_p, seed):
try:
bt.logging.info(
bt.logging.trace(
f"calling AWS Bedrock for {messages} with temperature: {temperature}, model: {model}, max_tokens: {max_tokens}, top_p: {top_p}"
)

Expand Down Expand Up @@ -746,7 +746,7 @@ async def extract_message(message):

async def call_stability(prompt, seed, steps, cfg_scale, width, height, samples, sampler):
# bt.logging.info(f"calling stability for {prompt, seed, steps, cfg_scale, width, height, samples, sampler}")
bt.logging.info(f"calling stability for {prompt[:50]}...")
bt.logging.trace(f"calling stability for {prompt[:50]}...")

# Run the synchronous stability_api.generate function in a separate thread
meta = await asyncio.to_thread(
Expand Down
3 changes: 2 additions & 1 deletion miner/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self):

self.BT_SUBTENSOR_NETWORK = 'test' if self.ENV == 'test' else 'finney'
self.WANDB_OFF = False if self.ENV == 'prod' else True
self.LOGGING_TRACE = False if self.ENV == 'prod' else True
# still can use the --logging.debug and --logging.trace to turn on logging
self.LOGGING_TRACE = False # if self.ENV == 'prod' else True
self.BLACKLIST_AMT = 5000 if self.ENV == 'prod' else 0
self.BLOCKS_PER_EPOCH = int(os.getenv('BLOCKS_PER_EPOCH', 100))
self.WAIT_NEXT_BLOCK_TIME = int(os.getenv('WAIT_NEXT_BLOCK_TIME', 1))
Expand Down
Loading

0 comments on commit 6288f3e

Please sign in to comment.