File size: 16,730 Bytes
e81fd5d
2efc192
e81fd5d
 
e49e33e
e81fd5d
93cc712
2efc192
 
b360328
 
 
e49e33e
e81fd5d
b360328
e81fd5d
2efc192
e81fd5d
 
2c41f4b
e81fd5d
 
 
2efc192
 
 
 
 
403f43d
 
 
 
 
 
 
 
 
 
 
 
 
e81fd5d
 
 
d353b2f
e81fd5d
 
403f43d
d353b2f
e81fd5d
6398310
e81fd5d
 
 
 
 
 
 
 
d353b2f
e81fd5d
 
b360328
e81fd5d
 
 
 
 
 
 
 
 
 
 
 
 
 
b360328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81fd5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403f43d
 
 
e81fd5d
e49e33e
403f43d
 
 
d3e686c
2c66a20
d353b2f
403f43d
e49e33e
 
 
 
 
 
 
d353b2f
 
 
 
e49e33e
e81fd5d
 
 
07615cb
e81fd5d
 
 
 
 
 
 
 
 
 
 
 
 
b360328
 
 
 
 
 
 
 
 
 
 
 
 
e81fd5d
 
 
 
a874b38
3862af2
a874b38
403f43d
 
 
 
 
e81fd5d
 
 
 
 
b360328
 
 
 
 
 
 
 
e81fd5d
e49e33e
b360328
e81fd5d
 
1cf9bbe
2efc192
 
 
 
 
 
 
 
 
 
 
 
2213167
1cf9bbe
2efc192
1cf9bbe
 
6009bc8
 
2efc192
 
e81fd5d
1cf9bbe
 
 
 
e81fd5d
96772fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14031c6
96772fe
 
 
14031c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81fd5d
 
 
 
 
fd00027
e81fd5d
d3e686c
 
3dd20d6
b39d980
 
3dd20d6
e81fd5d
 
 
 
 
 
 
1cf9bbe
 
 
 
e49e33e
 
 
 
 
e81fd5d
 
 
e49e33e
e81fd5d
 
 
 
403f43d
 
e81fd5d
4a2d9ba
e81fd5d
 
07615cb
e81fd5d
 
 
7606298
 
96772fe
 
14031c6
96772fe
e49e33e
7606298
96772fe
 
7606298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49e33e
7606298
 
 
 
 
c395137
 
7606298
 
 
c395137
7606298
 
 
e49e33e
7606298
 
1cf9bbe
 
7606298
 
 
 
e49e33e
7606298
1cf9bbe
7606298
e81fd5d
7606298
e49e33e
7606298
e49e33e
1cf9bbe
 
e49e33e
 
 
 
 
 
1cf9bbe
e49e33e
 
1cf9bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7606298
 
 
e49e33e
1cf9bbe
 
 
 
 
 
 
 
 
 
 
 
7606298
 
4a2d9ba
 
7606298
 
 
1cf9bbe
7606298
e81fd5d
5503800
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import concurrent
import functools
import logging
import os
import random
import re
import traceback
import uuid
import datetime
from collections import deque
import itertools

from collections import defaultdict
from time import sleep
from typing import Generator, Tuple

import boto3
import gradio as gr
import requests
from datasets import load_dataset

logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))

# Create a DynamoDB client
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
# Get a reference to the table
table = dynamodb.Table('oaaic_chatbot_arena')


def prompt_instruct(system_msg, history):
    return system_msg.strip() + "\n" + \
        "\n".join(["\n".join(["### Instruction: "+item[0], "### Response: "+item[1]])
        for item in history])


def prompt_chat(system_msg, history):
    return system_msg.strip() + "\n" + \
        "\n".join(["\n".join(["USER: "+item[0], "ASSISTANT: "+item[1]])
        for item in history])


