# 通过 vLLM 推理

> 将投影后的科学向量插入提示词嵌入，并通过 vLLM 生成答案。

---

LLMS 索引： [llms.txt](/Sage-SciPerception/zh/llms.txt)

---

推理时，将投影模块输出的科学向量插入语言模型的完整聊天提示词嵌入，再通过 `prompt_embeds` 发给 vLLM 的 `/v1/completions`。科学特征与投影在客户端准备，语言模型服务负责生成答案。

## 启动语言模型服务 {#serve}

先在兼容的推理环境中安装 [vLLM](https://docs.vllm.ai/en/latest/getting_started/installation/)。在具备相应 GPU 的环境中运行，并按模型与硬件选择张量并行度。以下命令以八路并行为例：

```bash
vllm serve /path/to/matching-base-model \
  --served-model-name sage-sciperception \
  --enable-prompt-embeds \
  --tensor-parallel-size 8 \
  --generation-config vllm \
  --max-model-len 8192 \
  --host 127.0.0.1 --port 8000
```

> [!WARNING] 使用权重声明的精确快照
> 部署所选 checkpoint `config.json` 中指定的语言模型快照。换用其他快照会改变客户端读取的嵌入表。

## 从已准备的晶体特征生成答案 {#crystal}

[晶体客户端](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/vllm_cif.py)会渲染 checkpoint 的原生 chat template，读取文本嵌入、插入科学向量，并将完整提示词嵌入发送给服务端。

准备 `cif_features.pt`：形状为 `[num_atoms, 64]` 的张量，内容为 [`cif/jarvis`](/docs/tasks/cif-jarvis/) 指定的真实 CHGNet 0.3.0 原子状态，保持原子顺序。服务就绪后运行：

```bash
python -m pip install --upgrade transformers
python examples/vllm_cif.py \
  --component ./checkpoints/projectors/glm-5.3-flash/cif/jarvis \
  --base-model /path/to/matching-base-model \
  --features /path/to/cif_features.pt \
  --api-base http://127.0.0.1:8000/v1 \
  --model sage-sciperception
```

本地语言模型目录提供与服务端一致的 tokenizer、配置和分片输入嵌入表。客户端只读取所需文本嵌入行，无需加载完整语言模型。

## 其他任务 {#other-tasks}

科学特征提取与任务适配科学模型是独立依赖，部分配套科学权重尚未在此发布；准备预测时需匹配各任务的 `scientific_inputs`。晶体示例从已准备好的特征开始，其他任务需要各自的输入准备与提示词组装。

另一条完整路径（含从原始信号提取特征）见 [NEU06 示例](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/neu06/README.md)。

## 参考 {#references}

- [vLLM prompt embedding 文档](https://docs.vllm.ai/en/latest/features/prompt_embeds/)
- [HF 推理指南](https://huggingface.co/internlm/Sage-SciPerception/blob/main/README_zh.md#推理使用)
