工具生态
AI 开发工具栈
Section titled “AI 开发工具栈”flowchart TD A[AI 开发工具栈] --> B[模型训练] A --> C[模型推理] A --> D[应用开发] A --> E[数据处理] B --> B1[PyTorch] B --> B2[HuggingFace] C --> C1[vLLM] C --> C2[Ollama] D --> D1[LangChain] D --> D2[LlamaIndex] E --> E1[Pandas] E --> E2[Chroma]PyTorch — 深度学习框架
Section titled “PyTorch — 深度学习框架”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)print(model(x).shape) # (4, 10)HuggingFace Transformers
Section titled “HuggingFace Transformers”from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("gpt2")tokenizer = AutoTokenizer.from_pretrained("gpt2")
inputs = tokenizer("The future of AI is", return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=30)print(tokenizer.decode(outputs[0]))LangChain
Section titled “LangChain”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.2ollama run llama3.2 "解释什么是注意力机制"import ollamaresponse = ollama.chat( model="llama3.2", messages=[{"role": "user", "content": "什么是 Transformer?"}],)print(response["message"]["content"])向量数据库:Chroma
Section titled “向量数据库:Chroma”import chromadb
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])工具选择指南
Section titled “工具选择指南”| 场景 | 推荐工具 |
|---|---|
| 模型训练 | PyTorch + HuggingFace |
| 快速原型 | Ollama + LangChain |
| 生产部署 | vLLM / TGI |
| 向量检索 | Chroma (原型) / Milvus (生产) |
| 数据标注 | Label Studio |
| 实验追踪 | Weights & Biases |