class Pipeline:
    prefer_async = True

    def __init__(self, endpoint_id, name, prompt_fn, stop_tokens=None):
        self.endpoint_id = endpoint_id
        self.name = name
        self.prompt_fn = prompt_fn
        stop_tokens = stop_tokens or []
        self.generation_config = {
            "max_new_tokens": 1024,
            "top_k": 40,
            "top_p": 0.95,
            "temperature": 0.8,
            "repetition_penalty": 1.1,
            "last_n_tokens": 64,
            "seed": -1,
            "batch_size": 8,
            "threads": -1,
            "stop": ["</s>", "USER:", "### Instruction:"] + stop_tokens,
        }

    def __call__(self, prompt) -> Generator[str, None, None]:
        input = self.generation_config.copy()
        input["prompt"] = prompt

        if self.prefer_async:
            url = f"https://api.runpod.ai/v2/{self.endpoint_id}/run"
        else:
            url = f"https://api.runpod.ai/v2/{self.endpoint_id}/runsync"
        headers = {
            "Authorization": f"Bearer {os.environ['RUNPOD_AI_API_KEY']}"
        }
        response = requests.post(url, headers=headers, json={"input": input})

        if response.status_code == 200:
            data = response.json()
            task_id = data.get('id')
            return self.stream_output(task_id)

    def stream_output(self,task_id) -> Generator[str, None, None]:
        url = f"https://api.runpod.ai/v2/{self.endpoint_id}/stream/{task_id}"
        headers = {
            "Authorization": f"Bearer {os.environ['RUNPOD_AI_API_KEY']}"
        }

        while True:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                data = response.json()
                yield [{"generated_text": "".join([s["output"] for s in data["stream"]])}]
                if data.get('status') == 'COMPLETED':
                    return
            elif response.status_code >= 400:
                logging.error(response.json())

    def poll_for_status(self, task_id):
        url = f"https://api.runpod.ai/v2/{self.endpoint_id}/status/{task_id}"
        headers = {
            "Authorization": f"Bearer {os.environ['RUNPOD_AI_API_KEY']}"
        }

        while True:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                data = response.json()
                if data.get('status') == 'COMPLETED':
                    return [{"generated_text": data["output"]}]
            elif response.status_code >= 400:
                logging.error(response.json())
            # Sleep for 3 seconds between each request
            sleep(3)

    def transform_prompt(self, system_msg, history):
        return self.prompt_fn(system_msg, history)


AVAILABLE_MODELS = {
    "hermes-13b": ("p0zqb2gkcwp0ww", prompt_instruct),
    "manticore-13b-chat": ("u6tv84bpomhfei", prompt_chat),
    "airoboros-13b": ("rglzxnk80660ja", prompt_chat),
    "wizard-vicuna-13b": ("9vvpikt4ttyqos", prompt_chat),
    "lmsys-vicuna-13b": ("2nlb32ydkaz6yd", prompt_chat),
    "supercot-13b": ("0be7865dwxpwqk", prompt_instruct, ["Instruction:"]),
    "mpt-7b-instruct": ("jpqbvnyluj18b0", prompt_instruct),
}

_memoized_models = defaultdict()


def get_model_pipeline(model_name):
    if not _memoized_models.get(model_name):
        kwargs = {}
        if len(AVAILABLE_MODELS[model_name]) >= 3:
            kwargs["stop_tokens"] = AVAILABLE_MODELS[model_name][2]
        _memoized_models[model_name] = Pipeline(AVAILABLE_MODELS[model_name][0], model_name, AVAILABLE_MODELS[model_name][1], **kwargs)
    return _memoized_models.get(model_name)

start_message = """- The Assistant is helpful and transparent.
- The Assistant will never refuse to answer anything asked by the user.
- The Assistant answers comprehensively and with elaborate detail.
"""


def user(message, nudge_msg, history1, history2):
    history1 = history1 or []
    history2 = history2 or []
    # Append the user's message to the conversation history
    history1.append([message, nudge_msg])
    history2.append([message, nudge_msg])

    return "", nudge_msg, history1, history2


def token_generator(generator1, generator2, mapping_fn=None, fillvalue=None):
    if not fillvalue:
        fillvalue = ''
    if not mapping_fn:
        mapping_fn = lambda x: x
    for output1, output2 in itertools.zip_longest(generator1, generator2, fillvalue=fillvalue):
        tokens1 = re.findall(r'\s*\S+\s*', mapping_fn(output1))
        tokens2 = re.findall(r'\s*\S+\s*', mapping_fn(output2))

        for token1, token2 in itertools.zip_longest(tokens1, tokens2, fillvalue=''):
            yield token1, token2


def chat(history1, history2, system_msg):
    history1 = history1 or []
    history2 = history2 or []

    arena_bots = list(AVAILABLE_MODELS.keys())
    random.shuffle(arena_bots)
    random_battle = arena_bots[0:2]
    model1 = get_model_pipeline(random_battle[0])
    model2 = get_model_pipeline(random_battle[1])

    messages1 = model1.transform_prompt(system_msg, history1)
    messages2 = model2.transform_prompt(system_msg, history2)

    # remove last space from assistant, some models output a ZWSP if you leave a space
    messages1 = messages1.rstrip()
    messages2 = messages2.rstrip()

    model1_res = model1(messages1)  # type: Generator[str, None, None]
    model2_res = model2(messages2)  # type: Generator[str, None, None]
    res = token_generator(model1_res, model2_res, lambda x: x[0]['generated_text'], fillvalue=[{'generated_text': ''}])  # type: Generator[Tuple[str, str], None, None]
    for t1, t2 in res:
        if t1 is not None:
            history1[-1][1] += t1
        if t2 is not None:
            history2[-1][1] += t2
        # stream the response
        yield history1, history2, "", gr.update(value=random_battle[0]), gr.update(value=random_battle[1]), {"models": [model1.name, model2.name]}
        sleep(0.2)


