generated from opentensor/bittensor-subnet-template
/
agent.py
140 lines (113 loc) · 5.41 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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 textwrap
import time
import bittensor as bt
from dataclasses import asdict
from prompting.tasks import Task
from prompting.llms import HuggingFaceLLM, vLLM_LLM
from prompting.cleaners.cleaner import CleanerPipeline
from prompting.persona import Persona, create_persona
from transformers import Pipeline
class HumanAgent(vLLM_LLM):
"Agent that impersonates a human user and makes queries based on its goal."
@property
def progress(self):
return int(self.task.complete)
@property
def finished(self):
return self.progress == 1
system_prompt_template = textwrap.dedent(
"""This is a roleplaying game where you are impersonating {mood} human user with a specific persona. As a human, you are using AI assistant to {desc} related to {topic} ({subtopic}) in a {tone} tone. You don't need to greet the assistant or be polite, unless this is part of your persona. The spelling and grammar of your messages should also reflect your persona.
Your singular focus is to use the assistant to {goal}: {query}
"""
)
def __init__(
self,
task: Task,
llm_pipeline: Pipeline,
system_template: str = None,
persona: Persona = None,
begin_conversation=True,
):
if persona is None:
persona = create_persona()
self.persona = persona
self.task = task
self.llm_pipeline = llm_pipeline
if system_template is not None:
self.system_prompt_template = system_template
self.system_prompt = self.system_prompt_template.format(
mood=self.persona.mood,
tone=self.persona.tone,
**self.task.__state_dict__(), # Adds desc, subject, topic
)
super().__init__(
llm_pipeline=llm_pipeline,
system_prompt=self.system_prompt,
max_new_tokens=256,
)
if begin_conversation:
bt.logging.info("🤖 Generating challenge query...")
# initiates the conversation with the miner
self.challenge = self.create_challenge()
def create_challenge(self) -> str:
"""Creates the opening question of the conversation which is based on the task query but dressed in the persona of the user."""
t0 = time.time()
cleaner = None
if hasattr(self.task, "cleaning_pipeline"):
cleaner = CleanerPipeline(cleaning_pipeline=self.task.cleaning_pipeline)
if self.task.challenge_type == "inference":
self.challenge = super().query(
message="Ask a question related to your goal", cleaner=cleaner
)
elif self.task.challenge_type == 'paraphrase':
self.challenge = self.task.challenge_template.next(self.task.query)
elif self.task.challenge_type == 'query':
self.challenge = self.task.query
else:
bt.logging.error(f"Task {self.task.name} has challenge type of: {self.task.challenge_type} which is not supported.")
self.challenge = self.task.format_challenge(self.challenge)
self.challenge_time = time.time() - t0
return self.challenge
def __state_dict__(self, full=False):
return {
"challenge": self.challenge,
"challenge_time": self.challenge_time,
**self.task.__state_dict__(full=full),
**asdict(self.persona),
"system_prompt": self.system_prompt,
}
def __str__(self):
return self.system_prompt
def __repr__(self):
return str(self)
def continue_conversation(self, miner_response: str):
# Generates response to miner response
self.query(miner_response)
# Updates current prompt with new state of conversation
# self.prompt = self.get_history_prompt()
def update_progress(
self, top_reward: float, top_response: str, continue_conversation=False
):
if top_reward > self.task.reward_threshold:
self.task.complete = True
self.messages.append({"content": top_response, "role": "user"})
bt.logging.info("Agent finished its goal")
return
if continue_conversation:
bt.logging.info(
"↪ Agent did not finish its goal, continuing conversation..."
)
self.continue_conversation(miner_response=top_response)