OpenCLAW 是一个开源的多模态检索增强生成系统,由清华大学的 NLP 实验室开发,以下是一份基础使用教程:

环境安装
系统要求
- Python 3.8+
- PyTorch 1.10+
- CUDA 11.0+(GPU运行)
安装步骤
cd OpenCLAW # 创建虚拟环境(可选) conda create -n openclaw python=3.8 conda activate openclaw # 安装依赖 pip install -r requirements.txt # 安装额外依赖(如需要) pip install torch torchvision transformers
模型下载
OpenCLAW 通常提供预训练模型,可通过以下方式获取:
# 从Hugging Face下载(如果可用) git lfs install git clone https://huggingface.co/thunlp/openclaw-base # 或从清华云盘下载 # 参考项目README中的下载链接
基础使用
命令行使用
# 运行基础示例
python demo.py --model_path ./models/openclaw-base \
--knowledge_base ./data/kb \
--query "你的查询问题"
Python API使用
from openclaw import OpenCLAW
# 初始化模型
model = OpenCLAW(
model_path="./models/openclaw-base",
kb_path="./data/knowledge_base"
)
# 单次查询
result = model.query("什么是人工智能?")
print(result["answer"])
print(result["sources"]) # 检索到的参考文献
# 多轮对话
model.chat_init()
response = model.chat("你好,请介绍下机器学习")
print(response)
构建知识库
from openclaw.knowledge_base import KnowledgeBaseBuilder
# 1. 从文本文件构建
builder = KnowledgeBaseBuilder()
builder.add_documents_from_dir("./docs")
# 2. 从PDF/Word文件构建
builder.add_documents_from_pdf("./papers/*.pdf")
# 3. 保存知识库
builder.save_kb("./data/my_kb")
# 4. 更新知识库
builder.update_document("doc_id", "新内容")
高级配置
配置文件示例(config.yaml)
model: name: "openclaw-base" device: "cuda:0" max_length: 2048 retrieval: top_k: 5 similarity_threshold: 0.7 method: "dense" # dense / sparse / hybrid generation: temperature: 0.7 max_new_tokens: 500 do_sample: true
自定义配置
from openclaw.config import Config
config = Config.load("config.yaml")
# 修改配置
config.retrieval.top_k = 10
config.generation.temperature = 0.8
model = OpenCLAW(config=config)
批量处理
# 批量问答
queries = ["问题1", "问题2", "问题3"]
results = model.batch_query(queries)
# 导出结果
import json
with open("results.json", "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=2)
Web界面启动
# 启动Gradio Web界面
python web_demo.py \
--model_path ./models/openclaw-base \
--share # 生成公共链接
API服务部署
# 启动FastAPI服务
python api_server.py \
--host 0.0.0.0 \
--port 8000 \
--model_path ./models/openclaw-base
# API调用示例
import requests
response = requests.post(
"http://localhost:8000/query",
json={"query": "你的问题", "history": []}
)
print(response.json())
常见问题解决
内存不足
# 减小批处理大小 config.generation.max_batch_size = 4 # 使用量化 model.load_model(load_in_8bit=True)
速度优化
# 启用缓存 config.cache_dir = "./cache" # 使用FAISS加速检索 config.retrieval.use_faiss = True
中文支持
# 确保使用中文分词器
model.tokenizer = AutoTokenizer.from_pretrained(
"bert-base-chinese",
trust_remote_code=True
)
监控和评估
# 记录日志
import logging
logging.basicConfig(level=logging.INFO)
# 评估检索质量
from openclaw.evaluation import RetrievalEvaluator
evaluator = RetrievalEvaluator()
metrics = evaluator.evaluate(model, test_data)
print(f"Recall@5: {metrics['recall@5']}")
注意事项
- 首次运行:首次使用会下载必要的预训练模型和依赖
- 硬件要求:建议至少16GB显存用于Base模型
- 知识库更新:定期更新知识库以获得最新信息
- 多语言:如需多语言支持,需切换相应的预训练模型
获取帮助
- GitHub Issues: 项目Issues页面
- 论文引用:参考项目中的CITATION文件
- 官方文档:查看项目Wiki页面
建议在使用前详细阅读项目README和文档,了解最新的功能更新和API变化。
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。