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

feature: Adds an OpenAI based miner #2

Merged
merged 27 commits into from
Feb 27, 2024
Merged

Conversation

ulises-jeremias
Copy link
Member

@ulises-jeremias ulises-jeremias commented Feb 26, 2024

  • Adds an OpenAI based miner

Summary by CodeRabbit

  • New Features
    • Introduced an OpenAI-based Bittensor miner utilizing GPT-3.5-turbo.
    • Added Docker Compose configurations for development and testing environments.
    • Implemented automated scripts for local network setup, miner, and validator operations.
    • Configured Dependabot for automatic dependency updates.
  • Documentation
    • Updated README with detailed installation and customization instructions.
    • Added comprehensive guides for setting up and running OpenAI Bittensor Miners.
  • Refactor
    • Renamed and updated import paths in the prompting module for clarity and consistency.
  • Chores
    • Introduced licensing information and updated copyright years.
    • Updated project setup configurations and environment setup scripts.

Copy link

coderabbitai bot commented Feb 26, 2024

Warning

Rate Limit Exceeded

@ulises-jeremias has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 22 minutes and 58 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 017df59 and f6cb3a6.

Walkthrough

The recent updates significantly enhance the Bittensor network's functionality and user experience. Key changes include improved installation instructions, clearer documentation, and the introduction of an OpenAI-based miner. Additionally, the project now incorporates a more structured approach to licensing, dependency management, and development environment setup. These modifications aim to streamline the setup process, enrich the mining experience with advanced AI capabilities, and ensure a more robust and developer-friendly ecosystem.

Changes

File(s) Summary
README.md Updates installation instructions, restructures sections for clarity, adds content for better understanding.
neurons/__init__.py, neurons/validators/__init__.py Introduces licensing information, imports validator module.
neurons/miners/openai/... Introduces OpenAI Bittensor Miner with dependencies, setup instructions, and integration with GPT-3.5-turbo model.
prompting/..., neurons/validators/validator.py Updates copyright year, renames modules, and adjusts import paths for consistency.
.devcontainer/..., Dockerfile, scripts/... Introduces Docker and development container configurations, automates network and wallet setup scripts.
.github/dependabot.yml Configures Dependabot for automatic dependency updates.
setup.py Updates module information for "bittensor_subnet_prompting".

"In the network's weave, changes unfold,
🐇 CodeRabbit hops, through code so bold.
With AI and miners, the future's bright,
Subnets and validators, in the moonlight.
🌟 Under the stars, new paths we tread,
Together we build, a web widespread."

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 99b5d0f and 819d8e2.
Files selected for processing (17)
  • README.md (4 hunks)
  • neurons/init.py (1 hunks)
  • neurons/miners/openai/miner.py (1 hunks)
  • neurons/miners/openai/requirements.txt (1 hunks)
  • neurons/validators/init.py (1 hunks)
  • neurons/validators/validator.py (2 hunks)
  • prompting/init.py (2 hunks)
  • prompting/base/miner.py (1 hunks)
  • prompting/base/neuron.py (1 hunks)
  • prompting/base/validator.py (2 hunks)
  • prompting/mock.py (1 hunks)
  • prompting/protocol.py (1 hunks)
  • prompting/validator/forward.py (3 hunks)
  • prompting/validator/reward.py (2 hunks)
  • setup.py (2 hunks)
  • tests/test_mock.py (1 hunks)
  • tests/test_template_validator.py (4 hunks)
Files skipped from review due to trivial changes (3)
  • README.md
  • neurons/validators/init.py
  • neurons/validators/validator.py
Additional comments: 28
neurons/__init__.py (1)
  • 19-19: The import statement for the validator module is correctly formatted. Ensure the validator module exists and is correctly named within the same package.
prompting/__init__.py (2)
  • 3-3: The update of the copyright year to 2024 is correctly applied.
  • 20-20: The version increment to "0.1.0" appropriately reflects the addition of new functionality.
prompting/validator/reward.py (2)
  • 3-4: The update of the copyright years to 2024 is correctly applied.
  • 26-26: The terminology change from "dummy request" to "prompting request" in the docstring improves clarity and aligns with the PR's context.
prompting/validator/forward.py (3)
  • 21-23: The import updates are correctly implemented, ensuring that the necessary modules are available for the forward function.
  • 40-50: The integration of the Prompting class enables enhanced interaction with the LLM, aligning with the PR's objectives.
  • 55-55: The use of prompting for synapse functionality correctly replaces the previous Dummy class, aligning with the PR's focus on leveraging OpenAI's API.
tests/test_mock.py (2)
  • 85-85: The modification to check synapse.content instead of synapse.dummy_output correctly aligns with the updated functionality in the codebase.
  • 91-91: Ensuring that synapse.content is checked in tests aligns with the modifications in the codebase, maintaining consistency.
setup.py (2)
  • 65-74: The updates to the project name, description, URLs, author information, and email address correctly reflect the project's current state and objectives.
  • 57-57: Reading the version from prompting/__init__.py centralizes version management, improving maintainability.
prompting/mock.py (2)
  • 91-96: The update to assign s.completion based on s.messages[0].content correctly aligns with the updated functionality in the codebase.
  • 96-96: Setting s.completion to an empty string if a timeout occurs ensures consistency with the updated codebase and the focus on leveraging OpenAI's API.
prompting/protocol.py (2)
  • 24-104: The introduction of the PromptingMixin class provides a structured way to manage chat sessions with an LLM, aligning with the PR's objectives.
  • 107-144: The Prompting class extends the functionality of PromptingMixin within the project's infrastructure, introducing valuable functionality consistent with the PR's focus.
