-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
184 lines (155 loc) · 5.2 KB
/
app.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from dotenv import load_dotenv
import json
from griptape.structures import Agent
from griptape.rules import Rule, Ruleset
from griptape.tools import CalculatorTool
from rich import print as rprint
from rich.markdown import Markdown
from rich.panel import Panel
from rich.style import Style
from rich.prompt import Prompt
load_dotenv() # Load your environment
#creating rulesets for agents
json_ruleset = Ruleset(
name='json_ruleset',
rules=[
Rule(
"Respond in plain text only with valid JSON objects that have the following keys: response, continue_chatting."
),
Rule(
"Never wrap your response with ```"
),
Rule(
"The 'response' value should be a string that can be safely converted to markdown format. Use '\\n' for new lines."
),
Rule(
"If it sounds like the person is done chatting, set 'continue_chatting' to false, otherwise it is true"
),
],
)
parts_ruleset = Ruleset(
name='parts_ruleset',
rules=[
Rule(
"Respond in plain text only with valid JSON objects that have the following keys: score, distribution."
),
Rule(
"Never wrap your response with ```"
),
Rule(
"'score' is initially 50. Use the users desired budget to decide a score between 50 and 100"
),
Rule(
"'distribution' should be a float between 0.33 and 0.67."
),
],
)
question_ruleset = Ruleset(
name='question_ruleset',
rules=[
Rule(
"Respond in plain text only with valid JSON objects that have the following keys: question, continue_chatting."
),
Rule(
"Never wrap your response with ```"
),
Rule(
"The 'question' value should be a string that can be safely converted to markdown format. Use '\\n' for new lines."
),
Rule(
"If it sounds like the person is done chatting, set 'continue_chatting' to false, otherwise it is true"
),
],
)
reader_ruleset = Ruleset(
name='reader_ruleset',
rules=[
Rule(
"Respond in plain text only with valid JSON objects that have the following keys: summary, continue_chatting."
),
Rule(
"Never wrap your response with ```"
),
Rule(
"The 'summary' value should be a string that can be safely converted to markdown format. Use '\\n' for new lines."
),
Rule(
"If it sounds like the person is done chatting, set 'continue_chatting' to false, otherwise it is true"
),
],
)
# Create subclasses for the Agents
class ChatAgent(Agent):
def respond(self, user_input):
agent_response = self.run(user_input)
data = json.loads(agent_response.output_task.output.value)
response = data["response"]
continue_chatting = data["continue_chatting"]
formatted_response = Markdown(response)
print("")
rprint(
Panel.fit(
formatted_response,
width=80,
style=Style(color="light_sea_green"),
)
)
print("")
return continue_chatting
class QuestionerAgent(Agent):
def question (self, topic):
agent_response = self.run(topic)
formatted_response = Markdown(agent_response)
rprint("")
rprint(Panel.fit(
formatted_response,
width=80,
style=Style(color="light_sea_green"),
))
rprint("")
return
def ask(self, topic):
agent_response = self.run(f"Ask a relevant question to gather more information about: {topic}")
data = json.loads(agent_response.output_task.output.value)
question = data["question"]
continue_chatting = data["continue_chatting"]
formatted_question = Markdown(question)
print("")
rprint(
Panel.fit(
formatted_question,
width=80,
style=Style(color="light_sea_green"),
)
)
print("")
return continue_chatting
# Creating the agents
chat_agent = ChatAgent(
rulesets=[json_ruleset],
)
questioner_agent = QuestionerAgent(
tools=[CalculatorTool()],
)
# Chat function
def chat(agent):
is_chatting = True
while is_chatting:
user_input = Prompt.ask("[grey50]Chat with the assistant")
is_chatting = agent.respond(user_input)
# Question function
def question_session(agent):
is_chatting = True
while is_chatting:
agent.ask()
def question(agent, topic):
is_questioning = True
while is_questioning:
is_questioning = agent.ask(topic)
if is_questioning:
answer = Prompt.ask("[grey50]Your answer")
# You can process the answer here if needed
# Introduce the agent
chat_agent.respond("Introduce yourself to the user.")
# Run the agent
chat(chat_agent)