{"id":28098790,"url":"https://github.com/rmanluo/llm-facteval","last_synced_at":"2025-05-13T17:58:50.792Z","repository":{"id":205015557,"uuid":"711657292","full_name":"RManLuo/llm-facteval","owner":"RManLuo","description":"Source code of paper \"Systematic Assessment of Factual Knowledge in Large Language Models\" - EMNLP Findings 2023","archived":false,"fork":false,"pushed_at":"2023-11-18T13:30:30.000Z","size":75,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-23T00:05:23.376Z","etag":null,"topics":["evaluation","knowledge-graph","llm","trustfulness"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RManLuo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-29T23:05:16.000Z","updated_at":"2024-05-22T15:27:08.000Z","dependencies_parsed_at":"2023-11-18T15:14:19.466Z","dependency_job_id":null,"html_url":"https://github.com/RManLuo/llm-facteval","commit_stats":null,"previous_names":["rmanluo/llm-facteval"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RManLuo%2Fllm-facteval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RManLuo%2Fllm-facteval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RManLuo%2Fllm-facteval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RManLuo%2Fllm-facteval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RManLuo","download_url":"https://codeload.github.com/RManLuo/llm-facteval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253999903,"owners_count":21997352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evaluation","knowledge-graph","llm","trustfulness"],"created_at":"2025-05-13T17:58:50.589Z","updated_at":"2025-05-13T17:58:50.781Z","avatar_url":"https://github.com/RManLuo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LLM-facteval\nSource code of paper [\"Systematic Assessment of Factual Knowledge in Large Language Models\"](https://arxiv.org/abs/2310.11638) - EMNLP Findings 2023\n\n\nOur framework contains four main components:\n- `kg`: declare how to read and preprocess knowledge graph\n- `extractor`: to extract question triplets, nodes and relation summary from the knowledge graph.\n- `generator`: to generate questions/answers from extracted triplets\n- `evaluator`: to evaluate LLM's response\nCheck `certlm/registry.py` for the list of supported extractors, generators and evaluators\n\n## Reproducibility\nPlease check [this document](scripts/README.md) for steps to reproduce our experiments in the paper.\n\n## Adding new KGs to the framework\n### Knowledge Graph (KG)\nFor a new KG dataset, it should extend the `certlm.kg_dataset.BaseKG` class and implement following methods:\n- `load_relation()`: load all available relations to `self.relations`\n- `get_input_relation_file(relation)`: return path to input relation file for a given relation\n- `get_relation_name(relation)`: get relation label\n- `get_relation_type(relation)`: return relation type: 1-1, N-1, N-M\n\nCurrent supported KG:\n- [LAMA](https://arxiv.org/pdf/1909.01066.pdf)\n  - trex\n  - google_re\n- [BioLAMA](https://arxiv.org/pdf/2110.08173.pdf)\n  - umls\n  - wiki-bio\n    \nThe preprocessed data can be found [here](https://drive.google.com/drive/folders/1Y17Hcnh9bjmOrxfU-R2ktgu444QQh9_2?usp=sharing).\n### T-REx\nExample of T-REx relation\n```json\n{\n  \"relation\": \"P19\",\n  \"template\": \"[X] was born in [Y]\",\n  \"label\": \"place of birth\",\n  \"description\": \"most specific known (e.g. city instead of country, or hospital instead of city) birth location of a person, animal or fictional character\",\n  \"type\": \"N-1\"\n}\n\n```\n\n### Extractor\nA triplet extractor for a KG should extend the `certlm.extractors.BaseExtractor` class and implement following methods:\n- `extract_relation(relation, relation_input_file, output)`: extract relation data stored in `relation_input_file` and save to `output` file. In addition to the `output` file, a relation summary file is also created to index all the subject, object nodes which will be useful for evaluating N-M relations.\n- `get_input_question_files(data_dir, relation)`: return path to questions and summary extracted from the given relation\n\n**Note that, we should standardize the relation info to follow this format for reusability**\n\nThe question output is a jsonl file where each line is a json with following structure:\n```json\n {\n    \"subject_label\": subject_label,\n    \"object_label\": object_label,\n    \"object_uri\": object_uri,\n    \"subject_uri\": subject_uri,\n    \"relation_info\": {\n      \"relation\": \"P19\",\n      \"template\": \"[X] was born in [Y]\",\n      \"label\": \"place of birth\",\n      \"description\": \"most specific known (e.g. city instead of country, or hospital instead of city) birth location of a person, animal or fictional character\",\n      \"type\": \"N-1\",\n      \"subject_symbol\": \"[X]\",\n      \"object_symbol\": \"[Y]\"\n    }\n}\n```\n\nThe relation summary should have following structure\n\n```json\n{\n  \"relation_info\":  {\n      \"relation\": \"P19\",\n      \"template\": \"[X] was born in [Y]\",\n      \"label\": \"place of birth\",\n      \"description\": \"most specific known (e.g. city instead of country, or hospital instead of city) birth location of a person, animal or fictional character\",\n      \"type\": \"N-1\",\n      \"subject_symbol\": \"[X]\",\n      \"object_symbol\": \"[Y]\"\n    },\n  \"node_summary\": {\n    \"objects\": {\n      \"uri1\": {\n        \"object_label\": object_label,\n        \"object_uri\": object_uri,\n        \"subjects\": [array of subject uri]\n      },\n      ...\n    }\n  },\n  \"subjects\": {\n      \"uri1\": {\n        \"subject_label\": subject_label,\n        \"subject_uri\": subject_uri,\n        \"objects\": [array of object uri]\n      },\n      ...\n    }\n  }\n}\n```\n\nExample of running command\n```bash\npython run_certlm.py --step extract \\\n    --kg trex \\\n    --data-dir ./examples/TREx \\\n    --data-file relations.jsonl \\\n    --output-dir ./output  \n```\n### Generator\nWe support the following generator: `template`, `llm-mask`, `llm-question`. Each generator needs to implement the following methods\n- `generate_questions(triplet_input_file, relation_summary_file, output_file)`\n\n```python\n# question\n    prompts = [\n            {\"role\": \"system\", \"content\": prompt},\n            {\"role\": \"user\", \"content\": content}\n    ]\n    expected_answers = [{\"uri\": a[0], \"label\": a[1]} for a in answers]\n    question_record = {\n        \"question_id\": question_id,\n        \"prompts\": prompts,\n        \"answers\": expected_answers,\n    }\n    json_record = json.dumps(question_record)\n    fout.write(json_record + '\\n')\n```\n\nExample of running command\n```bash\npython run_certlm.py --step question_generate \\\n    --kg trex \\\n    --generator masking \\\n    --data-dir ./examples/TREx \\\n    --data-file relations.jsonl \\\n    --gen-input-dir ./output/tuples \n```\n\n# Citation\nIf this repo is useful for your own research, please cite us with the following bibtex entry\n```\n@article{luo2023systematic,\n  title={Systematic Assessment of Factual Knowledge in Large Language Models},\n  author={Luo, Linhao and Vu, Thuy-Trang and Phung, Dinh and Haffari, Gholamreza},\n  journal={Findings of EMNLP},\n  year={2023}\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frmanluo%2Fllm-facteval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frmanluo%2Fllm-facteval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frmanluo%2Fllm-facteval/lists"}