tests/test_template_validator.py (2)
  • 24-31: The references to updated modules under neurons.validators and neurons.miners.openai.miner are correctly implemented, aligning the tests with the updated project structure.
  • 64-76: The integration of testing for the Prompting functionality ensures that the new features work as expected, aligning with the PR's objectives.
prompting/base/miner.py (1)
  • 27-28: The import paths have been correctly updated to reflect the project's new structure. This change is consistent with the restructuring objectives and should be verified across the project to ensure all references are correctly updated.
neurons/miners/openai/miner.py (6)
  • 3-3: The copyright year has been updated to 2024 for nanlabs, which is consistent with the project's ongoing development and contributions.
  • 29-29: The import statement for BaseMinerNeuron from prompting.base.miner correctly reflects the project's restructuring and the new location of the BaseMinerNeuron class.
  • 37-41: The OpenAIMiner class is introduced as a Langchain-based miner utilizing OpenAI's API. This class inherits from BaseMinerNeuron, indicating it builds upon the base functionality provided by the BaseMinerNeuron class.
  • 159-201: The blacklist method provides a mechanism to ignore requests from unrecognized entities. This is a critical security feature. Ensure that the logic for determining whether an entity is recognized aligns with the project's security requirements and that any changes to the metagraph or entity registration processes are accounted for in this method.
  • 203-232: The priority method assigns priority to incoming requests based on the calling entity's stake. This approach aligns with the goal of prioritizing more valuable or higher-priority requests. Ensure that the stake-based priority calculation is consistent with the project's overall strategy for handling requests.
  • 236-244: The main function demonstrates how to run the OpenAIMiner in a continuous loop, logging its operation and handling shutdown gracefully. Ensure that the logging interval and shutdown mechanism are appropriate for the intended deployment environment.
prompting/base/validator.py (3)
  • 3-3: The copyright year has been updated to 2024, which aligns with the PR objectives. Ensure that this update is consistent across all files mentioned in the PR to maintain uniformity.
  • 30-32: The import paths have been updated from the template package to the prompting package. This change is crucial for maintaining the correct module references after the renaming. Verify that all references to the template module across the project have been updated to prompting to ensure consistency and prevent import errors.
Verification successful

The search for any remaining references to the template module across the project returned no results, indicating that all references have indeed been updated to prompting. This confirms the correctness of the changes made in relation to the review comment.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining references to the 'template' module that might have been missed.
rg --type py 'from template' || echo "No references to 'template' module found."

Length of output: 129

* 3-3: The removal of the developer's name placeholder (`Yuma Rao`) and its replacement with `nanlabs` is noted. This change should be reflected in the documentation and any other relevant files to ensure that the copyright information is consistent across the project.

Comment on lines 27 to 35
from typing import List
from traceback import print_exception

from template.base.neuron import BaseNeuron
from template.mock import MockDendrite
from template.utils.config import add_validator_args
from prompting.base.neuron import BaseNeuron
from prompting.mock import MockDendrite
from prompting.utils.config import add_validator_args


class BaseValidatorNeuron(BaseNeuron):
Copy link

@coderabbitai coderabbitai bot Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-132]

Overall, the file validator.py appears to be well-structured and follows Python best practices. However, it's important to ensure that the changes made do not introduce any unintended side effects, especially with the import path changes. Additionally, consider adding or updating unit tests to cover the new functionality and changes introduced in this PR to ensure stability and reliability.

Would you like assistance in creating or updating unit tests for the BaseValidatorNeuron class and its methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai that would be great!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ulises-jeremias, based on the search results, there is an existing test file named tests/test_template_validator.py that references the BaseValidatorNeuron class. This file likely contains unit tests related to the class. Given this information, here are some steps to consider for enhancing the unit tests:

  1. Review Existing Tests: Start by reviewing tests/test_template_validator.py to understand the current test coverage. Identify which methods of BaseValidatorNeuron are being tested and how thoroughly they are covered.

  2. Identify Test Gaps: Compare the existing tests with the BaseValidatorNeuron class methods to identify any methods or scenarios that are not currently tested. This includes new functionality introduced in the PR, such as changes to import paths or any new methods added to the class.

  3. Update and Add Tests: Based on the gaps identified, update existing tests and add new tests to cover the untested methods and scenarios. Ensure that the tests cover both positive cases (expected behavior) and negative cases (handling of errors or unexpected inputs).

  4. Run and Validate Tests: After updating and adding tests, run the entire test suite to ensure that all tests pass and that the new tests adequately cover the expected behaviors.

  5. Continuous Integration: If not already set up, consider integrating these tests into a continuous integration (CI) pipeline to automatically run them on future PRs. This helps ensure that changes do not inadvertently break existing functionality.

If you need further assistance with specific test cases or have any other questions, feel free to ask!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for test files that might contain references to `BaseValidatorNeuron`.
fd --type f --extension py "test" | xargs rg "BaseValidatorNeuron"

Length of output: 233

Comment on lines +26 to +28
from prompting.utils.config import check_config, add_args, config
from prompting.utils.misc import ttl_get_block
from prompting import __spec_version__ as spec_version
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The verification process revealed references to 'template' in prompting/utils/config.py, prompting/__init__.py, and docs/stream_tutorial/config.py. These references may indicate that not all instances were updated as part of the project restructuring. It's recommended to manually verify these cases to determine if they are outdated references that need updating or if they are acceptable within their specific contexts.

Analysis chain