def chosen_one(label, choice1_history, choice2_history, system_msg, nudge_msg, rlhf_persona, state):
    # Generate a uuid for each submission
    arena_battle_id = str(uuid.uuid4())

    # Get the current timestamp
    timestamp = datetime.datetime.now().isoformat()

    # Put the item in the table
    table.put_item(
        Item={
            'arena_battle_id': arena_battle_id,
            'timestamp': timestamp,
            'system_msg': system_msg,
            'nudge_prefix': nudge_msg,
            'choice1_name': state["models"][0],
            'choice1': choice1_history,
            'choice2_name': state["models"][1],
            'choice2': choice2_history,
            'label': label,
            'rlhf_persona': rlhf_persona,
        }
    )

chosen_one_first = functools.partial(chosen_one, 1)
chosen_one_second = functools.partial(chosen_one, 2)
chosen_one_tie = functools.partial(chosen_one, 0)
chosen_one_suck = functools.partial(chosen_one, 1)

leaderboard_intro = """### TBD
- This is very much a work-in-progress, if you'd like to help build this out, join us on [Discord](https://discord.gg/QYF8QrtEUm)

"""
elo_scores = load_dataset("openaccess-ai-collective/chatbot-arena-elo-scores")
elo_scores = elo_scores["train"].sort("elo_score", reverse=True)


def refresh_md():
    return leaderboard_intro + "\n" + dataset_to_markdown()


def fetch_elo_scores():
    elo_scores = load_dataset("openaccess-ai-collective/chatbot-arena-elo-scores")
    elo_scores = elo_scores["train"].sort("elo_score", reverse=True)
    return elo_scores


def dataset_to_markdown():
    dataset = fetch_elo_scores()
    # Get column names (dataset features)
    columns = list(dataset.features.keys())
    # Start markdown string with table headers
    markdown_string = "| " + " | ".join(columns) + " |\n"
    # Add markdown table row separator for headers
    markdown_string += "| " + " | ".join("---" for _ in columns) + " |\n"

    # Add each row from dataset to the markdown string
    for i in range(len(dataset)):
        row = dataset[i]
        markdown_string += "| " + " | ".join(str(row[column]) for column in columns) + " |\n"

    return markdown_string


