-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge.py
More file actions
66 lines (54 loc) · 2.16 KB
/
merge.py
File metadata and controls
66 lines (54 loc) · 2.16 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
'''
Code for merging parse results using GPT.
This turned out to not work very well (GPT has issues with merging) and so is currently unused by any active code paths.
The gap might be addressable through some prompt engineering; we can revisit at a future point.
'''
import asyncio
import json
import gpt
from tasks import create_task_of_tasks
from utils import log_msg
def __group_parse_results(parse_results):
grouped = []
while len(parse_results):
to_group = parse_results[:3]
grouped.append('\n---\n'.join(to_group))
parse_results = parse_results[3:]
return grouped
async def async_merge_with_gpt(parse_results, model="gpt-3.5-turbo"):
log_msg('Sending connection heartbeat')
yield ' '
grouped_parse_results = __group_parse_results(parse_results)
# Merge groups of parse results cumulatively until we're ready for a final merge
while len(grouped_parse_results) > 1:
merge_work_fn = lambda merge_group: gpt.async_fetch_merge(merge_group, model=model, skip_on_error=True)
all_merging = create_task_of_tasks(
task_inputs=grouped_parse_results,
work_fn=merge_work_fn,
task_label='Merge'
)
merge_results = []
while True:
if all_merging.done():
merge_results = all_merging.result()
break
log_msg('Sending connection heartbeat')
yield ' '
await asyncio.sleep(10)
# Potentially redo grouping and merging for long lists
grouped_parse_results = __group_parse_results(merge_results)
log_msg('All preliminary merges complete')
all_parsed_text = grouped_parse_results[0]
log_msg('Fetching final merge of all parse outputs')
log_msg(all_parsed_text)
merge_task = asyncio.create_task(gpt.async_fetch_merge(all_parsed_text, model=model))
result = ''
while True:
if merge_task.done():
result = merge_task.result()
break
log_msg('Sending connection heartbeat')
yield ' '
await asyncio.sleep(10)
log_msg('Final merge complete, resturning result')
yield json.dumps({"translation": result})