该论文提出了一种新的框架,称为基于链式思维的实体关系分析(ERA-CoT),旨在解决涉及多个实体的复杂场景中的推理任务。通过提取文本中的所有实体及其显式关系,并基于这些关系和隐藏信息推断隐式关系,ERA-CoT显著提高了大语言模型(LLMs)的推理能力和问题回答的准确性。实验结果表明,ERA-CoT在各种基准测试中均优于现有的链式思维提示方法,在GPT-3.5上平均提升了5.1%的准确率。 本篇在论文代码的基础上增加了llama2模型的相关文件,修改了模型相关代码。
论文中提出的ERA-CoT框架包含五个步骤:
利用大型语言模型(LLMs)的信息提取能力,从文本中提取所有实体及其类型。具体来说,模型接受一个输入句子,利用其命名实体识别(NER)功能预测相应的实体范围和分类。为了提高实体提取的准确性,采用了自一致性(Self-Consistency, SC)方法,多次验证实体提取结果,确保提取的实体可靠。
在零样本设置下,探讨不同实体之间的显式关系。通过LLM的上下文理解能力,从文本中直接提取实体对之间的关系,生成关系三元组。同样地,使用SC方法评估显式关系的可靠性。
基于显式关系和文本中的隐藏信息推断实体之间的隐式关系。通过生成多个可能的隐式关系,并使用模型进行评分,确定这些关系的可靠性。具体来说,通过分析上下文中的隐含信息,推断出未显式提到但可能存在的实体关系。
使用模型对隐式关系的可靠性进行评分,设定阈值筛除低于阈值的隐式关系。这一步骤确保了最终关系集的高质量和准确性。
基于提取的实体以及获得的显式和隐式关系回答问题。在这个过程中,模型利用之前提取和过滤的关系信息,提供更准确和详细的答案。 通过这些步骤,ERA-CoT框架在处理复杂实体场景中的推理任务时展示了强大的性能和准确性提升.
论文通过在六个广泛采用的数据集上进行实验,验证了ERA-CoT的有效性,并与四种基线方法进行了对比。结果表明,ERA-CoT在几乎所有基准测试中均表现出色,平均提升了大约5.1%的准确率。在GPT-3.5和Llama-2两种大语言模型上,ERA-CoT在常识推理、数学推理和逻辑推理三种类型的问题上均表现出显著提升,表明增强模型的实体关系理解能力能够显著提高推理能力和问题回答的准确性。
安装成功
实体提取:
import json
# from config import args
prompt_prefix = '''Given a sentence, possible entities may include:'''
prompt_suffix = ''', Find all entities based on the provided sentence.'''
def get_ner_list(type_list_file):
try:
f = open(type_list_file, "r", encoding="utf-8")
entities = "["
for idx, entity in enumerate(f):
entities = entities + entity[:-1] + ","
entities = entities[:-1] + "]"
return entities
except FileNotFoundError as e:
raise FileNotFoundError('can\'t find the demo file: {}'.format(type_list_file))
def get_ner_prompt(type_list_file):
ner_prompt = prompt_prefix + get_ner_list(type_list_file) + prompt_suffix
return ner_prompt
def ner_sentence(ner_prompt, sentence):
prompt = ner_prompt + "\nSentence: " + sentence + "\nEntity: "
return prompt
关系提取:
import json
import logging
# from config import args
prompt_prefix = '''Given a sentence, and all entities within the sentence.
Extract all relationships between entities which directly stated in the sentence.
Every relationship stated as a triple: (E_A, E_B, Relation).\nSentence: '''
prompt_suffix = '''\nRelation: '''
def get_extract_prompt(entities, sent):
relation_prompt = prompt_prefix + sent + "\nEntities: " + entities + prompt_suffix
return relation_prompt
关系推理:
import json
import logging
# from config import args
prompt_prefix = '''Given a sentence, all entities, and all explicit relationships within the sentence.
Infer all possible implicit relationships between entities.
For each pair of entities, infer up to '''
prompt_mid = ''' implicit relationships.
Every relationship stated as a triple: (E_A, E_B, Relation)\nSentence: '''
prompt_suffix = '''\nRelation: '''
def get_infer_num(args):
return args.infer_num
def get_infer_prompt(args, entities, relation_ext, sent):
relation_prompt = (prompt_prefix + get_infer_num(args) + prompt_mid
+ sent + "\nExplicit Relationships:: " + relation_ext
+ "\nEntities: " + entities + prompt_suffix)
return relation_prompt