in src/models/struxgpt_v2.py [0:0]
def convert_struct_output(self, struct_item: Union[StructItem, str],
hi:int = 0, fi:int = 1, max_length=None) -> List[str]:
struct_item = self.check_struct_item(struct_item, StructItem)
if not struct_item.aspects[0].get_descs():
struct_item.set_aspect_descs()
if max_length is None:
return [struct_item.convert_struct_output(hi=hi, fi=fi)]
def _get_aspect_len(ai) -> int:
return self.count_token(struct_item.aspects[ai].get_descs(merge=True))
## Dual Pointer && DFS
res = []
a1 = 0
aspect_num = len(struct_item.aspects)
assert aspect_num > 0
while a1 < aspect_num:
curr_len = _get_aspect_len(a1)
if curr_len > max_length * 1.5 and len(struct_item.aspects[a1].subaspects):
_struct_item = struct_item.aspects[a1].upgrad_to_struct(prefix=f'{struct_item.scope} ')
res.extend(self.convert_struct_output(_struct_item, hi=hi, fi=fi, max_length=max_length))
a1 += 1
continue
## initialize a2 in each step, incase a1 = aspect_num - 1
a2 = a1 + 1
for a2 in range(a1 + 1, aspect_num):
## a2 is unreachable
next_len = _get_aspect_len(a2)
curr_len += next_len
if curr_len > max_length:
break
struct_tmp = deepcopy(struct_item)
struct_tmp.aspects = struct_tmp.aspects[a1:a2]
res.append(struct_tmp.convert_struct_output(hi=hi, fi=fi))
a1 = a2
return res