(chunk_key_dp,use_llm_func)
| 119 | context_entities = {key[0]: list(x[0].keys()) for key, x in zip(ordered_chunks, entity_results)} |
| 120 | already_processed = 0 |
| 121 | async def _process_single_content_relation(chunk_key_dp,use_llm_func): # for each chunk, run the func |
| 122 | nonlocal already_processed, already_entities, already_relations |
| 123 | chunk_key = chunk_key_dp[0] |
| 124 | content = chunk_key_dp[1] |
| 125 | entity_extract_prompt = PROMPTS["entity_extraction"] # give 3 examples in the prompt context |
| 126 | relation_extract_prompt = PROMPTS["relation_extraction"] |
| 127 | continue_prompt = PROMPTS["entiti_continue_extraction"] # means low quality in the last extraction |
| 128 | if_loop_prompt = PROMPTS["entiti_if_loop_extraction"] |
| 129 | entities = context_entities[chunk_key] |
| 130 | context_base_relation = dict( |
| 131 | tuple_delimiter=PROMPTS["DEFAULT_TUPLE_DELIMITER"], |
| 132 | record_delimiter=PROMPTS["DEFAULT_RECORD_DELIMITER"], |
| 133 | completion_delimiter=PROMPTS["DEFAULT_COMPLETION_DELIMITER"], |
| 134 | entities=",".join(entities) |
| 135 | ) |
| 136 | entity_extract_max_gleaning=1 |
| 137 | hint_prompt = relation_extract_prompt.format(**context_base_relation, input_text=content) # fill in the parameter |
| 138 | final_result = await use_llm_func(hint_prompt) # feed into LLM with the prompt |
| 139 | |
| 140 | history = pack_user_ass_to_openai_messages(hint_prompt, final_result) # set as history |
| 141 | for now_glean_index in range(entity_extract_max_gleaning): |
| 142 | glean_result = await use_llm_func(continue_prompt, history_messages=history) |
| 143 | |
| 144 | history += pack_user_ass_to_openai_messages(continue_prompt, glean_result) # add to history |
| 145 | final_result += glean_result |
| 146 | if now_glean_index == entity_extract_max_gleaning - 1: |
| 147 | break |
| 148 | |
| 149 | if_loop_result: str = await use_llm_func( # judge if we still need the next iteration |
| 150 | if_loop_prompt, history_messages=history |
| 151 | ) |
| 152 | if_loop_result = if_loop_result.strip().strip('"').strip("'").lower() |
| 153 | if if_loop_result != "yes": |
| 154 | break |
| 155 | |
| 156 | records = split_string_by_multi_markers( # split entities from result --> list of entities |
| 157 | final_result, |
| 158 | [context_base_relation["record_delimiter"], context_base_relation["completion_delimiter"]], |
| 159 | ) |
| 160 | # resolve the entities |
| 161 | maybe_nodes = defaultdict(list) |
| 162 | maybe_edges = defaultdict(list) |
| 163 | for record in records: |
| 164 | record = re.search(r"\((.*)\)", record) |
| 165 | if record is None: |
| 166 | continue |
| 167 | record = record.group(1) |
| 168 | record_attributes = split_string_by_multi_markers( # split entity |
| 169 | record, [context_base_relation["tuple_delimiter"]] |
| 170 | ) |
| 171 | if_entities = await _handle_single_entity_extraction( # get the name, type, desc, source_id of entity--> dict |
| 172 | record_attributes, chunk_key |
| 173 | ) |
| 174 | if if_entities is not None: |
| 175 | maybe_nodes[if_entities["entity_name"]].append(if_entities) |
| 176 | continue |
| 177 | |
| 178 | if_relation = await _handle_single_relationship_extraction( |
no test coverage detected