Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathcreate_data.py
More file actions
Latest commit
77 lines (61 loc) · 2.2 KB
/
Copy pathcreate_data.py
File metadata and controls
77 lines (61 loc) · 2.2 KB
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
importtyper
importjson
fromtransformersimportConversation
fromtyping_extensionsimportAnnotated
importhttpx
importtqdm
importasyncio
app=typer.Typer()
client=httpx.AsyncClient(timeout=None)
asyncdefrun(conv: Conversation, url: str):
payload= {"model":"tgi", "messages": conv.messages}
response=awaitclient.post(url, json=payload)
content=response.json()
message=content["choices"][0]["message"]
message.pop("name", None)
conv.add_message(message)
deffix_source(source):
ifsourceandsource[0]["from"] =="gpt":
# Skip if GPT is first to talk
source=source[1:]
new_source= []
foriteminsource:
role="assistant"ifitem["from"] =="gpt"else"user"
content=item["value"]
new_source.append({"role": role, "content": content})
returnnew_source
asyncdefrecreate_conversation(conversation, sem, url):
asyncwithsem:
conv=Conversation()
try:
formessageinconversation[::2]:
assertmessage["role"] =="user"
conv.add_message(message)
awaitrun(conv, url)
exceptExceptionase:
print(e)
pass
returnconv.messages
@app.command()
defmain(
*,
input_filename: Annotated[str, typer.Option("--input-filename")],
output_filename: Annotated[str, typer.Option("--output-filename")],
url: Annotated[str, typer.Option("--url")] ="http://localhost:8080/v1/chat/completions",
concurrency: Annotated[int, typer.Option("--concurrency")] =64
):
sem=asyncio.Semaphore(concurrency)
asyncdef_main():
withopen(input_filename, "r") asf:
input_data=json.loads(f.read())
conversations= [fix_source(source["conversations"]) forsourceininput_data]
futures= []
forconversationinconversations:
future=recreate_conversation(conversation, sem, url)
futures.append(future)
recreated_conversations=awaittqdm.asyncio.tqdm.gather(*futures)
withopen(output_filename, "w") asf:
json.dump(recreated_conversations, f, indent=4)
asyncio.run(_main())
if__name__=="__main__":
app()