Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
556 changes: 358 additions & 198 deletions README.md

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions llm/agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# AI Agent

> **从聊天机器人到智能体**

---

## 什么是 Agent?

**AI Agent = LLM + 工具 + 记忆 + 规划**

不只是回答问题,而是:
- 🤔 思考
- 🔍 搜索信息
- 🛠️ 调用工具
- 📊 分析结果
- 🎯 完成目标

---

## 核心组件

### 1. 大脑(LLM)

- 思考和决策
- 理解自然语言

### 2. 工具(Tools)

- 搜索引擎
- 代码执行
- API 调用
- 数据库查询

### 3. 记忆(Memory)

- 短期记忆:对话历史
- 长期记忆:知识库
- 向量存储

### 4. 规划(Planning)

- 目标分解
- 任务调度
- 迭代优化

---

## 经典框架

### ReAct (Reasoning + Acting)

```
Thought: 我需要搜索信息
Action: 调用搜索工具
Observation: 获得搜索结果
Thought: 分析结果
Action: 执行下一步
...
Final Answer: 最终答案
```

### AutoGPT

- 自动设定目标
- 自动拆解任务
- 自动执行和迭代

### LangChain Agents

```python
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI

tools = [
Tool(
name="Search",
func=search_func,
description="搜索最新信息"
),
Tool(
name="Calculator",
func=calculator_func,
description="执行计算"
)
]

agent = initialize_agent(
tools,
OpenAI(temperature=0),
agent="zero-shot-react-description"
)

agent.run("比特币当前价格是多少?乘以100等于多少?")
```

---

## 应用场景

### 1. 数据分析 Agent

```python
# 自动分析数据集
agent.analyze("sales_data.csv", goal="找出增长最快的区域")
```

### 2. 编程 Agent

```python
# 自动生成代码
agent.build_app("写一个待办事项Web应用")
```

### 3. 研究 Agent

```python
# 自动调研主题
agent.research("量子计算在AI中的应用")
```

---

## Agent 的挑战

| 挑战 | 说明 |
|------|------|
| **稳定性** | 难以保证正确执行 |
| **成本** | 多轮调用,消耗token |
| **安全性** | 工具调用有风险 |
| **评估** | 如何衡量Agent能力 |

---

## 学习资源

- [LangChain Documentation](https://python.langchain.com/)
- [AutoGPT GitHub](https://github.com/Significant-Gravitas/AutoGPT)
- [BabyAGI](https://github.com/yoheinakajima/babyagi)

---

**恭喜!你已经完成了 LLM 的核心学习!** 🎉
54 changes: 54 additions & 0 deletions llm/bert-series.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# BERT 系列

> **理解任务的首选**

---

## BERT vs GPT

| 特性 | BERT | GPT |
|------|------|-----|
| 架构 | Encoder-only | Decoder-only |
| 任务 | 理解(分类、抽取) | 生成 |
| 训练 | Masked LM + Next Sentence Prediction | Causal LM |
| 方向 | 双向 | 单向 |

## BERT 家族

| 模型 | 特点 |
|------|------|
| BERT-Base | 12层,110M参数 |
| BERT-Large | 24层,340M参数 |
| RoBERTa | 改进的BERT |
| ALBERT | 参数共享 |
| DeBERTa | 解耦注意力 |

## 经典任务

### 文本分类

```python
from transformers import BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
```

### 命名实体识别

```python
from transformers import BertForTokenClassification

model = BertForTokenClassification.from_pretrained('bert-base-cased')
```

### 问答系统

```python
from transformers import BertForQuestionAnswering

model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking')
```

---

**继续学习 [微调方法](fine-tuning.md)!**
93 changes: 93 additions & 0 deletions llm/fine-tuning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 微调方法

> **让通用模型适应你的任务**

---

## 为什么微调?

通用模型很强大,但:
- 不知道你的领域知识
- 不懂你的业务逻辑
- 可能风格不匹配

微调 = 让模型学你的数据

---

## 微调方法

### 1. Full Fine-tuning

更新所有参数

```python
from transformers import AutoModelForCausalLM, Trainer, TrainingArguments

model = AutoModelForCausalLM.from_pretrained("gpt2")

training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=4,
)

trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset,
)

trainer.train()
```

### 2. LoRA (Low-Rank Adaptation)

只训练少量参数

```python
from peft import LoraConfig, get_peft_model

lora_config = LoraConfig(
r=8,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.05,
bias="none",
)

model = get_peft_model(model, lora_config)
```

### 3. QLoRA

4-bit 量化 + LoRA

```python
from transformers import BitsAndBytesConfig

bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)

model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config,
)
```

---

## 什么时候用哪种方法?

| 场景 | 推荐 |
|------|------|
| 大数据 + 有算力 | Full Fine-tuning |
| 小数据 + 有限算力 | LoRA |
| 单卡 + 大模型 | QLoRA |

---

**继续学习 [RAG](rag.md)!**
45 changes: 45 additions & 0 deletions llm/gpt-series.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# GPT 系列

> **从 GPT-1 到 GPT-4,见证 AI 的进化**

---

## GPT 家族

| 模型 | 发布年份 | 参数量 | 特点 |
|------|----------|--------|------|
| GPT-1 | 2018 | 117M | 初次验证生成式预训练 |
| GPT-2 | 2019 | 1.5B | "Too Dangerous to Release" |
| GPT-3 | 2020 | 175B | Few-shot Learning 突破 |
| GPT-3.5 | 2022 | 未知 | ChatGPT 的基础 |
| GPT-4 | 2023 | 未知 | 多模态、推理能力飞跃 |

## 核心技术

### 1. 预训练 + 微调

- 自监督学习:预测下一个词
- 海量文本数据训练
- 通用性超强

### 2. Scaling Law

- 参数越大,能力越强
- 数据越多,效果越好
- 计算是关键

### 3. RLHF (人类反馈强化学习)

- InstructGPT 首创
- 让模型更安全、更有用

## 应用场景

- 对话系统(ChatGPT)
- 代码生成(Copilot)
- 文本创作
- 数据分析

---

**继续学习 [BERT 系列](bert-series.md)!**
Loading