Spaces:
Sleeping
Sleeping
yanyoyo
commited on
Commit
·
68dd607
1
Parent(s):
884eb73
nice commit
Browse files- app.py +73 -0
- data/README_zh-CN.md +304 -0
- data/mydataset.md +29 -0
- download_hf.py +7 -0
- llamaindex_RAG.py +34 -0
- llamaindex_internlm.py +11 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 4 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 8 |
+
st.title("llama_index_demo")
|
| 9 |
+
|
| 10 |
+
# 初始化模型
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def init_models():
|
| 13 |
+
embed_model = HuggingFaceEmbedding(
|
| 14 |
+
model_name="/root/model/sentence-transformer"
|
| 15 |
+
)
|
| 16 |
+
Settings.embed_model = embed_model
|
| 17 |
+
|
| 18 |
+
llm = HuggingFaceLLM(
|
| 19 |
+
model_name="/root/model/internlm2-chat-1_8b",
|
| 20 |
+
tokenizer_name="/root/model/internlm2-chat-1_8b",
|
| 21 |
+
model_kwargs={"trust_remote_code": True},
|
| 22 |
+
tokenizer_kwargs={"trust_remote_code": True}
|
| 23 |
+
)
|
| 24 |
+
Settings.llm = llm
|
| 25 |
+
|
| 26 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 27 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 28 |
+
query_engine = index.as_query_engine()
|
| 29 |
+
|
| 30 |
+
return query_engine
|
| 31 |
+
|
| 32 |
+
# 检查是否需要初始化模型
|
| 33 |
+
if 'query_engine' not in st.session_state:
|
| 34 |
+
st.session_state['query_engine'] = init_models()
|
| 35 |
+
|
| 36 |
+
def greet2(question):
|
| 37 |
+
response = st.session_state['query_engine'].query(question)
|
| 38 |
+
return response
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Store LLM generated responses
|
| 42 |
+
if "messages" not in st.session_state.keys():
|
| 43 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 44 |
+
|
| 45 |
+
# Display or clear chat messages
|
| 46 |
+
for message in st.session_state.messages:
|
| 47 |
+
with st.chat_message(message["role"]):
|
| 48 |
+
st.write(message["content"])
|
| 49 |
+
|
| 50 |
+
def clear_chat_history():
|
| 51 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 52 |
+
|
| 53 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 54 |
+
|
| 55 |
+
# Function for generating LLaMA2 response
|
| 56 |
+
def generate_llama_index_response(prompt_input):
|
| 57 |
+
return greet2(prompt_input)
|
| 58 |
+
|
| 59 |
+
# User-provided prompt
|
| 60 |
+
if prompt := st.chat_input():
|
| 61 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 62 |
+
with st.chat_message("user"):
|
| 63 |
+
st.write(prompt)
|
| 64 |
+
|
| 65 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
| 66 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 67 |
+
with st.chat_message("assistant"):
|
| 68 |
+
with st.spinner("Thinking..."):
|
| 69 |
+
response = generate_llama_index_response(prompt)
|
| 70 |
+
placeholder = st.empty()
|
| 71 |
+
placeholder.markdown(response)
|
| 72 |
+
message = {"role": "assistant", "content": response}
|
| 73 |
+
st.session_state.messages.append(message)
|
data/README_zh-CN.md
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div align="center">
|
| 2 |
+
<img src="https://github.com/InternLM/lmdeploy/assets/36994684/0cf8d00f-e86b-40ba-9b54-dc8f1bc6c8d8" width="600"/>
|
| 3 |
+
<br /><br />
|
| 4 |
+
|
| 5 |
+
[](https://github.com/InternLM/xtuner/stargazers)
|
| 6 |
+
[](https://github.com/InternLM/xtuner/blob/main/LICENSE)
|
| 7 |
+
[](https://pypi.org/project/xtuner/)
|
| 8 |
+
[](https://pypi.org/project/xtuner/)
|
| 9 |
+
[](https://github.com/InternLM/xtuner/issues)
|
| 10 |
+
[](https://github.com/InternLM/xtuner/issues)
|
| 11 |
+
|
| 12 |
+
👋 加入我们:[](https://cdn.vansin.top/internlm/xtuner.jpg)
|
| 13 |
+
[](https://twitter.com/intern_lm)
|
| 14 |
+
[](https://discord.gg/xa29JuW87d)
|
| 15 |
+
|
| 16 |
+
🔍 探索我们的模型:
|
| 17 |
+
[](https://huggingface.co/xtuner)
|
| 18 |
+
[](https://www.modelscope.cn/organization/xtuner)
|
| 19 |
+
[](https://openxlab.org.cn/usercenter/xtuner)
|
| 20 |
+
[](https://www.wisemodel.cn/organization/xtuner)
|
| 21 |
+
|
| 22 |
+
[English](README.md) | 简体中文
|
| 23 |
+
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
## 🚀 Speed Benchmark
|
| 27 |
+
|
| 28 |
+
- XTuner 与 LLaMA-Factory 在 Llama2-7B 模型上的训练效率对比
|
| 29 |
+
|
| 30 |
+
<div align=center>
|
| 31 |
+
<img src="https://github.com/InternLM/xtuner/assets/41630003/9c9dfdf4-1efb-4daf-84bf-7c379ae40b8b" style="width:80%">
|
| 32 |
+
</div>
|
| 33 |
+
|
| 34 |
+
- XTuner 与 LLaMA-Factory 在 Llama2-70B 模型上的训练效率对比
|
| 35 |
+
|
| 36 |
+
<div align=center>
|
| 37 |
+
<img src="https://github.com/InternLM/xtuner/assets/41630003/5ba973b8-8885-4b72-b51b-c69fa1583bdd" style="width:80%">
|
| 38 |
+
</div>
|
| 39 |
+
|
| 40 |
+
## 🎉 更新
|
| 41 |
+
- **\[2024/07\]** 支持 [MiniCPM](xtuner/configs/minicpm/) 模型!
|
| 42 |
+
- **\[2024/07\]** 支持训练 [DPO](https://github.com/InternLM/xtuner/tree/main/xtuner/configs/dpo), [ORPO](https://github.com/InternLM/xtuner/tree/main/xtuner/configs/orpo) 还有 [Reward Model](https://github.com/InternLM/xtuner/tree/main/xtuner/configs/reward_model) ! 并且能够支持打包数据以及序列并行功能! 请参考 [文档](https://xtuner.readthedocs.io/zh-cn/latest/dpo/overview.html) 了解更多信息。
|
| 43 |
+
- **\[2024/07\]** 支持 [InternLM 2.5](xtuner/configs/internlm/internlm2_5_chat_7b/) 模型!
|
| 44 |
+
- **\[2024/06\]** 支持 [DeepSeek V2](xtuner/configs/deepseek/deepseek_v2_chat/) models! **训练速度提升一倍!**
|
| 45 |
+
- **\[2024/04\]** 多模态大模型 [LLaVA-Phi-3-mini](https://huggingface.co/xtuner/llava-phi-3-mini-hf) 发布!快速开始请查阅此[文档](xtuner/configs/llava/phi3_mini_4k_instruct_clip_vit_large_p14_336)!
|
| 46 |
+
- **\[2024/04\]** 多模态大模型 [LLaVA-Llama-3-8B](https://huggingface.co/xtuner/llava-llama-3-8b) 和 [LLaVA-Llama-3-8B-v1.1](https://huggingface.co/xtuner/llava-llama-3-8b-v1_1) 发布!快速开始请查阅此[文档](xtuner/configs/llava/llama3_8b_instruct_clip_vit_large_p14_336)!
|
| 47 |
+
- **\[2024/04\]** 支持 [Llama 3](xtuner/configs/llama) 模型!
|
| 48 |
+
- **\[2024/04\]** 支持序列并行训练策略以实现语言模型超长上下文训练!\[[文档](https://github.com/InternLM/xtuner/blob/docs/docs/zh_cn/acceleration/train_extreme_long_sequence.rst)\] \[[速度基准](https://github.com/InternLM/xtuner/blob/docs/docs/zh_cn/acceleration/benchmark.rst)\]
|
| 49 |
+
- **\[2024/02\]** 支持 [Gemma](xtuner/configs/gemma) 模型!
|
| 50 |
+
- **\[2024/02\]** 支持 [Qwen1.5](xtuner/configs/qwen/qwen1_5) 模型!
|
| 51 |
+
- **\[2024/01\]** 支持 [InternLM2](xtuner/configs/internlm) 模型!同时,最新版的多模态大模型 [LLaVA-Internlm2-7B](https://huggingface.co/xtuner/llava-internlm2-7b) / [20B](https://huggingface.co/xtuner/llava-internlm2-20b) 发布,其表现出强大的性能!
|
| 52 |
+
- **\[2024/01\]** 支持 [DeepSeek-MoE](https://huggingface.co/deepseek-ai/deepseek-moe-16b-chat) 模型!20GB 显存即可实现 QLoRA 微调,4x80GB 即可实现全参数微调。快速开始请查阅相关[配置文件](xtuner/configs/deepseek/)!
|
| 53 |
+
- **\[2023/12\]** 🔥 支持多模态模型 VLM([LLaVA-v1.5](https://github.com/haotian-liu/LLaVA))预训练和指令微调!快速开始请查阅此[文档](xtuner/configs/llava/README_zh-CN.md)!
|
| 54 |
+
- **\[2023/12\]** 🔥 支持 [Mixtral 8x7B](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) 模型!快速开始请查阅此[文档](xtuner/configs/mixtral/README.md)!
|
| 55 |
+
- **\[2023/11\]** 支持 [ChatGLM3-6B](xtuner/configs/chatglm) 模型!
|
| 56 |
+
- **\[2023/10\]** 支持 [MSAgent-Bench](https://modelscope.cn/datasets/damo/MSAgent-Bench) 数据集,并且微调所得大语言模型可应用至 [Lagent](https://github.com/InternLM/lagent) 框架!
|
| 57 |
+
- **\[2023/10\]** 优化数据处理逻辑以兼容 `system` 字段,相关细节请查阅[文档](docs/zh_cn/user_guides/dataset_format.md)!
|
| 58 |
+
- **\[2023/09\]** 支持 [InternLM-20B](xtuner/configs/internlm) 系列模型!
|
| 59 |
+
- **\[2023/09\]** 支持 [Baichuan2](xtuner/configs/baichuan) 系列模型!
|
| 60 |
+
- **\[2023/08\]** XTuner 正式发布!众多微调模型已上传至 [HuggingFace](https://huggingface.co/xtuner)!
|
| 61 |
+
|
| 62 |
+
## 📖 介绍
|
| 63 |
+
|
| 64 |
+
XTuner 是一个高效、灵活、全能的轻量化大模型微调工具库。
|
| 65 |
+
|
| 66 |
+
**高效**
|
| 67 |
+
|
| 68 |
+
- 支持大语言模型 LLM、多模态图文模型 VLM 的预训练及轻量级微调。XTuner 支持在 8GB 显存下微调 7B 模型,同时也支持多节点跨设备微调更大尺度模型(70B+)。
|
| 69 |
+
- 自动分发高性能算子(如 FlashAttention、Triton kernels 等)以加速训练吞吐。
|
| 70 |
+
- 兼容 [DeepSpeed](https://github.com/microsoft/DeepSpeed) 🚀,轻松应用各种 ZeRO 训练优化策略。
|
| 71 |
+
|
| 72 |
+
**灵活**
|
| 73 |
+
|
| 74 |
+
- 支持多种大语言模型,包括但不限于 [InternLM](https://huggingface.co/internlm)、[Mixtral-8x7B](https://huggingface.co/mistralai)、[Llama 2](https://huggingface.co/meta-llama)、[ChatGLM](https://huggingface.co/THUDM)、[Qwen](https://huggingface.co/Qwen)、[Baichuan](https://huggingface.co/baichuan-inc)。
|
| 75 |
+
- 支持多模态图文模型 LLaVA 的预训练与微调。利用 XTuner 训得模型 [LLaVA-InternLM2-20B](https://huggingface.co/xtuner/llava-internlm2-20b) 表现优异。
|
| 76 |
+
- 精心设计的数据管道,兼容任意数据格式,开源数据或自定义数据皆可快速上手。
|
| 77 |
+
- 支持 [QLoRA](http://arxiv.org/abs/2305.14314)、[LoRA](http://arxiv.org/abs/2106.09685)、全量参数微调等多种微调算法,支撑用户根据具体需求作出最优选择。
|
| 78 |
+
|
| 79 |
+
**全能**
|
| 80 |
+
|
| 81 |
+
- 支持增量预训练、指令微调与 Agent 微调。
|
| 82 |
+
- 预定义众多开源对话模版,支持与开源或训练所得模型进行对话。
|
| 83 |
+
- 训练所得模型可无缝接入部署工具库 [LMDeploy](https://github.com/InternLM/lmdeploy)、大规模评测工具库 [OpenCompass](https://github.com/open-compass/opencompass) 及 [VLMEvalKit](https://github.com/open-compass/VLMEvalKit)。
|
| 84 |
+
|
| 85 |
+
## 🔥 支持列表
|
| 86 |
+
|
| 87 |
+
<table>
|
| 88 |
+
<tbody>
|
| 89 |
+
<tr align="center" valign="middle">
|
| 90 |
+
<td>
|
| 91 |
+
<b>模型</b>
|
| 92 |
+
</td>
|
| 93 |
+
<td>
|
| 94 |
+
<b>数据集</b>
|
| 95 |
+
</td>
|
| 96 |
+
<td>
|
| 97 |
+
<b>数据格式</b>
|
| 98 |
+
</td>
|
| 99 |
+
<td>
|
| 100 |
+
<b>微调算法</b>
|
| 101 |
+
</td>
|
| 102 |
+
</tr>
|
| 103 |
+
<tr valign="top">
|
| 104 |
+
<td align="left" valign="top">
|
| 105 |
+
<ul>
|
| 106 |
+
<li><a href="https://huggingface.co/internlm">InternLM 2 / 2.5</a></li>
|
| 107 |
+
<li><a href="https://huggingface.co/meta-llama">Llama 2 / 3</a></li>
|
| 108 |
+
<li><a href="https://huggingface.co/collections/microsoft/phi-3-6626e15e9585a200d2d761e3">Phi-3</a></li>
|
| 109 |
+
<li><a href="https://huggingface.co/THUDM/chatglm2-6b">ChatGLM2</a></li>
|
| 110 |
+
<li><a href="https://huggingface.co/THUDM/chatglm3-6b">ChatGLM3</a></li>
|
| 111 |
+
<li><a href="https://huggingface.co/Qwen/Qwen-7B">Qwen</a></li>
|
| 112 |
+
<li><a href="https://huggingface.co/baichuan-inc/Baichuan2-7B-Base">Baichuan2</a></li>
|
| 113 |
+
<li><a href="https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1">Mixtral</a></li>
|
| 114 |
+
<li><a href="https://huggingface.co/deepseek-ai/DeepSeek-V2-Chat">DeepSeek V2</a></li>
|
| 115 |
+
<li><a href="https://huggingface.co/google">Gemma</a></li>
|
| 116 |
+
<li><a href="https://huggingface.co/openbmb">MiniCPM</a></li>
|
| 117 |
+
<li>...</li>
|
| 118 |
+
</ul>
|
| 119 |
+
</td>
|
| 120 |
+
<td>
|
| 121 |
+
<ul>
|
| 122 |
+
<li><a href="https://modelscope.cn/datasets/damo/MSAgent-Bench">MSAgent-Bench</a></li>
|
| 123 |
+
<li><a href="https://huggingface.co/datasets/fnlp/moss-003-sft-data">MOSS-003-SFT</a> 🔧</li>
|
| 124 |
+
<li><a href="https://huggingface.co/datasets/tatsu-lab/alpaca">Alpaca en</a> / <a href="https://huggingface.co/datasets/silk-road/alpaca-data-gpt4-chinese">zh</a></li>
|
| 125 |
+
<li><a href="https://huggingface.co/datasets/WizardLM/WizardLM_evol_instruct_V2_196k">WizardLM</a></li>
|
| 126 |
+
<li><a href="https://huggingface.co/datasets/timdettmers/openassistant-guanaco">oasst1</a></li>
|
| 127 |
+
<li><a href="https://huggingface.co/datasets/garage-bAInd/Open-Platypus">Open-Platypus</a></li>
|
| 128 |
+
<li><a href="https://huggingface.co/datasets/HuggingFaceH4/CodeAlpaca_20K">Code Alpaca</a></li>
|
| 129 |
+
<li><a href="https://huggingface.co/datasets/burkelibbey/colors">Colorist</a> 🎨</li>
|
| 130 |
+
<li><a href="https://github.com/WangRongsheng/ChatGenTitle">Arxiv GenTitle</a></li>
|
| 131 |
+
<li><a href="https://github.com/LiuHC0428/LAW-GPT">Chinese Law</a></li>
|
| 132 |
+
<li><a href="https://huggingface.co/datasets/Open-Orca/OpenOrca">OpenOrca</a></li>
|
| 133 |
+
<li><a href="https://huggingface.co/datasets/shibing624/medical">Medical Dialogue</a></li>
|
| 134 |
+
<li>...</li>
|
| 135 |
+
</ul>
|
| 136 |
+
</td>
|
| 137 |
+
<td>
|
| 138 |
+
<ul>
|
| 139 |
+
<li><a href="docs/zh_cn/user_guides/incremental_pretraining.md">Incremental Pre-training</a> </li>
|
| 140 |
+
<li><a href="docs/zh_cn/user_guides/single_turn_conversation.md">Single-turn Conversation SFT</a> </li>
|
| 141 |
+
<li><a href="docs/zh_cn/user_guides/multi_turn_conversation.md">Multi-turn Conversation SFT</a> </li>
|
| 142 |
+
</ul>
|
| 143 |
+
</td>
|
| 144 |
+
<td>
|
| 145 |
+
<ul>
|
| 146 |
+
<li><a href="http://arxiv.org/abs/2305.14314">QLoRA</a></li>
|
| 147 |
+
<li><a href="http://arxiv.org/abs/2106.09685">LoRA</a></li>
|
| 148 |
+
<li>全量参数微调</li>
|
| 149 |
+
<li><a href="https://arxiv.org/abs/2305.18290">DPO</a></li>
|
| 150 |
+
<li><a href="https://arxiv.org/abs/2403.07691">ORPO</a></li>
|
| 151 |
+
<li>Reward Model</a></li>
|
| 152 |
+
</ul>
|
| 153 |
+
</td>
|
| 154 |
+
</tr>
|
| 155 |
+
</tbody>
|
| 156 |
+
</table>
|
| 157 |
+
|
| 158 |
+
## 🛠️ 快速上手
|
| 159 |
+
|
| 160 |
+
### 安装
|
| 161 |
+
|
| 162 |
+
- 推荐使用 conda 先构建一个 Python-3.10 的虚拟环境
|
| 163 |
+
|
| 164 |
+
```bash
|
| 165 |
+
conda create --name xtuner-env python=3.10 -y
|
| 166 |
+
conda activate xtuner-env
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
- 通过 pip 安装 XTuner:
|
| 170 |
+
|
| 171 |
+
```shell
|
| 172 |
+
pip install -U xtuner
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
亦可集成 DeepSpeed 安装:
|
| 176 |
+
|
| 177 |
+
```shell
|
| 178 |
+
pip install -U 'xtuner[deepspeed]'
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
- 从源码安装 XTuner:
|
| 182 |
+
|
| 183 |
+
```shell
|
| 184 |
+
git clone https://github.com/InternLM/xtuner.git
|
| 185 |
+
cd xtuner
|
| 186 |
+
pip install -e '.[all]'
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
### 微调
|
| 190 |
+
|
| 191 |
+
XTuner 支持微调大语言模型。数据集预处理指南请查阅[文档](./docs/zh_cn/user_guides/dataset_prepare.md)。
|
| 192 |
+
|
| 193 |
+
- **步骤 0**,准备配置文件。XTuner 提供多个开箱即用的配置文件,用户可以通过下列命令查看:
|
| 194 |
+
|
| 195 |
+
```shell
|
| 196 |
+
xtuner list-cfg
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
或者,如果所提供的配置文件不能满足使用需求,请导出所提供的配置文件并进行相应更改:
|
| 200 |
+
|
| 201 |
+
```shell
|
| 202 |
+
xtuner copy-cfg ${CONFIG_NAME} ${SAVE_PATH}
|
| 203 |
+
vi ${SAVE_PATH}/${CONFIG_NAME}_copy.py
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
- **步骤 1**,开始微调。
|
| 207 |
+
|
| 208 |
+
```shell
|
| 209 |
+
xtuner train ${CONFIG_NAME_OR_PATH}
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
例如,我们可以利用 QLoRA 算法在 oasst1 数据集上微调 InternLM2.5-Chat-7B:
|
| 213 |
+
|
| 214 |
+
```shell
|
| 215 |
+
# 单卡
|
| 216 |
+
xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --deepspeed deepspeed_zero2
|
| 217 |
+
# 多卡
|
| 218 |
+
(DIST) NPROC_PER_NODE=${GPU_NUM} xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --deepspeed deepspeed_zero2
|
| 219 |
+
(SLURM) srun ${SRUN_ARGS} xtuner train internlm2_5_chat_7b_qlora_oasst1_e3 --launcher slurm --deepspeed deepspeed_zero2
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
- `--deepspeed` 表示使用 [DeepSpeed](https://github.com/microsoft/DeepSpeed) 🚀 来优化训练过程。XTuner 内置了多种策略,包括 ZeRO-1、ZeRO-2、ZeRO-3 等。如果用户期望关闭此功能,请直接移除此参数。
|
| 223 |
+
|
| 224 |
+
- 更多示例,请查阅[文档](./docs/zh_cn/user_guides/finetune.md)。
|
| 225 |
+
|
| 226 |
+
- **步骤 2**,将保存的 PTH 模型(如果使用的DeepSpeed,则将会是一个文件夹)转换为 HuggingFace 模型:
|
| 227 |
+
|
| 228 |
+
```shell
|
| 229 |
+
xtuner convert pth_to_hf ${CONFIG_NAME_OR_PATH} ${PTH} ${SAVE_PATH}
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
### 对话
|
| 233 |
+
|
| 234 |
+
XTuner 提供与大语言模型对话的工具。
|
| 235 |
+
|
| 236 |
+
```shell
|
| 237 |
+
xtuner chat ${NAME_OR_PATH_TO_LLM} --adapter {NAME_OR_PATH_TO_ADAPTER} [optional arguments]
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
例如:
|
| 241 |
+
|
| 242 |
+
与 InternLM2.5-Chat-7B 对话:
|
| 243 |
+
|
| 244 |
+
```shell
|
| 245 |
+
xtuner chat internlm/internlm2-chat-7b --prompt-template internlm2_chat
|
| 246 |
+
```
|
| 247 |
+
|
| 248 |
+
更多示例,请查阅[文档](./docs/zh_cn/user_guides/chat.md)。
|
| 249 |
+
|
| 250 |
+
### 部署
|
| 251 |
+
|
| 252 |
+
- **步骤 0**,将 HuggingFace adapter 合并到大语言模型:
|
| 253 |
+
|
| 254 |
+
```shell
|
| 255 |
+
xtuner convert merge \
|
| 256 |
+
${NAME_OR_PATH_TO_LLM} \
|
| 257 |
+
${NAME_OR_PATH_TO_ADAPTER} \
|
| 258 |
+
${SAVE_PATH} \
|
| 259 |
+
--max-shard-size 2GB
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
- **步骤 1**,使用任意推理框架部署微调后的大语言模型,例如 [LMDeploy](https://github.com/InternLM/lmdeploy) 🚀:
|
| 263 |
+
|
| 264 |
+
```shell
|
| 265 |
+
pip install lmdeploy
|
| 266 |
+
python -m lmdeploy.pytorch.chat ${NAME_OR_PATH_TO_LLM} \
|
| 267 |
+
--max_new_tokens 256 \
|
| 268 |
+
--temperture 0.8 \
|
| 269 |
+
--top_p 0.95 \
|
| 270 |
+
--seed 0
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
🔥 追求速度更快、显存占用更低的推理?欢迎体验 [LMDeploy](https://github.com/InternLM/lmdeploy) 提供的 4-bit 量化!使用指南请见[文档](https://github.com/InternLM/lmdeploy/tree/main#quantization)。
|
| 274 |
+
|
| 275 |
+
### 评测
|
| 276 |
+
|
| 277 |
+
- 推荐使用一站式平台 [OpenCompass](https://github.com/InternLM/opencompass) 来评测大语言模型,其目前已涵盖 50+ 数据集的约 30 万条题目。
|
| 278 |
+
|
| 279 |
+
## 🤝 贡献指南
|
| 280 |
+
|
| 281 |
+
我们感谢所有的贡献者为改进和提升 XTuner 所作出的努力。请参考[贡献指南](.github/CONTRIBUTING.md)来了解参与项目贡献的相关指引。
|
| 282 |
+
|
| 283 |
+
## 🎖️ 致谢
|
| 284 |
+
|
| 285 |
+
- [Llama 2](https://github.com/facebookresearch/llama)
|
| 286 |
+
- [DeepSpeed](https://github.com/microsoft/DeepSpeed)
|
| 287 |
+
- [QLoRA](https://github.com/artidoro/qlora)
|
| 288 |
+
- [LMDeploy](https://github.com/InternLM/lmdeploy)
|
| 289 |
+
- [LLaVA](https://github.com/haotian-liu/LLaVA)
|
| 290 |
+
|
| 291 |
+
## 🖊️ 引用
|
| 292 |
+
|
| 293 |
+
```bibtex
|
| 294 |
+
@misc{2023xtuner,
|
| 295 |
+
title={XTuner: A Toolkit for Efficiently Fine-tuning LLM},
|
| 296 |
+
author={XTuner Contributors},
|
| 297 |
+
howpublished = {\url{https://github.com/InternLM/xtuner}},
|
| 298 |
+
year={2023}
|
| 299 |
+
}
|
| 300 |
+
```
|
| 301 |
+
|
| 302 |
+
## 开源许可证
|
| 303 |
+
|
| 304 |
+
该项目采用 [Apache License 2.0 开源许可证](LICENSE)。同时,请遵守所使用的模型与数据集的许可证。
|
data/mydataset.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 介绍
|
| 2 |
+
|
| 3 |
+
作家yanyoyo写了一部名为《星际织梦:银河边缘的回响》的科幻小说。
|
| 4 |
+
|
| 5 |
+
# 内容
|
| 6 |
+
|
| 7 |
+
在浩瀚无垠的银河系边缘,有一颗名为艾泽拉的神秘星球。这颗星球上的文明高度发达,却又隐藏着不为人知的危机。它的天空总是被绚丽多彩的能量光带所笼罩,那是星球核心泄露的能量与宇宙射线交织而成的奇景,也是艾泽拉人赖以生存却又畏惧的力量源泉。
|
| 8 |
+
|
| 9 |
+
主人公林,是一位年轻且极具天赋的科学家,就职于艾泽拉最顶尖的科研机构——星耀研究院。他一头乌黑的短发下,是一双炯炯有神、总是闪烁着探索光芒的眼睛,身形矫健,透着一股对未知不屈的冲劲。林每日沉浸在实验室中,试图破解能量光带背后隐藏的宇宙密码,期望找到稳定星球核心、拯救艾泽拉于潜在覆灭危机的方法。
|
| 10 |
+
|
| 11 |
+
一日,林像往常一样在实验室里忙碌,无意间触发了一个古老的信号装置。瞬间,实验室被一道耀眼的蓝光笼罩,等光芒散去,林发现自己置身于一个陌生而又似曾相识的空间。周围的墙壁上闪烁着奇异的符文,这些符文像是有生命一般,不断流动、组合,向他诉说着一段被尘封的历史。
|
| 12 |
+
|
| 13 |
+
原来,艾泽拉星球并非一直如此繁荣。数百万年前,艾泽拉人曾经历过一场灭顶之灾,战争、疾病、资源枯竭让整个星球濒临崩溃。就在此时,一群来自遥远星系的神秘访客降临,他们带来了先进的科技与知识,帮助艾泽拉人重建家园,并引导他们利用星球核心的能量,开启了新的文明篇章。然而,在这看似慷慨相助的背后,却隐藏着一个惊天阴谋。这些神秘访客其实是宇宙中的星际掠夺者,他们在艾泽拉星球的核心深处埋下了一颗“定时炸弹”——一种能够逐渐吞噬星球能量、并在关键时刻引发大爆炸的装置,一旦爆炸,不仅艾泽拉会灰飞烟灭,周边星系都会受到波及。而如今,距离引爆时间已然所剩无几。
|
| 14 |
+
|
| 15 |
+
得知真相的林心急如焚,他决定踏上一段惊心动魄的星际之旅,寻找解除危机的方法。林带着自己最得力的助手,精通机械工程的艾丽,以及对宇宙能量有着独特感知力的灵能者凯,一同驾驶着他们改装后的星际飞船“曙光号”,向着银河系的中心进发。艾丽身形娇小,却有着一双巧手,能在错综复杂的机械部件中穿梭自如,修复飞船的任何故障;凯则一袭白色长袍,眼神深邃空灵,他能凭借灵能感知危险、探寻隐藏在宇宙迷雾中的线索。
|
| 16 |
+
|
| 17 |
+
他们的第一站是位于银河系悬臂交界处的星尘集市,这是一个汇聚了全宇宙各类生物、科技与情报的繁华之地。在这里,鱼龙混杂,稍有不慎就可能陷入万劫不复之地。林他们小心翼翼地穿梭在拥挤的街道,与形形色色的外星商贩讨价还价,试图从他们口中获取关于那颗“定时炸弹”以及解除方法的蛛丝马迹。功夫不负有心人,在一个偏僻的角落,他们遇到了一位隐居多年的老星际探险家,老探险家听闻他们的来意后,长叹一声,拿出了一份古老的星图,指向图上一个标记为“幻光深渊”的地方,说道:“传说那里藏着宇宙初创时的原始能量,若能将其引导,或许能中和你们星球核心的隐患,但那地方危险重重,进去的人从未有活着出来的。”
|
| 18 |
+
|
| 19 |
+
林他们没有丝毫犹豫,谢过老探险家后,便朝着“幻光深渊”全速前进。当接近“幻光深渊”时,飞船上的所有仪器都开始失灵,强大的引力拉扯着“曙光号”,仿佛要将它拽入无尽的黑暗。关键时刻,凯集中精神,用灵能包裹住飞船,勉强稳住了局势。林和艾丽则争分夺秒地抢修仪器,根据老探险家提供的线索,调整飞船的动力输出,试图以一种特殊的频率震动,与深渊中的原始能量产生共鸣,打开一条安全通道。
|
| 20 |
+
|
| 21 |
+
经过一番惊心动魄的操作,他们终于成功进入“幻光深渊”。眼前的景象让他们目瞪口呆,五彩斑斓的能量漩涡四处飞舞,每一个漩涡都蕴含着足以毁灭星球的力量。林凭借着扎实的科学知识,计算出能量共鸣的节点,指挥艾丽操控飞船小心翼翼地靠近。就在即将接触节点的瞬间,一只巨大的宇宙巨兽从深渊深处咆哮而出,它周身环绕着黑色的闪电,张开血盆大口,向着“曙光号”扑来。凯立刻挺身而出,用灵能构建起一道防护屏障,为林和艾丽争取时间。林当机立断,启动飞船上的能量采集装置,在艾丽精准的操作下,成功采集到了足够的原始能量。随后,凯拼尽全力,将灵能屏障化作一股冲击力,击退巨兽,三人驾驶飞船夺路而逃。
|
| 22 |
+
|
| 23 |
+
带着采集到的原始能量,他们马不停蹄地赶回艾泽拉星球。然而,归途中并不平静,���际掠夺者得知他们的行动后,派出了舰队进行拦截。一场激烈的星际大战在浩瀚星空展开,激光炮的光芒、导弹的轨迹交织成一片绚烂而致命的光网。林驾驶着“曙光号”灵活穿梭,艾丽负责操控武器系统,精准回击敌人,凯则运用灵能干扰掠夺者舰队的通讯与导航,让他们阵脚大乱。在激烈的交火中,“曙光号”多处受损,但凭借着三人的默契配合与顽强斗志,他们一次次化险为夷,突破重围,终于回到了艾泽拉星球。
|
| 24 |
+
|
| 25 |
+
回到家乡后,林迅速将原始能量引入星球核心,与那股危险的能量展开了一场惊心动魄的拔河比赛。在艾丽和凯的协助下,林不眠不休地调整能量参数,引导能量流向。整个星球都在颤抖,天空中的能量光带变得更加狂暴,仿佛在预示着这场生死较量的胜负未卜。关键时刻,林凭借着对科学的执着与对家乡的热爱,找到了能量平衡的关键点,成功解除了危机,让艾泽拉星球重归平静。
|
| 26 |
+
|
| 27 |
+
当一切尘埃落定,林成为了艾泽拉星球的英雄,他的故事被人们世代传颂。而这一段惊心动魄的星际冒险,也成为了艾泽拉人心中永远的传奇,激励着后来者不断探索宇宙的奥秘,守护家园的安宁。
|
| 28 |
+
|
| 29 |
+
这部小说的作者 yanyoyo,希望通过这个故事,带领读者畅游浩瀚宇宙,感受科幻世界中勇气、智慧与团结的力量,在未知的星际中寻找希望的曙光。
|
download_hf.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# 设置环境变量
|
| 4 |
+
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
|
| 5 |
+
|
| 6 |
+
# 下载模型
|
| 7 |
+
os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')
|
llamaindex_RAG.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 3 |
+
|
| 4 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 6 |
+
|
| 7 |
+
#初始化一个HuggingFaceEmbedding对象,用于将文本转换为向量表示
|
| 8 |
+
embed_model = HuggingFaceEmbedding(
|
| 9 |
+
#指定了一个预训练的sentence-transformer模型的路径
|
| 10 |
+
model_name="/root/model/sentence-transformer"
|
| 11 |
+
)
|
| 12 |
+
#将创建的嵌入模型赋值给全局设置的embed_model属性,
|
| 13 |
+
#这样在后续的索引构建过程中就会使用这个模型。
|
| 14 |
+
Settings.embed_model = embed_model
|
| 15 |
+
|
| 16 |
+
llm = HuggingFaceLLM(
|
| 17 |
+
model_name="/root/model/internlm2-chat-1_8b",
|
| 18 |
+
tokenizer_name="/root/model/internlm2-chat-1_8b",
|
| 19 |
+
model_kwargs={"trust_remote_code":True},
|
| 20 |
+
tokenizer_kwargs={"trust_remote_code":True}
|
| 21 |
+
)
|
| 22 |
+
#设置全局的llm属性,这样在索引查询时会使用这个模型。
|
| 23 |
+
Settings.llm = llm
|
| 24 |
+
|
| 25 |
+
#从指定目录读取所有文档,并加载数据到内存中
|
| 26 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 27 |
+
#创建一个VectorStoreIndex,并使用之前加载的文档来构建索引。
|
| 28 |
+
# 此索引将文档转换为向量,并存储这些向量以便于快速检索。
|
| 29 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 30 |
+
# 创建一个查询引擎,这个引擎可以接收查询并返回相关文档的响应。
|
| 31 |
+
query_engine = index.as_query_engine()
|
| 32 |
+
response = query_engine.query("xtuner是什么?")
|
| 33 |
+
|
| 34 |
+
print(response)
|
llamaindex_internlm.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 2 |
+
from llama_index.core.llms import ChatMessage
|
| 3 |
+
llm = HuggingFaceLLM(
|
| 4 |
+
model_name="/root/model/internlm2-chat-1_8b",
|
| 5 |
+
tokenizer_name="/root/model/internlm2-chat-1_8b",
|
| 6 |
+
model_kwargs={"trust_remote_code":True},
|
| 7 |
+
tokenizer_kwargs={"trust_remote_code":True}
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
rsp = llm.chat(messages=[ChatMessage(content="xtuner是什么?")])
|
| 11 |
+
print(rsp)
|