forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
201 lines (175 loc) · 7.53 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
# -*- coding: utf-8 -*-
"""Simple REPL frontend."""
import requests
import typer
app = typer.Typer()
# debug constants
POST_ID = "1234"
USER_POST_ID = "5678"
USER = {"id": "1234", "display_name": "John Doe", "auth_method": "local"}
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, api_key: str):
"""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(f"/api/v1/tasks/{task['id']}/ack", {"post_id": POST_ID})
summary = typer.prompt("Enter your summary")
# 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(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(f"/api/v1/tasks/{task['id']}/ack", {"post_id": POST_ID})
prompt = typer.prompt("Enter your prompt")
# 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(f"/api/v1/tasks/{task['id']}/ack", {"post_id": POST_ID})
reply = typer.prompt("Enter your reply")
# 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(f"/api/v1/tasks/{task['id']}/ack", {"post_id": POST_ID})
reply = typer.prompt("Enter your reply")
# 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(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) 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(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) 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":
if addressed_user := task["addressed_user"]:
typer.echo(f"Hey, {addressed_user['display_name']}! Thank you!")
else:
typer.echo("Task done!")
case _:
typer.echo(f"Unknown task type {task['type']}")
if __name__ == "__main__":
app()