Skip to content

Commit

Permalink
Add sentiment analysis to reward calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Feb 26, 2024
1 parent d361df3 commit 46ca4fd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 0 additions & 1 deletion prompting/validator/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ async def forward(self):
# Log the results for monitoring purposes.
bt.logging.info(f"Received responses: {responses}")

# TODO(developer): Define how the validator scores responses.
# Adjust the scores based on responses from miners.
rewards = get_rewards(self, query=self.step, responses=responses)

Expand Down
10 changes: 7 additions & 3 deletions prompting/validator/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from functools import reduce
import textblob
import torch
from typing import List


def reward(query: int, response: int) -> float:
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.
Expand All @@ -30,13 +32,15 @@ def reward(query: int, response: int) -> float:
- 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 get_rewards(
self,
query: int,
responses: List[float],
responses: List[str],
) -> torch.FloatTensor:
"""
Returns a tensor of rewards for the given query and responses.
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bittensor
torch
torch
textblob==0.18.0.post0

0 comments on commit 46ca4fd

Please sign in to comment.