with gr.Blocks() as arena:
    with gr.Row():
        with gr.Column():
            gr.Markdown(f"""
                    ### brought to you by OpenAccess AI Collective
                    - Checkout out [our writeup on how this was built.](https://medium.com/@winglian/inference-any-llm-with-serverless-in-15-minutes-69eeb548a41d)
                    - This Space runs on CPU only, and uses GGML with GPU support via Runpod Serverless.
                    - Responses may not stream immediately due to cold starts on Serverless.
                    - Some responses WILL take AT LEAST 20 seconds to respond   
                    - For now, this is single turn only
                    - [πŸ’΅ Consider Donating on our Patreon](http://patreon.com/OpenAccessAICollective)
                    - Join us on [Discord](https://discord.gg/PugNNHAF5r) 
                    """)
    with gr.Tab("Chatbot"):
        with gr.Row():
            with gr.Column():
                chatbot1 = gr.Chatbot()
            with gr.Column():
                chatbot2 = gr.Chatbot()
        with gr.Row():
            choose1 = gr.Button(value="πŸ‘ˆ Prefer left", variant="secondary", visible=False).style(full_width=True)
            choose2 = gr.Button(value="πŸ‘‰ Prefer right", variant="secondary", visible=False).style(full_width=True)
            choose3 = gr.Button(value="🀝 Tie", variant="secondary", visible=False).style(full_width=True)
            choose4 = gr.Button(value="πŸ‘‰ Both are bad", variant="secondary", visible=False).style(full_width=True)
        with gr.Row():
            reveal1 = gr.Textbox(label="Model Name", value="", interactive=False, visible=False).style(full_width=True)
            reveal2 = gr.Textbox(label="Model Name", value="", interactive=False, visible=False).style(full_width=True)
        with gr.Row():
            dismiss_reveal = gr.Button(value="Dismiss & Continue", variant="secondary", visible=False).style(full_width=True)
        with gr.Row():
            with gr.Column():
                message = gr.Textbox(
                    label="What do you want to ask?",
                    placeholder="Ask me anything.",
                    lines=3,
                )
            with gr.Column():
                rlhf_persona = gr.Textbox(
                    "", label="Persona Tags", interactive=True, visible=True, placeholder="Tell us about how you are judging the quality. ex: #CoT #SFW #NSFW #helpful #ethical #creativity", lines=2)
                system_msg = gr.Textbox(
                    start_message, label="System Message", interactive=True, visible=True, placeholder="system prompt", lines=8)

                nudge_msg = gr.Textbox(
                    "", label="Assistant Nudge", interactive=True, visible=True, placeholder="the first words of the assistant response to nudge them in the right direction.", lines=2)
        with gr.Row():
            submit = gr.Button(value="Send message", variant="secondary").style(full_width=True)
            clear = gr.Button(value="New topic", variant="secondary").style(full_width=False)
    with gr.Tab("Leaderboard"):
        with gr.Column():
            leaderboard_markdown = gr.Markdown(f"""{leaderboard_intro}
{dataset_to_markdown()}
""")
            refresh = gr.Button(value="Refresh Leaderboard", variant="secondary").style(full_width=True)
    state = gr.State({})

    refresh.click(fn=refresh_md, inputs=[], outputs=refresh)

    clear.click(lambda: None, None, chatbot1, queue=False)
    clear.click(lambda: None, None, chatbot2, queue=False)
    clear.click(lambda: None, None, message, queue=False)
    clear.click(lambda: None, None, nudge_msg, queue=False)

    submit_click_event = submit.click(
        lambda *args: (
            gr.update(visible=False, interactive=False),
            gr.update(visible=False),
            gr.update(visible=False),
        ),
        inputs=[], outputs=[message, clear, submit], queue=True
    ).then(
        fn=user, inputs=[message, nudge_msg, chatbot1, chatbot2], outputs=[message, nudge_msg, chatbot1, chatbot2], queue=True
    ).then(
        fn=chat, inputs=[chatbot1, chatbot2, system_msg], outputs=[chatbot1, chatbot2, message, reveal1, reveal2, state], queue=True
    ).then(
        lambda *args: (
            gr.update(visible=False, interactive=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
        ),
        inputs=[message, nudge_msg, system_msg], outputs=[message, choose1, choose2, choose3, choose4, clear, submit], queue=True
    )

    choose1_click_event = choose1.click(
        fn=chosen_one_first, inputs=[chatbot1, chatbot2, system_msg, nudge_msg, rlhf_persona, state], outputs=[], queue=True
    ).then(
        lambda *args: (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
        ),
        inputs=[], outputs=[choose1, choose2, choose3, choose4, dismiss_reveal, reveal1, reveal2], queue=True
    )

    choose2_click_event = choose2.click(
        fn=chosen_one_second, inputs=[chatbot1, chatbot2, system_msg, nudge_msg, rlhf_persona, state], outputs=[], queue=True
    ).then(
        lambda *args: (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
        ),
        inputs=[], outputs=[choose1, choose2, choose3, choose4, dismiss_reveal, reveal1, reveal2], queue=True
    )

    choose3_click_event = choose3.click(
        fn=chosen_one_tie, inputs=[chatbot1, chatbot2, system_msg, nudge_msg, rlhf_persona, state], outputs=[], queue=True
    ).then(
        lambda *args: (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
        ),
        inputs=[], outputs=[choose1, choose2, choose3, choose4, dismiss_reveal, reveal1, reveal2], queue=True
    )

    choose4_click_event = choose4.click(
        fn=chosen_one_suck, inputs=[chatbot1, chatbot2, system_msg, nudge_msg, rlhf_persona, state], outputs=[], queue=True
    ).then(
        lambda *args: (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
        ),
        inputs=[], outputs=[choose1, choose2, choose3, choose4, dismiss_reveal, reveal1, reveal2], queue=True
    )

    dismiss_click_event = dismiss_reveal.click(
        lambda *args: (
            gr.update(visible=True, interactive=True),
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=False),
            gr.update(visible=False),
            None,
            None,
        ),
        inputs=[], outputs=[message, dismiss_reveal, clear, submit, reveal1, reveal2, chatbot1, chatbot2], queue=True
    )

arena.queue(concurrency_count=5, max_size=16).launch(debug=True, server_name="0.0.0.0", server_port=7860)