The import paths have been updated from template to prompting, aligning with the project's restructuring. Ensure all references to these modules elsewhere in the project have been similarly updated for consistency.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all references to the old `template` module have been updated to `prompting`.
rg --type py 'template' || echo "All references to 'template' have been updated."

Length of output: 313

@@ -0,0 +1,3 @@
openai==1.9.0
langchain==0.1.0
python-dotenv
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider specifying a version for python-dotenv to ensure compatibility and predictability of the environment across different installations.

@ulises-jeremias ulises-jeremias changed the title feature: Adds a Langchain-based miner which uses OpenAI's API as the LLM feature: Adds an OpenAI based miner Feb 26, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 819d8e2 and 3799d7b.
Files selected for processing (3)
  • neurons/miners/openai/README.md (1 hunks)
  • neurons/miners/openai/miner.py (1 hunks)
  • neurons/miners/openai/requirements.txt (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • neurons/miners/openai/miner.py
  • neurons/miners/openai/requirements.txt
Additional comments: 4
neurons/miners/openai/README.md (4)
  • 3-3: The introduction clearly explains the purpose and functionality of the Bittensor Miner, including its use of OpenAI's GPT-3.5-turbo model. This provides a good overview for users or developers new to the project.
  • 7-8: The prerequisites section is concise and informative, listing the minimum Python version and the OpenAI Python API with a link. This is helpful for setting up the environment correctly.
  • 12-14: The installation instructions are clear and straightforward, guiding the user through cloning the repository and installing the required packages. This should facilitate a smooth setup process for new users.
  • 16-22: The instructions for setting the OpenAI API key in the environment variable are clear and include a verification step, which is a good practice to ensure the key is set correctly.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 12

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3799d7b and d361df3.
Files selected for processing (5)
  • README.md (4 hunks)
  • neurons/miners/openai/README.md (1 hunks)
  • neurons/validators/validator.py (3 hunks)
  • prompting/protocol.py (1 hunks)
  • prompting/validator/forward.py (3 hunks)
Additional comments: 8
neurons/validators/validator.py (2)
  • 3-3: The copyright year has been updated to 2024, which is consistent with the current year. This change is accurate and reflects the latest modifications to the file.
  • 59-59: The forward method's implementation using the prompting module is a placeholder for the actual logic. It's important to ensure that the forward function from the prompting.validator is fully integrated and tested within the validator's workflow.
prompting/validator/forward.py (3)
  • 3-3: The copyright year has been updated to 2024, accurately reflecting the current year and the latest modifications to the file.
  • 21-23: The imports from the prompting package, including the Prompting module and related utilities, are correctly implemented. This change aligns with the PR objectives to utilize the prompting package for improved synapse integration.
  • 42-48: The creation of a new Prompting instance with specific character information and criteria is a significant improvement for synapse integration. It's important to ensure that the character information and criteria are appropriately configured for the intended use case.
Verification successful

The verification process has confirmed that the character information and criteria are consistently defined and used across the codebase, specifically within prompting/validator/forward.py, prompting/protocol.py, and in various test files. This consistency supports the conclusion that these configurations are appropriately tailored for their intended use case, aligning with the initial review comment's emphasis on ensuring suitable configuration for synapse integration.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the character information and criteria are suitable for the intended use case.
cat prompting/config.py | grep 'character_info\|criteria'

Length of output: 111



Script:

#!/bin/bash
# Search the entire codebase for 'character_info' and 'criteria' to verify their configuration.
rg 'character_info|criteria' -t py

Length of output: 1940

prompting/protocol.py (3)
  • 3-3: The copyright year has been updated to 2024, which is consistent with the current year and reflects the latest modifications to the file.
  • 24-91: The PromptingMixin class is well-designed, providing a clear structure for managing chat sessions with an LLM. The use of Pydantic for model definition ensures type safety and validation. It's important to ensure that the fields and methods of this class are adequately documented and tested, especially considering its central role in the prompting functionality.
  • 111-148: The Prompting class extends PromptingMixin with Bittensor Synapse integration, encapsulating the functionalities required for a simplified chat session with an LLM. The design choice to inherit from both PromptingMixin and bt.Synapse is appropriate for leveraging existing Synapse functionalities while adding specific prompting capabilities. Ensure that the deserialize method and any other overridden Synapse methods are correctly implemented and tested.

@@ -0,0 +1,34 @@
# OpenAI Bittensor Miner

This repository contains a Bittensor Miner that uses OpenAI's GPT-3.5-turbo model as its synapse. The miner connects to the Bittensor network, registers its wallet, and serves the GPT-3.5-turbo model to the network by attaching the prompt function to the axon.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction clearly explains the purpose and functionality of the OpenAI Bittensor Miner, which is beneficial for new users. However, it might be helpful to include a brief overview of Bittensor and how this miner fits into the larger ecosystem for complete newcomers.

Comment on lines +7 to +9
- Python 3.8+
- OpenAI Python API (<https://github.com/openai/openai>)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prerequisites section is concise and informative, listing the minimum requirements for using the miner. It's good practice to also mention any potential compatibility issues or additional setup steps for different operating systems if applicable.

Comment on lines +12 to +22
1. Clone the repository
2. Install the required packages with `pip install -r requirements.txt`
3. Ensure that you have your OpenAI key in your os environment variable

```bash
# Sets your openai key in os envs variable
export OPENAI_API_KEY='your_openai_key_here'

# Verifies if openai key is set correctly
echo "$OPENAI_API_KEY"
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The installation instructions are clear and straightforward. Including the commands for setting the OpenAI API key in the environment variables is particularly helpful. It might be beneficial to also provide guidance on how to permanently set environment variables for users who are not familiar with the process.

echo "$OPENAI_API_KEY"
```

For more configuration options related to the wallet, axon, subtensor, logging, and metagraph, please refer to the Bittensor documentation.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the previous comment regarding the direct link to the Bittensor documentation, it's advisable to follow up on that suggestion. Adding a direct link to the relevant section of the Bittensor documentation would indeed enhance the user experience.

- For more configuration options related to the wallet, axon, subtensor, logging, and metagraph, please refer to the Bittensor documentation.
+ For more configuration options related to the wallet, axon, subtensor, logging, and metagraph, please refer to the Bittensor documentation [here](https://docs.bittensor.com/).

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
For more configuration options related to the wallet, axon, subtensor, logging, and metagraph, please refer to the Bittensor documentation.
For more configuration options related to the wallet, axon, subtensor, logging, and metagraph, please refer to the Bittensor documentation [here](https://docs.bittensor.com/).

Comment on lines +28 to +34
To run the OpenAI Bittensor Miner with default settings, use the following command:

```bash
python3 -m pip install -r ./neurons/miners/openai/requirements.txt
export OPENAI_API_KEY='sk-yourkey'
python3 ./neurons/miners/openai/miner.py
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example usage section provides a clear and concise way to get started with the miner. It might be useful to include a brief explanation of what each command does for users who are less familiar with command-line operations.

Comment on lines +10 to +26
# Installation

This repository requires python3.8 or higher. To install, simply clone this repository and install the requirements.

```bash
git clone https://github.com/nanlabs/bittensor-subnet-example.git
cd bittensor-subnet-example
python -m pip install -r requirements.txt
python -m pip install -e .
```

If you are running a specific miner or validator, you might need install its specific requirements. For example, the Langchain-based miner requires the following:

```bash
cd neurons/miners/openai
python -m pip install -r requirements.txt
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated installation instructions are clear and provide a straightforward path for users to set up the project. Including specific instructions for the Langchain-based miner is helpful. It might be beneficial to also mention any potential prerequisites or compatibility issues specific to the Langchain-based miner.

Comment on lines +39 to +57
- [Quickstarter template](#quickstarter-template)
- [In order to simplify the building of subnets, this template abstracts away the complexity of the underlying blockchain and other boilerplate code. While the default behavior of the template is sufficient for a simple subnet, you should customize the template in order to meet your specific requirements](#in-order-to-simplify-the-building-of-subnets-this-template-abstracts-away-the-complexity-of-the-underlying-blockchain-and-other-boilerplate-code-while-the-default-behavior-of-the-template-is-sufficient-for-a-simple-subnet-you-should-customize-the-template-in-order-to-meet-your-specific-requirements)
- [Introduction](#introduction)
- [Example](#example)
- [Installation](#installation-1)
- [Before you proceed](#before-you-proceed)
- [Install](#install)
- [Writing your own incentive mechanism](#writing-your-own-incentive-mechanism)
- [Subnet Links](#subnet-links)
- [License](#license)
- [License](#license)

---

## Quickstarter template

This template contains all the required installation instructions, scripts, and files and functions for:

- Building Bittensor subnets.
- Creating custom incentive mechanisms and running these mechanisms on the subnets.
- Creating custom incentive mechanisms and running these mechanisms on the subnets.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction of the Quickstarter template section is a valuable addition, providing users with an overview of what the template offers. It would enhance clarity to briefly describe each bullet point, explaining how the template facilitates building Bittensor subnets and creating custom incentive mechanisms.

Comment on lines +63 to +76
**IMPORTANT**: If you are new to Bittensor subnets, read this section before proceeding to [Installation](#installation) section.

The Bittensor blockchain hosts multiple self-contained incentive mechanisms called **subnets**. Subnets are playing fields in which:

- Subnet miners who produce value, and
- Subnet validators who produce consensus

determine together the proper distribution of TAO for the purpose of incentivizing the creation of value, i.e., generating digital commodities, such as intelligence or data.
determine together the proper distribution of TAO for the purpose of incentivizing the creation of value, i.e., generating digital commodities, such as intelligence or data.

Each subnet consists of:

- Subnet miners and subnet validators.
- A protocol using which the subnet miners and subnet validators interact with one another. This protocol is part of the incentive mechanism.
- The Bittensor API using which the subnet miners and subnet validators interact with Bittensor's onchain consensus engine [Yuma Consensus](https://bittensor.com/documentation/validating/yuma-consensus). The Yuma Consensus is designed to drive these actors: subnet validators and subnet miners, into agreement on who is creating value and what that value is worth.
- The Bittensor API using which the subnet miners and subnet validators interact with Bittensor's onchain consensus engine [Yuma Consensus](https://bittensor.com/documentation/validating/yuma-consensus). The Yuma Consensus is designed to drive these actors: subnet validators and subnet miners, into agreement on who is creating value and what that value is worth.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Introduction section does an excellent job of explaining the concept of Bittensor subnets and their components. This section is crucial for newcomers to understand the ecosystem. Including examples or links to existing subnets could further help users grasp the practical applications of the template.

Comment on lines +94 to +100
Before you proceed with the installation of the subnet, note the following:

- Use these instructions to run your subnet locally for your development and testing, or on Bittensor testnet or on Bittensor mainnet.
- **IMPORTANT**: We **strongly recommend** that you first run your subnet locally and complete your development and testing before running the subnet on Bittensor testnet. Furthermore, make sure that you next run your subnet on Bittensor testnet before running it on the Bittensor mainnet.
- You can run your subnet either as a subnet owner, or as a subnet validator or as a subnet miner.
- You can run your subnet either as a subnet owner, or as a subnet validator or as a subnet miner.
- **IMPORTANT:** Make sure you are aware of the minimum compute requirements for your subnet. See the [Minimum compute YAML configuration](./min_compute.yml).
- Note that installation instructions differ based on your situation: For example, installing for local development and testing will require a few additional steps compared to installing for testnet. Similarly, installation instructions differ for a subnet owner vs a validator or a miner.
- Note that installation instructions differ based on your situation: For example, installing for local development and testing will require a few additional steps compared to installing for testnet. Similarly, installation instructions differ for a subnet owner vs a validator or a miner.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Before you proceed subsection under Installation provides important considerations for users. Highlighting the recommendation to first run subnets locally and then on the testnet before moving to the mainnet is prudent. It might be useful to link to resources on how to set up a local development environment for users unfamiliar with the process.

Comment on lines +113 to +121

- `template/protocol.py`: Contains the definition of the wire-protocol used by miners and validators.
- `neurons/miner.py`: Script that defines the miner's behavior, i.e., how the miner responds to requests from validators.
- `neurons/validator.py`: This script defines the validator's behavior, i.e., how the validator requests information from the miners and determines the scores.
- `template/forward.py`: Contains the definition of the validator's forward pass.
- `template/reward.py`: Contains the definition of how validators reward miner responses.

In addition to the above files, you should also update the following files:

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Writing your own incentive mechanism section is a critical guide for users looking to customize the template. The detailed instructions and the mention of specific files to edit are helpful. Including a step-by-step guide or examples of custom incentive mechanisms could further assist users in this process.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d361df3 and 46ca4fd.
Files selected for processing (3)
  • prompting/validator/forward.py (4 hunks)
  • prompting/validator/reward.py (2 hunks)
  • requirements.txt (1 hunks)
Files skipped from review due to trivial changes (1)
  • requirements.txt
Files skipped from review as they are similar to previous changes (1)
  • prompting/validator/forward.py
Additional comments: 3
prompting/validator/reward.py (3)
  • 3-4: The copyright years have been updated to 2024 for adanmauri and ulises-jeremias. Ensure that these updates are consistent with the project's licensing agreements and copyright policies.
  • 20-21: The import statements now include textblob and torch, which are essential for the new functionality. Ensure that these dependencies are correctly listed in the project's requirements.txt file to avoid runtime errors.
  • 43-43: The get_rewards method now accepts a list of response strings instead of integers, aligning with the updated reward function. Ensure that all calls to get_rewards throughout the codebase have been updated to pass the correct argument types.

Comment on lines 26 to 37
def reward(query: int, response: str) -> float:
"""
Reward the miner response to the dummy request. This method returns a reward
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.

Returns:
- float: The reward value for the miner.
"""

return 1.0 if response == query * 2 else 0
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
return sentiment_sum / len(blob.sentences)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reward function has been significantly modified to compute sentiment-based rewards using TextBlob. This change aligns with the PR's objective to calculate sentiment-based rewards. However, consider handling the case where blob.sentences is empty to avoid division by zero errors.

-    return sentiment_sum / len(blob.sentences)
+    num_sentences = len(blob.sentences)
+    return sentiment_sum / num_sentences if num_sentences > 0 else 0.0

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def reward(query: int, response: str) -> float:
"""
Reward the miner response to the dummy request. This method returns a reward
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.
Returns:
- float: The reward value for the miner.
"""
return 1.0 if response == query * 2 else 0
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
return sentiment_sum / len(blob.sentences)
def reward(query: int, response: str) -> float:
"""
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.
Returns:
- float: The reward value for the miner.
"""
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
num_sentences = len(blob.sentences)
return sentiment_sum / num_sentences if num_sentences > 0 else 0.0

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 46ca4fd and 9f655fc.
Files selected for processing (2)
  • neurons/validators/validator.py (3 hunks)
  • prompting/validator/reward.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • neurons/validators/validator.py
Additional comments: 3
prompting/validator/reward.py (3)
  • 3-4: The copyright years have been updated to 2024, which aligns with the current year. However, it's important to ensure that all contributors listed are correct and have agreed to the changes.
  • 20-21: The addition of textblob and torch imports indicates new dependencies for sentiment analysis and tensor operations, respectively. Ensure that these packages are included in the project's requirements.txt file to avoid runtime errors.
Verification successful

The executed script successfully found instances of 'textblob' and 'torch' in one or more requirements.txt files within the project. This confirms that the necessary dependencies for sentiment analysis and tensor operations, as mentioned in the review comment, are indeed included in the project's dependencies list. Therefore, the initial concern regarding the inclusion of these packages in the requirements.txt file to avoid runtime errors has been addressed.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify if textblob and torch are included in requirements.txt
grep -E 'textblob|torch' neurons/miners/openai/requirements.txt

Length of output: 65



Script:

#!/bin/bash
# Search for all requirements.txt files in the project and check if they contain 'textblob' or 'torch'
fd requirements.txt --exec grep -E 'textblob|torch' {}

Length of output: 87

* 45-45: The `get_rewards` method correctly iterates over responses to generate a tensor of rewards. However, it references `self.device`, which implies that this function is part of a class, but the class context is not provided in the snippet. Ensure that `self.device` is properly initialized and accessible within the class this method belongs to.

Comment on lines +26 to +39
def reward(query: int, response: str) -> float:
"""
Reward the miner response to the dummy request. This method returns a reward
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.

Returns:
- float: The reward value for the miner.
"""

return 1.0 if response == query * 2 else 0
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
sentiment_avg = sentiment_sum / len(blob.sentences)
sentiment_normalized = (sentiment_avg + 1) / 2
return sentiment_normalized
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reward function has been significantly modified to calculate sentiment-based rewards. This is a creative approach to evaluating miner responses. However, there are a few considerations:

  • The function signature incorrectly lists query: int as a parameter, but it is not used within the function. This could be a leftover from the previous implementation and should be removed if unnecessary.
  • Dividing by zero could occur if blob.sentences is empty. Adding a check to ensure len(blob.sentences) > 0 before division would prevent potential runtime errors.
  • Normalizing sentiment scores to a range of 0 to 1 is a good practice, but the rationale behind this specific normalization formula should be documented for clarity.
- def reward(query: int, response: str) -> float:
+ def reward(response: str) -> float:
  ...
  sentiment_avg = sentiment_sum / len(blob.sentences) if len(blob.sentences) > 0 else 0

Consider adding a comment explaining the normalization formula used for sentiment scores.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def reward(query: int, response: str) -> float:
"""
Reward the miner response to the dummy request. This method returns a reward
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.
Returns:
- float: The reward value for the miner.
"""
return 1.0 if response == query * 2 else 0
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
sentiment_avg = sentiment_sum / len(blob.sentences)
sentiment_normalized = (sentiment_avg + 1) / 2
return sentiment_normalized
def reward(response: str) -> float:
"""
Reward the miner response to the prompting request. This method returns a reward
value for the miner, which is used to update the miner's score.
Returns:
- float: The reward value for the miner.
"""
blob = textblob.TextBlob(response)
sentiment_sum = reduce(lambda x, y: x + y, [sentence.sentiment.polarity for sentence in blob.sentences])
sentiment_avg = sentiment_sum / len(blob.sentences) if len(blob.sentences) > 0 else 0
sentiment_normalized = (sentiment_avg + 1) / 2
return sentiment_normalized

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9f655fc and 596392b.
Files ignored due to path filters (1)
  • compose.yml is excluded by: !**/*.yml
Files selected for processing (3)
  • .devcontainer/compose.yml (1 hunks)
  • .devcontainer/devcontainer.json (1 hunks)
  • .github/dependabot.yml (1 hunks)
Additional comments: 3
.github/dependabot.yml (1)
  • 1-12: The Dependabot configuration is correctly set up to automate dependency updates for devcontainers on a weekly basis. It's important to monitor these automated updates for potential breaking changes or issues that might arise.
.devcontainer/compose.yml (1)
  • 1-26: The Docker Compose configuration is set up correctly for a development environment. Ensure the volume mount path "..:/workspaces:cached" matches the expected project structure and that any customization follows Docker best practices.
.devcontainer/devcontainer.json (1)
  • 1-57: The development container configuration for VS Code is correctly set up, including beneficial features for Python development and code quality tools. The commented-out options offer flexibility for further customization based on specific project needs.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 596392b and 60c69d8.
Files ignored due to path filters (1)
  • compose.yml is excluded by: !**/*.yml
Files selected for processing (8)
  • Dockerfile (1 hunks)
  • scripts/localnet.sh (1 hunks)
  • scripts/run_miner.sh (1 hunks)
  • scripts/run_validator.sh (1 hunks)
  • scripts/setup_devcontainer.sh (1 hunks)
  • scripts/setup_miner_wallet.sh (1 hunks)
  • scripts/setup_validator_wallet.sh (1 hunks)
  • setup.py (2 hunks)
Additional comments: 1
Dockerfile (1)
  • 1-13: The Dockerfile is well-structured and follows best practices for setting up a Rust development environment, including installing necessary libraries, setting up Rust, and cleaning up apt lists to reduce the image size. No changes are recommended.

@@ -0,0 +1,3 @@
#!/bin/bash

python neurons/miners/openai/miner.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name miner --wallet.hotkey default --logging.debug
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making the script more flexible by allowing command-line arguments or environment variables to override the default values for --netuid, --subtensor.chain_endpoint, --wallet.name, and --wallet.hotkey. This would make the script more adaptable to different environments or user setups. Additionally, using python3 explicitly instead of python can help ensure compatibility with Python 3 environments.

- python neurons/miners/openai/miner.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name miner --wallet.hotkey default --logging.debug
+ #!/bin/bash
+ NETUID=${1:-1}
+ CHAIN_ENDPOINT=${2:-"ws://127.0.0.1:9946"}
+ WALLET_NAME=${3:-"miner"}
+ WALLET_HOTKEY=${4:-"default"}
+ python3 neurons/miners/openai/miner.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
python neurons/miners/openai/miner.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name miner --wallet.hotkey default --logging.debug
#!/bin/bash
NETUID=${1:-1}
CHAIN_ENDPOINT=${2:-"ws://127.0.0.1:9946"}
WALLET_NAME=${3:-"miner"}
WALLET_HOTKEY=${4:-"default"}
python3 neurons/miners/openai/miner.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug

@@ -0,0 +1,3 @@
#!/bin/bash

python neurons/validators/validator.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name validator --wallet.hotkey default --logging.debug
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the run_miner.sh script, consider making this script more flexible by allowing command-line arguments or environment variables to override the default values. Additionally, using python3 explicitly can help ensure compatibility with Python 3 environments.

- python neurons/validators/validator.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name validator --wallet.hotkey default --logging.debug
+ #!/bin/bash
+ NETUID=${1:-1}
+ CHAIN_ENDPOINT=${2:-"ws://127.0.0.1:9946"}
+ WALLET_NAME=${3:-"validator"}
+ WALLET_HOTKEY=${4:-"default"}
+ python3 neurons/validators/validator.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
python neurons/validators/validator.py --netuid 1 --subtensor.chain_endpoint ws://127.0.0.1:9946 --wallet.name validator --wallet.hotkey default --logging.debug
#!/bin/bash
NETUID=${1:-1}
CHAIN_ENDPOINT=${2:-"ws://127.0.0.1:9946"}
WALLET_NAME=${3:-"validator"}
WALLET_HOTKEY=${4:-"default"}
python3 neurons/validators/validator.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug

Comment on lines 3 to 23
# Logic for setting up and running the environment
setup_environment() {
# Install the bittensor-prompting-example python package
python -m pip install -e .

# Create and set up wallets
# This section can be skipped if wallets are already set up
if [ ! -f ~/.bittensor/wallets/owner/coldkeypub.txt ]; then
btcli wallet new_coldkey --wallet.name owner --no_password --no_prompt
fi
}

# Call setup_environment every time
setup_environment

# Run the localnet using node-subtensor
FEATURES='pow-faucet runtime-benchmarks' BT_DEFAULT_TOKEN_WALLET=$(cat ~/.bittensor/wallets/owner/coldkeypub.txt | grep -oP '"ss58Address": "\K[^"]+') ./scripts/localnet.sh &

# Register a subnet (this needs to be run each time we start a new local chain)
btcli subnet create --wallet.name owner --wallet.hotkey default --subtensor.chain_endpoint ws://127.0.0.1:9946 --no_prompt

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script effectively automates the setup of the development environment. Consider adding more comments to explain each step for clarity, especially for new contributors. Additionally, it might be beneficial to check the success of critical commands (e.g., wallet creation, subnet registration) and handle potential errors gracefully.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 60c69d8 and 8946557.
Files selected for processing (3)
  • .devcontainer/common-debian.sh (1 hunks)
  • .devcontainer/devcontainer.json (1 hunks)
  • Dockerfile (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • .devcontainer/devcontainer.json
  • Dockerfile
Additional comments: 8
.devcontainer/common-debian.sh (8)
  • 22-27: This block simplifies user management by treating "none" or "root" usernames as root. This is a practical approach but ensure that this behavior is clearly documented to avoid confusion.
  • 37-38: Setting DEBIAN_FRONTEND=noninteractive is a good practice to avoid prompts during package installation in scripts. This ensures that the script can run without manual intervention.
  • 41-49: The function apt-get-update-if-needed is a smart way to avoid unnecessary apt-get update calls. This can save time and reduce network traffic when running the script multiple times.
  • 104-109: Upgrading packages and removing unnecessary ones is a good practice for maintaining a clean environment. Ensure that this behavior is configurable or documented, as it might not be desired in all scenarios.
  • 111-117: Locale configuration is essential for many applications. This approach ensures that at least the en_US.UTF-8 locale is available, which is a sensible default.
  • 135-140: Adding sudo support for the non-root user enhances flexibility. Ensure that this is clearly documented, as it grants significant privileges.
  • 142-147: Adding ~/.local/bin to the PATH is a good practice for user convenience. This ensures that locally installed binaries are easily accessible.
  • 164-171: Writing a marker file to track the script's progress is a clever way to avoid redundant operations. This is especially useful in development environments where the script might be run multiple times.

Comment on lines +17 to +20
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling and user feedback. The script exits if not run as root, but consider providing guidance on how to correct this, such as suggesting the exact command to run with sudo.

Comment on lines +51 to +102
# Run install apt-utils to avoid debconf warning then verify presence of other common developer tools and dependencies
if [ "${PACKAGES_ALREADY_INSTALLED}" != "true" ]; then
apt-get-update-if-needed

PACKAGE_LIST="apt-utils \
git \
openssh-client \
less \
iproute2 \
procps \
curl \
wget \
unzip \
nano \
jq \
lsb-release \
ca-certificates \
apt-transport-https \
dialog \
gnupg2 \
libc6 \
libgcc1 \
libgssapi-krb5-2 \
libicu[0-9][0-9] \
liblttng-ust0 \
libstdc++6 \
zlib1g \
locales \
sudo"

# Install libssl1.1 if available
if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
PACKAGE_LIST="${PACKAGE_LIST} libssl1.1"
fi

# Install appropriate version of libssl1.0.x if available
LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
# Debian 9
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.2"
elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
# Ubuntu 18.04, 16.04, earlier
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.0"
fi
fi

echo "Packages to verify are installed: ${PACKAGE_LIST}"
apt-get -y install --no-install-recommends ${PACKAGE_LIST} 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )

PACKAGES_ALREADY_INSTALLED="true"
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section handles package installation efficiently by checking if the packages are already installed. However, consider adding error handling for the apt-get install command to manage potential failures gracefully.

Comment on lines +119 to +133
# Create or update a non-root user to match UID/GID - see https://aka.ms/vscode-remote/containers/non-root-user.
if id -u $USERNAME > /dev/null 2>&1; then
# User exists, update if needed
if [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
groupmod --gid $USER_GID $USERNAME
usermod --gid $USER_GID $USERNAME
fi
if [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
usermod --uid $USER_UID $USERNAME
fi
else
# Create user
groupadd --gid $USER_GID $USERNAME
useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User management based on UID/GID is crucial for container environments to avoid permission issues. This implementation is thorough but ensure that potential errors during user/group modification are handled appropriately.

Comment on lines +149 to +162
# Optionally install and configure zsh
if [ "${INSTALL_ZSH}" = "true" ] && [ ! -d "/root/.oh-my-zsh" ] && [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
apt-get-update-if-needed
apt-get install -y zsh
curl -fsSLo- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash 2>&1
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
if [ "${USERNAME}" != "root" ]; then
cp -fR /root/.oh-my-zsh /home/$USERNAME
cp -f /root/.zshrc /home/$USERNAME
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
fi
ZSH_ALREADY_INSTALLED="true"
fi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing and configuring zsh as an optional step provides flexibility for users who prefer zsh over bash. However, the security concern raised by the static analysis tool regarding piping data from curl into bash must be addressed.

Consider verifying the integrity of the script before execution or providing an alternative method for installing zsh and oh-my-zsh that does not involve piping from curl to bash.

-    curl -fsSLo- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash 2>&1
+    # Consider downloading the script, verifying its integrity, and then executing it.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
# Optionally install and configure zsh
if [ "${INSTALL_ZSH}" = "true" ] && [ ! -d "/root/.oh-my-zsh" ] && [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
apt-get-update-if-needed
apt-get install -y zsh
curl -fsSLo- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash 2>&1
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
if [ "${USERNAME}" != "root" ]; then
cp -fR /root/.oh-my-zsh /home/$USERNAME
cp -f /root/.zshrc /home/$USERNAME
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
fi
ZSH_ALREADY_INSTALLED="true"
fi
# Optionally install and configure zsh
if [ "${INSTALL_ZSH}" = "true" ] && [ ! -d "/root/.oh-my-zsh" ] && [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
apt-get-update-if-needed
apt-get install -y zsh
# Consider downloading the script, verifying its integrity, and then executing it.
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
if [ "${USERNAME}" != "root" ]; then
cp -fR /root/.oh-my-zsh /home/$USERNAME
cp -f /root/.zshrc /home/$USERNAME
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
fi
ZSH_ALREADY_INSTALLED="true"
fi

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8946557 and 40d4b6f.
Files selected for processing (5)
  • .devcontainer/compose.yml (1 hunks)
  • .devcontainer/devcontainer.json (1 hunks)
  • scripts/run_openai_miner.sh (1 hunks)
  • scripts/run_validator.sh (1 hunks)
  • scripts/setup_devcontainer.sh (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • .devcontainer/compose.yml
  • .devcontainer/devcontainer.json
  • scripts/run_validator.sh
  • scripts/setup_devcontainer.sh
Additional comments: 2
scripts/run_openai_miner.sh (2)
  • 1-1: The shebang line correctly specifies bash as the interpreter, ensuring the script is executed in a Bash environment.
  • 3-6: The script uses positional parameters with default values for NETUID, CHAIN_ENDPOINT, WALLET_NAME, and WALLET_HOTKEY. This approach is clear and flexible, allowing users to easily customize the script's behavior.

WALLET_NAME=${3:-"openai_miner"}
WALLET_HOTKEY=${4:-"default"}

python -m pip install --upgrade --user -r ./neurons/miners/openai/requirements.txt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command to install dependencies from requirements.txt is well-formed. However, using --user might not be suitable in all environments, such as Docker containers or virtual environments, where the global installation is preferred.

Consider removing --user to allow for more flexibility in different execution environments, or add a check to determine the best installation strategy based on the environment.


python -m pip install --upgrade --user -r ./neurons/miners/openai/requirements.txt

python ./neurons/miners/openai/miner.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command to run the miner.py script is correctly formatted, passing all necessary arguments. However, it's important to quote variables to prevent word splitting and globbing issues, which can lead to unexpected behavior or security vulnerabilities.

- python ./neurons/miners/openai/miner.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug 
+ python ./neurons/miners/openai/miner.py --netuid "$NETUID" --subtensor.chain_endpoint "$CHAIN_ENDPOINT" --wallet.name "$WALLET_NAME" --wallet.hotkey "$WALLET_HOTKEY" --logging.debug

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
python ./neurons/miners/openai/miner.py --netuid $NETUID --subtensor.chain_endpoint $CHAIN_ENDPOINT --wallet.name $WALLET_NAME --wallet.hotkey $WALLET_HOTKEY --logging.debug
python ./neurons/miners/openai/miner.py --netuid "$NETUID" --subtensor.chain_endpoint "$CHAIN_ENDPOINT" --wallet.name "$WALLET_NAME" --wallet.hotkey "$WALLET_HOTKEY" --logging.debug

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 40d4b6f and c43517d.
Files selected for processing (4)
  • scripts/run_openai_miner.sh (1 hunks)
  • scripts/run_validator.sh (1 hunks)
  • scripts/setup_miner_wallet.sh (1 hunks)
  • scripts/setup_validator_wallet.sh (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • scripts/run_openai_miner.sh
  • scripts/run_validator.sh
  • scripts/setup_miner_wallet.sh
  • scripts/setup_validator_wallet.sh

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c43517d and 82e179f.
Files selected for processing (4)
  • scripts/run_openai_miner.sh (1 hunks)
  • scripts/run_validator.sh (1 hunks)
  • scripts/setup_miner_wallet.sh (1 hunks)
  • scripts/setup_validator_wallet.sh (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • scripts/run_openai_miner.sh
  • scripts/run_validator.sh
  • scripts/setup_miner_wallet.sh
  • scripts/setup_validator_wallet.sh

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 82e179f and 017df59.
Files ignored due to path filters (1)
  • docs/vscode-open-in-container.gif is excluded by: !**/*.gif
Files selected for processing (1)
  • README.md (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • README.md

@ulises-jeremias ulises-jeremias merged commit af4ca39 into main Feb 27, 2024
@ulises-jeremias ulises-jeremias deleted the feature/openai-miner branch February 27, 2024 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant