forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
214 lines (184 loc) · 7.94 KB
/
__main__.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
"""Simple REPL frontend."""
import random
import requests
import typer
app = typer.Typer()
# debug constants
USER = {"id": "1234", "display_name": "John Doe", "auth_method": "local"}
def _random_post_id():
return str(random.randint(1000, 9999))
def _render_message(message: dict) -> str:
"""Render a message to the user."""
if message["is_assistant"]:
return f"Assistant: {message['text']}"
return f"User: {message['text']}"
@app.command()
def main(backend_url: str = "http://127.0.0.1:8080", api_key: str = "DUMMY_KEY"):
"""Simple REPL frontend."""
def _post(path: str, json: dict) -> dict:
response = requests.post(f"{backend_url}{path}", json=json, headers={"X-API-Key": api_key})
response.raise_for_status()
return response.json()
typer.echo("Requesting work...")
tasks = [_post("/api/v1/tasks/", {"type": "random"})]
while tasks:
task = tasks.pop(0)
match (task["type"]):
case "summarize_story":
typer.echo("Summarize the following story:")
typer.echo(task["story"])
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
summary = typer.prompt("Enter your summary")
user_post_id = _random_post_id()
# send interaction
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "text_reply_to_post",
"post_id": post_id,
"user_post_id": user_post_id,
"text": summary,
"user": USER,
},
)
tasks.append(new_task)
case "rate_summary":
typer.echo("Rate the following summary:")
typer.echo(task["summary"])
typer.echo("Full text:")
typer.echo(task["full_text"])
typer.echo(f"Rating scale: {task['scale']['min']} - {task['scale']['max']}")
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
rating = typer.prompt("Enter your rating", type=int)
# send interaction
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "post_rating",
"post_id": post_id,
"rating": rating,
"user": USER,
},
)
tasks.append(new_task)
case "initial_prompt":
typer.echo("Please provide an initial prompt to the assistant.")
if task["hint"]:
typer.echo(f"Hint: {task['hint']}")
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
prompt = typer.prompt("Enter your prompt")
user_post_id = _random_post_id()
# send interaction
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "text_reply_to_post",
"post_id": post_id,
"user_post_id": user_post_id,
"text": prompt,
"user": USER,
},
)
tasks.append(new_task)
case "user_reply":
typer.echo("Please provide a reply to the assistant.")
typer.echo("Here is the conversation so far:")
for message in task["conversation"]["messages"]:
typer.echo(_render_message(message))
if task["hint"]:
typer.echo(f"Hint: {task['hint']}")
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
reply = typer.prompt("Enter your reply")
user_post_id = _random_post_id()
# send interaction
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "text_reply_to_post",
"post_id": post_id,
"user_post_id": user_post_id,
"text": reply,
"user": USER,
},
)
tasks.append(new_task)
case "assistant_reply":
typer.echo("Act as the assistant and reply to the user.")
typer.echo("Here is the conversation so far:")
for message in task["conversation"]["messages"]:
typer.echo(_render_message(message))
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
reply = typer.prompt("Enter your reply")
user_post_id = _random_post_id()
# send interaction
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "text_reply_to_post",
"post_id": post_id,
"user_post_id": user_post_id,
"text": reply,
"user": USER,
},
)
tasks.append(new_task)
case "rank_initial_prompts":
typer.echo("Rank the following prompts:")
for idx, prompt in enumerate(task["prompts"], start=1):
typer.echo(f"{idx}: {prompt}")
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
ranking_str = typer.prompt("Enter the prompt numbers in order of preference, separated by commas")
ranking = [int(x) - 1 for x in ranking_str.split(",")]
# send ranking
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "post_ranking",
"post_id": post_id,
"ranking": ranking,
"user": USER,
},
)
tasks.append(new_task)
case "rank_user_replies" | "rank_assistant_replies":
typer.echo("Here is the conversation so far:")
for message in task["conversation"]["messages"]:
typer.echo(_render_message(message))
typer.echo("Rank the following replies:")
for idx, reply in enumerate(task["replies"], start=1):
typer.echo(f"{idx}: {reply}")
# acknowledge task
post_id = _random_post_id()
_post(f"/api/v1/tasks/{task['id']}/ack", {"post_id": post_id})
ranking_str = typer.prompt("Enter the reply numbers in order of preference, separated by commas")
ranking = [int(x) - 1 for x in ranking_str.split(",")]
# send ranking
new_task = _post(
"/api/v1/tasks/interaction",
{
"type": "post_ranking",
"post_id": post_id,
"ranking": ranking,
"user": USER,
},
)
tasks.append(new_task)
case "task_done":
typer.echo("Task done!")
case _:
typer.echo(f"Unknown task type {task['type']}")
if __name__ == "__main__":
app()