StructGPT: 구조화 데이터 위에서 LLM이 추론할 수 있게 하는 범용 Framework
StructGPT: A General Framework for Large Language Model to Reason over Structured Data
TL;DR Highlight
Knowledge Graph, 테이블, DB에서 LLM이 직접 데이터를 읽고 추론하도록 전용 인터페이스를 붙여주는 프레임워크
Who Should Read
Knowledge Graph나 관계형 DB를 LLM과 연동해서 QA 시스템을 만들려는 백엔드/ML 개발자. 구조화 데이터를 LLM 프롬프트에 어떻게 넣을지 고민 중인 분.
Core Mechanics
- LLM이 구조화 데이터(KG, 테이블, DB)를 직접 이해하기 어렵다는 문제를 '전용 인터페이스 + 반복 읽기-추론' 루프로 해결
- 구조화 데이터를 블랙박스로 보고, 각 타입별 데이터 접근 함수(예: Extract_Neighbor_Relations, Extract_Columns)를 API처럼 설계
- 매 반복마다 인터페이스 호출 → 결과 텍스트 변환(linearization) → LLM 생성 순서로 동작해서, 한 번에 전체 데이터를 넣지 않아도 됨
- ChatGPT(gpt-3.5-turbo)와 Davinci-003(text-davinci-003) 두 LLM 모두에 붙여서 zero-shot / few-shot 실험 진행
- 별도 파인튜닝 없이 full-data supervised 모델과 근접한 성능을 달성
- 에러 분석 결과, KGQA는 relation 선택 실패(74%), Text-to-SQL은 추론 오류(63%)가 주요 병목으로 확인됨
Evidence
- KGQA WebQSP에서 ChatGPT zero-shot 61.2 → StructGPT 72.6 (+11.4% Hits@1)
- TableQA TabFact에서 ChatGPT zero-shot 82.9 → StructGPT few-shot 87.6 (+4.7% accuracy)
- Text-to-SQL Spider에서 ChatGPT zero-shot 70.1 → StructGPT few-shot 77.8 (+7.7% execution accuracy)
- ChatGPT August 버전에서도 WebQSP 62.1→75.3, WTQ 41.1→50.4, Spider 75.2→77.1로 버전 무관하게 일관된 향상 확인
How to Apply
- DB 연동 QA를 만드는 경우: Extract_Table&Column_Name → 관련 테이블 선택 → Extract_Tables_Information → SQL 생성 순서로 LLM 호출을 체인처럼 구성하면 됨. 전체 스키마를 한 번에 프롬프트에 쑤셔넣지 않아도 됨.
- Knowledge Graph 기반 검색 시스템을 구축할 때: 엔티티의 이웃 관계만 먼저 뽑아 LLM에 넘겨 필터링하고, 선택된 관계로 트리플을 추출하는 2단계 호출로 LLM 컨텍스트 낭비를 줄일 수 있음.
- 테이블 QA가 필요한 경우: 컬럼명 → 관련 컬럼 내용 → 서브테이블 순으로 LLM이 점진적으로 범위를 좁히게 하면 긴 테이블도 컨텍스트 초과 없이 처리 가능.
Code Example
# StructGPT 스타일 KG 추론 체인 예시 (pseudo-code)
def structgpt_kgqa(question, topic_entity, kg):
# Step 1: 이웃 관계 추출
relations = kg.extract_neighbor_relations(topic_entity)
linearized = ', '.join(relations)
prompt1 = f"""
The candidate relations of '{topic_entity}': {linearized}.
The question is: {question}
Provide only one relevant relation that's present in the candidates.
Relevant relation:"""
selected_relation = llm_generate(prompt1).strip()
# Step 2: 해당 관계의 트리플 추출
triples = kg.extract_triples(topic_entity, [selected_relation])
linearized_triples = '; '.join([f'({e}, {r}, {t})' for e, r, t in triples])
prompt2 = f"""
The triples are: {linearized_triples}
Based on these triples, answer the question: {question}
Provide only one answer entity.
Answer:"""
answer = llm_generate(prompt2).strip()
return answer
# Text-to-SQL 체인 예시
def structgpt_text2sql(question, db):
# Step 1: 전체 테이블/컬럼명 추출
schema_summary = db.extract_table_column_names()
prompt1 = f"""
Here are the SQLite tables: {schema_summary}
Question: {question}
Which tables do you need to complete the SQLite SQL query?
Tables:"""
selected_tables = llm_generate(prompt1).strip().split(', ')
# Step 2: 선택된 테이블 상세 정보(FK 포함) 추출
table_info = db.extract_tables_information(selected_tables)
prompt2 = f"""
### SQLite tables with their properties:
{table_info}
### Question: {question}
### Complete SQLite SQL query only with no explanation:
SELECT"""
sql = 'SELECT ' + llm_generate(prompt2).strip()
return sqlTerminology
Related Resources
Original Abstract (Expand)
In this paper, we study how to improve the zero-shot reasoning ability of large language models~(LLMs) over structured data in a unified way. Inspired by the study on tool augmentation for LLMs, we develop an \emph{Iterative Reading-then-Reasoning~(IRR)} approach for solving question answering tasks based on structured data, called \textbf{StructGPT}. In our approach, we construct the specialized function to collect relevant evidence from structured data (\ie \emph{reading}), and let LLMs concentrate the reasoning task based on the collected information (\ie \emph{reasoning}). Specially, we propose an \emph{invoking-linearization-generation} procedure to support LLMs in reasoning on the structured data with the help of the external interfaces. By iterating this procedures with provided interfaces, our approach can gradually approach the target answer to a given query. Extensive experiments conducted on three types of structured data demonstrate the effectiveness of our approach, which can significantly boost the performance of ChatGPT and achieve comparable performance against the full-data supervised-tuning baselines. Our codes and data are publicly available at~\url{https://github.com/RUCAIBox/StructGPT}.