工具生态
PyTorch — 深度学习框架
Section titled “PyTorch — 深度学习框架”绝大多数 AI 研究基于 PyTorch。提供了灵活的自动微分和 GPU 加速。
import torchimport torch.nn as nn
# 定义一个简单的 MLPclass MLP(nn.Module): def __init__(self): super().__init__() self.layers = nn.Sequential( nn.Linear(768, 256), nn.ReLU(), nn.Dropout(0.1), nn.Linear(256, 10), )
def forward(self, x): return self.layers(x)
model = MLP()x = torch.randn(4, 768) # batch=4output = model(x)print(output.shape) # torch.Size([4, 10])HuggingFace Transformers — 预训练模型库
Section titled “HuggingFace Transformers — 预训练模型库”from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载模型和分词器model_name = "gpt2"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)
# 生成文本inputs = tokenizer("The future of AI is", return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=30)print(tokenizer.decode(outputs[0]))LangChain — LLM 应用框架
Section titled “LangChain — LLM 应用框架”from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplate
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
prompt = ChatPromptTemplate.from_messages([ ("system", "你是一个 AI 技术专家。"), ("human", "用一句话解释 {concept}"),])
chain = prompt | llmresponse = chain.invoke({"concept": "Transformer"})print(response.content)Ollama — 本地运行大模型
Section titled “Ollama — 本地运行大模型”# 安装后下载模型ollama pull llama3.2
# 命令行使用ollama run llama3.2 "解释什么是注意力机制"
# Python 调用pip install ollamaimport ollama
response = ollama.chat( model="llama3.2", messages=[{"role": "user", "content": "什么是 Transformer?"}],)print(response["message"]["content"])import chromadb
# 创建 Chroma 客户端client = chromadb.Client()collection = client.create_collection("docs")
# 添加文档collection.add( documents=["Transformer 是 Attention 机制的架构", "CNN 用于图像处理"], ids=["doc1", "doc2"],)
# 查询results = collection.query( query_texts=["什么是 Attention"], n_results=1,)print(results["documents"][0]) # ['Transformer 是 Attention 机制的架构']工具选择指南
Section titled “工具选择指南”| 场景 | 推荐工具 |
|---|---|
| 模型训练 | PyTorch + HuggingFace |
| 快速原型 | Ollama + LangChain |
| 生产部署 | vLLM / TGI |
| 向量检索 | Chroma (原型) / Milvus (生产) |
| 数据标注 | Label Studio |
| 实验追踪 | Weights & Biases |