224 lines
5.1 KiB
Markdown
Executable File
224 lines
5.1 KiB
Markdown
Executable File
# LLM关系抽取实现总结
|
|
|
|
## PlantConnectome的方法
|
|
|
|
### 1. 预处理阶段 (离线)
|
|
- **工具**: GPT-4o
|
|
- **输入**: 71,000篇论文 (abstract + fulltext)
|
|
- **输出**: 3,207,996个关系三元组
|
|
- **存储**: MongoDB + CSV
|
|
|
|
### 2. 查询阶段 (在线)
|
|
- **工具**: GPT-4o-mini
|
|
- **功能**: 关系验证、同义词提取
|
|
- **Prompt示例**:
|
|
```python
|
|
prompt = f"""
|
|
You are a scientific expert in molecular biology...
|
|
Validate whether a given source-target interaction is correctly derived...
|
|
|
|
**Scientific Text:**
|
|
{text}
|
|
|
|
**Proposed Relationship:**
|
|
- Source: {source}
|
|
- Interaction: {interaction}
|
|
- Target: {target}
|
|
|
|
Respond in structured format:
|
|
- Validation Status: (✅ Correct / ❓ Uncertain / ❌ Incorrect)
|
|
- Supporting Evidence: Quote exact sentences...
|
|
- Explanation: Justify the assessment...
|
|
"""
|
|
```
|
|
|
|
### 3. 核心特点
|
|
- **实体消歧**: 使用GPT-4o进行实体标准化
|
|
- **关系分类**: 9大类,788个关系词
|
|
- **证据溯源**: 每个关系都有原文证据
|
|
- **置信度评分**: 验证时给出正确/不确定/错误
|
|
|
|
---
|
|
|
|
## 我们的实现
|
|
|
|
### 已实现
|
|
|
|
#### 1. 规则抽取器 (llm_extractor_simple.py)
|
|
```python
|
|
class LLMRelationExtractor:
|
|
def extract_relations(self, text, gene_id):
|
|
# 规则1: 表达关系
|
|
pattern: r'highly expressed in ([\w\s]+)'
|
|
→ relation: highly_expressed_in
|
|
|
|
# 规则2: 功能关系
|
|
keyword: 'transporter' + 'sucrose'
|
|
→ relation: transports
|
|
|
|
# 规则3: 定位关系
|
|
keyword: 'membrane'
|
|
→ relation: localizes_to
|
|
|
|
# 规则4: 调控关系
|
|
pattern: r'regulates ([\w\s]+)'
|
|
→ relation: regulates
|
|
```
|
|
|
|
#### 2. 抽取结果
|
|
- **表达关系**: 9个 (highly_expressed_in)
|
|
- **其他关系**: 0个 (文本太短)
|
|
|
|
### 问题分析
|
|
|
|
#### 问题1: chunks文本太短
|
|
```
|
|
SlSUT2 chunk: 125字符
|
|
- 只有基本信息,没有描述性文本
|
|
- 无法抽取关系
|
|
|
|
SlSUT1 chunk: 514字符
|
|
- 有Expression Profile
|
|
- 成功抽取表达关系
|
|
```
|
|
|
|
#### 问题2: 缺少原始论文文本
|
|
- PC使用完整论文文本 (abstract + fulltext)
|
|
- 我们只用gene-centric chunks
|
|
- chunks是摘要,缺少详细描述
|
|
|
|
#### 问题3: 没有使用真正的LLM
|
|
- 当前使用规则匹配
|
|
- 需要OpenAI API key使用GPT-4o
|
|
- 规则无法处理复杂句式
|
|
|
|
---
|
|
|
|
## 解决方案
|
|
|
|
### 方案A: 使用原始论文文本 (推荐)
|
|
|
|
```python
|
|
# 从V12.1提取的章节文本中抽取
|
|
sections = {
|
|
"Expression Analysis": "...详细表达描述...",
|
|
"Functional Characterization": "...功能描述...",
|
|
"Subcellular Localization": "...定位描述..."
|
|
}
|
|
|
|
# 使用LLM抽取
|
|
for section_name, text in sections.items():
|
|
relations = llm_extractor.extract(text, gene_id)
|
|
```
|
|
|
|
### 方案B: 使用真正的LLM (GPT-4o)
|
|
|
|
```python
|
|
import openai
|
|
|
|
client = openai.OpenAI(api_key=api_key)
|
|
|
|
prompt = f"""
|
|
从以下文本中提取基因关系:
|
|
{text}
|
|
|
|
基因: {gene_id}
|
|
|
|
输出JSON格式:
|
|
[
|
|
{{
|
|
"source": "{gene_id}",
|
|
"relationship": "expressed_in",
|
|
"target": "root",
|
|
"target_type": "tissue",
|
|
"evidence": "..."
|
|
}}
|
|
]
|
|
"""
|
|
|
|
response = client.chat.completions.create(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": prompt}],
|
|
temperature=0
|
|
)
|
|
```
|
|
|
|
### 方案C: 结合PC的预训练模型
|
|
|
|
```python
|
|
# 使用PC的320万关系作为训练数据
|
|
# 微调自己的抽取模型
|
|
# 或使用PC的关系验证API
|
|
```
|
|
|
|
---
|
|
|
|
## 实施建议
|
|
|
|
### 短期 (今天)
|
|
1. 从V12.1的完整章节文本抽取
|
|
2. 使用规则+简单LLM (如果API可用)
|
|
|
|
### 中期 (本周)
|
|
1. 获取OpenAI API key
|
|
2. 实现完整的GPT-4o抽取
|
|
3. 批量处理所有基因
|
|
|
|
### 长期 (本月)
|
|
1. 构建自己的抽取模型
|
|
2. 结合PC的关系验证
|
|
3. 持续优化Prompt
|
|
|
|
---
|
|
|
|
## 关键代码
|
|
|
|
### 完整的LLM抽取器
|
|
```python
|
|
class PlantConnectomeStyleExtractor:
|
|
def __init__(self, api_key):
|
|
self.client = openai.OpenAI(api_key=api_key)
|
|
|
|
def extract(self, text, gene_id):
|
|
prompt = self._build_prompt(text, gene_id)
|
|
response = self.client.chat.completions.create(
|
|
model="gpt-4o-mini",
|
|
messages=[
|
|
{"role": "system", "content": "You are a precise scientific information extractor."},
|
|
{"role": "user", "content": prompt}
|
|
],
|
|
temperature=0.0,
|
|
response_format={"type": "json_object"}
|
|
)
|
|
return json.loads(response.choices[0].message.content)
|
|
|
|
def _build_prompt(self, text, gene_id):
|
|
return f"""Extract gene relationships from:
|
|
|
|
Gene: {gene_id}
|
|
Text: {text}
|
|
|
|
Output JSON with fields:
|
|
- source: gene name
|
|
- relationship: one of [expressed_in, transports, regulates, ...]
|
|
- target: related entity
|
|
- target_type: [tissue, metabolite, process, ...]
|
|
- evidence: exact quote from text
|
|
- confidence: 0.0-1.0
|
|
"""
|
|
```
|
|
|
|
---
|
|
|
|
## 总结
|
|
|
|
| 维度 | PlantConnectome | 我们的实现 |
|
|
|------|-----------------|-----------|
|
|
| 方法 | GPT-4o处理71K论文 | 规则匹配40基因 |
|
|
| 规模 | 320万关系 | 107关系 |
|
|
| 精度 | 高 (LLM理解) | 中 (规则匹配) |
|
|
| 成本 | 高 (API费用) | 低 (本地规则) |
|
|
| 可扩展 | 好 (批量API) | 好 (无需API) |
|
|
|
|
**建议**: 先用规则快速验证,再用LLM提升精度
|