# Text Generation WebUI

运行最流行的支持所有模型格式的 LLM 界面。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行： [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、显存和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价价格）
4. 配置您的订单：
   * 选择 Docker 镜像
   * 设置端口（用于 SSH 的 TCP，Web 界面的 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问您的服务器

* 在以下位置查找连接详情： **我的订单**
* Web 界面：使用 HTTP 端口的 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 为什么选择 Text Generation WebUI？

* 支持 GGUF、GPTQ、AWQ、EXL2、HF 格式
* 内置聊天、笔记本和 API 模式
* 扩展：语音、角色、多模态
* 微调支持
* 即时切换模型

## 要求

| 模型大小    | 最小显存 | 推荐       |
| ------- | ---- | -------- |
| 7B（Q4）  | 6GB  | 按小时费率    |
| 13B（Q4） | 10GB | RTX 3080 |
| 30B（Q4） | 20GB | 512x512  |
| 70B（Q4） | 40GB | 2s       |

## 快速部署

**Docker 镜像：**

```
atinoda/text-generation-webui:default-nvidia
```

**端口：**

```
22/tcp
7860/http
5000/http
5005/http
```

**环境：**

```
EXTRA_LAUNCH_ARGS=--listen --api
```

## 手动安装

**镜像：**

```
nvidia/cuda:12.1.0-devel-ubuntu22.04
```

**端口：**

```
22/tcp
7860/http
5000/http
```

**命令：**

```bash
apt-get update && apt-get install -y git python3 python3-pip && \
cd /workspace && \
git clone https://github.com/oobabooga/text-generation-webui.git && \
cd text-generation-webui && \
pip install -r requirements.txt && \
python server.py --listen --api
```

## 访问您的服务

部署后，在以下位置查找您的 `http_pub` URL： **我的订单**:

1. 前往 **我的订单** 页面
2. 单击您的订单
3. 查找 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 而不是 `localhost` 在下面的示例中。

## 访问 WebUI

1. 等待部署完成
2. 在以下位置查找端口 7860 的映射 **我的订单**
3. 打开： `http://<proxy>:<port>`

## 下载模型

### 来自 HuggingFace（在 WebUI 中）

1. 前往 **A100** 选项卡
2. 输入模型名称： `bartowski/Meta-Llama-3.1-8B-Instruct-GGUF`
3. 点击 **下载**

### 通过命令行

```bash
cd /workspace/text-generation-webui

# 下载 GGUF 模型
python download-model.py bartowski/Meta-Llama-3.1-8B-Instruct-GGUF

# 下载特定文件
python download-model.py bartowski/Meta-Llama-3.1-8B-Instruct-GGUF --specific-file Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf
```

### 推荐模型

**用于聊天：**

```bash

# Llama 2 聊天（7B，速度快）
python download-model.py bartowski/Meta-Llama-3.1-8B-Instruct-GGUF

# Mistral Instruct（优秀）
python download-model.py bartowski/Mistral-7B-Instruct-v0.3-GGUF

# OpenHermes（通用出色）
python download-model.py bartowski/OpenHermes-2.5-Mistral-7B-GGUF
```

**用于代码：**

```bash

# CodeLlama
python download-model.py bartowski/CodeLlama-13B-Instruct-GGUF

# DeepSeek Coder
python download-model.py bartowski/DeepSeek-Coder-V2-Lite-Instruct-GGUF
```

**用于角色扮演：**

```bash

# MythoMax
python download-model.py bartowski/MythoMax-L2-13B-GGUF
```

## 加载模型

### GGUF（推荐大多数用户）

1. **A100** 选项卡 → 选择模型文件夹
2. **模型加载器：** llama.cpp
3. 设置 **n-gpu-layers：**
   * RTX 3090：35-40
   * RTX 4090：45-50
   * A100：80+
4. 点击 **加载**

### GPTQ（快速，量化）

1. 下载 GPTQ 模型
2. **模型加载器：** ExLlama\_HF 或 AutoGPTQ
3. 加载模型

### EXL2（最快）

```bash

# 安装 exllamav2
pip install exllamav2
```

1. 下载 EXL2 模型
2. **模型加载器：** ExLlamav2\_HF
3. 加载

## 聊天配置

### 角色设置

1. 前往 **参数量** → **角色**
2. 创建或加载角色卡片
3. 设置：
   * 名称
   * 背景/人格
   * 示例对话

### 指令模式

对于经过指令微调的模型：

1. **参数量** → **指令模板**
2. 选择与您的模型匹配的模板：
   * Llama-2-chat
   * Mistral
   * ChatML
   * Alpaca

## API 使用

### 启用 API

从以下设置开始 `--api` flag（默认端口 5000）

### 兼容 OpenAI 的 API

```python
import openai

openai.api_base = "http://localhost:5000/v1"
openai.api_key = "not-needed"

response = openai.ChatCompletion.create(
    model="any",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
```

### 原生 API

```python
import requests

response = requests.post(
    "http://localhost:5000/api/v1/generate",
    json={
        "prompt": "写一个关于",
        "max_new_tokens": 200,
        "temperature": 0.7
    }
)
print(response.json()["results"][0]["text"])
```

## 扩展

### 安装扩展

```bash
cd /workspace/text-generation-webui/extensions

# Silero TTS（语音）
git clone https://github.com/oobabooga/text-generation-webui-extensions

# SuperBoogav2（RAG/长期记忆）

# 已包含，在 UI 中启用
```

### 启用扩展

1. **会话** 选项卡 → **扩展**
2. 勾选所需扩展的复选框
3. 点击 **应用并重启**

### 热门扩展

| 扩展                | 用途   |
| ----------------- | ---- |
| silero\_tts       | 语音输出 |
| whisper\_stt      | 语音输入 |
| superbooga        | 文档问答 |
| sd\_api\_pictures | 图像生成 |
| 多模态               | 图像理解 |

## 性能调优

### GGUF 设置

```
n_gpu_layers: 35    # GPU 层数（更多 = 更快）
n_ctx: 4096         # 上下文长度
n_batch: 512        # 批次大小
threads: 8          # CPU 线程数
```

### 内存优化

对于显存受限的情况：

```bash
python server.py --listen --n-gpu-layers 20 --no-mmap
```

### 速度优化

```bash

# 使用带 cuBLAS 的 llama.cpp
python server.py --listen --loader llama.cpp --n-gpu-layers 50 --threads 8
```

## 微调（LoRA）

### 训练 选项卡

1. 前往 **训练** 选项卡
2. 加载基础模型
3. 上传数据集（JSON 格式）
4. 配置：
   * LoRA 秩：8-32
   * 学习率：1e-4
   * 训练轮数：3-5
5. 开始训练

### 数据集格式

```json
[
  {"instruction": "Summarize this:", "input": "Long text...", "output": "Summary..."},
  {"instruction": "Translate to French:", "input": "Hello", "output": "Bonjour"}
]
```

## 保存您的工作

```bash

# 保存模型
rsync -avz /workspace/text-generation-webui/models/ backup-server:/models/

# 保存角色
rsync -avz /workspace/text-generation-webui/characters/ backup-server:/characters/

# 保存 LoRA
rsync -avz /workspace/text-generation-webui/loras/ backup-server:/loras/
```

## # 使用固定种子以获得一致结果

### 模型无法加载

* 检查显存使用情况： `nvidia-smi`
* 减少 `n_gpu_layers`
* 使用更小的量化（Q4\_K\_M → Q4\_K\_S）

### 生成速度慢

* 增加 `n_gpu_layers`
* 使用 EXL2 代替 GGUF
* 启用 `--no-mmap`

{% hint style="danger" %}
**内存不足**
{% endhint %}

在生成期间 - 减少 \`n\_ctx\`（上下文长度） - 使用 \`--n-gpu-layers 0\` 仅使用 CPU - 尝试更小的模型

## 下载所有所需的检查点

检查文件完整性

| GPU     | 验证 CUDA 兼容性 | 费用估算    | CLORE.AI 市场的典型费率（截至 2024 年）： |
| ------- | ----------- | ------- | ---------------------------- |
| 按小时费率   | \~$0.03     | \~$0.70 | \~$0.12                      |
| 速度      | \~$0.06     | \~$1.50 | \~$0.25                      |
| 512x512 | \~$0.10     | \~$2.30 | \~$0.40                      |
| 按日费率    | \~$0.17     | \~$4.00 | \~$0.70                      |
| 4 小时会话  | \~$0.25     | \~$6.00 | \~$1.00                      |

*RTX 3060* [*CLORE.AI 市场*](https://clore.ai/marketplace) *A100 40GB*

**A100 80GB**

* 使用 **竞价** 价格随提供商和需求而异。请查看
* 以获取当前费率。 **CLORE** 节省费用：
* 市场用于灵活工作负载（通常便宜 30-50%）


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/text-generation-webui.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
