大模型基础与生态

1. 主流模型对比

模型 参数量级 特点 适用场景
DeepSeek-V3 671B MoE 国产开源,性价比高 复杂推理、代码
GPT-4o 多模态 OpenAI 最新旗舰 全能型任务
Claude 3.5 多模态 长上下文强(200K) 长文档分析
LLaMA 3.1 8B/70B/405B 开源,可本地部署 私有化部署
Mistral 7B/22B 稀疏注意力,高效 边缘部署
Qwen 2.5 7B/14B/72B 中文优化,开源 中文场景

2. API 调用方式

2.1 OpenAI 兼容 API(通用)


import openai

client = openai.OpenAI(
    api_key="sk-xxxx",
    base_url="https://api.deepseek.com"  # DeepSeek / 硅基流动等兼容接口
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "你是一个运维助手"},
        {"role": "user", "content": "如何查看 Kubernetes Pod 的资源使用情况?"}
    ],
    temperature=0.7,
    max_tokens=1024,
)
print(response.choices[0].message.content)

2.2 流式输出(Streaming)


stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "解释什么是 Docker"}],
    stream=True
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='', flush=True)

2.3 Function Calling(工具调用)


tools = [
    {
        "type": "function",
        "function": {
            "name": "get_k8s_pod_status",
            "description": "获取 K8s Pod 状态",
            "parameters": {
                "type": "object",
                "properties": {
                    "namespace": {"type": "string"},
                    "pod_name": {"type": "string"}
                },
                "required": ["namespace", "pod_name"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "查看 default 命名空间下 nginx pod 的状态"}],
    tools=tools
)
print(response.choices[0].message.tool_calls)

3. Prompt Engineering

3.1 常用技巧

技巧 说明 示例
Few-shot 提供示例答案 示例:输入... 输出...
Chain-of-Thought 让模型逐步推理 请一步步思考
System Prompt 定义角色和行为 你是一个资深SRE
Output Format 指定输出格式 请以 JSON 格式输出
Temperature 控制随机性 0=确定,1=创意
Top-P 采样范围 通常 0.9 左右

3.2 运维场景 Prompt 模板


# 告警分析
SYSTEM_PROMPT = '''你是一个资深运维工程师(SRE)。当收到告警时,请:
1. 分析告警级别和影响范围
2. 给出可能的原因(Top 3)
3. 提供排查步骤(按优先级排序)
4. 给出修复建议

回复格式:
- 级别:[P0/P1/P2/P3]
- 影响:[简述]
- 可能原因:[列表]
- 排查步骤:[步骤]
- 修复建议:[建议]'''

# 数据库慢查询分析
DBA_PROMPT = '''你是一个 DBA 专家。请分析以下 MySQL 慢查询日志:
1. 识别性能瓶颈(索引缺失?全表扫描?)
2. 给出优化建议(加索引/改写查询/调整配置)
3. 预估优化效果'''

4. 模型评测


# 使用 lm-evaluation-harness 评测模型
git clone https://github.com/EleutherAI/lm-evaluation-harness.git
cd lm-evaluation-harness
pip install -e .

# 评测 MMLU 基准
lm_eval --model hf \
  --model_args pretrained=microsoft/Phi-3-mini-128k-instruct \
  --tasks mmlu \
  --batch_size 8

5. 下一步