├── .gitignore ├── README.md ├── agent └── qwen2_openai_function_call │ ├── README.md │ ├── openai_api.py │ ├── openai_test.py │ └── qwen_generation_utils.py ├── ai-answer ├── capture.py ├── capture_png.png └── requirements.txt ├── ai-detection └── README.md ├── ai-digital └── README.md ├── ai-picture └── README.md ├── ai-video └── README.md ├── ai-voice └── README.md ├── assets ├── ai-app.png ├── guo.jpg ├── image-20240430221857315.png ├── landscape-latest.png ├── rag-技术路线.png ├── sologan.png ├── 基于知识库的大模型问答.png ├── 手把手教你大模型RAG框架架构 .jpg ├── 程序锅锅公众号-1714486122128.jpg └── 程序锅锅公众号.jpg ├── git-pull-push.sh ├── llm-api └── README.md ├── llm-data └── README.md ├── llm-rag ├── .env.example ├── RAG │ ├── Embeddings.py │ ├── FaissVectorBase.py │ ├── LLM.py │ ├── PaddleEmbedding.py │ ├── PostEmbedding.py │ ├── VectorBase.py │ ├── ZillizVectorStore.py │ ├── __pycache__ │ │ ├── Embeddings.cpython-310.pyc │ │ ├── FaissVectorBase.cpython-310.pyc │ │ ├── LLM.cpython-310.pyc │ │ ├── PaddleEmbedding.cpython-310.pyc │ │ ├── VectorBase.cpython-310.pyc │ │ ├── ZillizVectorStore.cpython-310.pyc │ │ └── utils.cpython-310.pyc │ ├── config.ini │ └── utils.py ├── README.md ├── assets │ └── image-20240429233550548.png ├── clk_test.py ├── data │ ├── README.md │ └── test.py ├── docement.json ├── dtest.py ├── example.py ├── images │ ├── RAG.png │ └── Retrieval-Augmented Generation(RAG-Learning).png ├── paper.md ├── pip_list.txt ├── requirements.txt ├── storage │ └── README.md └── tutorial │ ├── 01.如何调用大模型API │ ├── README.md │ └── test.py │ ├── 02.RAG介绍 │ ├── README.md │ └── 绘图1.eddx │ ├── 03.部署环境准备 │ └── test.py │ ├── 04.知识库构建 │ └── prepare.py │ ├── 05.基于知识库的大模型问答 │ ├── .env │ ├── 05-knowledge-prepare.ipynb │ ├── 05-knowledge-test.ipynb │ ├── RAG │ │ ├── Embeddings.py │ │ ├── FaissVectorBase.py │ │ ├── LLM.py │ │ ├── PaddleEmbedding.py │ │ ├── PostEmbedding.py │ │ ├── VectorBase.py │ │ ├── ZillizVectorStore.py │ │ ├── __pycache__ │ │ │ ├── Embeddings.cpython-311.pyc │ │ │ ├── Embeddings.cpython-38.pyc │ │ │ ├── PaddleEmbedding.cpython-311.pyc │ │ │ ├── PaddleEmbedding.cpython-38.pyc │ │ │ ├── VectorBase.cpython-311.pyc │ │ │ ├── VectorBase.cpython-38.pyc │ │ │ └── utils.cpython-38.pyc │ │ ├── config.ini │ │ └── utils.py │ └── README.md │ ├── 06.改进-用自己的embedding模型 │ ├── 06-self-embedding.ipynb │ └── README.md │ ├── 07.封装镜像对外提供服务 │ ├── ServerTest.py │ ├── app.py │ ├── prepare.py │ ├── response.py │ └── runserver.py │ ├── 08.改进-基于Faiss的大模型知识索引构建 │ ├── .env │ ├── 08-Faiss-prepare.ipynb │ ├── 08-Faiss-test.ipynb │ ├── Chapter08-Faiss.ipynb │ ├── RAG │ │ ├── Embeddings.py │ │ ├── FaissVectorBase.py │ │ ├── LLM.py │ │ ├── PaddleEmbedding.py │ │ ├── PostEmbedding.py │ │ ├── VectorBase.py │ │ ├── ZillizVectorStore.py │ │ ├── __pycache__ │ │ │ ├── Embeddings.cpython-311.pyc │ │ │ ├── Embeddings.cpython-38.pyc │ │ │ ├── PaddleEmbedding.cpython-311.pyc │ │ │ ├── PaddleEmbedding.cpython-38.pyc │ │ │ ├── VectorBase.cpython-311.pyc │ │ │ └── VectorBase.cpython-38.pyc │ │ ├── config.ini │ │ └── utils.py │ └── README.md │ ├── 09.改进-使用Milvus向量数据库 │ ├── .env │ ├── RAG │ │ ├── Embeddings.py │ │ ├── FaissVectorBase.py │ │ ├── LLM.py │ │ ├── PaddleEmbedding.py │ │ ├── PostEmbedding.py │ │ ├── VectorBase.py │ │ ├── ZillizVectorStore.py │ │ ├── __pycache__ │ │ │ ├── Embeddings.cpython-311.pyc │ │ │ ├── Embeddings.cpython-38.pyc │ │ │ ├── PaddleEmbedding.cpython-311.pyc │ │ │ ├── PaddleEmbedding.cpython-38.pyc │ │ │ ├── VectorBase.cpython-311.pyc │ │ │ └── VectorBase.cpython-38.pyc │ │ ├── config.ini │ │ └── utils.py │ ├── README.md │ ├── cloud-vectordb-examples │ │ ├── README.md │ │ ├── milvus-java │ │ │ ├── README.md │ │ │ ├── READMEServerless.md │ │ │ ├── pom.xml │ │ │ └── src │ │ │ │ └── main │ │ │ │ ├── java │ │ │ │ ├── demo │ │ │ │ │ ├── HelloZillizVectorDB.java │ │ │ │ │ └── HelloZillizVectorDBServerless.java │ │ │ │ └── util │ │ │ │ │ └── PropertyFilesUtil.java │ │ │ │ └── resources │ │ │ │ ├── RunSettings.properties │ │ │ │ └── RunSettingsServerless.properties │ │ ├── node │ │ │ ├── .gitignore │ │ │ ├── HelloZillizCloud.js │ │ │ ├── HelloZillizCloud.serverless.js │ │ │ ├── README.md │ │ │ ├── config.js │ │ │ ├── config.serverless.js │ │ │ ├── package.json │ │ │ ├── utils.js │ │ │ └── yarn.lock │ │ ├── python │ │ │ ├── README.md │ │ │ ├── README_serverless.md │ │ │ ├── config.ini │ │ │ ├── config_serverless.ini │ │ │ ├── hello_zilliz_vectordb.py │ │ │ └── hello_zilliz_vectordb_serverless.py │ │ └── zilliz_go │ │ │ ├── HelloZillizCloud.go │ │ │ ├── README.md │ │ │ ├── config.go │ │ │ ├── go.mod │ │ │ └── go.sum │ ├── config.ini │ ├── prepare.py │ └── test.py │ └── 10.前端构建 │ ├── config.ini │ ├── requirements.txt │ ├── test.py │ └── web_demo.py ├── llm-train ├── baichuan13B │ ├── README.md │ ├── examples │ │ ├── README_zh.md │ │ ├── inference │ │ │ ├── api_demo.sh │ │ │ ├── cli_demo.sh │ │ │ ├── vllm_demo.sh │ │ │ └── web_demo.sh │ │ ├── run_baichuan13B_merge_lora.sh │ │ └── run_baichuan13B_sft_lora.sh │ └── src │ │ ├── api_demo.py │ │ ├── cli_demo.py │ │ ├── evaluate.py │ │ ├── export_model.py │ │ ├── llmtuner │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ └── __init__.cpython-311.pyc │ │ ├── api │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── __init__.cpython-311.pyc │ │ │ │ ├── app.cpython-310.pyc │ │ │ │ ├── app.cpython-311.pyc │ │ │ │ └── protocol.cpython-310.pyc │ │ │ ├── app.py │ │ │ └── protocol.py │ │ ├── chat │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── base_engine.cpython-310.pyc │ │ │ │ ├── chat_model.cpython-310.pyc │ │ │ │ ├── hf_engine.cpython-310.pyc │ │ │ │ └── vllm_engine.cpython-310.pyc │ │ │ ├── base_engine.py │ │ │ ├── chat_model.py │ │ │ ├── hf_engine.py │ │ │ └── vllm_engine.py │ │ ├── data │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── aligner.cpython-310.pyc │ │ │ │ ├── collator.cpython-310.pyc │ │ │ │ ├── formatter.cpython-310.pyc │ │ │ │ ├── loader.cpython-310.pyc │ │ │ │ ├── parser.cpython-310.pyc │ │ │ │ ├── preprocess.cpython-310.pyc │ │ │ │ ├── template.cpython-310.pyc │ │ │ │ └── utils.cpython-310.pyc │ │ │ ├── aligner.py │ │ │ ├── collator.py │ │ │ ├── formatter.py │ │ │ ├── loader.py │ │ │ ├── parser.py │ │ │ ├── preprocess.py │ │ │ ├── template.py │ │ │ └── utils.py │ │ ├── eval │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── evaluator.cpython-310.pyc │ │ │ │ └── template.cpython-310.pyc │ │ │ ├── evaluator.py │ │ │ └── template.py │ │ ├── extras │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── callbacks.cpython-310.pyc │ │ │ │ ├── constants.cpython-310.pyc │ │ │ │ ├── logging.cpython-310.pyc │ │ │ │ ├── misc.cpython-310.pyc │ │ │ │ ├── packages.cpython-310.pyc │ │ │ │ └── ploting.cpython-310.pyc │ │ │ ├── callbacks.py │ │ │ ├── constants.py │ │ │ ├── logging.py │ │ │ ├── misc.py │ │ │ ├── packages.py │ │ │ └── ploting.py │ │ ├── hparams │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── data_args.cpython-310.pyc │ │ │ │ ├── evaluation_args.cpython-310.pyc │ │ │ │ ├── finetuning_args.cpython-310.pyc │ │ │ │ ├── generating_args.cpython-310.pyc │ │ │ │ ├── model_args.cpython-310.pyc │ │ │ │ └── parser.cpython-310.pyc │ │ │ ├── data_args.py │ │ │ ├── evaluation_args.py │ │ │ ├── finetuning_args.py │ │ │ ├── generating_args.py │ │ │ ├── model_args.py │ │ │ └── parser.py │ │ ├── model │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── adapter.cpython-310.pyc │ │ │ │ ├── loader.cpython-310.pyc │ │ │ │ └── patcher.cpython-310.pyc │ │ │ ├── adapter.py │ │ │ ├── loader.py │ │ │ ├── patcher.py │ │ │ └── utils │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── attention.cpython-310.pyc │ │ │ │ ├── checkpointing.cpython-310.pyc │ │ │ │ ├── embedding.cpython-310.pyc │ │ │ │ ├── longlora.cpython-310.pyc │ │ │ │ ├── misc.cpython-310.pyc │ │ │ │ ├── mod.cpython-310.pyc │ │ │ │ ├── moe.cpython-310.pyc │ │ │ │ ├── quantization.cpython-310.pyc │ │ │ │ ├── rope.cpython-310.pyc │ │ │ │ ├── unsloth.cpython-310.pyc │ │ │ │ ├── valuehead.cpython-310.pyc │ │ │ │ └── visual.cpython-310.pyc │ │ │ │ ├── attention.py │ │ │ │ ├── checkpointing.py │ │ │ │ ├── embedding.py │ │ │ │ ├── longlora.py │ │ │ │ ├── misc.py │ │ │ │ ├── mod.py │ │ │ │ ├── moe.py │ │ │ │ ├── quantization.py │ │ │ │ ├── rope.py │ │ │ │ ├── unsloth.py │ │ │ │ ├── valuehead.py │ │ │ │ └── visual.py │ │ ├── train │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── tuner.cpython-310.pyc │ │ │ │ └── utils.cpython-310.pyc │ │ │ ├── dpo │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── trainer.py │ │ │ │ └── workflow.py │ │ │ ├── orpo │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── trainer.py │ │ │ │ └── workflow.py │ │ │ ├── ppo │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ ├── utils.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── trainer.py │ │ │ │ ├── utils.py │ │ │ │ └── workflow.py │ │ │ ├── pt │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── trainer.py │ │ │ │ └── workflow.py │ │ │ ├── rm │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── metric.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── metric.py │ │ │ │ ├── trainer.py │ │ │ │ └── workflow.py │ │ │ ├── sft │ │ │ │ ├── __init__.py │ │ │ │ ├── __pycache__ │ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ │ ├── metric.cpython-310.pyc │ │ │ │ │ ├── trainer.cpython-310.pyc │ │ │ │ │ └── workflow.cpython-310.pyc │ │ │ │ ├── metric.py │ │ │ │ ├── trainer.py │ │ │ │ └── workflow.py │ │ │ ├── tuner.py │ │ │ └── utils.py │ │ └── webui │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ ├── chatter.cpython-310.pyc │ │ │ ├── common.cpython-310.pyc │ │ │ ├── css.cpython-310.pyc │ │ │ ├── engine.cpython-310.pyc │ │ │ ├── interface.cpython-310.pyc │ │ │ ├── locales.cpython-310.pyc │ │ │ ├── manager.cpython-310.pyc │ │ │ ├── runner.cpython-310.pyc │ │ │ └── utils.cpython-310.pyc │ │ │ ├── chatter.py │ │ │ ├── common.py │ │ │ ├── components │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── __init__.cpython-310.pyc │ │ │ │ ├── chatbot.cpython-310.pyc │ │ │ │ ├── data.cpython-310.pyc │ │ │ │ ├── eval.cpython-310.pyc │ │ │ │ ├── export.cpython-310.pyc │ │ │ │ ├── infer.cpython-310.pyc │ │ │ │ ├── top.cpython-310.pyc │ │ │ │ └── train.cpython-310.pyc │ │ │ ├── chatbot.py │ │ │ ├── data.py │ │ │ ├── eval.py │ │ │ ├── export.py │ │ │ ├── infer.py │ │ │ ├── top.py │ │ │ └── train.py │ │ │ ├── css.py │ │ │ ├── engine.py │ │ │ ├── interface.py │ │ │ ├── locales.py │ │ │ ├── manager.py │ │ │ ├── runner.py │ │ │ └── utils.py │ │ ├── train_bash.py │ │ ├── train_web.py │ │ └── web_demo.py ├── comment │ ├── README.md │ ├── lora权重 │ │ └── 网盘权重链接.txt │ └── requirement.txt ├── gpt2 │ ├── README.md │ ├── fineweb.py │ ├── gpt2.log │ ├── hellaswag.py │ ├── log │ │ └── log.txt │ ├── play.ipynb │ └── train_gpt2.py └── lora │ ├── README.md │ └── lora_finetuning.ipynb ├── paper └── LLM_Survey_Chinese.pdf └── pic └── awesome-ai-agents ├── README.md ├── assets ├── .DS_Store ├── footer.png └── landscape-latest.png ├── image-1.png ├── image-2.png └── image.png /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/.gitignore -------------------------------------------------------------------------------- /agent/qwen2_openai_function_call/README.md: -------------------------------------------------------------------------------- 1 | 1、openai_api.py 2 | 3 | - 封装本地qwen2模型,支持OpenAI API接口规范对外提供服务。 4 | - 支持function call 5 | 6 | 2、openai_test.py 7 | 8 | 测试代码 9 | 10 | 3、qwen_generation_utils.py 11 | 12 | 依赖文件 -------------------------------------------------------------------------------- /agent/qwen2_openai_function_call/openai_test.py: -------------------------------------------------------------------------------- 1 | from openai import OpenAI 2 | client = OpenAI(api_key="sk-...",base_url="http://localhost:8000/v1") 3 | completion = client.chat.completions.create(model="gpt-3.5-turbo", 4 | messages=[ 5 | {"role": "user", "content": "What is the weather like in Boston?"}, 6 | {"role": "assistant", "content": "", "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}}, 7 | {"role": "function", "name": "get_current_weather", "content": """{\"temperature\": "22", \"unit\": \"celsius\", \"description\": \"Sunny\"}"""}], 8 | functions=[ 9 | { 10 | "name": "get_current_weather", 11 | "description": "Get the current weather in a given location", 12 | "parameters": { 13 | "type": "object", 14 | "properties": { 15 | "location": { 16 | "type": "string", 17 | "description": "The city and state, e.g. San Francisco, CA" 18 | }, 19 | "unit": { 20 | "type": "string", 21 | "enum": ["celsius", "fahrenheit"] 22 | } 23 | }, 24 | "required": ["location"] 25 | } 26 | } 27 | ] 28 | ) 29 | 30 | print(completion.choices[0].message) -------------------------------------------------------------------------------- /ai-answer/capture_png.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/ai-answer/capture_png.png -------------------------------------------------------------------------------- /ai-answer/requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2019.11.28 2 | chardet==3.0.4 3 | idna==2.8 4 | Pillow==6.2.1 5 | requests==2.22.0 6 | urllib3==1.25.7 7 | pillow -------------------------------------------------------------------------------- /ai-detection/README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

6 | 7 | 8 | ## 目录 9 | 10 | - 🔥 [服务器基础环境安装及常用工具](#服务器基础环境安装及常用工具) 11 | - 🐫[PyCharm安装](#pycharm安装) 12 | - 🐼[Anaconda安装及原理介绍](#Anaconda安装) 13 | - 💪[实战:用VSCode远程服务器开发](https://zhuanlan.zhihu.com/p/693420628) 14 | - 🐎 [大模型调用总结](#大模型调用) 15 | - 🐎[主流大模型API调用总结](#主流大模型API调用总结) 16 | - 🍚[大模型部署推理](#大模型部署推理) 17 | - 🍄[检索增强生成(RAG)](#检索增强生成) 18 | - 🗼[RAG原理介绍](#检索增强生成) 19 | - 🌼 [ragflow介绍](#ragflow) 20 | - 💪 [实战:自己手写一个最简单的RAG](#实战-手写一个最简单的RAG) 21 | - 💪 [实战:基于ragflow做了款初中历史辅导工具](#实战-基于ragflow做一款初中历史辅导工具) 22 | - 🎪[向量数据库技术](#向量数据库) 23 | - 🐮 [向量数据库原理介绍](#向量数据库原理介绍) 24 | - 💪 [实战-自己手写一个最简单的向量数据库](#实战-自己手写一个最简单的向量数据库) 25 | - 🚅 [大模型训练](#大模型训练) 26 | - ❄️[模型训练基础知识](#模型训练基础知识) 27 | - 🚀 [大模型训练神器LLaMAFactory](#LLaMAFactory大模型训练) 28 | - 💪[实战-用LLaMAFactory微调数百种大模型](#实战-基于LLaMAFactory微调) 29 | - 💪[实战-微调大模型实现商品评价情感预测](#实战-大模型做情感预测) 30 | - 🚀 [大模型Agent框架](#Agent) 31 | - 🏠 [Agent原理介绍](#Agent原理介绍) 32 | - 💪 [实战-自己手写一个最简单的Agent](#自己手写一个最简单的Agent) 33 | - ♻️ [AI图像生成](#AI图像生成) 34 | - 📐 [StableDiffusion1.5](#StableDiffusion1.5) 35 | - 💎 [SDXL](#SDXL) 36 | - 🍦 [ControlNet](#ControlNet) 37 | - 💪 [实战-基于easyphoto实现AI换脸](#实战-基于easyphoto实现AI换脸) 38 | - :house_with_garden: [AI语音合成](#AI语音合成) 39 | - ♻️ [VITS-fast-fine-tuning](#VITS-fast-fine-tuning) 40 | - 📐 [GPT-SoVITS](#GPT-SoVITS) 41 | - 💪 [实战-克隆自己的声音](#克隆自己的声音) 42 | - 🎵 [AI音乐合成](#AI音乐合成) 43 | - 📐 [Suno](#Suno) 44 | - 📞 [我用AI技术做了一个虚拟女友](#我用AI技术做了一个虚拟女友) 45 | - 💬 [AI技术应用交流群](#AI技术应用交流群) 46 | - 👥 [微信公众号](#微信公众号) -------------------------------------------------------------------------------- /ai-digital/README.md: -------------------------------------------------------------------------------- 1 | # ai-app 2 | 本项目旨在分享人工智能相关应用技术原理以及实战经验,包括大模型、语音合成、数字人、图像生成等。 3 | -------------------------------------------------------------------------------- /ai-picture/README.md: -------------------------------------------------------------------------------- 1 | # ai-app 2 | 本项目旨在分享人工智能相关应用技术原理以及实战经验,包括大模型、语音合成、数字人、图像生成等。 3 | -------------------------------------------------------------------------------- /ai-video/README.md: -------------------------------------------------------------------------------- 1 | # ai-app 2 | 本项目旨在分享人工智能相关应用技术原理以及实战经验,包括大模型、语音合成、数字人、图像生成等。 3 | -------------------------------------------------------------------------------- /ai-voice/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/ai-voice/README.md -------------------------------------------------------------------------------- /assets/ai-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/ai-app.png -------------------------------------------------------------------------------- /assets/guo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/guo.jpg -------------------------------------------------------------------------------- /assets/image-20240430221857315.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/image-20240430221857315.png -------------------------------------------------------------------------------- /assets/landscape-latest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/landscape-latest.png -------------------------------------------------------------------------------- /assets/rag-技术路线.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/rag-技术路线.png -------------------------------------------------------------------------------- /assets/sologan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/sologan.png -------------------------------------------------------------------------------- /assets/基于知识库的大模型问答.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/基于知识库的大模型问答.png -------------------------------------------------------------------------------- /assets/手把手教你大模型RAG框架架构 .jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/手把手教你大模型RAG框架架构 .jpg -------------------------------------------------------------------------------- /assets/程序锅锅公众号-1714486122128.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/程序锅锅公众号-1714486122128.jpg -------------------------------------------------------------------------------- /assets/程序锅锅公众号.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/assets/程序锅锅公众号.jpg -------------------------------------------------------------------------------- /git-pull-push.sh: -------------------------------------------------------------------------------- 1 | git pull 2 | git add . 3 | 4 | #time=`date -Iminutes` 5 | time=`date +"%Y-%m-%d_%H:%M:%S"` 6 | 7 | echo $time 8 | 9 | commit_info="update-""$time" 10 | 11 | git commit -m $commit_info 12 | 13 | git push origin main 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /llm-api/README.md: -------------------------------------------------------------------------------- 1 | ### 可用训练数据整理 2 | 3 | 4 | | 数据集 | 介绍 | 5 | | ------------------------------------------------------------ | ------------------------------------------------------------ | 6 | | [firefly-train-1.1M](https://huggingface.co/datasets/YeungNLP/firefly-train-1.1M) | 包含了23种常见的中文NLP任务的数据,并且构造了许多与中华文化相关的数据,如对联、作诗、文言文翻译、散文、金庸小说等。对于每个任务,由人工书写若干种指令模板,保证数据的高质量与丰富度,数据量为115万。 | 7 | | [shareAI/CodeChat](https://huggingface.co/datasets/shareAI/CodeChat) | 主要包含逻辑推理、代码问答、代码生成相关语料样本。 | 8 | | [shareAI/ShareGPT-Chinese-English-90k](https://huggingface.co/datasets/shareAI/ShareGPT-Chinese-English-90k) | 中英文平行双语优质人机问答数据集,覆盖真实复杂场景下的用户提问。(包含大量多轮对话) | 9 | | [ruozhiba](https://huggingface.co/datasets/LooksJuicy/ruozhiba) | 弱智吧数据问答,据说比较锻炼模型的心智能力。 | 10 | | [DPO-EN-ZH-20k](https://huggingface.co/datasets/hiyouga/DPO-En-Zh-20k) | 包含大量偏好对齐的问答对数据<好,差>,有助于进一步提升chat模型的对话质量,使其生成内容更加详细、适合人类偏好。 | 11 | | [glaive-function-calling-v2-sharegpt](https://huggingface.co/datasets/hiyouga/glaive-function-calling-v2-sharegpt) | 包含大量工具函数选择、调用和具体参数数据,有助于提升模型的自主工具选择与使用能力。 | 12 | | [Agent-FLAN](https://huggingface.co/datasets/internlm/Agent-FLAN) | (纯英文)类型同上, 包含大量工具使用数据,有助于提升模型的工具使用能力。 | 13 | | [Agent-Instruct](https://huggingface.co/datasets/THUDM/AgentInstruct) | (纯英文)类型同上, 包含大量agent演示数据,有助于提升模型的工具使用、模拟能力。 | 14 | | [CogVLM-sft-311K](https://huggingface.co/datasets/THUDM/CogVLM-SFT-311K) | (中文) 包含带图片问答数据,可以训练模型看图问答、看图生成代码能力。 | 15 | | [ShareGPT4-V ](https://huggingface.co/datasets/Lin-Chen/ShareGPT4V) | (英文) 类型同上,包含带图片问答数据,可以训练模型看图问答、看图生成代码能力。 | 16 | | [web-QA](https://huggingface.co/datasets/THUDM/webglm-qa) | (纯英文) 包含大量(网页文章 -> 问题 -> 答案)数据,可以提升模型在RAG、文档问答、网页问答等垂直场景表现能力。欢迎翻译成中文进行开源 | 17 | | [Humaneval-x](https://huggingface.co/datasets/THUDM/humaneval-x) | (纯英文) 包含cpp、java、go、js等代码的测试数据,可以评测模型生成代码能力。 | 18 | | [longBench](https://huggingface.co/datasets/THUDM/LongBench) | (中、英文) 包含长样本问答数据,可以评测模型在输入内容比较长时候的任务能力。(长上下文) | 19 | | [Llama3 中文化数据集](https://modelscope.cn/datasets/baicai003/Llama3-Chinese-dataset/summary) | 该数据集已统一处理为firefly格式,可以配合firefly工具直接训练llama3中文模型。 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 欢迎提issue补充,要求中文且一问一答形式,适合用于提升llama3任务能力的数据集 -------------------------------------------------------------------------------- /llm-rag/.env.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY='your openai key' 2 | OPENAI_BASE_URL='https://api.openai.com/v1' 3 | 4 | ZHIPUAI_API_KEY='your zhipuai key' -------------------------------------------------------------------------------- /llm-rag/RAG/FaissVectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | 10 | import faiss 11 | 12 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 13 | import numpy as np 14 | from tqdm import tqdm 15 | 16 | from RAG.VectorBase import VectorStore 17 | 18 | 19 | class FaissVectorStore(VectorStore): 20 | 21 | def __init__(self, document: List[str] = ['']) -> None: 22 | super().__init__(document) 23 | self.document = document 24 | 25 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 26 | self.vectors = [] 27 | for doc in tqdm(self.document, desc="Calculating embeddings"): 28 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 29 | return self.vectors 30 | 31 | def persist(self, path: str = 'storage'): 32 | if not os.path.exists(path): 33 | os.makedirs(path) 34 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 35 | json.dump(self.document, f, ensure_ascii=False) 36 | if self.vectors: 37 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 38 | json.dump(self.vectors, f) 39 | 40 | def load_vector(self, path: str = 'storage'): 41 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 42 | self.vectors = json.load(f) 43 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 44 | self.document = json.load(f) 45 | 46 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 47 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 48 | 49 | def generateIndexFlatL2(self): 50 | dim, measure = 768, faiss.METRIC_L2 51 | param = 'Flat' 52 | vectors = np.array([vector for vector in self.vectors]).astype('float32') 53 | index = faiss.index_factory(dim, param, measure) 54 | index.add(vectors) # 将向量库中的向量加入到index中 55 | return index 56 | 57 | 58 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 59 | 60 | start_time = time.time() 61 | index = self.generateIndexFlatL2() 62 | end_time = time.time() 63 | print(' 构建索引 cost %f second' % (end_time - start_time)) 64 | query_vector = np.array([EmbeddingModel.get_embedding(query)]).astype("float32") 65 | 66 | end_time = time.time() 67 | D, I = index.search(query_vector, k) # xq为待检索向量,返回的I为每个待检索query最相似TopK的索引list,D为其对应的距离 68 | print(' 检索 cost %f second' % (time.time() - end_time)) 69 | return np.array(self.document)[I[0,:]].tolist() 70 | 71 | 72 | -------------------------------------------------------------------------------- /llm-rag/RAG/PaddleEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PaddleEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self, 11 | model:str="rocketqa-zh-base-query-encoder", 12 | batch_size:int =1, 13 | **kwargs 14 | 15 | ) -> None: 16 | self.client = Taskflow("feature_extraction",model=model ,batch_size = batch_size,return_tensors='np',**kwargs) 17 | 18 | def get_embedding(self, text: str) -> List[float]: 19 | text_embeds = self.client([text]) 20 | result = text_embeds["features"] 21 | data = result[0] 22 | return data.tolist() 23 | 24 | def get_embeddings(self, text: List[str]) -> List[List[float]]: 25 | text_embeds = self.client(text) 26 | result = text_embeds["features"] 27 | data = result 28 | return data.tolist() 29 | 30 | -------------------------------------------------------------------------------- /llm-rag/RAG/PostEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PostEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self,model:str 11 | ) -> None: 12 | self.client = Taskflow("feature_extraction",model=model ,return_tensors='np') 13 | 14 | def get_embedding(self, text: str,model:str = "rocketqa-zh-base-query-encoder") -> List[float]: 15 | text_embeds = self.client([text]) 16 | result = text_embeds["features"] 17 | data = result[0] 18 | return data.tolist() 19 | 20 | def get_embeddings(self, text: List[str],model:str) -> List[List[float]]: 21 | 22 | text_embeds = self.client(text) 23 | result = text_embeds["features"] 24 | data = result 25 | return data.tolist() 26 | 27 | -------------------------------------------------------------------------------- /llm-rag/RAG/VectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 10 | import numpy as np 11 | from tqdm import tqdm 12 | 13 | 14 | class VectorStore(): 15 | def __init__(self, document: List[str] = ['']) -> None: 16 | self.document = document 17 | 18 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 19 | 20 | self.vectors = [] 21 | for doc in tqdm(self.document, desc="Calculating embeddings"): 22 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 23 | return self.vectors 24 | 25 | def batch_split_list(self, lst,batch_size): 26 | return [lst[i:i + batch_size] for i in range(0, len(lst), batch_size)] 27 | 28 | def get_vector_batch(self, EmbeddingModel: BaseEmbeddings, batch:int) -> List[List[float]]: 29 | 30 | self.vectors = [] 31 | self.document = self.batch_split_list(self.document, batch) 32 | for doc in tqdm(self.document, desc="Calculating embeddings"): 33 | self.vectors.extend(EmbeddingModel.get_embeddings(doc)) 34 | return self.vectors 35 | 36 | def persist(self, path: str = 'storage'): 37 | if not os.path.exists(path): 38 | os.makedirs(path) 39 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 40 | json.dump(self.document, f, ensure_ascii=False) 41 | if self.vectors: 42 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 43 | json.dump(self.vectors, f) 44 | 45 | def load_vector(self, path: str = 'storage'): 46 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 47 | self.vectors = json.load(f) 48 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 49 | self.document = json.load(f) 50 | 51 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 52 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 53 | 54 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 55 | query_vector = EmbeddingModel.get_embedding(query) 56 | 57 | end_time = time.time() 58 | 59 | result = np.array([self.get_similarity(query_vector, vector) 60 | for vector in self.vectors]) 61 | print(' 检索 cost %f second' % (time.time() - end_time)) 62 | return np.array(self.document)[result.argsort()[-k:][::-1]].tolist() 63 | -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/Embeddings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/Embeddings.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/FaissVectorBase.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/FaissVectorBase.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/LLM.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/LLM.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/PaddleEmbedding.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/PaddleEmbedding.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/VectorBase.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/VectorBase.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/ZillizVectorStore.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/ZillizVectorStore.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/RAG/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /llm-rag/RAG/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/assets/image-20240429233550548.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/assets/image-20240429233550548.png -------------------------------------------------------------------------------- /llm-rag/clk_test.py: -------------------------------------------------------------------------------- 1 | import tiktoken_ext.openai_public 2 | import inspect 3 | 4 | print(dir(tiktoken_ext.openai_public)) 5 | # The encoder we want is cl100k_base, we see this as a possible function 6 | 7 | print(inspect.getsource(tiktoken_ext.openai_public.cl100k_base)) 8 | # The URL should be in the 'load_tiktoken_bpe function call' 9 | 10 | import hashlib 11 | 12 | # 我的blobpath是https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken 13 | blobpath = "https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken" 14 | cache_key = hashlib.sha1(blobpath.encode()).hexdigest() 15 | print(cache_key) 16 | 17 | import os 18 | print(os.getenv("ZHIPUAI_API_KEY")) 19 | -------------------------------------------------------------------------------- /llm-rag/data/README.md: -------------------------------------------------------------------------------- 1 | 此目录用于存放原始数据 2 | 3 | 数据链接:https://pan.baidu.com/s/1zxuk7KPtp6kr_mWnM6Sikg?pwd=y0fk 提取码:y0fk -------------------------------------------------------------------------------- /llm-rag/data/test.py: -------------------------------------------------------------------------------- 1 | 哈哈哈 -------------------------------------------------------------------------------- /llm-rag/dtest.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | from typing import Dict, List, Optional, Tuple, Union 4 | 5 | import PyPDF2 6 | import markdown 7 | import html2text 8 | import json 9 | from tqdm import tqdm 10 | import tiktoken 11 | from bs4 import BeautifulSoup 12 | import re 13 | 14 | enc = tiktoken.get_encoding("cl100k_base") 15 | 16 | 17 | file_path = "./data/history_24/baihuabeishi.txt" 18 | 19 | with open(file_path, 'r', encoding='utf-8') as file: 20 | content = file.read() 21 | 22 | 23 | 24 | curr_len = 0 25 | curr_chunk = '' 26 | min_token_len = 600 27 | max_token_len = 1600 28 | cover_content = 150 29 | lines = content.split('\n') # 假设以换行符分割文本为行 30 | chunk_text = [] 31 | chunk_lenth = [] 32 | 33 | for line in lines: 34 | line = line.replace(' ', '') 35 | line_len = len(enc.encode(line)) 36 | if curr_len > max_token_len: 37 | while (curr_len > max_token_len): 38 | split_a = enc.encode(curr_chunk)[:max_token_len] 39 | split_b = enc.encode(curr_chunk)[max_token_len:] 40 | curr_chunk = enc.decode(split_a) 41 | chunk_text.append(curr_chunk) 42 | chunk_lenth.append(max_token_len) 43 | curr_chunk = curr_chunk[-cover_content:] + enc.decode(split_b) 44 | curr_len = cover_content + curr_len - max_token_len 45 | else: 46 | 47 | if(curr_len <= min_token_len): 48 | curr_chunk += line 49 | curr_chunk += '\n' 50 | curr_len += line_len 51 | curr_len += 1 52 | else: 53 | chunk_text.append(curr_chunk) 54 | chunk_lenth.append(curr_len) 55 | curr_chunk = curr_chunk[-cover_content:] + line 56 | curr_len = line_len + cover_content 57 | 58 | if curr_chunk: 59 | chunk_text.append(curr_chunk) 60 | chunk_lenth.append(curr_len) 61 | 62 | chunk_str =json.dumps(chunk_text,indent=4,sort_keys=True,ensure_ascii=False)#设置缩进级别为4# 排序键sort keys=True 63 | 64 | 65 | with open(f"./docement.json", 'w', encoding='utf-8') as f: 66 | json.dump(chunk_str, f,ensure_ascii=False) 67 | 68 | print(chunk_text) 69 | 70 | print(chunk_lenth) 71 | 72 | print(max(chunk_lenth)) 73 | print(f"chunk_lenth中最大为:{max(chunk_lenth)};\nchunk_lenth中最小为:{min(chunk_lenth)};\nchunk_text中有{len(chunk_text)}个\n") 74 | -------------------------------------------------------------------------------- /llm-rag/example.py: -------------------------------------------------------------------------------- 1 | from RAG.VectorBase import VectorStore 2 | from RAG.utils import ReadFiles 3 | from RAG.LLM import OpenAIChat, InternLMChat, GLM4Chat 4 | from RAG.Embeddings import JinaEmbedding, ZhipuEmbedding 5 | 6 | 7 | # 保存数据库之后 8 | vector = VectorStore() 9 | 10 | vector.load_vector('./storage') # 加载本地的数据库 11 | 12 | question = 'git如何申请个人访问令牌?' 13 | 14 | embedding = ZhipuEmbedding() # 创建EmbeddingModel 15 | 16 | content = vector.query(question, EmbeddingModel=embedding, k=1)[0] 17 | #chat = OpenAIChat(model='gpt-3.5-turbo-1106') 18 | chat = GLM4Chat() 19 | print(chat.chat(question, [], content)) 20 | 21 | -------------------------------------------------------------------------------- /llm-rag/images/RAG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/images/RAG.png -------------------------------------------------------------------------------- /llm-rag/images/Retrieval-Augmented Generation(RAG-Learning).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/images/Retrieval-Augmented Generation(RAG-Learning).png -------------------------------------------------------------------------------- /llm-rag/paper.md: -------------------------------------------------------------------------------- 1 | # RAG (Retrieved-Augmented Generation) for LLM:A Curated Collection 2 | 3 | This Markdown is dedicated to curating high-quality papers, resources, and tools related to RAG in the context of Large Language Models (LLM). RAG bridges the gap between retrieval-based and generation-based methods, offering a promising approach for knowledge-intensive tasks. 4 | 5 | | Name | Paper Link | 6 | | ------------------------------------------------------------ | ----------------------------------------- | 7 | | When Large Language Models Meet Vector Databases: A Survey | [paper](http://arxiv.org/abs/2402.01763) | 8 | | Retrieval-Augmented Generation for Large Language Models: A Survey | [paper](https://arxiv.org/abs/2312.10997) | 9 | | Learning to Filter Context for Retrieval-Augmented Generation | [paper](http://arxiv.org/abs/2311.08377) | 10 | | In-Context Retrieval-Augmented Language Models | [paper](https://arxiv.org/abs/2302.00083) | 11 | 12 | -------------------------------------------------------------------------------- /llm-rag/requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | zhipuai 3 | numpy 4 | python-dotenv 5 | torch 6 | torchvision 7 | torchaudio 8 | transformers 9 | tqdm 10 | PyPDF2 11 | markdown 12 | html2text 13 | tiktoken 14 | beautifulsoup4 15 | fastapi 16 | uvicorn 17 | loguru 18 | pipetool 19 | opsapp -------------------------------------------------------------------------------- /llm-rag/storage/README.md: -------------------------------------------------------------------------------- 1 | 此目录用于存放知识库。 2 | 3 | 数据链接:https://pan.baidu.com/s/1zxuk7KPtp6kr_mWnM6Sikg?pwd=y0fk 提取码:y0fk -------------------------------------------------------------------------------- /llm-rag/tutorial/01.如何调用大模型API/README.md: -------------------------------------------------------------------------------- 1 | 大家好,我是程序锅。 2 | 3 | github上的代码封装程度高,不利于小白学习入门。 4 | 5 | 6 | 常规的大模型RAG框架有langchain等,但是langchain等框架源码理解困难,debug源码上手难度大。 7 | 8 | 因此,写一个人人都能看懂、人人都能修改的大模型RAG框架代码。 9 | 10 | 主要分为4个章节,分别为: 11 | 12 | **1. 如何调用大模型API** 13 | 14 | 2. RAG背景介绍 15 | 16 | 03. 依赖环境安装 17 | 18 | 04. 知识库构建 19 | 20 | 05. 基于知识库的大模型问答 21 | 22 | 本篇文章将介绍如何调用大模型API 23 | 24 | ## 一、大模型API介绍 25 | 26 | 大模型的使用可以分为本地调用和远程调用。由于本地硬件资源有限,我们一般选择远程调用大模型(后续小项目,也可自己本地部署大模型)。 27 | 28 | 目前市面上的大模型有ChatGPT、GPT4、GLM4、文心一言等等,OpenAI的产品由于有关原因被限制,在此我们采用智谱AI的**GLM4**作为实验对象。 29 | 30 | 1. 登录智谱AI开放平台获取获取API_key 31 | 32 | 33 | (1) 注册账号 34 | 35 | >https://maas.aminer.cn/ 36 | 37 | ![image](https://files.mdnice.com/user/30915/740dbe02-f714-420e-85b6-04a274e19d37.png) 38 | 39 | 40 | 新注册账号有18元的额度,足够实验了。 41 | 42 | (2) 获取API_KEY 43 | 44 | 45 | ![1](https://files.mdnice.com/user/30915/ebb45bef-f5b9-4e99-b89a-0340902fb249.png) 46 | 47 | 48 | 49 | ![2](https://files.mdnice.com/user/30915/f12c7022-35bc-421d-a695-fe297a791c7b.png) 50 | 51 | 注意,请不要泄露自己的API Keys! 52 | 53 | 2. API文档说明 54 | 55 | 请牢记上图的API key,现在我们看一看GLM4的API调用文档。 56 | 57 | (1)查看接口文档 58 | ![image](https://files.mdnice.com/user/30915/776e4a61-1c36-40c2-83d8-07a4d2389a4d.png) 59 | 60 | (2)查看调用示例 61 | ![image](https://files.mdnice.com/user/30915/d1a6f619-987d-4a7b-b79e-faed77d1e80a.png) 62 | 63 | 当然,我这里只讨论最简单的API调用形式,还有很多接口的参数没有利用到。 64 | 65 | 后续我们的小项目会根据实际需求,修改代码。 66 | 67 | ## 三、代码实现 68 | 69 | 接下来,代码实现它。 70 | 71 | ``` python 72 | from zhipuai import ZhipuAI 73 | client = ZhipuAI(api_key="") # 填写您自己的APIKey 74 | response = client.chat.completions.create( 75 | model="glm-4", # 填写需要调用的模型名称 76 | messages=[ 77 | {"role": "user", "content": "作为一名营销专家,请为我的产品创作一个吸引人的slogan"}, 78 | {"role": "assistant", "content": "当然,为了创作一个吸引人的slogan,请告诉我一些关于您产品的信息"}, 79 | {"role": "user", "content": "智谱AI开放平台"}, 80 | {"role": "assistant", "content": "智启未来,谱绘无限一智谱AI,让创新触手可及!"}, 81 | {"role": "user", "content": "创造一个更精准、吸引人的slogan"} 82 | ], 83 | ) 84 | print(response.choices[0].message) 85 | ``` 86 | 87 | 这里修改API key即可 88 | ![image](https://files.mdnice.com/user/30915/1bfb2c48-3223-4430-9871-6c82d277c41b.png) 89 | 90 | 这样就完成了一个非常简单的大模型API调用,下一章我们将介绍RAG的思路。 -------------------------------------------------------------------------------- /llm-rag/tutorial/01.如何调用大模型API/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from zhipuai import ZhipuAI 4 | client = ZhipuAI(api_key=os.getenv("ZHIPUAI_API_KEY")) # 填写您自己的APIKey 5 | response = client.chat.completions.create( 6 | model="glm-4", # 填写需要调用的模型名称 7 | messages=[ 8 | {"role": "user", "content": "作为一名自媒体工作者,请为我的产品创作一个吸引人的slogan"}, 9 | {"role": "assistant", "content": "当然,为了创作一个吸引人的slogan,请告诉我一些关于您产品的信息"}, 10 | {"role": "user", "content": "程序锅讲大模型"} 11 | ] 12 | 13 | ) 14 | print(response.choices[0].message) -------------------------------------------------------------------------------- /llm-rag/tutorial/02.RAG介绍/绘图1.eddx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/02.RAG介绍/绘图1.eddx -------------------------------------------------------------------------------- /llm-rag/tutorial/03.部署环境准备/test.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/03.部署环境准备/test.py -------------------------------------------------------------------------------- /llm-rag/tutorial/04.知识库构建/prepare.py: -------------------------------------------------------------------------------- 1 | 2 | from RAG.VectorBase import VectorStore 3 | from RAG.utils import ReadFiles 4 | from RAG.LLM import OpenAIChat, InternLMChat, GLM4Chat 5 | from RAG.Embeddings import JinaEmbedding, ZhipuEmbedding 6 | 7 | docs = ReadFiles('../../data/github_data').get_content(min_token_len=600, max_token_len=1800, cover_content=150) # 获得data目录下的所有文件内容并分割 8 | vector = VectorStore(docs) 9 | embedding = ZhipuEmbedding() # 创建Zhipu EmbeddingModel 10 | vector.get_vector(EmbeddingModel=embedding) 11 | vector.persist(path='../../storage/github_data') # 将向量和文档内容保存到storage目录下,下次再用就可以直接加载本地的数据库 -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/.env: -------------------------------------------------------------------------------- 1 | ZHIPUAI_API_KEY='a164da03cc0xxxxxxx2e5e395ee8.f91DdW1FKWTyFjtj' 2 | DASHSCOPE_API_KEY='sk-feddf4791axxxxxxx21f8cc335e83' -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/05-knowledge-prepare.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 一、知识库构建(采用../../data/github_data数据)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 8, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "ZHIPUAI_API_KEY\n", 20 | "DASHSCOPE_API_KEY\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "import os\n", 26 | "from dotenv import dotenv_values\n", 27 | "env_variables = dotenv_values('.env')\n", 28 | "for var in env_variables:\n", 29 | " print(var)\n", 30 | " os.environ[var] = env_variables[var]" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 9, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stderr", 40 | "output_type": "stream", 41 | "text": [ 42 | "Calculating embeddings: 0%| | 0/11 [00:00 None: 22 | super().__init__(document) 23 | self.document = document 24 | 25 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 26 | self.vectors = [] 27 | for doc in tqdm(self.document, desc="Calculating embeddings"): 28 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 29 | return self.vectors 30 | 31 | def persist(self, path: str = 'storage'): 32 | if not os.path.exists(path): 33 | os.makedirs(path) 34 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 35 | json.dump(self.document, f, ensure_ascii=False) 36 | if self.vectors: 37 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 38 | json.dump(self.vectors, f) 39 | 40 | def load_vector(self, path: str = 'storage'): 41 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 42 | self.vectors = json.load(f) 43 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 44 | self.document = json.load(f) 45 | 46 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 47 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 48 | 49 | def generateIndexFlatL2(self): 50 | dim, measure = 768, faiss.METRIC_L2 51 | param = 'Flat' 52 | vectors = np.array([vector for vector in self.vectors]).astype('float32') 53 | index = faiss.index_factory(dim, param, measure) 54 | index.add(vectors) # 将向量库中的向量加入到index中 55 | return index 56 | 57 | 58 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 59 | 60 | start_time = time.time() 61 | index = self.generateIndexFlatL2() 62 | end_time = time.time() 63 | print(' 构建索引 cost %f second' % (end_time - start_time)) 64 | query_vector = np.array([EmbeddingModel.get_embedding(query)]).astype("float32") 65 | 66 | end_time = time.time() 67 | D, I = index.search(query_vector, k) # xq为待检索向量,返回的I为每个待检索query最相似TopK的索引list,D为其对应的距离 68 | print(' 检索 cost %f second' % (time.time() - end_time)) 69 | return np.array(self.document)[I[0,:]].tolist() 70 | 71 | 72 | -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/PaddleEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PaddleEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self, 11 | model:str="rocketqa-zh-base-query-encoder", 12 | batch_size:int =1, 13 | **kwargs 14 | 15 | ) -> None: 16 | self.client = Taskflow("feature_extraction",model=model ,batch_size = batch_size,return_tensors='np',**kwargs) 17 | 18 | def get_embedding(self, text: str) -> List[float]: 19 | text_embeds = self.client([text]) 20 | result = text_embeds["features"] 21 | data = result[0] 22 | return data.tolist() 23 | 24 | def get_embeddings(self, text: List[str]) -> List[List[float]]: 25 | text_embeds = self.client(text) 26 | result = text_embeds["features"] 27 | data = result 28 | return data.tolist() 29 | 30 | -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/PostEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PostEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self,model:str 11 | ) -> None: 12 | self.client = Taskflow("feature_extraction",model=model ,return_tensors='np') 13 | 14 | def get_embedding(self, text: str,model:str = "rocketqa-zh-base-query-encoder") -> List[float]: 15 | text_embeds = self.client([text]) 16 | result = text_embeds["features"] 17 | data = result[0] 18 | return data.tolist() 19 | 20 | def get_embeddings(self, text: List[str],model:str) -> List[List[float]]: 21 | 22 | text_embeds = self.client(text) 23 | result = text_embeds["features"] 24 | data = result 25 | return data.tolist() 26 | 27 | -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/VectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 10 | import numpy as np 11 | from tqdm import tqdm 12 | 13 | 14 | class VectorStore(): 15 | def __init__(self, document: List[str] = ['']) -> None: 16 | self.document = document 17 | 18 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 19 | 20 | self.vectors = [] 21 | for doc in tqdm(self.document, desc="Calculating embeddings"): 22 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 23 | return self.vectors 24 | 25 | def batch_split_list(self, lst,batch_size): 26 | return [lst[i:i + batch_size] for i in range(0, len(lst), batch_size)] 27 | 28 | def get_vector_batch(self, EmbeddingModel: BaseEmbeddings, batch:int) -> List[List[float]]: 29 | 30 | self.vectors = [] 31 | self.document = self.batch_split_list(self.document, batch) 32 | for doc in tqdm(self.document, desc="Calculating embeddings"): 33 | self.vectors.extend(EmbeddingModel.get_embeddings(doc)) 34 | return self.vectors 35 | 36 | def persist(self, path: str = 'storage'): 37 | if not os.path.exists(path): 38 | os.makedirs(path) 39 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 40 | json.dump(self.document, f, ensure_ascii=False) 41 | if self.vectors: 42 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 43 | json.dump(self.vectors, f) 44 | 45 | def load_vector(self, path: str = 'storage'): 46 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 47 | self.vectors = json.load(f) 48 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 49 | self.document = json.load(f) 50 | 51 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 52 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 53 | 54 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 55 | query_vector = EmbeddingModel.get_embedding(query) 56 | 57 | end_time = time.time() 58 | 59 | result = np.array([self.get_similarity(query_vector, vector) 60 | for vector in self.vectors]) 61 | print(' 检索 cost %f second' % (time.time() - end_time)) 62 | return np.array(self.document)[result.argsort()[-k:][::-1]].tolist() 63 | -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/Embeddings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/Embeddings.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/Embeddings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/Embeddings.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/VectorBase.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/VectorBase.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/VectorBase.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/VectorBase.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/05.基于知识库的大模型问答/RAG/__pycache__/utils.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/RAG/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/tutorial/05.基于知识库的大模型问答/README.md: -------------------------------------------------------------------------------- 1 | - 05-knowledge-prepare.ipynb 用于构建最基础的向量数据库 2 | - 05-knowledge-test.ipynb 用于基于最基础向量数据库智能问答 3 | - .env 配置大模型key 4 | - /RAG 放置RAG核心代码 -------------------------------------------------------------------------------- /llm-rag/tutorial/06.改进-用自己的embedding模型/README.md: -------------------------------------------------------------------------------- 1 | - 06-self-embedding.ipynb 用于体验paddlenlp中的embedding算法 -------------------------------------------------------------------------------- /llm-rag/tutorial/07.封装镜像对外提供服务/ServerTest.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import numpy as np 4 | import requests 5 | 6 | req = {"input": "你好我想了解一下大模型","model":"rocketqa-zh-base-query-encoder"} 7 | emb_url = "http://127.0.0.1:5000" 8 | response_raw = requests.post(emb_url+"/v1/embedding", json=req) 9 | 10 | if response_raw.status_code >= 400 and response_raw.status_code != 503: 11 | raise Exception(f"{vars(response_raw)}") 12 | 13 | response = response_raw.json() 14 | if "errors" in response: 15 | raise Exception(", ".join(response["errors"])) 16 | 17 | if response: 18 | # print("result",response) 19 | data = np.asarray(json.loads(response).tolist()) 20 | print(data) 21 | 22 | -------------------------------------------------------------------------------- /llm-rag/tutorial/07.封装镜像对外提供服务/app.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | import sys 4 | from typing import List 5 | 6 | from pydantic import BaseModel 7 | 8 | from RAG.PaddleEmbedding import PaddleEmbedding 9 | 10 | base_dir = os.path.dirname(__file__) 11 | sys.path.append(base_dir) 12 | from fastapi import APIRouter 13 | 14 | router = APIRouter() 15 | API_SUMMARY = 'API' 16 | # -------------------- Business -------------------- 17 | from loguru import logger 18 | 19 | AVAIL_MODELS = ["PaddlePaddle/ernie_vil-2.0-base-zh","rocketqa-zh-base-query-encoder","rocketqa-zh-dureader-query-encoder"] 20 | 21 | 22 | class Parameters(BaseModel): 23 | input: List[str] 24 | model: str 25 | 26 | @router.get("/v1/models") 27 | async def v1_models(): 28 | output = { 29 | "object": "list", 30 | "data": [ 31 | { 32 | "model_name": "PaddlePaddle/ernie_vil-2.0-base-zh", 33 | "embedding_dim": 768, 34 | "describle": "", 35 | }, 36 | { 37 | "model_name": "rocketqa-zh-base-query-encoder", 38 | "embedding_dim": 768, 39 | "describle": "", 40 | }, 41 | { 42 | "model_name": "rocketqa-zh-dureader-query-encoder", 43 | "embedding_dim": 768, 44 | "describle": "", 45 | } 46 | ] 47 | } 48 | return output 49 | 50 | 51 | @router.post("/v1/embeddings") 52 | async def v1_completions(param: Parameters) -> List[List[float]]: 53 | if param.model in AVAIL_MODELS: 54 | return PaddleEmbedding(model=param.model).get_embeddings(param.input) 55 | else: 56 | from response import fail 57 | return fail() 58 | 59 | 60 | -------------------------------------------------------------------------------- /llm-rag/tutorial/07.封装镜像对外提供服务/prepare.py: -------------------------------------------------------------------------------- 1 | from RAG.PaddleEmbedding import PaddleEmbedding 2 | from RAG.VectorBase import VectorStore 3 | 4 | import os 5 | tiktoken_cache_dir = "./" 6 | os.environ["TIKTOKEN_CACHE_DIR"] = tiktoken_cache_dir 7 | from RAG.utils import ReadFiles 8 | 9 | embedding_model = "rocketqa-zh-base-query-encoder" 10 | batch = 8 11 | docs = ReadFiles('../../data').get_content(max_token_len=600, cover_content=150) # 获得data目录下的所有文件内容并分割 12 | vector = VectorStore(docs) 13 | #embedding = ZhipuEmbedding() # 创建EmbeddingModel 14 | embedding = PaddleEmbedding(model=embedding_model,batch_size=batch) # 创建EmbeddingModel 15 | vector.get_vector_batch(EmbeddingModel=embedding,batch=batch) 16 | #vector.get_vector(EmbeddingModel=embedding) 17 | 18 | vector.persist(path='storage') # 将向量和文档内容保存到storage目录下,下次再用就可以直接加载本地的数据库 -------------------------------------------------------------------------------- /llm-rag/tutorial/07.封装镜像对外提供服务/response.py: -------------------------------------------------------------------------------- 1 | """基础返回结构""" 2 | 3 | 4 | def base_response(code, msg, data=None): 5 | """基础返回格式""" 6 | if data is None: 7 | data = {} 8 | result = { 9 | "code": code, 10 | "message": msg, 11 | "data": data 12 | } 13 | return result 14 | 15 | 16 | def success(data=None, msg='Success'): 17 | """成功返回格式""" 18 | return base_response(200, msg, data) 19 | 20 | 21 | def fail(code=-1, msg='Fail', param=None): 22 | """失败返回格式""" 23 | result = { 24 | "error": { 25 | "message": msg, 26 | "param": param, 27 | "code": code 28 | } 29 | } 30 | return result -------------------------------------------------------------------------------- /llm-rag/tutorial/07.封装镜像对外提供服务/runserver.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import uvicorn,sys 3 | from opsapp.app.main import get_application, get_para 4 | app = get_application() 5 | # -------------------- 业务 -------------------- 6 | from app import router as application 7 | 8 | app.include_router(application) 9 | 10 | 11 | if __name__ == '__main__': 12 | host, port = get_para() 13 | if len(sys.argv) == 2: 14 | port = int(sys.argv[1]) 15 | port = 10090 16 | uvicorn.run(app='__main__:app', host=host, port=port, reload=True) 17 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/.env: -------------------------------------------------------------------------------- 1 | ZHIPUAI_API_KEY='your ZHIPUAI_API_KEY key' 2 | DASHSCOPE_API_KEY='your DASHSCOPE_API_KEY key' -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/08-Faiss-prepare.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 一、知识库构建" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from RAG.PaddleEmbedding import PaddleEmbedding\n", 17 | "from RAG.VectorBase import VectorStore\n", 18 | "from RAG.utils import ReadFiles\n", 19 | "\n", 20 | "embedding_model = \"rocketqa-zh-base-query-encoder\"\n", 21 | "\n", 22 | "docs = ReadFiles('../../data/history_24').get_content(max_token_len=600, cover_content=150) # 获得data目录下的所有文件内容并分割\n", 23 | "vector = VectorStore(docs)\n", 24 | "#embedding = ZhipuEmbedding() # 创建EmbeddingModel\n", 25 | "embedding = PaddleEmbedding(model=embedding_model) # 创建EmbeddingModel\n", 26 | "#vector.get_vector_batch(EmbeddingModel=embedding,batch=batch)\n", 27 | "vector.get_vector(EmbeddingModel=embedding)\n", 28 | "\n", 29 | "vector.persist(path='../../storage/history_24') # 将向量和文档内容保存到storage目录下,下次再用就可以直接加载本地的数据库" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [] 38 | } 39 | ], 40 | "metadata": { 41 | "language_info": { 42 | "name": "python" 43 | } 44 | }, 45 | "nbformat": 4, 46 | "nbformat_minor": 2 47 | } 48 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/Chapter08-Faiss.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "nbformat": 4, 17 | "nbformat_minor": 2 18 | } 19 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/FaissVectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | 10 | import faiss 11 | 12 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 13 | import numpy as np 14 | from tqdm import tqdm 15 | 16 | from RAG.VectorBase import VectorStore 17 | 18 | 19 | class FaissVectorStore(VectorStore): 20 | 21 | def __init__(self, document: List[str] = ['']) -> None: 22 | super().__init__(document) 23 | self.document = document 24 | 25 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 26 | self.vectors = [] 27 | for doc in tqdm(self.document, desc="Calculating embeddings"): 28 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 29 | return self.vectors 30 | 31 | def persist(self, path: str = 'storage'): 32 | if not os.path.exists(path): 33 | os.makedirs(path) 34 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 35 | json.dump(self.document, f, ensure_ascii=False) 36 | if self.vectors: 37 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 38 | json.dump(self.vectors, f) 39 | 40 | def load_vector(self, path: str = 'storage'): 41 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 42 | self.vectors = json.load(f) 43 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 44 | self.document = json.load(f) 45 | 46 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 47 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 48 | 49 | def generateIndexFlatL2(self): 50 | dim, measure = 768, faiss.METRIC_L2 51 | param = 'Flat' 52 | vectors = np.array([vector for vector in self.vectors]).astype('float32') 53 | index = faiss.index_factory(dim, param, measure) 54 | index.add(vectors) # 将向量库中的向量加入到index中 55 | return index 56 | 57 | 58 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 59 | 60 | start_time = time.time() 61 | index = self.generateIndexFlatL2() 62 | end_time = time.time() 63 | print(' 构建索引 cost %f second' % (end_time - start_time)) 64 | query_vector = np.array([EmbeddingModel.get_embedding(query)]).astype("float32") 65 | 66 | end_time = time.time() 67 | D, I = index.search(query_vector, k) # xq为待检索向量,返回的I为每个待检索query最相似TopK的索引list,D为其对应的距离 68 | print(' 检索 cost %f second' % (time.time() - end_time)) 69 | return np.array(self.document)[I[0,:]].tolist() 70 | 71 | 72 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/PaddleEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PaddleEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self, 11 | model:str="rocketqa-zh-base-query-encoder", 12 | batch_size:int =1, 13 | **kwargs 14 | 15 | ) -> None: 16 | self.client = Taskflow("feature_extraction",model=model ,batch_size = batch_size,return_tensors='np',**kwargs) 17 | 18 | def get_embedding(self, text: str) -> List[float]: 19 | text_embeds = self.client([text]) 20 | result = text_embeds["features"] 21 | data = result[0] 22 | return data.tolist() 23 | 24 | def get_embeddings(self, text: List[str]) -> List[List[float]]: 25 | text_embeds = self.client(text) 26 | result = text_embeds["features"] 27 | data = result 28 | return data.tolist() 29 | 30 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/PostEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PostEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self,model:str 11 | ) -> None: 12 | self.client = Taskflow("feature_extraction",model=model ,return_tensors='np') 13 | 14 | def get_embedding(self, text: str,model:str = "rocketqa-zh-base-query-encoder") -> List[float]: 15 | text_embeds = self.client([text]) 16 | result = text_embeds["features"] 17 | data = result[0] 18 | return data.tolist() 19 | 20 | def get_embeddings(self, text: List[str],model:str) -> List[List[float]]: 21 | 22 | text_embeds = self.client(text) 23 | result = text_embeds["features"] 24 | data = result 25 | return data.tolist() 26 | 27 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/VectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 10 | import numpy as np 11 | from tqdm import tqdm 12 | 13 | 14 | class VectorStore(): 15 | def __init__(self, document: List[str] = ['']) -> None: 16 | self.document = document 17 | 18 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 19 | 20 | self.vectors = [] 21 | for doc in tqdm(self.document, desc="Calculating embeddings"): 22 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 23 | return self.vectors 24 | 25 | def batch_split_list(self, lst,batch_size): 26 | return [lst[i:i + batch_size] for i in range(0, len(lst), batch_size)] 27 | 28 | def get_vector_batch(self, EmbeddingModel: BaseEmbeddings, batch:int) -> List[List[float]]: 29 | 30 | self.vectors = [] 31 | self.document = self.batch_split_list(self.document, batch) 32 | for doc in tqdm(self.document, desc="Calculating embeddings"): 33 | self.vectors.extend(EmbeddingModel.get_embeddings(doc)) 34 | return self.vectors 35 | 36 | def persist(self, path: str = 'storage'): 37 | if not os.path.exists(path): 38 | os.makedirs(path) 39 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 40 | json.dump(self.document, f, ensure_ascii=False) 41 | if self.vectors: 42 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 43 | json.dump(self.vectors, f) 44 | 45 | def load_vector(self, path: str = 'storage'): 46 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 47 | self.vectors = json.load(f) 48 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 49 | self.document = json.load(f) 50 | 51 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 52 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 53 | 54 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 55 | query_vector = EmbeddingModel.get_embedding(query) 56 | 57 | end_time = time.time() 58 | 59 | result = np.array([self.get_similarity(query_vector, vector) 60 | for vector in self.vectors]) 61 | print(' 检索 cost %f second' % (time.time() - end_time)) 62 | return np.array(self.document)[result.argsort()[-k:][::-1]].tolist() 63 | -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/Embeddings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/Embeddings.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/Embeddings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/Embeddings.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/VectorBase.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/VectorBase.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/VectorBase.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/__pycache__/VectorBase.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/RAG/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/tutorial/08.改进-基于Faiss的大模型知识索引构建/README.md: -------------------------------------------------------------------------------- 1 | - `08-Faiss-prepare.ipynb` 用于构建Faiss向量数据库 2 | - `08-Faiss-test.ipynb` 用于基于Faiss向量数据库智能问答 3 | - `.env` 配置大模型key 4 | - `/RAG` 放置RAG核心代码 -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/.env: -------------------------------------------------------------------------------- 1 | ZHIPUAI_API_KEY='your ZHIPUAI_API_KEY key' 2 | DASHSCOPE_API_KEY='your DASHSCOPE_API_KEY key' -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/FaissVectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | 10 | import faiss 11 | 12 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 13 | import numpy as np 14 | from tqdm import tqdm 15 | 16 | from RAG.VectorBase import VectorStore 17 | 18 | 19 | class FaissVectorStore(VectorStore): 20 | 21 | def __init__(self, document: List[str] = ['']) -> None: 22 | super().__init__(document) 23 | self.document = document 24 | 25 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 26 | self.vectors = [] 27 | for doc in tqdm(self.document, desc="Calculating embeddings"): 28 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 29 | return self.vectors 30 | 31 | def persist(self, path: str = 'storage'): 32 | if not os.path.exists(path): 33 | os.makedirs(path) 34 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 35 | json.dump(self.document, f, ensure_ascii=False) 36 | if self.vectors: 37 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 38 | json.dump(self.vectors, f) 39 | 40 | def load_vector(self, path: str = 'storage'): 41 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 42 | self.vectors = json.load(f) 43 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 44 | self.document = json.load(f) 45 | 46 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 47 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 48 | 49 | def generateIndexFlatL2(self): 50 | dim, measure = 768, faiss.METRIC_L2 51 | param = 'Flat' 52 | vectors = np.array([vector for vector in self.vectors]).astype('float32') 53 | index = faiss.index_factory(dim, param, measure) 54 | index.add(vectors) # 将向量库中的向量加入到index中 55 | return index 56 | 57 | 58 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 59 | 60 | start_time = time.time() 61 | index = self.generateIndexFlatL2() 62 | end_time = time.time() 63 | print(' 构建索引 cost %f second' % (end_time - start_time)) 64 | query_vector = np.array([EmbeddingModel.get_embedding(query)]).astype("float32") 65 | 66 | end_time = time.time() 67 | D, I = index.search(query_vector, k) # xq为待检索向量,返回的I为每个待检索query最相似TopK的索引list,D为其对应的距离 68 | print(' 检索 cost %f second' % (time.time() - end_time)) 69 | return np.array(self.document)[I[0,:]].tolist() 70 | 71 | 72 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/PaddleEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PaddleEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self, 11 | model:str="rocketqa-zh-base-query-encoder", 12 | batch_size:int =1, 13 | **kwargs 14 | 15 | ) -> None: 16 | self.client = Taskflow("feature_extraction",model=model ,batch_size = batch_size,return_tensors='np',**kwargs) 17 | 18 | def get_embedding(self, text: str) -> List[float]: 19 | text_embeds = self.client([text]) 20 | result = text_embeds["features"] 21 | data = result[0] 22 | return data.tolist() 23 | 24 | def get_embeddings(self, text: List[str]) -> List[List[float]]: 25 | text_embeds = self.client(text) 26 | result = text_embeds["features"] 27 | data = result 28 | return data.tolist() 29 | 30 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/PostEmbedding.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from paddlenlp import Taskflow 4 | 5 | from RAG.Embeddings import BaseEmbeddings 6 | 7 | 8 | class PostEmbedding(BaseEmbeddings): 9 | def __init__( 10 | self,model:str 11 | ) -> None: 12 | self.client = Taskflow("feature_extraction",model=model ,return_tensors='np') 13 | 14 | def get_embedding(self, text: str,model:str = "rocketqa-zh-base-query-encoder") -> List[float]: 15 | text_embeds = self.client([text]) 16 | result = text_embeds["features"] 17 | data = result[0] 18 | return data.tolist() 19 | 20 | def get_embeddings(self, text: List[str],model:str) -> List[List[float]]: 21 | 22 | text_embeds = self.client(text) 23 | result = text_embeds["features"] 24 | data = result 25 | return data.tolist() 26 | 27 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/VectorBase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | import os 6 | import time 7 | from typing import Dict, List, Optional, Tuple, Union 8 | import json 9 | from RAG.Embeddings import BaseEmbeddings, OpenAIEmbedding, JinaEmbedding, ZhipuEmbedding 10 | import numpy as np 11 | from tqdm import tqdm 12 | 13 | 14 | class VectorStore(): 15 | def __init__(self, document: List[str] = ['']) -> None: 16 | self.document = document 17 | 18 | def get_vector(self, EmbeddingModel: BaseEmbeddings) -> List[List[float]]: 19 | 20 | self.vectors = [] 21 | for doc in tqdm(self.document, desc="Calculating embeddings"): 22 | self.vectors.append(EmbeddingModel.get_embedding(doc)) 23 | return self.vectors 24 | 25 | def batch_split_list(self, lst,batch_size): 26 | return [lst[i:i + batch_size] for i in range(0, len(lst), batch_size)] 27 | 28 | def get_vector_batch(self, EmbeddingModel: BaseEmbeddings, batch:int) -> List[List[float]]: 29 | 30 | self.vectors = [] 31 | self.document = self.batch_split_list(self.document, batch) 32 | for doc in tqdm(self.document, desc="Calculating embeddings"): 33 | self.vectors.extend(EmbeddingModel.get_embeddings(doc)) 34 | return self.vectors 35 | 36 | def persist(self, path: str = 'storage'): 37 | if not os.path.exists(path): 38 | os.makedirs(path) 39 | with open(f"{path}/doecment.json", 'w', encoding='utf-8') as f: 40 | json.dump(self.document, f, ensure_ascii=False) 41 | if self.vectors: 42 | with open(f"{path}/vectors.json", 'w', encoding='utf-8') as f: 43 | json.dump(self.vectors, f) 44 | 45 | def load_vector(self, path: str = 'storage'): 46 | with open(f"{path}/vectors.json", 'r', encoding='utf-8') as f: 47 | self.vectors = json.load(f) 48 | with open(f"{path}/doecment.json", 'r', encoding='utf-8') as f: 49 | self.document = json.load(f) 50 | 51 | def get_similarity(self, vector1: List[float], vector2: List[float]) -> float: 52 | return BaseEmbeddings.cosine_similarity(vector1, vector2) 53 | 54 | def query(self, query: str, EmbeddingModel: BaseEmbeddings, k: int = 1) -> List[str]: 55 | query_vector = EmbeddingModel.get_embedding(query) 56 | 57 | end_time = time.time() 58 | 59 | result = np.array([self.get_similarity(query_vector, vector) 60 | for vector in self.vectors]) 61 | print(' 检索 cost %f second' % (time.time() - end_time)) 62 | return np.array(self.document)[result.argsort()[-k:][::-1]].tolist() 63 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/Embeddings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/Embeddings.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/Embeddings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/Embeddings.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/PaddleEmbedding.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/PaddleEmbedding.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/VectorBase.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/VectorBase.cpython-311.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/VectorBase.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/__pycache__/VectorBase.cpython-38.pyc -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/RAG/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/README.md: -------------------------------------------------------------------------------- 1 | - `cloud-vectordb-examples` 云上milvus调用例子 2 | - `/RAG` 放置RAG核心代码 3 | - `prepare.py` 用于构建向量数据库 4 | - `test.py` 用于基于向量数据库问答 5 | - `.env` 用于配置大模型key 6 | - `config.ini`用于配置云milvus向量库 -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/README.md: -------------------------------------------------------------------------------- 1 | # cloud-vectordb-examples 2 | This repository containslanguage examples demonstrating how to use Zilliz Cloud VectorDB. 3 | 4 | ## Introduction 5 | [Zilliz Cloud VectorDB](https://cloud.zilliz.com) is a cloud-based service that provides fast and efficient search capabilities for vector data. It is based on Milvus, an open-source vector database developed by Zilliz. 6 | 7 | This repository provides two examples that show how to use Zilliz Cloud VectorDB with Python and Java. 8 | 9 | ## Python Example 10 | https://github.com/zilliztech/cloud-vectordb-examples/tree/master/python 11 | 12 | ## Java Example 13 | https://github.com/zilliztech/cloud-vectordb-examples/tree/master/milvus-java 14 | 15 | ## Node.js Example 16 | https://github.com/zilliztech/cloud-vectordb-examples/tree/master/node 17 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | 5 | Java 8+ 6 | Apache Maven 3.6+ 7 | 8 | ### Git clone the example code repo 9 | git clone https://github.com/zilliztech/cloud-vectordb-examples 10 | 11 | ### Go to milvus-java folder 12 | cd cloud-vectordb-examples 13 | cd milvus-java 14 | 15 | ### Modify uri, token in configuration file.(resources/RunSettings.properties) 16 | uri = https://in01-XXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com:XXXXX 17 | user = db_admin 18 | password = ******** 19 | 20 | ### Compile project 21 | mvn compile 22 | 23 | ### Run HelloZillizVectorDB.java 24 | mvn exec:java -Dexec.mainClass="demo.HelloZillizVectorDB" 25 | 26 | ### It should print information on the console 27 | Connecting to DB: https://in01-XXXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com:XXXXX 28 | Success! 29 | Creating example collection: book 30 | Schema: {...} 31 | Success! 32 | Inserting 2000 entities... 33 | Succeed in 0.3321 seconds! 34 | Flushing... 35 | Succeed in 0.81 seconds! 36 | Building AutoIndex... 37 | Succeed in 18.9318 seconds! 38 | Loading collection... 39 | Succeed in 1.718 seconds! 40 | Searching vector:[[...][...]...] 41 | search 0 latency: 0.0154 seconds! 42 | Searching vector:[[...][...]...] 43 | search 1 latency: 0.0147 seconds! 44 | Searching vector:[[...][...]...] 45 | search 2 latency: 0.0151 seconds! 46 | ... 47 | ... 48 | 49 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/READMEServerless.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | 5 | Java 8+ 6 | Apache Maven 3.6+ 7 | 8 | ### Git clone the example code repo 9 | git clone https://github.com/zilliztech/cloud-vectordb-examples 10 | 11 | ### Go to milvus-java folder 12 | cd cloud-vectordb-examples 13 | cd milvus-java 14 | 15 | ### Modify uri, token in configuration file.(resources/RunSettingsServerless.properties) 16 | uri = https://in01-XXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com 17 | token = replace-this-with-your-token 18 | 19 | ### Compile project 20 | mvn compile 21 | 22 | ### Run HelloZillizVectorDBServerless.java 23 | mvn exec:java -Dexec.mainClass="demo.HelloZillizVectorDBServerless" 24 | 25 | ### It should print information on the console 26 | Connecting to DB: https://in01-XXXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com 27 | Success! 28 | Creating example collection: book 29 | Schema: {...} 30 | Success! 31 | Inserting 2000 entities... 32 | Succeed in 0.3321 seconds! 33 | Flushing... 34 | Succeed in 0.81 seconds! 35 | Building AutoIndex... 36 | Succeed in 18.9318 seconds! 37 | Loading collection... 38 | Succeed in 1.718 seconds! 39 | Searching vector:[[...][...]...] 40 | search 0 latency: 0.0154 seconds! 41 | Searching vector:[[...][...]...] 42 | search 1 latency: 0.0147 seconds! 43 | Searching vector:[[...][...]...] 44 | search 2 latency: 0.0151 seconds! 45 | ... 46 | ... 47 | 48 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | milvus-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | 15 | 16 | 17 | 18 | io.milvus 19 | milvus-sdk-java 20 | 2.3.2 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/src/main/java/util/PropertyFilesUtil.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | 4 | import java.io.BufferedInputStream; 5 | import java.io.InputStream; 6 | import java.nio.file.Files; 7 | import java.nio.file.Paths; 8 | import java.util.HashMap; 9 | import java.util.Properties; 10 | 11 | public class PropertyFilesUtil { 12 | public static HashMap readPropertyFile(String propertyFileName) { 13 | HashMap hashMap=new HashMap<>(); 14 | Properties prop = new Properties(); 15 | try { 16 | InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(propertyFileName))); 17 | prop.load(in); 18 | for (String key : prop.stringPropertyNames()) { 19 | hashMap.put(key, prop.getProperty(key)); 20 | } 21 | in.close(); 22 | } catch (Exception e) { 23 | throw new RuntimeException(e); 24 | } 25 | return hashMap; 26 | } 27 | 28 | public static String getRunValue(String key){ 29 | HashMap hashMap = PropertyFilesUtil.readPropertyFile("./src/main/resources/RunSettings.properties"); 30 | String value; 31 | value=hashMap.get(key); 32 | return value; 33 | } 34 | 35 | public static String getRunValueServerless(String key){ 36 | HashMap hashMap = PropertyFilesUtil.readPropertyFile("./src/main/resources/RunSettingsServerless.properties"); 37 | String value; 38 | value=hashMap.get(key); 39 | return value; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/src/main/resources/RunSettings.properties: -------------------------------------------------------------------------------- 1 | uri = https://in01-XXXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com:XXXXX 2 | user = db_admin 3 | password = ******** 4 | #optinal, please check your Zilliz Cloud connection guide 5 | token = db_admin:password -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/milvus-java/src/main/resources/RunSettingsServerless.properties: -------------------------------------------------------------------------------- 1 | uri = https://in01-XXXXXXXXXXXXX.api.gcp-us-west1.cloud-uat3.zilliz.com 2 | token = 1077814287e7a5d2ea7b491eae242fba0c53feac1ce32537a0350335a46b4f4c85063e78c0c346fd6f7d7f72ab01637d62563377 3 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/HelloZillizCloud.js: -------------------------------------------------------------------------------- 1 | import { 2 | MilvusClient, 3 | DataType, 4 | ConsistencyLevelEnum, 5 | } from "@zilliz/milvus2-sdk-node"; 6 | import { config } from "./config.js"; 7 | import { isVersionAtLeast } from "./utils.js"; 8 | 9 | if (!isVersionAtLeast("2.2.17", MilvusClient.sdkInfo.version)) { 10 | console.warn( 11 | `Please upgrade your node sdk version, it should >= 2.2.17, your sdk version is ${MilvusClient.sdkInfo.version}` 12 | ); 13 | process.exit(); 14 | } 15 | 16 | const { uri, user, password, token } = config; 17 | 18 | // connecting 19 | console.info(`Connecting to DB: ${uri}`); 20 | const client = new MilvusClient({ 21 | address: uri, 22 | username: user, 23 | password: password, 24 | }); 25 | 26 | /* 27 | Please check your connection guide in Zilliz Cloud console, if the cluster provides a token, you can use it to authenticate your cluster\ 28 | // token based cluster 29 | const client = new MilvusClient({ 30 | address: uri, 31 | token: token, 32 | }); 33 | */ 34 | 35 | (async () => { 36 | // dimension 37 | const dimension = 64; 38 | const collection_name = "hello_milvus"; 39 | const num_of_rows = 1000; 40 | // create colleciton 41 | console.time(`Creating example collection: ${collection_name}`); 42 | await client.createCollection({ 43 | collection_name, 44 | dimension, 45 | }); 46 | console.info(`Success!`); 47 | console.timeEnd(`Creating example collection: ${collection_name}`); 48 | 49 | const data = []; 50 | Array(num_of_rows) 51 | .fill(1) 52 | .forEach(() => { 53 | data.push({ 54 | id: Math.floor(Math.random() * 100000), 55 | vector: [...Array(dimension)].map(() => Math.random()), 56 | }); 57 | }); 58 | // inserting 59 | console.time(`Inserting 1000 entities successfully`); 60 | await client.insert({ 61 | collection_name, 62 | data, 63 | }); 64 | console.timeEnd(`Inserting 1000 entities successfully`); 65 | 66 | await client.flushSync({ collection_names: [collection_name] }); 67 | console.log(`Flush data successfully`); 68 | 69 | // search 70 | console.time(`Searching vector`); 71 | const res = await client.search({ 72 | collection_name, 73 | vector: data[0]["vector"], 74 | output_fields: ["id"], 75 | vector_type: DataType.FloatVector, 76 | limit: 10, 77 | consistency_level: ConsistencyLevelEnum.Bounded, 78 | }); 79 | console.timeEnd(`Searching vector`); 80 | console.log(res); 81 | })(); 82 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/HelloZillizCloud.serverless.js: -------------------------------------------------------------------------------- 1 | import { MilvusClient, ConsistencyLevelEnum } from "@zilliz/milvus2-sdk-node"; 2 | import { config } from "./config.serverless.js"; 3 | import { isVersionAtLeast } from "./utils.js"; 4 | 5 | if (!isVersionAtLeast("2.2.17", MilvusClient.sdkInfo.version)) { 6 | console.warn( 7 | `Please upgrade your node sdk version, it should >= 2.2.17, your version is ${MilvusClient.sdkInfo.version}` 8 | ); 9 | process.exit(); 10 | } 11 | 12 | const { uri, token } = config; 13 | 14 | // connecting 15 | console.info(`Connecting to DB: ${uri}`); 16 | const client = new MilvusClient({ 17 | address: uri, 18 | token: token, 19 | }); 20 | console.info(`Success!`); 21 | 22 | (async () => { 23 | // dimension 24 | const dimension = 64; 25 | const collection_name = "hello_milvus"; 26 | const num_of_rows = 1000; 27 | 28 | // create colleciton 29 | console.time(`Creating example collection: ${collection_name}`); 30 | await client.createCollection({ 31 | collection_name, 32 | dimension, 33 | }); 34 | console.timeEnd(`Creating example collection: ${collection_name}`); 35 | 36 | const data = []; 37 | Array(num_of_rows) 38 | .fill(1) 39 | .forEach(() => { 40 | data.push({ 41 | id: Math.floor(Math.random() * 100000), 42 | vector: [...Array(dimension)].map(() => Math.random()), 43 | }); 44 | }); 45 | // inserting 46 | console.time(`Inserting 1000 entities successfully`); 47 | await client.insert({ 48 | collection_name, 49 | data, 50 | }); 51 | console.timeEnd(`Inserting 1000 entities successfully`); 52 | 53 | // flush 54 | await client.flushSync({ collection_names: [collection_name] }); 55 | console.log(`Flush data successfully`); 56 | // search 57 | console.time(`Searching vector`); 58 | const res = await client.search({ 59 | collection_name, 60 | vector: data[0]["vector"], 61 | output_fields: ["id"], 62 | limit: 10, 63 | consistency_level: ConsistencyLevelEnum.Bounded, 64 | }); 65 | console.timeEnd(`Searching vector`); 66 | console.log(res); 67 | })(); 68 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | 5 | Node 14+ 6 | 7 | ### Git clone the example code repo 8 | 9 | ```bash 10 | git clone https://github.com/zilliztech/cloud-vectordb-examples 11 | ``` 12 | 13 | ### Go to milvus-node folder 14 | 15 | ```bash 16 | cd cloud-vectordb-examples 17 | cd node 18 | ``` 19 | 20 | ### Install sdk 21 | 22 | ```bash 23 | npm install 24 | ``` 25 | 26 | #### Dedicated cluster 27 | 28 | ##### Modify uri, user name and user password in configuration file.(config.js) 29 | 30 | ```javascript 31 | { 32 | uri: " https://in01-XXXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com:XXXXX", 33 | token: "username:password", 34 | }; 35 | ``` 36 | 37 | #### Serverless cluster 38 | 39 | ##### Modify uri, token in configuration file.(config.js) 40 | 41 | ```javascript 42 | { 43 | uri: "https://in03-XXXXXXXXXXXXX.api.gcp-us-west1.cloud-uat3.zilliz.com", 44 | token: "api-key", 45 | }; 46 | 47 | ``` 48 | 49 | ### Run HelloZillizCloud.js 50 | 51 | ```shell 52 | npm install 53 | node HelloZillizCloud.js 54 | ``` 55 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/config.js: -------------------------------------------------------------------------------- 1 | export const config = { 2 | uri: "https://in03-XXXXXXXXXXXXX.api.gcp-us-west1.cloud-uat3.zilliz.com:port", 3 | user: 'username', 4 | passsword: 'password', 5 | token: 'some-token', // optinal, please check your Zilliz Cloud connection guide 6 | }; 7 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/config.serverless.js: -------------------------------------------------------------------------------- 1 | export const config = { 2 | uri: "https://in03-XXXXXXXXXXXXX.api.gcp-us-west1.cloud-uat3.zilliz.com", 3 | token: 4 | "cfda6bf09e57d2608851bd30ec0a76469297011bce00e87cd1189ab202d52ec59103c0942e7bdab776d7b6b3cbf487ed2c3c61ff", 5 | }; 6 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "dependencies": { 4 | "@zilliz/milvus2-sdk-node": "2.3.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/node/utils.js: -------------------------------------------------------------------------------- 1 | export const isVersionAtLeast = (targetVersion, version) => { 2 | const targetParts = targetVersion.split(".").map(Number); 3 | const parts = version.split(".").map(Number); 4 | for (let i = 0; i < targetParts.length; i++) { 5 | if (parts[i] > targetParts[i]) { 6 | return true; 7 | } else if (parts[i] < targetParts[i]) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | }; 13 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/python/README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | Install Python 3.7+ 5 | pip3 6 | 7 | 8 | ### Git clone the example code repo 9 | git clone https://github.com/zilliztech/cloud-vectordb-examples.git 10 | 11 | ### Install pymilvus 12 | pip3 install pymilvus==2.3.4 13 | 14 | ### Go to python folder 15 | cd cloud-vectordb-examples 16 | cd python 17 | 18 | ### Modify uri,username and password in the configuration file.(config.ini) 19 | uri = https://in01-XXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com:XXXXX 20 | user = db_admin 21 | password = ******** 22 | 23 | ### Run hello_zilliz_vectordb.py 24 | python3 hello_zilliz_vectordb.py 25 | 26 | ### It should print information on the console 27 | Connecting to DB: https://in01-xxxxxxxxxxxxx.aws-us-west-2.vectordb.zillizcloud.com:XXXXX 28 | Success! 29 | Creating example collection: book 30 | Schema: {...} 31 | Success! 32 | Inserting 2000 entities... 33 | Succeed in 0.3021 seconds! 34 | Flushing... 35 | Succeed in 0.77 seconds! 36 | Building AutoIndex... 37 | Succeed in 18.9118 seconds! 38 | Loading collection... 39 | Succeed in 2.5229 seconds! 40 | Searching vector:[[...][...]...] 41 | search 0 latency: 0.0057 seconds! 42 | Searching vector:[[...][...]...] 43 | search 1 latency: 0.0049 seconds! 44 | Searching vector:[[...][...]...] 45 | search 2 latency: 0.0051 seconds! 46 | ... 47 | ... 48 | 49 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/python/README_serverless.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | Install Python 3.7+ 5 | pip3 6 | 7 | 8 | ### Git clone the example code repo 9 | git clone https://github.com/zilliztech/cloud-vectordb-examples.git 10 | 11 | ### Install pymilvus 12 | pip3 install pymilvus==2.3.4 13 | 14 | ### Go to python folder 15 | cd cloud-vectordb-examples 16 | cd python 17 | 18 | ### Modify uri, token in the configuration file.(config_serverless.ini) 19 | uri = https://in01-XXXXXXXXXXXX.aws-us-west-2.vectordb.zillizcloud.com 20 | token = replace-this-with-your-token 21 | 22 | ### Run hello_zilliz_vectordb_serverless.py 23 | python3 hello_zilliz_vectordb_serverless.py 24 | 25 | ### It should print information on the console 26 | Connecting to DB: https://in01-xxxxxxxxxxxxx.aws-us-west-2.vectordb.zillizcloud.com 27 | Success! 28 | Creating example collection: book 29 | Schema: {...} 30 | Success! 31 | Inserting 2000 entities... 32 | Succeed in 0.3021 seconds! 33 | Flushing... 34 | Succeed in 0.77 seconds! 35 | Building AutoIndex... 36 | Succeed in 18.9118 seconds! 37 | Loading collection... 38 | Succeed in 2.5229 seconds! 39 | Searching vector:[[...][...]...] 40 | search 0 latency: 0.0057 seconds! 41 | Searching vector:[[...][...]...] 42 | search 1 latency: 0.0049 seconds! 43 | Searching vector:[[...][...]...] 44 | search 2 latency: 0.0051 seconds! 45 | ... 46 | ... 47 | 48 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/python/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/python/config_serverless.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in03-XXXXXXXXXXXXX.api.gcp-us-west1.cloud-uat3.zilliz.com 3 | token = 1077814287e7a5d2ea7b491eae242fba0c53feac1ce32537a0350335a46b4f4c85063e78c0c346fd6f7d7f72ab01637d62563377 4 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/zilliz_go/README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | ### Prerequisites 4 | go >= 1.18 5 | 6 | ### Git clone the example code repo 7 | git clone https://github.com/zilliztech/cloud-vectordb-examples.git 8 | 9 | ### Install milvus-sdk-go 10 | go get -u github.com/milvus-io/milvus-sdk-go/v2 11 | 12 | ### Go to milvus_go folder 13 | cd cloud-vectordb-examples 14 | cd milvus_go 15 | 16 | ### Modify uri, username and user password in the configuration file.(config.go) 17 | ```go 18 | const ( 19 | uri = "https://in01-XXXXXXXXXXXXX.aws-us-east-2.vectordb.zillizcloud.com:XXXXX" 20 | user = "db_admin" 21 | password = "XXXXXXXXXXXXX" 22 | ) 23 | ``` 24 | 25 | ### Run HelloZillizCloud.go 26 | go run HelloZillizCloud.go 27 | 28 | ### It should print information on the console 29 | ``` 30 | Connecting to DB: https://in01-XXXXXXXXXXXXX.aws-us-east-2.vectordb.zillizcloud.com:XXXXX 31 | Success! 32 | Creating example collection: book 33 | Success! 34 | Inserting 100000 entities... 35 | Succeed in 2.391643208s 36 | Building AutoIndex... 37 | Succeed in 1.170188209s 38 | Loading collection... 39 | Succeed in 9.246094375s 40 | Search... 41 | Succeed in 584.865167ms 42 | &{ [1 560 412 773 747 171 14 598 506 897]} 43 | [0 14.840795 15.720606 16.305355 16.57472 16.69268 16.71979 16.856443 16.934048 17.061205] 44 | ``` 45 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/zilliz_go/config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | const ( 4 | uri = "https://in01-XXXXXXXXXXXXX.aws-us-east-2.vectordb.zillizcloud.com:XXXXX" 5 | user = "db_admin" 6 | password = "XXXXXXXXXXXXX" 7 | ) 8 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/cloud-vectordb-examples/zilliz_go/go.mod: -------------------------------------------------------------------------------- 1 | module zilliz_go 2 | 3 | go 1.18 4 | 5 | require github.com/milvus-io/milvus-sdk-go/v2 v2.2.3 6 | 7 | require ( 8 | github.com/cockroachdb/errors v1.9.1 // indirect 9 | github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect 10 | github.com/cockroachdb/redact v1.1.3 // indirect 11 | github.com/getsentry/sentry-go v0.12.0 // indirect 12 | github.com/gogo/protobuf v1.3.2 // indirect 13 | github.com/golang/protobuf v1.5.3 // indirect 14 | github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect 15 | github.com/kr/pretty v0.3.0 // indirect 16 | github.com/kr/text v0.2.0 // indirect 17 | github.com/milvus-io/milvus-proto/go-api v0.0.0-20230522080721-2975bfe7a190 // indirect 18 | github.com/pkg/errors v0.9.1 // indirect 19 | github.com/rogpeppe/go-internal v1.8.1 // indirect 20 | github.com/tidwall/gjson v1.14.4 // indirect 21 | github.com/tidwall/match v1.1.1 // indirect 22 | github.com/tidwall/pretty v1.2.0 // indirect 23 | golang.org/x/net v0.20.0 // indirect 24 | golang.org/x/sys v0.16.0 // indirect 25 | golang.org/x/text v0.14.0 // indirect 26 | google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect 27 | google.golang.org/grpc v1.56.3 // indirect 28 | google.golang.org/protobuf v1.32.0 // indirect 29 | ) 30 | -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/prepare.py: -------------------------------------------------------------------------------- 1 | from RAG.PaddleEmbedding import PaddleEmbedding 2 | from RAG.VectorBase import VectorStore 3 | from RAG.utils import ReadFiles 4 | 5 | embedding_model = "rocketqa-zh-base-query-encoder" 6 | 7 | docs = ReadFiles('../../data/history_24').get_content(min_token_len=1600,max_token_len=3200, cover_content=150) # 获得data目录下的所有文件内容并分割 8 | vector = VectorStore(docs) 9 | embedding = PaddleEmbedding(model=embedding_model) # 创建EmbeddingModel 10 | vector.get_vector(EmbeddingModel=embedding) 11 | vector.persist(path='../../storage_test/history_24') # 将向量和文档内容保存到storage目录下,下次再用就可以直接加载本地的数据库 -------------------------------------------------------------------------------- /llm-rag/tutorial/09.改进-使用Milvus向量数据库/test.py: -------------------------------------------------------------------------------- 1 | from RAG.Embeddings import ZhipuEmbedding 2 | from RAG.FaissVectorBase import FaissVectorStore 3 | from RAG.LLM import GLM4Chat 4 | 5 | # 保存数据库之后 6 | from RAG.PaddleEmbedding import PaddleEmbedding 7 | from RAG.ZillizVectorStore import ZillizVectorStore 8 | 9 | 10 | storage = "../../storage/history_24_1" 11 | vector = ZillizVectorStore() 12 | vector.load_vector(storage) 13 | # vector.prepare_entity() 14 | # vector.insert() 15 | # vector.flush() 16 | 17 | 18 | question = '介绍一下秦二世' 19 | 20 | embedding = PaddleEmbedding() # 创建EmbeddingModel 21 | 22 | content = vector.query(question, EmbeddingModel=embedding, k=3)[0] 23 | print(f'知识库输出:{content}') 24 | 25 | chat = GLM4Chat() 26 | 27 | print(chat.chat(question, [], content)) 28 | 29 | -------------------------------------------------------------------------------- /llm-rag/tutorial/10.前端构建/config.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | uri = https://in01-84b7d73154bd768.ali-cn-beijing.vectordb.zilliz.com.cn:19530 3 | user = db_admin 4 | password = Pz8|KS,T9vpP1nFT 5 | #optinal, please check your Zilliz Cloud connection guide 6 | token = db_admin:password 7 | collection_name = history_1 8 | dim = 768 -------------------------------------------------------------------------------- /llm-rag/tutorial/10.前端构建/requirements.txt: -------------------------------------------------------------------------------- 1 | gradio 2 | mdtex2html -------------------------------------------------------------------------------- /llm-rag/tutorial/10.前端构建/test.py: -------------------------------------------------------------------------------- 1 | 2 | str = "锅" 3 | print(f"{str}的unicode编码为:{hex(ord(str))}") 4 | print(f"{str}的UTF-8编码为:{str.encode('UTF-8')}") -------------------------------------------------------------------------------- /llm-train/baichuan13B/README.md: -------------------------------------------------------------------------------- 1 | 我们提供了多样化的大模型微调示例脚本。 2 | 3 | 1.examples文件夹 4 | 5 | ``` 6 | │ README_zh.md 7 | │ run_baichuan13B_merge_lora.sh #用于合并lora权重 8 | │ run_baichuan13B_sft_lora.sh #用于基于baichuan13B通过lora微调 9 | └─inference # 服务构建 10 | api_demo.sh # API推理服务构建 11 | cli_demo.sh # 交互式推理服务构建 12 | vllm_demo.sh # vllm推理服务构建 13 | web_demo.sh # web推理服务构建 14 | ``` 15 | 16 | 2.src文件夹 17 | 18 | 提供了训练微调核心代码 19 | 20 | 21 | 22 | 以上代码运行,请务必下载和安装好[LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory),如安装环境有问题,可联系我要代码conda env环境。 -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/README_zh.md: -------------------------------------------------------------------------------- 1 | 我们提供了多样化的大模型微调示例脚本。 2 | 3 | ``` 4 | │ README_zh.md 5 | │ run_baichuan13B_merge_lora.sh #用于合并lora权重 6 | │ run_baichuan13B_sft_lora.sh #用于基于baichuan13B通过lora微调 7 | └─inference # 服务构建 8 | api_demo.sh # API推理服务构建 9 | cli_demo.sh # 交互式推理服务构建 10 | vllm_demo.sh # vllm推理服务构建 11 | web_demo.sh # web推理服务构建 12 | ``` 13 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/inference/api_demo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CUDA_VISIBLE_DEVICES=2 API_PORT=10099 python ../../src/api_demo.py \ 4 | --model_name_or_path /workspace/test/LLaMA-Factory/models/Baichuan2-13B-Chat-sft-0430 \ 5 | --template baichuan2 \ 6 | --finetuning_type lora 7 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/inference/cli_demo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CUDA_VISIBLE_DEVICES=2 python ../../src/cli_demo.py \ 4 | --model_name_or_path /workspace/test/models/baichuan-inc/Baichuan2-13B-Chat \ 5 | --adapter_name_or_path ../../saves/Baichuan2-13B-Chat/lora/train_2024-04-30-13-10-46 \ 6 | --template baichuan2 \ 7 | --finetuning_type lora 8 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/inference/vllm_demo.sh: -------------------------------------------------------------------------------- 1 | CUDA_VISIBLE_DEVICES=2,3 API_PORT=10099 python ../../src/api_demo.py \ 2 | --model_name_or_path /workspace/test/LLaMA-Factory/models/Baichuan2-13B-Chat-sft-0430 \ 3 | --template baichuan2 \ 4 | --infer_backend vllm \ 5 | --vllm_enforce_eager \ 6 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/inference/web_demo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # add `--visual_inputs True` to load MLLM 3 | 4 | CUDA_VISIBLE_DEVICES=2 python ../../src/web_demo.py \ 5 | --model_name_or_path /workspace/test/models/baichuan-inc/Baichuan2-13B-Chat \ 6 | --adapter_name_or_path ../../saves/Baichuan2-13B-Chat/lora/train_2024-04-30-13-10-46 \ 7 | --template baichuan2 \ 8 | --finetuning_type lora 9 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/run_baichuan13B_merge_lora.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # DO NOT use quantized model or quantization_bit when merging lora weights 3 | 4 | CUDA_VISIBLE_DEVICES=2 python ../../src/export_model.py \ 5 | --model_name_or_path ~/models/baichuan-inc/Baichuan2-13B-Chat \ 6 | --adapter_name_or_path saves/Baichuan2-13B-Chat/lora/train_2024-04-30-13-10-46 \ 7 | --template baichuan2 \ 8 | --finetuning_type lora \ 9 | --export_dir ../../models/Baichuan2-13B-Chat-sft-0430 \ 10 | --export_size 2 \ 11 | --export_device cuda \ 12 | --export_legacy_format False 13 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/examples/run_baichuan13B_sft_lora.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CUDA_VISIBLE_DEVICES=2 python src/train_bash.py \ 4 | --stage sft \ 5 | --do_train True \ 6 | --model_name_or_path /workspace/test/models/baichuan-inc/Baichuan2-13B-Chat \ 7 | --adapter_name_or_path saves/Baichuan2-13B-Chat/lora/train_2024-04-30-13-10-46 \ 8 | --finetuning_type lora \ 9 | --template baichuan2 \ 10 | --flash_attn auto \ 11 | --dataset_dir data \ 12 | --dataset test \ 13 | --cutoff_len 1024 \ 14 | --learning_rate 5e-05 \ 15 | --num_train_epochs 10.0 \ 16 | --max_samples 100000 \ 17 | --per_device_train_batch_size 4 \ 18 | --gradient_accumulation_steps 8 \ 19 | --lr_scheduler_type cosine \ 20 | --max_grad_norm 1.0 \ 21 | --logging_steps 5 \ 22 | --save_steps 100 \ 23 | --warmup_steps 0 \ 24 | --optim adamw_torch \ 25 | --report_to none \ 26 | --output_dir saves/Baichuan2-13B-Chat/lora/train_2024-04-30-13-10-46 \ 27 | --fp16 True \ 28 | --lora_rank 8 \ 29 | --lora_alpha 16 \ 30 | --lora_dropout 0 \ 31 | --lora_target W_pack \ 32 | --plot_loss True 33 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/api_demo.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import uvicorn 4 | 5 | from llmtuner import ChatModel, create_app 6 | 7 | 8 | def main(): 9 | chat_model = ChatModel() 10 | app = create_app(chat_model) 11 | print("Visit http://localhost:{}/docs for API document.".format(os.environ.get("API_PORT", 8000))) 12 | uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("API_PORT", 8000)), workers=1) 13 | 14 | 15 | if __name__ == "__main__": 16 | main() 17 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/cli_demo.py: -------------------------------------------------------------------------------- 1 | from llmtuner import ChatModel 2 | from llmtuner.extras.misc import torch_gc 3 | 4 | 5 | try: 6 | import platform 7 | 8 | if platform.system() != "Windows": 9 | import readline # noqa: F401 10 | except ImportError: 11 | print("Install `readline` for a better experience.") 12 | 13 | 14 | def main(): 15 | chat_model = ChatModel() 16 | messages = [] 17 | print("Welcome to the CLI application, use `clear` to remove the history, use `exit` to exit the application.") 18 | 19 | while True: 20 | try: 21 | query = input("\nUser: ") 22 | except UnicodeDecodeError: 23 | print("Detected decoding error at the inputs, please set the terminal encoding to utf-8.") 24 | continue 25 | except Exception: 26 | raise 27 | 28 | if query.strip() == "exit": 29 | break 30 | 31 | if query.strip() == "clear": 32 | messages = [] 33 | torch_gc() 34 | print("History has been removed.") 35 | continue 36 | 37 | messages.append({"role": "user", "content": query}) 38 | print("Assistant: ", end="", flush=True) 39 | 40 | response = "" 41 | for new_text in chat_model.stream_chat(messages): 42 | print(new_text, end="", flush=True) 43 | response += new_text 44 | print() 45 | messages.append({"role": "assistant", "content": response}) 46 | 47 | 48 | if __name__ == "__main__": 49 | main() 50 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/evaluate.py: -------------------------------------------------------------------------------- 1 | from llmtuner import Evaluator 2 | 3 | 4 | def main(): 5 | Evaluator().eval() 6 | 7 | 8 | if __name__ == "__main__": 9 | main() 10 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/export_model.py: -------------------------------------------------------------------------------- 1 | from llmtuner import export_model 2 | 3 | 4 | def main(): 5 | export_model() 6 | 7 | 8 | if __name__ == "__main__": 9 | main() 10 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/__init__.py: -------------------------------------------------------------------------------- 1 | # Level: api, webui > chat, eval, train > data, model > extras, hparams 2 | 3 | from .api import create_app 4 | from .chat import ChatModel 5 | from .eval import Evaluator 6 | from .train import export_model, run_exp 7 | from .webui import create_ui, create_web_demo 8 | 9 | 10 | __version__ = "0.7.0" 11 | __all__ = ["create_app", "ChatModel", "Evaluator", "export_model", "run_exp", "create_ui", "create_web_demo"] 12 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__init__.py: -------------------------------------------------------------------------------- 1 | from .app import create_app 2 | 3 | 4 | __all__ = ["create_app"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/api/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__pycache__/app.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/api/__pycache__/app.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__pycache__/app.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/api/__pycache__/app.cpython-311.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/api/__pycache__/protocol.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/api/__pycache__/protocol.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__init__.py: -------------------------------------------------------------------------------- 1 | from .base_engine import BaseEngine 2 | from .chat_model import ChatModel 3 | 4 | 5 | __all__ = ["BaseEngine", "ChatModel"] 6 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/chat/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__pycache__/base_engine.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/chat/__pycache__/base_engine.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__pycache__/chat_model.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/chat/__pycache__/chat_model.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__pycache__/hf_engine.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/chat/__pycache__/hf_engine.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/__pycache__/vllm_engine.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/chat/__pycache__/vllm_engine.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/chat/base_engine.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | from dataclasses import dataclass 3 | from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, List, Literal, Optional, Sequence, Union 4 | 5 | 6 | if TYPE_CHECKING: 7 | from numpy.typing import NDArray 8 | from transformers import PreTrainedModel, PreTrainedTokenizer 9 | from vllm import AsyncLLMEngine 10 | 11 | from ..data import Template 12 | from ..hparams import DataArguments, FinetuningArguments, GeneratingArguments, ModelArguments 13 | 14 | 15 | @dataclass 16 | class Response: 17 | response_text: str 18 | response_length: int 19 | prompt_length: int 20 | finish_reason: Literal["stop", "length"] 21 | 22 | 23 | class BaseEngine(ABC): 24 | model: Union["PreTrainedModel", "AsyncLLMEngine"] 25 | tokenizer: "PreTrainedTokenizer" 26 | can_generate: bool 27 | template: "Template" 28 | generating_args: Dict[str, Any] 29 | 30 | @abstractmethod 31 | def __init__( 32 | self, 33 | model_args: "ModelArguments", 34 | data_args: "DataArguments", 35 | finetuning_args: "FinetuningArguments", 36 | generating_args: "GeneratingArguments", 37 | ) -> None: ... 38 | 39 | @abstractmethod 40 | async def start( 41 | self, 42 | ) -> None: ... 43 | 44 | @abstractmethod 45 | async def chat( 46 | self, 47 | messages: Sequence[Dict[str, str]], 48 | system: Optional[str] = None, 49 | tools: Optional[str] = None, 50 | image: Optional["NDArray"] = None, 51 | **input_kwargs, 52 | ) -> List["Response"]: ... 53 | 54 | @abstractmethod 55 | async def stream_chat( 56 | self, 57 | messages: Sequence[Dict[str, str]], 58 | system: Optional[str] = None, 59 | tools: Optional[str] = None, 60 | image: Optional["NDArray"] = None, 61 | **input_kwargs, 62 | ) -> AsyncGenerator[str, None]: ... 63 | 64 | @abstractmethod 65 | async def get_scores( 66 | self, 67 | batch_input: List[str], 68 | **input_kwargs, 69 | ) -> List[float]: ... 70 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__init__.py: -------------------------------------------------------------------------------- 1 | from .collator import PairwiseDataCollatorWithPadding 2 | from .loader import get_dataset 3 | from .template import Template, get_template_and_fix_tokenizer, templates 4 | from .utils import Role, split_dataset 5 | 6 | 7 | __all__ = [ 8 | "PairwiseDataCollatorWithPadding", 9 | "get_dataset", 10 | "Template", 11 | "get_template_and_fix_tokenizer", 12 | "templates", 13 | "Role", 14 | "split_dataset", 15 | ] 16 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/aligner.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/aligner.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/collator.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/collator.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/formatter.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/formatter.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/loader.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/loader.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/parser.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/parser.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/preprocess.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/preprocess.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/template.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/template.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/data/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/data/collator.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | from typing import Any, Dict, List, Sequence, Tuple 3 | 4 | import torch 5 | from transformers import DataCollatorForSeq2Seq 6 | 7 | 8 | @dataclass 9 | class PairwiseDataCollatorWithPadding(DataCollatorForSeq2Seq): 10 | r""" 11 | Data collator for pairwise data. 12 | """ 13 | 14 | def _pad_labels(self, batch: torch.Tensor, positions: List[Tuple[int, int]]) -> torch.Tensor: 15 | r""" 16 | Masks out the input ids except for the responses. 17 | """ 18 | padded_labels = [] 19 | for feature, (prompt_len, answer_len) in zip(batch, positions): 20 | if self.tokenizer.padding_side == "left": 21 | start, end = feature.size(0) - answer_len, feature.size(0) 22 | else: 23 | start, end = prompt_len, prompt_len + answer_len 24 | padded_tensor = self.label_pad_token_id * torch.ones_like(feature) 25 | padded_tensor[start:end] = feature[start:end] 26 | padded_labels.append(padded_tensor) 27 | return torch.stack(padded_labels, dim=0).contiguous() # in contiguous memory 28 | 29 | def __call__(self, features: Sequence[Dict[str, Any]]) -> Dict[str, torch.Tensor]: 30 | r""" 31 | Pads batched data to the longest sequence in the batch. 32 | 33 | We generate 2 * n examples where the first n examples represent chosen examples and 34 | the last n examples represent rejected examples. 35 | """ 36 | concatenated_features = [] 37 | label_positions = [] 38 | for key in ("chosen_ids", "rejected_ids"): 39 | for feature in features: 40 | prompt_len, answer_len = len(feature["prompt_ids"]), len(feature[key]) 41 | concatenated_features.append( 42 | { 43 | "input_ids": feature["prompt_ids"] + feature[key], 44 | "attention_mask": [1] * (prompt_len + answer_len), 45 | } 46 | ) 47 | label_positions.append((prompt_len, answer_len)) 48 | 49 | batch = super().__call__(concatenated_features) 50 | batch["labels"] = self._pad_labels(batch["input_ids"], label_positions) 51 | return batch 52 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/eval/__init__.py: -------------------------------------------------------------------------------- 1 | from .evaluator import Evaluator 2 | 3 | 4 | __all__ = ["Evaluator"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/eval/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/eval/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/eval/__pycache__/evaluator.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/eval/__pycache__/evaluator.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/eval/__pycache__/template.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/eval/__pycache__/template.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/eval/template.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | from typing import Dict, List, Sequence, Tuple 3 | 4 | from ..data import Role 5 | from ..extras.constants import CHOICES 6 | 7 | 8 | @dataclass 9 | class EvalTemplate: 10 | system: str 11 | choice: str 12 | answer: str 13 | prefix: str 14 | 15 | def _parse_example(self, example: Dict[str, str]) -> Tuple[str, str]: 16 | r""" 17 | input: a dict with keys {"question", "A", "B", "C", "D", "answer"} 18 | output: a tuple of (prompt, response) 19 | """ 20 | candidates = [self.choice.format(choice=ch, content=example[ch]) for ch in CHOICES if ch in example] 21 | return "".join([example["question"]] + candidates + [self.answer]), example["answer"] 22 | 23 | def format_example( 24 | self, target_data: Dict[str, str], support_set: Sequence[Dict[str, str]], subject_name: str 25 | ) -> List[Dict[str, str]]: 26 | r""" 27 | Converts dataset examples to messages. 28 | """ 29 | messages = [] 30 | for k in range(len(support_set)): 31 | prompt, response = self._parse_example(support_set[k]) 32 | messages.append({"role": Role.USER.value, "content": prompt}) 33 | messages.append({"role": Role.ASSISTANT.value, "content": response}) 34 | 35 | prompt, response = self._parse_example(target_data) 36 | messages.append({"role": Role.USER.value, "content": prompt}) 37 | messages.append({"role": Role.ASSISTANT.value, "content": response}) 38 | messages[0]["content"] = self.system.format(subject=subject_name) + messages[0]["content"] 39 | return messages 40 | 41 | 42 | eval_templates: Dict[str, "EvalTemplate"] = {} 43 | 44 | 45 | def _register_eval_template(name: str, system: str, choice: str, answer: str, prefix: str) -> None: 46 | eval_templates[name] = EvalTemplate(system=system, choice=choice, answer=answer, prefix=prefix) 47 | 48 | 49 | def get_eval_template(name: str) -> "EvalTemplate": 50 | eval_template = eval_templates.get(name, None) 51 | assert eval_template is not None, "Template {} does not exist.".format(name) 52 | return eval_template 53 | 54 | 55 | _register_eval_template( 56 | name="en", 57 | system="The following are multiple choice questions (with answers) about {subject}.\n\n", 58 | choice="\n{choice}. {content}", 59 | answer="\nAnswer: ", 60 | prefix=" ", 61 | ) 62 | 63 | 64 | _register_eval_template( 65 | name="zh", 66 | system="以下是中国关于{subject}考试的单项选择题,请选出其中的正确答案。\n\n", 67 | choice="\n{choice}. {content}", 68 | answer="\n答案:", 69 | prefix=" ", 70 | ) 71 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__init__.py -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/callbacks.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/callbacks.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/constants.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/constants.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/logging.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/logging.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/misc.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/misc.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/packages.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/packages.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/__pycache__/ploting.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/extras/__pycache__/ploting.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/logging.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import sys 3 | 4 | 5 | class LoggerHandler(logging.Handler): 6 | r""" 7 | Logger handler used in Web UI. 8 | """ 9 | 10 | def __init__(self): 11 | super().__init__() 12 | self.log = "" 13 | 14 | def reset(self): 15 | self.log = "" 16 | 17 | def emit(self, record): 18 | if record.name == "httpx": 19 | return 20 | log_entry = self.format(record) 21 | self.log += log_entry 22 | self.log += "\n\n" 23 | 24 | 25 | def get_logger(name: str) -> logging.Logger: 26 | r""" 27 | Gets a standard logger with a stream hander to stdout. 28 | """ 29 | formatter = logging.Formatter( 30 | fmt="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S" 31 | ) 32 | handler = logging.StreamHandler(sys.stdout) 33 | handler.setFormatter(formatter) 34 | 35 | logger = logging.getLogger(name) 36 | logger.setLevel(logging.INFO) 37 | logger.addHandler(handler) 38 | 39 | return logger 40 | 41 | 42 | def reset_logging() -> None: 43 | r""" 44 | Removes basic config of root logger. (unused in script) 45 | """ 46 | root = logging.getLogger() 47 | list(map(root.removeHandler, root.handlers)) 48 | list(map(root.removeFilter, root.filters)) 49 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/packages.py: -------------------------------------------------------------------------------- 1 | import importlib.metadata 2 | import importlib.util 3 | from typing import TYPE_CHECKING 4 | 5 | from packaging import version 6 | 7 | 8 | if TYPE_CHECKING: 9 | from packaging.version import Version 10 | 11 | 12 | def _is_package_available(name: str) -> bool: 13 | return importlib.util.find_spec(name) is not None 14 | 15 | 16 | def _get_package_version(name: str) -> "Version": 17 | try: 18 | return version.parse(importlib.metadata.version(name)) 19 | except Exception: 20 | return version.parse("0.0.0") 21 | 22 | 23 | def is_fastapi_availble(): 24 | return _is_package_available("fastapi") 25 | 26 | 27 | def is_flash_attn2_available(): 28 | return _is_package_available("flash_attn") and _get_package_version("flash_attn") > version.parse("2.0.0") 29 | 30 | 31 | def is_galore_available(): 32 | return _is_package_available("galore_torch") 33 | 34 | 35 | def is_gradio_available(): 36 | return _is_package_available("gradio") 37 | 38 | 39 | def is_jieba_available(): 40 | return _is_package_available("jieba") 41 | 42 | 43 | def is_matplotlib_available(): 44 | return _is_package_available("matplotlib") 45 | 46 | 47 | def is_nltk_available(): 48 | return _is_package_available("nltk") 49 | 50 | 51 | def is_pillow_available(): 52 | return _is_package_available("PIL") 53 | 54 | 55 | def is_requests_available(): 56 | return _is_package_available("requests") 57 | 58 | 59 | def is_rouge_available(): 60 | return _is_package_available("rouge_chinese") 61 | 62 | 63 | def is_sdpa_available(): 64 | return _get_package_version("torch") > version.parse("2.1.1") 65 | 66 | 67 | def is_starlette_available(): 68 | return _is_package_available("sse_starlette") 69 | 70 | 71 | def is_uvicorn_available(): 72 | return _is_package_available("uvicorn") 73 | 74 | 75 | def is_vllm_available(): 76 | return _is_package_available("vllm") 77 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/extras/ploting.py: -------------------------------------------------------------------------------- 1 | import json 2 | import math 3 | import os 4 | from typing import List 5 | 6 | from transformers.trainer import TRAINER_STATE_NAME 7 | 8 | from .logging import get_logger 9 | from .packages import is_matplotlib_available 10 | 11 | 12 | if is_matplotlib_available(): 13 | import matplotlib.pyplot as plt 14 | 15 | 16 | logger = get_logger(__name__) 17 | 18 | 19 | def smooth(scalars: List[float]) -> List[float]: 20 | r""" 21 | EMA implementation according to TensorBoard. 22 | """ 23 | last = scalars[0] 24 | smoothed = list() 25 | weight = 1.8 * (1 / (1 + math.exp(-0.05 * len(scalars))) - 0.5) # a sigmoid function 26 | for next_val in scalars: 27 | smoothed_val = last * weight + (1 - weight) * next_val 28 | smoothed.append(smoothed_val) 29 | last = smoothed_val 30 | return smoothed 31 | 32 | 33 | def plot_loss(save_dictionary: os.PathLike, keys: List[str] = ["loss"]) -> None: 34 | with open(os.path.join(save_dictionary, TRAINER_STATE_NAME), "r", encoding="utf-8") as f: 35 | data = json.load(f) 36 | 37 | for key in keys: 38 | steps, metrics = [], [] 39 | for i in range(len(data["log_history"])): 40 | if key in data["log_history"][i]: 41 | steps.append(data["log_history"][i]["step"]) 42 | metrics.append(data["log_history"][i][key]) 43 | 44 | if len(metrics) == 0: 45 | logger.warning(f"No metric {key} to plot.") 46 | continue 47 | 48 | plt.figure() 49 | plt.plot(steps, metrics, color="#1f77b4", alpha=0.4, label="original") 50 | plt.plot(steps, smooth(metrics), color="#1f77b4", label="smoothed") 51 | plt.title("training {} of {}".format(key, save_dictionary)) 52 | plt.xlabel("step") 53 | plt.ylabel(key) 54 | plt.legend() 55 | figure_path = os.path.join(save_dictionary, "training_{}.png".format(key.replace("/", "_"))) 56 | plt.savefig(figure_path, format="png", dpi=100) 57 | print("Figure saved at:", figure_path) 58 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__init__.py: -------------------------------------------------------------------------------- 1 | from .data_args import DataArguments 2 | from .evaluation_args import EvaluationArguments 3 | from .finetuning_args import FinetuningArguments 4 | from .generating_args import GeneratingArguments 5 | from .model_args import ModelArguments 6 | from .parser import get_eval_args, get_infer_args, get_train_args 7 | 8 | 9 | __all__ = [ 10 | "DataArguments", 11 | "EvaluationArguments", 12 | "FinetuningArguments", 13 | "GeneratingArguments", 14 | "ModelArguments", 15 | "get_eval_args", 16 | "get_infer_args", 17 | "get_train_args", 18 | ] 19 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/data_args.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/data_args.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/evaluation_args.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/evaluation_args.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/finetuning_args.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/finetuning_args.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/generating_args.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/generating_args.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/model_args.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/model_args.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/parser.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/hparams/__pycache__/parser.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/evaluation_args.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dataclasses import dataclass, field 3 | from typing import Literal, Optional 4 | 5 | from datasets import DownloadMode 6 | 7 | 8 | @dataclass 9 | class EvaluationArguments: 10 | r""" 11 | Arguments pertaining to specify the evaluation parameters. 12 | """ 13 | 14 | task: str = field( 15 | metadata={"help": "Name of the evaluation task."}, 16 | ) 17 | task_dir: str = field( 18 | default="evaluation", 19 | metadata={"help": "Path to the folder containing the evaluation datasets."}, 20 | ) 21 | batch_size: int = field( 22 | default=4, 23 | metadata={"help": "The batch size per GPU for evaluation."}, 24 | ) 25 | seed: int = field( 26 | default=42, 27 | metadata={"help": "Random seed to be used with data loaders."}, 28 | ) 29 | lang: Literal["en", "zh"] = field( 30 | default="en", 31 | metadata={"help": "Language used at evaluation."}, 32 | ) 33 | n_shot: int = field( 34 | default=5, 35 | metadata={"help": "Number of examplars for few-shot learning."}, 36 | ) 37 | save_dir: Optional[str] = field( 38 | default=None, 39 | metadata={"help": "Path to save the evaluation results."}, 40 | ) 41 | download_mode: DownloadMode = field( 42 | default=DownloadMode.REUSE_DATASET_IF_EXISTS, 43 | metadata={"help": "Download mode used for the evaluation datasets."}, 44 | ) 45 | 46 | def __post_init__(self): 47 | if self.save_dir is not None and os.path.exists(self.save_dir): 48 | raise ValueError("`save_dir` already exists, use another one.") 49 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/hparams/generating_args.py: -------------------------------------------------------------------------------- 1 | from dataclasses import asdict, dataclass, field 2 | from typing import Any, Dict 3 | 4 | 5 | @dataclass 6 | class GeneratingArguments: 7 | r""" 8 | Arguments pertaining to specify the decoding parameters. 9 | """ 10 | 11 | do_sample: bool = field( 12 | default=True, 13 | metadata={"help": "Whether or not to use sampling, use greedy decoding otherwise."}, 14 | ) 15 | temperature: float = field( 16 | default=0.95, 17 | metadata={"help": "The value used to modulate the next token probabilities."}, 18 | ) 19 | top_p: float = field( 20 | default=0.7, 21 | metadata={ 22 | "help": "The smallest set of most probable tokens with probabilities that add up to top_p or higher are kept." 23 | }, 24 | ) 25 | top_k: int = field( 26 | default=50, 27 | metadata={"help": "The number of highest probability vocabulary tokens to keep for top-k filtering."}, 28 | ) 29 | num_beams: int = field( 30 | default=1, 31 | metadata={"help": "Number of beams for beam search. 1 means no beam search."}, 32 | ) 33 | max_length: int = field( 34 | default=1024, 35 | metadata={"help": "The maximum length the generated tokens can have. It can be overridden by max_new_tokens."}, 36 | ) 37 | max_new_tokens: int = field( 38 | default=1024, 39 | metadata={"help": "The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt."}, 40 | ) 41 | repetition_penalty: float = field( 42 | default=1.0, 43 | metadata={"help": "The parameter for repetition penalty. 1.0 means no penalty."}, 44 | ) 45 | length_penalty: float = field( 46 | default=1.0, 47 | metadata={"help": "Exponential penalty to the length that is used with beam-based generation."}, 48 | ) 49 | 50 | def to_dict(self) -> Dict[str, Any]: 51 | args = asdict(self) 52 | if args.get("max_new_tokens", -1) > 0: 53 | args.pop("max_length", None) 54 | else: 55 | args.pop("max_new_tokens", None) 56 | return args 57 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/__init__.py: -------------------------------------------------------------------------------- 1 | from .loader import load_config, load_model, load_tokenizer 2 | from .utils.misc import find_all_linear_modules 3 | from .utils.valuehead import load_valuehead_params 4 | 5 | 6 | __all__ = [ 7 | "load_config", 8 | "load_model", 9 | "load_tokenizer", 10 | "load_valuehead_params", 11 | "find_all_linear_modules", 12 | ] 13 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/__pycache__/adapter.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/__pycache__/adapter.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/__pycache__/loader.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/__pycache__/loader.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/__pycache__/patcher.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/__pycache__/patcher.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__init__.py -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/attention.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/attention.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/checkpointing.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/checkpointing.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/embedding.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/embedding.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/longlora.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/longlora.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/misc.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/misc.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/mod.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/mod.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/moe.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/moe.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/quantization.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/quantization.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/rope.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/rope.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/unsloth.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/unsloth.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/valuehead.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/valuehead.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/visual.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/model/utils/__pycache__/visual.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/attention.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING 2 | 3 | from ...extras.logging import get_logger 4 | from ...extras.packages import is_flash_attn2_available, is_sdpa_available 5 | 6 | 7 | if TYPE_CHECKING: 8 | from transformers import PretrainedConfig 9 | 10 | from ...hparams import ModelArguments 11 | 12 | 13 | logger = get_logger(__name__) 14 | 15 | 16 | def configure_attn_implementation(config: "PretrainedConfig", model_args: "ModelArguments") -> None: 17 | if model_args.flash_attn == "auto": 18 | return 19 | 20 | elif model_args.flash_attn == "off": 21 | requested_attn_implementation = "eager" 22 | 23 | elif model_args.flash_attn == "sdpa": 24 | if not is_sdpa_available(): 25 | logger.warning("Torch>=2.1.1 is required for SDPA attention.") 26 | return 27 | 28 | requested_attn_implementation = "sdpa" 29 | elif model_args.flash_attn == "fa2": 30 | if not is_flash_attn2_available(): 31 | logger.warning("FlashAttention-2 is not installed.") 32 | return 33 | 34 | requested_attn_implementation = "flash_attention_2" 35 | else: 36 | raise NotImplementedError("Unknown attention type: {}".format(model_args.flash_attn)) 37 | 38 | if getattr(config, "model_type", None) == "internlm2": # special case for custom models 39 | setattr(config, "attn_implementation", requested_attn_implementation) 40 | else: 41 | setattr(config, "_attn_implementation", requested_attn_implementation) 42 | 43 | 44 | def print_attn_implementation(config: "PretrainedConfig") -> None: 45 | if getattr(config, "model_type", None) == "internlm2": # special case for custom models 46 | attn_implementation = getattr(config, "attn_implementation", None) 47 | else: 48 | attn_implementation = getattr(config, "_attn_implementation", None) 49 | 50 | if attn_implementation == "flash_attention_2": 51 | logger.info("Using FlashAttention-2 for faster training and inference.") 52 | elif attn_implementation == "sdpa": 53 | logger.info("Using torch SDPA for faster training and inference.") 54 | else: 55 | logger.info("Using vanilla Attention implementation.") 56 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/embedding.py: -------------------------------------------------------------------------------- 1 | import math 2 | from contextlib import nullcontext 3 | from typing import TYPE_CHECKING 4 | 5 | import torch 6 | from transformers.integrations import is_deepspeed_zero3_enabled 7 | 8 | from ...extras.logging import get_logger 9 | 10 | 11 | if TYPE_CHECKING: 12 | from transformers import PreTrainedModel, PreTrainedTokenizer 13 | 14 | 15 | logger = get_logger(__name__) 16 | 17 | 18 | def _noisy_mean_initialization(embed_weight: torch.Tensor, num_new_tokens: int) -> None: 19 | embedding_dim = embed_weight.size(1) 20 | avg_weight = embed_weight[:-num_new_tokens].mean(dim=0, keepdim=True) 21 | noise_weight = torch.empty_like(embed_weight[-num_new_tokens:]) 22 | noise_weight.normal_(mean=0, std=(1.0 / math.sqrt(embedding_dim))) 23 | embed_weight[-num_new_tokens:] = avg_weight + noise_weight 24 | 25 | 26 | def resize_embedding_layer(model: "PreTrainedModel", tokenizer: "PreTrainedTokenizer") -> None: 27 | r""" 28 | Resize token embeddings. 29 | """ 30 | if is_deepspeed_zero3_enabled(): 31 | import deepspeed # type: ignore 32 | 33 | params = [model.get_input_embeddings().weight] 34 | if model.get_output_embeddings() is not None and not model.config.tie_word_embeddings: 35 | params.append(model.get_output_embeddings().weight) 36 | 37 | context_maybe_zero3 = deepspeed.zero.GatheredParameters(params, modifier_rank=0) 38 | else: 39 | context_maybe_zero3 = nullcontext() 40 | 41 | with context_maybe_zero3: 42 | current_embedding_size = model.get_input_embeddings().weight.size(0) 43 | 44 | if len(tokenizer) > current_embedding_size: 45 | if getattr(model, "quantization_method", None): 46 | raise ValueError("Cannot resize embedding layers of a quantized model.") 47 | 48 | if not isinstance(model.get_output_embeddings(), torch.nn.Linear): 49 | raise ValueError("Current model does not support resizing embedding layers.") 50 | 51 | model.resize_token_embeddings(len(tokenizer), pad_to_multiple_of=64) 52 | with context_maybe_zero3: 53 | new_embedding_size = model.get_input_embeddings().weight.size(0) 54 | num_new_tokens = new_embedding_size - current_embedding_size 55 | _noisy_mean_initialization(model.get_input_embeddings().weight.data, num_new_tokens) 56 | _noisy_mean_initialization(model.get_output_embeddings().weight.data, num_new_tokens) 57 | 58 | logger.info("Resized token embeddings from {} to {}.".format(current_embedding_size, new_embedding_size)) 59 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/mod.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING 2 | 3 | from ...extras.constants import MOD_SUPPORTED_MODELS 4 | 5 | 6 | if TYPE_CHECKING: 7 | from transformers import PretrainedConfig, PreTrainedModel 8 | 9 | from ...hparams import ModelArguments 10 | 11 | 12 | def load_mod_pretrained_model(**init_kwargs) -> "PreTrainedModel": 13 | from MoD import AutoMoDModelForCausalLM 14 | 15 | return AutoMoDModelForCausalLM.from_pretrained(**init_kwargs) 16 | 17 | 18 | def convert_pretrained_model_to_mod( 19 | model: "PreTrainedModel", config: "PretrainedConfig", model_args: "ModelArguments" 20 | ) -> "PreTrainedModel": 21 | from MoD import apply_mod_to_hf 22 | 23 | if getattr(config, "model_type", None) not in MOD_SUPPORTED_MODELS: 24 | raise ValueError("Current model is not supported by mixture-of-depth.") 25 | 26 | model = apply_mod_to_hf(model) 27 | model = model.to(model_args.compute_dtype) 28 | return model 29 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/moe.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING 2 | 3 | from transformers.integrations import is_deepspeed_zero3_enabled 4 | from transformers.utils.versions import require_version 5 | 6 | 7 | if TYPE_CHECKING: 8 | from transformers import PretrainedConfig, PreTrainedModel 9 | 10 | from ...hparams import ModelArguments 11 | 12 | 13 | def add_z3_leaf_module(model: "PreTrainedModel") -> None: 14 | r""" 15 | Sets module as a leaf module to skip partitioning in deepspeed zero3. 16 | """ 17 | if not is_deepspeed_zero3_enabled(): 18 | return 19 | 20 | require_version("deepspeed>=0.13.0", "To fix: pip install deepspeed>=0.13.0") 21 | from deepspeed.utils import set_z3_leaf_modules # type: ignore 22 | 23 | if getattr(model.config, "model_type", None) == "mixtral": 24 | from transformers.models.mixtral.modeling_mixtral import MixtralSparseMoeBlock 25 | 26 | set_z3_leaf_modules(model, [MixtralSparseMoeBlock]) 27 | 28 | if getattr(model.config, "model_type", None) == "qwen2moe": 29 | from transformers.models.qwen2_moe.modeling_qwen2_moe import Qwen2MoeSparseMoeBlock 30 | 31 | set_z3_leaf_modules(model, [Qwen2MoeSparseMoeBlock]) 32 | 33 | if getattr(model.config, "model_type", None) == "jamba": 34 | from transformers.models.jamba.modeling_jamba import JambaSparseMoeBlock 35 | 36 | set_z3_leaf_modules(model, [JambaSparseMoeBlock]) 37 | 38 | if getattr(model.config, "model_type", None) == "dbrx": 39 | from transformers.models.dbrx.modeling_dbrx import DbrxFFN 40 | 41 | set_z3_leaf_modules(model, [DbrxFFN]) 42 | 43 | 44 | def configure_moe(config: "PretrainedConfig", model_args: "ModelArguments", is_trainable: bool) -> None: 45 | if model_args.moe_aux_loss_coef is not None: 46 | if getattr(config, "model_type", None) in ["jamba", "mixtral", "qwen2_moe"]: 47 | setattr(config, "router_aux_loss_coef", model_args.moe_aux_loss_coef) 48 | 49 | elif getattr(config, "model_type", None) == "deepseek": 50 | setattr(config, "aux_loss_alpha", model_args.moe_aux_loss_coef) 51 | 52 | if getattr(config, "model_type", None) in ["dbrx", "jamba", "mixtral", "qwen2_moe"]: 53 | setattr(config, "output_router_logits", is_trainable) 54 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/rope.py: -------------------------------------------------------------------------------- 1 | import math 2 | from typing import TYPE_CHECKING 3 | 4 | from ...extras.logging import get_logger 5 | 6 | 7 | if TYPE_CHECKING: 8 | from transformers import PretrainedConfig 9 | 10 | from ...hparams import ModelArguments 11 | 12 | 13 | logger = get_logger(__name__) 14 | 15 | 16 | def configure_rope(config: "PretrainedConfig", model_args: "ModelArguments", is_trainable: bool) -> None: 17 | if model_args.rope_scaling is None: 18 | return 19 | 20 | if not hasattr(config, "rope_scaling"): 21 | logger.warning("Current model does not support RoPE scaling.") 22 | return 23 | 24 | if is_trainable: 25 | if model_args.rope_scaling == "dynamic": 26 | logger.warning( 27 | "Dynamic NTK scaling may not work well with fine-tuning. " 28 | "See: https://github.com/huggingface/transformers/pull/24653" 29 | ) 30 | 31 | current_max_length = getattr(config, "max_position_embeddings", None) 32 | if current_max_length and model_args.model_max_length > current_max_length: 33 | logger.info( 34 | "Enlarge max model length from {} to {}.".format(current_max_length, model_args.model_max_length) 35 | ) 36 | setattr(config, "max_position_embeddings", model_args.model_max_length) 37 | scaling_factor = float(math.ceil(model_args.model_max_length / current_max_length)) 38 | else: 39 | logger.warning("Input length is smaller than max length. Consider increase input length.") 40 | scaling_factor = 1.0 41 | else: 42 | scaling_factor = 2.0 43 | 44 | setattr(config, "rope_scaling", {"type": model_args.rope_scaling, "factor": scaling_factor}) 45 | logger.info( 46 | "Using {} scaling strategy and setting scaling factor to {}".format(model_args.rope_scaling, scaling_factor) 47 | ) 48 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/valuehead.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict 2 | 3 | import torch 4 | from transformers.utils import cached_file 5 | 6 | from ...extras.constants import V_HEAD_SAFE_WEIGHTS_NAME, V_HEAD_WEIGHTS_NAME 7 | from ...extras.logging import get_logger 8 | 9 | 10 | if TYPE_CHECKING: 11 | from transformers import PretrainedConfig, PreTrainedModel 12 | 13 | from ...hparams import ModelArguments 14 | 15 | 16 | logger = get_logger(__name__) 17 | 18 | 19 | def configure_valuehead(config: "PretrainedConfig") -> None: 20 | if getattr(config, "model_type", None) == "llava": 21 | setattr(config, "hidden_size", getattr(config.vision_config, "intermediate_size", None)) 22 | 23 | 24 | def load_valuehead_params(path_or_repo_id: str, model_args: "ModelArguments") -> Dict[str, torch.Tensor]: 25 | r""" 26 | Loads value head parameters from Hugging Face Hub or local disk. 27 | 28 | Returns: dict with keys `v_head.summary.weight` and `v_head.summary.bias`. 29 | """ 30 | kwargs = {"path_or_repo_id": path_or_repo_id, "cache_dir": model_args.cache_dir, "token": model_args.hf_hub_token} 31 | 32 | try: 33 | from safetensors import safe_open 34 | 35 | vhead_file = cached_file(filename=V_HEAD_SAFE_WEIGHTS_NAME, **kwargs) 36 | with safe_open(vhead_file, framework="pt", device="cpu") as f: 37 | return {key: f.get_tensor(key) for key in f.keys()} 38 | except Exception as err: 39 | logger.info("Failed to load {}: {}".format(V_HEAD_SAFE_WEIGHTS_NAME, str(err))) 40 | 41 | try: 42 | vhead_file = cached_file(filename=V_HEAD_WEIGHTS_NAME, **kwargs) 43 | return torch.load(vhead_file, map_location="cpu") 44 | except Exception as err: 45 | logger.info("Failed to load {}: {}".format(V_HEAD_WEIGHTS_NAME, str(err))) 46 | 47 | logger.info("Provided path ({}) does not contain value head weights.".format(path_or_repo_id)) 48 | logger.info("Ignore these messages if you are not resuming the training of a value head model.") 49 | return None 50 | 51 | 52 | def prepare_valuehead_model(model: "PreTrainedModel") -> None: 53 | if getattr(model.config, "model_type", None) == "llava": 54 | setattr(model, "lm_head", model.language_model.get_output_embeddings()) 55 | setattr(model, "_keys_to_ignore_on_save", ["lm_head.weight"]) 56 | 57 | if getattr(model.config, "model_type", None) == "chatglm": 58 | setattr(model, "lm_head", model.transformer.output_layer) 59 | setattr(model, "_keys_to_ignore_on_save", ["lm_head.weight"]) 60 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/model/utils/visual.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Tuple 2 | 3 | import torch 4 | 5 | from ...extras.logging import get_logger 6 | 7 | 8 | if TYPE_CHECKING: 9 | from transformers import PreTrainedModel 10 | 11 | from ...hparams import ModelArguments 12 | 13 | 14 | logger = get_logger(__name__) 15 | 16 | 17 | def autocast_projector_dtype( 18 | model: "PreTrainedModel", model_args: "ModelArguments", mm_projector_name: str = "multi_modal_projector" 19 | ) -> None: 20 | def _mm_projector_forward_post_hook( 21 | module: "torch.nn.Module", args: Tuple["torch.Tensor"], output: "torch.Tensor" 22 | ) -> "torch.Tensor": 23 | return output.to(model_args.compute_dtype) 24 | 25 | if hasattr(model, mm_projector_name): 26 | logger.info("Casting multimodal projector outputs in {}.".format(model_args.compute_dtype)) 27 | mm_projector: "torch.nn.Module" = getattr(model, mm_projector_name) 28 | mm_projector.register_forward_hook(_mm_projector_forward_post_hook) 29 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/__init__.py: -------------------------------------------------------------------------------- 1 | from .tuner import export_model, run_exp 2 | 3 | 4 | __all__ = ["export_model", "run_exp"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/__pycache__/tuner.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/__pycache__/tuner.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/dpo/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_dpo 2 | 3 | 4 | __all__ = ["run_dpo"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/dpo/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/orpo/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_orpo 2 | 3 | 4 | __all__ = ["run_orpo"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/orpo/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/orpo/workflow.py: -------------------------------------------------------------------------------- 1 | # Inspired by: https://github.com/huggingface/trl/blob/main/examples/research_projects/stack_llama_2/scripts/dpo_llama2.py 2 | 3 | from typing import TYPE_CHECKING, List, Optional 4 | 5 | from ...data import PairwiseDataCollatorWithPadding, get_dataset, split_dataset 6 | from ...extras.constants import IGNORE_INDEX 7 | from ...extras.ploting import plot_loss 8 | from ...hparams import ModelArguments 9 | from ...model import load_model, load_tokenizer 10 | from ..utils import create_modelcard_and_push 11 | from .trainer import CustomORPOTrainer 12 | 13 | 14 | if TYPE_CHECKING: 15 | from transformers import Seq2SeqTrainingArguments, TrainerCallback 16 | 17 | from ...hparams import DataArguments, FinetuningArguments 18 | 19 | 20 | def run_orpo( 21 | model_args: "ModelArguments", 22 | data_args: "DataArguments", 23 | training_args: "Seq2SeqTrainingArguments", 24 | finetuning_args: "FinetuningArguments", 25 | callbacks: Optional[List["TrainerCallback"]] = None, 26 | ): 27 | tokenizer_module = load_tokenizer(model_args) 28 | tokenizer = tokenizer_module["tokenizer"] 29 | dataset = get_dataset(model_args, data_args, training_args, stage="rm", **tokenizer_module) 30 | model = load_model(tokenizer, model_args, finetuning_args, training_args.do_train) 31 | 32 | data_collator = PairwiseDataCollatorWithPadding( 33 | tokenizer=tokenizer, 34 | pad_to_multiple_of=8, 35 | label_pad_token_id=IGNORE_INDEX if data_args.ignore_pad_token_for_loss else tokenizer.pad_token_id, 36 | ) 37 | 38 | # Update arguments 39 | training_args.remove_unused_columns = False # important for pairwise dataset 40 | 41 | # Initialize our Trainer 42 | trainer = CustomORPOTrainer( 43 | model=model, 44 | args=training_args, 45 | finetuning_args=finetuning_args, 46 | tokenizer=tokenizer, 47 | data_collator=data_collator, 48 | callbacks=callbacks, 49 | **split_dataset(dataset, data_args, training_args), 50 | ) 51 | 52 | # Training 53 | if training_args.do_train: 54 | train_result = trainer.train(resume_from_checkpoint=training_args.resume_from_checkpoint) 55 | trainer.save_model() 56 | trainer.log_metrics("train", train_result.metrics) 57 | trainer.save_metrics("train", train_result.metrics) 58 | trainer.save_state() 59 | if trainer.is_world_process_zero() and finetuning_args.plot_loss: 60 | plot_loss(training_args.output_dir, keys=["loss", "eval_loss", "rewards/accuracies", "sft_loss"]) 61 | 62 | # Evaluation 63 | if training_args.do_eval: 64 | metrics = trainer.evaluate(metric_key_prefix="eval") 65 | trainer.log_metrics("eval", metrics) 66 | trainer.save_metrics("eval", metrics) 67 | 68 | # Create model card 69 | create_modelcard_and_push(trainer, model_args, data_args, training_args, finetuning_args) 70 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_ppo 2 | 3 | 4 | __all__ = ["run_ppo"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/ppo/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/utils.py: -------------------------------------------------------------------------------- 1 | import json 2 | from contextlib import nullcontext 3 | from typing import TYPE_CHECKING, Dict, List, Literal, Optional 4 | 5 | import torch 6 | from transformers.integrations import is_deepspeed_zero3_enabled 7 | 8 | from ...extras.packages import is_requests_available 9 | 10 | 11 | if TYPE_CHECKING: 12 | from transformers import PreTrainedModel 13 | from trl import AutoModelForCausalLMWithValueHead 14 | 15 | if is_requests_available(): 16 | import requests 17 | 18 | 19 | def get_rewards_from_server(server_url: str, messages: List[str]) -> List[torch.Tensor]: 20 | headers = {"Content-Type": "application/json"} 21 | payload = {"model": "model", "messages": messages} 22 | response = requests.post(server_url, json=payload, headers=headers) 23 | rewards = json.loads(response.text)["scores"] 24 | return torch.Tensor(rewards) 25 | 26 | 27 | def replace_model(model: "AutoModelForCausalLMWithValueHead", target: Literal["default", "reward"]) -> None: 28 | if is_deepspeed_zero3_enabled(): 29 | import deepspeed # type: ignore 30 | 31 | params = [model.v_head.summary.weight, model.v_head.summary.bias] 32 | context_maybe_zero3 = deepspeed.zero.GatheredParameters(params, modifier_rank=0) 33 | else: 34 | context_maybe_zero3 = nullcontext() 35 | 36 | with context_maybe_zero3: 37 | if target == "reward": # save default head temporarily 38 | setattr(model, "default_head_weight", model.v_head.summary.weight.data.detach().clone()) 39 | setattr(model, "default_head_bias", model.v_head.summary.bias.data.detach().clone()) 40 | 41 | model.pretrained_model.set_adapter(target) # set the LoRA adapter to be active 42 | model.v_head.summary.weight.data = model.get_buffer("{}_head_weight".format(target)).detach().clone() 43 | model.v_head.summary.bias.data = model.get_buffer("{}_head_bias".format(target)).detach().clone() 44 | 45 | 46 | def dump_layernorm(model: "PreTrainedModel") -> Dict[str, torch.Tensor]: 47 | layer_norm_params = {} 48 | for name, param in model.named_parameters(): 49 | if param.data.dtype == torch.float32: 50 | layer_norm_params[name] = param.data.detach().clone() 51 | param.data = param.data.to(model.config.torch_dtype) 52 | 53 | return layer_norm_params 54 | 55 | 56 | def restore_layernorm(model: "PreTrainedModel", layernorm_params: Optional[Dict[str, torch.Tensor]] = None) -> None: 57 | for name, param in model.named_parameters(): 58 | if name in layernorm_params: 59 | param.data = layernorm_params[name] 60 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/ppo/workflow.py: -------------------------------------------------------------------------------- 1 | # Inspired by: https://github.com/lvwerra/trl/blob/main/examples/research_projects/stack_llama/scripts/rl_training.py 2 | 3 | from typing import TYPE_CHECKING, List, Optional 4 | 5 | from transformers import DataCollatorWithPadding 6 | 7 | from ...data import get_dataset 8 | from ...extras.callbacks import FixValueHeadModelCallback 9 | from ...extras.misc import fix_valuehead_checkpoint 10 | from ...extras.ploting import plot_loss 11 | from ...model import load_model, load_tokenizer 12 | from ..utils import create_ref_model, create_reward_model 13 | from .trainer import CustomPPOTrainer 14 | 15 | 16 | if TYPE_CHECKING: 17 | from transformers import Seq2SeqTrainingArguments, TrainerCallback 18 | 19 | from ...hparams import DataArguments, FinetuningArguments, GeneratingArguments, ModelArguments 20 | 21 | 22 | def run_ppo( 23 | model_args: "ModelArguments", 24 | data_args: "DataArguments", 25 | training_args: "Seq2SeqTrainingArguments", 26 | finetuning_args: "FinetuningArguments", 27 | generating_args: "GeneratingArguments", 28 | callbacks: Optional[List["TrainerCallback"]] = None, 29 | ): 30 | tokenizer_module = load_tokenizer(model_args) 31 | tokenizer = tokenizer_module["tokenizer"] 32 | dataset = get_dataset(model_args, data_args, training_args, stage="ppo", **tokenizer_module) 33 | model = load_model(tokenizer, model_args, finetuning_args, training_args.do_train, add_valuehead=True) 34 | 35 | tokenizer.padding_side = "left" # use left-padding in generation while using right-padding in training 36 | data_collator = DataCollatorWithPadding(tokenizer=tokenizer) 37 | 38 | # Create reference model and reward model 39 | ref_model = create_ref_model(model_args, finetuning_args, add_valuehead=True) 40 | reward_model = create_reward_model(model, model_args, finetuning_args) 41 | 42 | # Initialize our Trainer 43 | ppo_trainer = CustomPPOTrainer( 44 | model_args=model_args, 45 | training_args=training_args, 46 | finetuning_args=finetuning_args, 47 | generating_args=generating_args, 48 | callbacks=callbacks + [FixValueHeadModelCallback()], 49 | model=model, 50 | reward_model=reward_model, 51 | ref_model=ref_model, 52 | tokenizer=tokenizer, 53 | dataset=dataset, 54 | data_collator=data_collator, 55 | ) 56 | 57 | # Training 58 | if training_args.do_train: 59 | ppo_trainer.ppo_train(resume_from_checkpoint=training_args.resume_from_checkpoint) 60 | ppo_trainer.save_model() 61 | if training_args.should_save: 62 | fix_valuehead_checkpoint(model, training_args.output_dir, training_args.save_safetensors) 63 | ppo_trainer.save_state() # must be called after save_model to have a folder 64 | if ppo_trainer.is_world_process_zero() and finetuning_args.plot_loss: 65 | plot_loss(training_args.output_dir, keys=["loss", "reward"]) 66 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_pt 2 | 3 | 4 | __all__ = ["run_pt"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/pt/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/trainer.py: -------------------------------------------------------------------------------- 1 | from types import MethodType 2 | from typing import TYPE_CHECKING, Optional 3 | 4 | from transformers import Trainer 5 | 6 | from ...extras.logging import get_logger 7 | from ..utils import create_custom_optimzer, create_custom_scheduler 8 | 9 | 10 | if TYPE_CHECKING: 11 | import torch 12 | 13 | from ...hparams import FinetuningArguments 14 | 15 | 16 | logger = get_logger(__name__) 17 | 18 | 19 | class CustomTrainer(Trainer): 20 | r""" 21 | Inherits Trainer for custom optimizer. 22 | """ 23 | 24 | def __init__(self, finetuning_args: "FinetuningArguments", **kwargs) -> None: 25 | super().__init__(**kwargs) 26 | self.finetuning_args = finetuning_args 27 | if finetuning_args.use_badam: 28 | from badam import clip_grad_norm_for_sparse_tensor 29 | 30 | self.accelerator.clip_grad_norm_ = MethodType(clip_grad_norm_for_sparse_tensor, self.accelerator) 31 | 32 | def create_optimizer(self) -> "torch.optim.Optimizer": 33 | if self.optimizer is None: 34 | self.optimizer = create_custom_optimzer(self.model, self.args, self.finetuning_args) 35 | return super().create_optimizer() 36 | 37 | def create_scheduler( 38 | self, num_training_steps: int, optimizer: Optional["torch.optim.Optimizer"] = None 39 | ) -> "torch.optim.lr_scheduler.LRScheduler": 40 | create_custom_scheduler(self.args, num_training_steps, optimizer) 41 | return super().create_scheduler(num_training_steps, optimizer) 42 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/pt/workflow.py: -------------------------------------------------------------------------------- 1 | # Inspired by: https://github.com/huggingface/transformers/blob/v4.34.1/examples/pytorch/language-modeling/run_clm.py 2 | 3 | import math 4 | from typing import TYPE_CHECKING, List, Optional 5 | 6 | from transformers import DataCollatorForLanguageModeling 7 | 8 | from ...data import get_dataset, split_dataset 9 | from ...extras.ploting import plot_loss 10 | from ...model import load_model, load_tokenizer 11 | from ..utils import create_modelcard_and_push 12 | from .trainer import CustomTrainer 13 | 14 | 15 | if TYPE_CHECKING: 16 | from transformers import Seq2SeqTrainingArguments, TrainerCallback 17 | 18 | from ...hparams import DataArguments, FinetuningArguments, ModelArguments 19 | 20 | 21 | def run_pt( 22 | model_args: "ModelArguments", 23 | data_args: "DataArguments", 24 | training_args: "Seq2SeqTrainingArguments", 25 | finetuning_args: "FinetuningArguments", 26 | callbacks: Optional[List["TrainerCallback"]] = None, 27 | ): 28 | tokenizer_module = load_tokenizer(model_args) 29 | tokenizer = tokenizer_module["tokenizer"] 30 | dataset = get_dataset(model_args, data_args, training_args, stage="pt", **tokenizer_module) 31 | model = load_model(tokenizer, model_args, finetuning_args, training_args.do_train) 32 | data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) 33 | 34 | # Initialize our Trainer 35 | trainer = CustomTrainer( 36 | model=model, 37 | args=training_args, 38 | finetuning_args=finetuning_args, 39 | tokenizer=tokenizer, 40 | data_collator=data_collator, 41 | callbacks=callbacks, 42 | **split_dataset(dataset, data_args, training_args), 43 | ) 44 | 45 | # Training 46 | if training_args.do_train: 47 | train_result = trainer.train(resume_from_checkpoint=training_args.resume_from_checkpoint) 48 | trainer.save_model() 49 | trainer.log_metrics("train", train_result.metrics) 50 | trainer.save_metrics("train", train_result.metrics) 51 | trainer.save_state() 52 | if trainer.is_world_process_zero() and finetuning_args.plot_loss: 53 | plot_loss(training_args.output_dir, keys=["loss", "eval_loss"]) 54 | 55 | # Evaluation 56 | if training_args.do_eval: 57 | metrics = trainer.evaluate(metric_key_prefix="eval") 58 | try: 59 | perplexity = math.exp(metrics["eval_loss"]) 60 | except OverflowError: 61 | perplexity = float("inf") 62 | 63 | metrics["perplexity"] = perplexity 64 | trainer.log_metrics("eval", metrics) 65 | trainer.save_metrics("eval", metrics) 66 | 67 | # Create model card 68 | create_modelcard_and_push(trainer, model_args, data_args, training_args, finetuning_args) 69 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_rm 2 | 3 | 4 | __all__ = ["run_rm"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/metric.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/metric.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/rm/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/rm/metric.py: -------------------------------------------------------------------------------- 1 | from typing import Dict, Sequence, Tuple, Union 2 | 3 | import numpy as np 4 | 5 | 6 | def compute_accuracy(eval_preds: Sequence[Union[np.ndarray, Tuple[np.ndarray]]]) -> Dict[str, float]: 7 | preds, _ = eval_preds 8 | return {"accuracy": (preds[0] > preds[1]).sum() / len(preds[0])} 9 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/__init__.py: -------------------------------------------------------------------------------- 1 | from .workflow import run_sft 2 | 3 | 4 | __all__ = ["run_sft"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/metric.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/metric.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/trainer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/trainer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/workflow.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/train/sft/__pycache__/workflow.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/train/sft/metric.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | from typing import TYPE_CHECKING, Dict, Sequence, Tuple, Union 3 | 4 | import numpy as np 5 | 6 | from ...extras.constants import IGNORE_INDEX 7 | from ...extras.packages import is_jieba_available, is_nltk_available, is_rouge_available 8 | 9 | 10 | if TYPE_CHECKING: 11 | from transformers.tokenization_utils import PreTrainedTokenizer 12 | 13 | if is_jieba_available(): 14 | import jieba # type: ignore 15 | 16 | if is_nltk_available(): 17 | from nltk.translate.bleu_score import SmoothingFunction, sentence_bleu 18 | 19 | if is_rouge_available(): 20 | from rouge_chinese import Rouge 21 | 22 | 23 | @dataclass 24 | class ComputeMetrics: 25 | r""" 26 | Wraps the tokenizer into metric functions, used in Seq2SeqPeftTrainer. 27 | """ 28 | 29 | tokenizer: "PreTrainedTokenizer" 30 | 31 | def __call__(self, eval_preds: Sequence[Union[np.ndarray, Tuple[np.ndarray]]]) -> Dict[str, float]: 32 | r""" 33 | Uses the model predictions to compute metrics. 34 | """ 35 | preds, labels = eval_preds 36 | score_dict = {"rouge-1": [], "rouge-2": [], "rouge-l": [], "bleu-4": []} 37 | 38 | preds = np.where(preds != IGNORE_INDEX, preds, self.tokenizer.pad_token_id) 39 | labels = np.where(labels != IGNORE_INDEX, labels, self.tokenizer.pad_token_id) 40 | 41 | decoded_preds = self.tokenizer.batch_decode(preds, skip_special_tokens=True) 42 | decoded_labels = self.tokenizer.batch_decode(labels, skip_special_tokens=True) 43 | 44 | for pred, label in zip(decoded_preds, decoded_labels): 45 | hypothesis = list(jieba.cut(pred)) 46 | reference = list(jieba.cut(label)) 47 | 48 | if len(" ".join(hypothesis).split()) == 0 or len(" ".join(reference).split()) == 0: 49 | result = {"rouge-1": {"f": 0.0}, "rouge-2": {"f": 0.0}, "rouge-l": {"f": 0.0}} 50 | else: 51 | rouge = Rouge() 52 | scores = rouge.get_scores(" ".join(hypothesis), " ".join(reference)) 53 | result = scores[0] 54 | 55 | for k, v in result.items(): 56 | score_dict[k].append(round(v["f"] * 100, 4)) 57 | 58 | bleu_score = sentence_bleu([list(label)], list(pred), smoothing_function=SmoothingFunction().method3) 59 | score_dict["bleu-4"].append(round(bleu_score * 100, 4)) 60 | 61 | return {k: float(np.mean(v)) for k, v in score_dict.items()} 62 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__init__.py: -------------------------------------------------------------------------------- 1 | from .interface import create_ui, create_web_demo 2 | 3 | 4 | __all__ = ["create_ui", "create_web_demo"] 5 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/chatter.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/chatter.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/common.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/common.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/css.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/css.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/engine.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/engine.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/interface.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/interface.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/locales.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/locales.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/manager.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/manager.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/runner.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/runner.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/__pycache__/utils.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/__pycache__/utils.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__init__.py: -------------------------------------------------------------------------------- 1 | from .chatbot import create_chat_box 2 | from .eval import create_eval_tab 3 | from .export import create_export_tab 4 | from .infer import create_infer_tab 5 | from .top import create_top 6 | from .train import create_train_tab 7 | 8 | 9 | __all__ = [ 10 | "create_chat_box", 11 | "create_eval_tab", 12 | "create_export_tab", 13 | "create_infer_tab", 14 | "create_top", 15 | "create_train_tab", 16 | ] 17 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/chatbot.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/chatbot.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/data.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/data.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/eval.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/eval.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/export.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/export.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/infer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/infer.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/top.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/top.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/train.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/llm-train/baichuan13B/src/llmtuner/webui/components/__pycache__/train.cpython-310.pyc -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/chatbot.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict, Tuple 2 | 3 | from ...data import Role 4 | from ...extras.packages import is_gradio_available 5 | from ..utils import check_json_schema 6 | 7 | 8 | if is_gradio_available(): 9 | import gradio as gr 10 | 11 | 12 | if TYPE_CHECKING: 13 | from gradio.components import Component 14 | 15 | from ..engine import Engine 16 | 17 | 18 | def create_chat_box( 19 | engine: "Engine", visible: bool = False 20 | ) -> Tuple["Component", "Component", Dict[str, "Component"]]: 21 | with gr.Column(visible=visible) as chat_box: 22 | chatbot = gr.Chatbot(show_copy_button=True) 23 | messages = gr.State([]) 24 | with gr.Row(): 25 | with gr.Column(scale=4): 26 | with gr.Row(): 27 | with gr.Column(): 28 | role = gr.Dropdown(choices=[Role.USER.value, Role.OBSERVATION.value], value=Role.USER.value) 29 | system = gr.Textbox(show_label=False) 30 | tools = gr.Textbox(show_label=False, lines=3) 31 | 32 | with gr.Column() as image_box: 33 | image = gr.Image(sources=["upload"], type="numpy") 34 | 35 | query = gr.Textbox(show_label=False, lines=8) 36 | submit_btn = gr.Button(variant="primary") 37 | 38 | with gr.Column(scale=1): 39 | max_new_tokens = gr.Slider(8, 4096, value=512, step=1) 40 | top_p = gr.Slider(0.01, 1.0, value=0.7, step=0.01) 41 | temperature = gr.Slider(0.01, 1.5, value=0.95, step=0.01) 42 | clear_btn = gr.Button() 43 | 44 | tools.input(check_json_schema, inputs=[tools, engine.manager.get_elem_by_id("top.lang")]) 45 | 46 | submit_btn.click( 47 | engine.chatter.append, 48 | [chatbot, messages, role, query], 49 | [chatbot, messages, query], 50 | ).then( 51 | engine.chatter.stream, 52 | [chatbot, messages, system, tools, image, max_new_tokens, top_p, temperature], 53 | [chatbot, messages], 54 | ) 55 | clear_btn.click(lambda: ([], []), outputs=[chatbot, messages]) 56 | 57 | return ( 58 | chatbot, 59 | messages, 60 | dict( 61 | chat_box=chat_box, 62 | role=role, 63 | system=system, 64 | tools=tools, 65 | image_box=image_box, 66 | image=image, 67 | query=query, 68 | submit_btn=submit_btn, 69 | max_new_tokens=max_new_tokens, 70 | top_p=top_p, 71 | temperature=temperature, 72 | clear_btn=clear_btn, 73 | ), 74 | ) 75 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/eval.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict 2 | 3 | from ...extras.packages import is_gradio_available 4 | from ..common import DEFAULT_DATA_DIR, list_dataset 5 | from .data import create_preview_box 6 | 7 | 8 | if is_gradio_available(): 9 | import gradio as gr 10 | 11 | 12 | if TYPE_CHECKING: 13 | from gradio.components import Component 14 | 15 | from ..engine import Engine 16 | 17 | 18 | def create_eval_tab(engine: "Engine") -> Dict[str, "Component"]: 19 | input_elems = engine.manager.get_base_elems() 20 | elem_dict = dict() 21 | 22 | with gr.Row(): 23 | dataset_dir = gr.Textbox(value=DEFAULT_DATA_DIR, scale=2) 24 | dataset = gr.Dropdown(multiselect=True, scale=4) 25 | preview_elems = create_preview_box(dataset_dir, dataset) 26 | 27 | input_elems.update({dataset_dir, dataset}) 28 | elem_dict.update(dict(dataset_dir=dataset_dir, dataset=dataset, **preview_elems)) 29 | 30 | with gr.Row(): 31 | cutoff_len = gr.Slider(value=1024, minimum=4, maximum=8192, step=1) 32 | max_samples = gr.Textbox(value="100000") 33 | batch_size = gr.Slider(value=8, minimum=1, maximum=512, step=1) 34 | predict = gr.Checkbox(value=True) 35 | 36 | input_elems.update({cutoff_len, max_samples, batch_size, predict}) 37 | elem_dict.update(dict(cutoff_len=cutoff_len, max_samples=max_samples, batch_size=batch_size, predict=predict)) 38 | 39 | with gr.Row(): 40 | max_new_tokens = gr.Slider(10, 2048, value=128, step=1) 41 | top_p = gr.Slider(0.01, 1, value=0.7, step=0.01) 42 | temperature = gr.Slider(0.01, 1.5, value=0.95, step=0.01) 43 | output_dir = gr.Textbox() 44 | 45 | input_elems.update({max_new_tokens, top_p, temperature, output_dir}) 46 | elem_dict.update(dict(max_new_tokens=max_new_tokens, top_p=top_p, temperature=temperature, output_dir=output_dir)) 47 | 48 | with gr.Row(): 49 | cmd_preview_btn = gr.Button() 50 | start_btn = gr.Button(variant="primary") 51 | stop_btn = gr.Button(variant="stop") 52 | 53 | with gr.Row(): 54 | resume_btn = gr.Checkbox(visible=False, interactive=False) 55 | process_bar = gr.Slider(visible=False, interactive=False) 56 | 57 | with gr.Row(): 58 | output_box = gr.Markdown() 59 | 60 | output_elems = [output_box, process_bar] 61 | elem_dict.update( 62 | dict( 63 | cmd_preview_btn=cmd_preview_btn, 64 | start_btn=start_btn, 65 | stop_btn=stop_btn, 66 | resume_btn=resume_btn, 67 | process_bar=process_bar, 68 | output_box=output_box, 69 | ) 70 | ) 71 | 72 | cmd_preview_btn.click(engine.runner.preview_eval, input_elems, output_elems, concurrency_limit=None) 73 | start_btn.click(engine.runner.run_eval, input_elems, output_elems) 74 | stop_btn.click(engine.runner.set_abort) 75 | resume_btn.change(engine.runner.monitor, outputs=output_elems, concurrency_limit=None) 76 | 77 | dataset_dir.change(list_dataset, [dataset_dir], [dataset], queue=False) 78 | 79 | return elem_dict 80 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/infer.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict 2 | 3 | from ...extras.packages import is_gradio_available 4 | from .chatbot import create_chat_box 5 | 6 | 7 | if is_gradio_available(): 8 | import gradio as gr 9 | 10 | 11 | if TYPE_CHECKING: 12 | from gradio.components import Component 13 | 14 | from ..engine import Engine 15 | 16 | 17 | def create_infer_tab(engine: "Engine") -> Dict[str, "Component"]: 18 | input_elems = engine.manager.get_base_elems() 19 | elem_dict = dict() 20 | 21 | infer_backend = gr.Dropdown(choices=["huggingface", "vllm"], value="huggingface") 22 | with gr.Row(): 23 | load_btn = gr.Button() 24 | unload_btn = gr.Button() 25 | 26 | info_box = gr.Textbox(show_label=False, interactive=False) 27 | 28 | input_elems.update({infer_backend}) 29 | elem_dict.update(dict(infer_backend=infer_backend, load_btn=load_btn, unload_btn=unload_btn, info_box=info_box)) 30 | 31 | chatbot, messages, chat_elems = create_chat_box(engine, visible=False) 32 | elem_dict.update(chat_elems) 33 | 34 | load_btn.click(engine.chatter.load_model, input_elems, [info_box]).then( 35 | lambda: gr.Column(visible=engine.chatter.loaded), outputs=[chat_elems["chat_box"]] 36 | ) 37 | 38 | unload_btn.click(engine.chatter.unload_model, input_elems, [info_box]).then( 39 | lambda: ([], []), outputs=[chatbot, messages] 40 | ).then(lambda: gr.Column(visible=engine.chatter.loaded), outputs=[chat_elems["chat_box"]]) 41 | 42 | engine.manager.get_elem_by_id("top.visual_inputs").change( 43 | lambda enabled: gr.Column(visible=enabled), 44 | [engine.manager.get_elem_by_id("top.visual_inputs")], 45 | [chat_elems["image_box"]], 46 | ) 47 | 48 | return elem_dict 49 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/components/top.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict 2 | 3 | from ...data import templates 4 | from ...extras.constants import METHODS, SUPPORTED_MODELS 5 | from ...extras.packages import is_gradio_available 6 | from ..common import get_model_path, get_template, get_visual, list_adapters, save_config 7 | from ..utils import can_quantize 8 | 9 | 10 | if is_gradio_available(): 11 | import gradio as gr 12 | 13 | 14 | if TYPE_CHECKING: 15 | from gradio.components import Component 16 | 17 | 18 | def create_top() -> Dict[str, "Component"]: 19 | available_models = list(SUPPORTED_MODELS.keys()) + ["Custom"] 20 | 21 | with gr.Row(): 22 | lang = gr.Dropdown(choices=["en", "ru", "zh"], scale=1) 23 | model_name = gr.Dropdown(choices=available_models, scale=3) 24 | model_path = gr.Textbox(scale=3) 25 | 26 | with gr.Row(): 27 | finetuning_type = gr.Dropdown(choices=METHODS, value="lora", scale=1) 28 | adapter_path = gr.Dropdown(multiselect=True, allow_custom_value=True, scale=5) 29 | refresh_btn = gr.Button(scale=1) 30 | 31 | with gr.Accordion(open=False) as advanced_tab: 32 | with gr.Row(): 33 | quantization_bit = gr.Dropdown(choices=["none", "8", "4"], value="none", scale=2) 34 | template = gr.Dropdown(choices=list(templates.keys()), value="default", scale=2) 35 | rope_scaling = gr.Radio(choices=["none", "linear", "dynamic"], value="none", scale=3) 36 | booster = gr.Radio(choices=["none", "flashattn2", "unsloth"], value="none", scale=3) 37 | visual_inputs = gr.Checkbox(scale=1) 38 | 39 | model_name.change(list_adapters, [model_name, finetuning_type], [adapter_path], queue=False).then( 40 | get_model_path, [model_name], [model_path], queue=False 41 | ).then(get_template, [model_name], [template], queue=False).then( 42 | get_visual, [model_name], [visual_inputs], queue=False 43 | ) # do not save config since the below line will save 44 | 45 | model_path.change(save_config, inputs=[lang, model_name, model_path], queue=False) 46 | 47 | finetuning_type.change(list_adapters, [model_name, finetuning_type], [adapter_path], queue=False).then( 48 | can_quantize, [finetuning_type], [quantization_bit], queue=False 49 | ) 50 | 51 | refresh_btn.click(list_adapters, [model_name, finetuning_type], [adapter_path], queue=False) 52 | 53 | return dict( 54 | lang=lang, 55 | model_name=model_name, 56 | model_path=model_path, 57 | finetuning_type=finetuning_type, 58 | adapter_path=adapter_path, 59 | refresh_btn=refresh_btn, 60 | advanced_tab=advanced_tab, 61 | quantization_bit=quantization_bit, 62 | template=template, 63 | rope_scaling=rope_scaling, 64 | booster=booster, 65 | visual_inputs=visual_inputs, 66 | ) 67 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/css.py: -------------------------------------------------------------------------------- 1 | CSS = r""" 2 | .duplicate-button { 3 | margin: auto !important; 4 | color: white !important; 5 | background: black !important; 6 | border-radius: 100vh !important; 7 | } 8 | 9 | .modal-box { 10 | position: fixed !important; 11 | top: 50%; 12 | left: 50%; 13 | transform: translate(-50%, -50%); /* center horizontally */ 14 | max-width: 1000px; 15 | max-height: 750px; 16 | overflow-y: auto; 17 | background-color: var(--input-background-fill); 18 | flex-wrap: nowrap !important; 19 | border: 2px solid black !important; 20 | z-index: 1000; 21 | padding: 10px; 22 | } 23 | 24 | .dark .modal-box { 25 | border: 2px solid white !important; 26 | } 27 | """ 28 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/engine.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Any, Dict 2 | 3 | from .chatter import WebChatModel 4 | from .common import get_model_path, list_dataset, load_config 5 | from .locales import LOCALES 6 | from .manager import Manager 7 | from .runner import Runner 8 | from .utils import get_time 9 | 10 | 11 | if TYPE_CHECKING: 12 | from gradio.components import Component 13 | 14 | 15 | class Engine: 16 | def __init__(self, demo_mode: bool = False, pure_chat: bool = False) -> None: 17 | self.demo_mode = demo_mode 18 | self.pure_chat = pure_chat 19 | self.manager = Manager() 20 | self.runner = Runner(self.manager, demo_mode) 21 | self.chatter = WebChatModel(self.manager, demo_mode, lazy_init=(not pure_chat)) 22 | 23 | def _update_component(self, input_dict: Dict[str, Dict[str, Any]]) -> Dict["Component", "Component"]: 24 | r""" 25 | Gets the dict to update the components. 26 | """ 27 | output_dict: Dict["Component", "Component"] = {} 28 | for elem_id, elem_attr in input_dict.items(): 29 | elem = self.manager.get_elem_by_id(elem_id) 30 | output_dict[elem] = elem.__class__(**elem_attr) 31 | 32 | return output_dict 33 | 34 | def resume(self): 35 | user_config = load_config() if not self.demo_mode else {} 36 | lang = user_config.get("lang", None) or "en" 37 | 38 | init_dict = {"top.lang": {"value": lang}, "infer.chat_box": {"visible": self.chatter.loaded}} 39 | 40 | if not self.pure_chat: 41 | init_dict["train.dataset"] = {"choices": list_dataset().choices} 42 | init_dict["eval.dataset"] = {"choices": list_dataset().choices} 43 | init_dict["train.output_dir"] = {"value": "train_{}".format(get_time())} 44 | init_dict["train.config_path"] = {"value": "{}.json".format(get_time())} 45 | init_dict["eval.output_dir"] = {"value": "eval_{}".format(get_time())} 46 | init_dict["infer.image_box"] = {"visible": False} 47 | 48 | if user_config.get("last_model", None): 49 | init_dict["top.model_name"] = {"value": user_config["last_model"]} 50 | init_dict["top.model_path"] = {"value": get_model_path(user_config["last_model"])} 51 | 52 | yield self._update_component(init_dict) 53 | 54 | if self.runner.alive and not self.demo_mode and not self.pure_chat: 55 | yield {elem: elem.__class__(value=value) for elem, value in self.runner.running_data.items()} 56 | if self.runner.do_train: 57 | yield self._update_component({"train.resume_btn": {"value": True}}) 58 | else: 59 | yield self._update_component({"eval.resume_btn": {"value": True}}) 60 | 61 | def change_lang(self, lang: str): 62 | return { 63 | elem: elem.__class__(**LOCALES[elem_name][lang]) 64 | for elem_name, elem in self.manager.get_elem_iter() 65 | if elem_name in LOCALES 66 | } 67 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/interface.py: -------------------------------------------------------------------------------- 1 | from ..extras.packages import is_gradio_available 2 | from .common import save_config 3 | from .components import ( 4 | create_chat_box, 5 | create_eval_tab, 6 | create_export_tab, 7 | create_infer_tab, 8 | create_top, 9 | create_train_tab, 10 | ) 11 | from .css import CSS 12 | from .engine import Engine 13 | 14 | 15 | if is_gradio_available(): 16 | import gradio as gr 17 | 18 | 19 | def create_ui(demo_mode: bool = False) -> gr.Blocks: 20 | engine = Engine(demo_mode=demo_mode, pure_chat=False) 21 | 22 | with gr.Blocks(title="LLaMA Board", css=CSS) as demo: 23 | if demo_mode: 24 | gr.HTML("

LLaMA Board: A One-stop Web UI for Getting Started with LLaMA Factory

") 25 | gr.HTML( 26 | '

Visit ' 27 | "LLaMA Factory for details.

" 28 | ) 29 | gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button") 30 | 31 | engine.manager.add_elems("top", create_top()) 32 | lang: "gr.Dropdown" = engine.manager.get_elem_by_id("top.lang") 33 | 34 | with gr.Tab("Train"): 35 | engine.manager.add_elems("train", create_train_tab(engine)) 36 | 37 | with gr.Tab("Evaluate & Predict"): 38 | engine.manager.add_elems("eval", create_eval_tab(engine)) 39 | 40 | with gr.Tab("Chat"): 41 | engine.manager.add_elems("infer", create_infer_tab(engine)) 42 | 43 | if not demo_mode: 44 | with gr.Tab("Export"): 45 | engine.manager.add_elems("export", create_export_tab(engine)) 46 | 47 | demo.load(engine.resume, outputs=engine.manager.get_elem_list(), concurrency_limit=None) 48 | lang.change(engine.change_lang, [lang], engine.manager.get_elem_list(), queue=False) 49 | lang.input(save_config, inputs=[lang], queue=False) 50 | 51 | return demo 52 | 53 | 54 | def create_web_demo() -> gr.Blocks: 55 | engine = Engine(pure_chat=True) 56 | 57 | with gr.Blocks(title="Web Demo", css=CSS) as demo: 58 | lang = gr.Dropdown(choices=["en", "zh"]) 59 | engine.manager.add_elems("top", dict(lang=lang)) 60 | 61 | _, _, chat_elems = create_chat_box(engine, visible=True) 62 | engine.manager.add_elems("infer", chat_elems) 63 | 64 | demo.load(engine.resume, outputs=engine.manager.get_elem_list(), concurrency_limit=None) 65 | lang.change(engine.change_lang, [lang], engine.manager.get_elem_list(), queue=False) 66 | lang.input(save_config, inputs=[lang], queue=False) 67 | 68 | return demo 69 | 70 | 71 | if __name__ == "__main__": 72 | create_ui().queue().launch(server_name="0.0.0.0", server_port=None, share=False, inbrowser=True) 73 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/llmtuner/webui/manager.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING, Dict, Generator, List, Set, Tuple 2 | 3 | 4 | if TYPE_CHECKING: 5 | from gradio.components import Component 6 | 7 | 8 | class Manager: 9 | def __init__(self) -> None: 10 | self._id_to_elem: Dict[str, "Component"] = {} 11 | self._elem_to_id: Dict["Component", str] = {} 12 | 13 | def add_elems(self, tab_name: str, elem_dict: Dict[str, "Component"]) -> None: 14 | r""" 15 | Adds elements to manager. 16 | """ 17 | for elem_name, elem in elem_dict.items(): 18 | elem_id = "{}.{}".format(tab_name, elem_name) 19 | self._id_to_elem[elem_id] = elem 20 | self._elem_to_id[elem] = elem_id 21 | 22 | def get_elem_list(self) -> List["Component"]: 23 | r""" 24 | Returns the list of all elements. 25 | """ 26 | return list(self._id_to_elem.values()) 27 | 28 | def get_elem_iter(self) -> Generator[Tuple[str, "Component"], None, None]: 29 | r""" 30 | Returns an iterator over all elements with their names. 31 | """ 32 | for elem_id, elem in self._id_to_elem.items(): 33 | yield elem_id.split(".")[-1], elem 34 | 35 | def get_elem_by_id(self, elem_id: str) -> "Component": 36 | r""" 37 | Gets element by id. 38 | 39 | Example: top.lang, train.dataset 40 | """ 41 | return self._id_to_elem[elem_id] 42 | 43 | def get_id_by_elem(self, elem: "Component") -> str: 44 | r""" 45 | Gets id by element. 46 | """ 47 | return self._elem_to_id[elem] 48 | 49 | def get_base_elems(self) -> Set["Component"]: 50 | r""" 51 | Gets the base elements that are commonly used. 52 | """ 53 | return { 54 | self._id_to_elem["top.lang"], 55 | self._id_to_elem["top.model_name"], 56 | self._id_to_elem["top.model_path"], 57 | self._id_to_elem["top.finetuning_type"], 58 | self._id_to_elem["top.adapter_path"], 59 | self._id_to_elem["top.quantization_bit"], 60 | self._id_to_elem["top.template"], 61 | self._id_to_elem["top.rope_scaling"], 62 | self._id_to_elem["top.booster"], 63 | self._id_to_elem["top.visual_inputs"], 64 | } 65 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/train_bash.py: -------------------------------------------------------------------------------- 1 | from llmtuner import run_exp 2 | 3 | 4 | def main(): 5 | run_exp() 6 | 7 | 8 | def _mp_fn(index): 9 | # For xla_spawn (TPUs) 10 | main() 11 | 12 | 13 | if __name__ == "__main__": 14 | main() 15 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/train_web.py: -------------------------------------------------------------------------------- 1 | from llmtuner import create_ui 2 | 3 | 4 | def main(): 5 | create_ui().queue().launch(server_name="0.0.0.0", server_port=None, share=False, inbrowser=True) 6 | 7 | 8 | if __name__ == "__main__": 9 | main() 10 | -------------------------------------------------------------------------------- /llm-train/baichuan13B/src/web_demo.py: -------------------------------------------------------------------------------- 1 | from llmtuner import create_web_demo 2 | 3 | 4 | def main(): 5 | create_web_demo().queue().launch(server_name="0.0.0.0", server_port=10099, share=False, inbrowser=True) 6 | 7 | 8 | if __name__ == "__main__": 9 | main() 10 | -------------------------------------------------------------------------------- /llm-train/comment/README.md: -------------------------------------------------------------------------------- 1 | requirement.txt 包含项目所用的所有依赖,直接pip install -r requirement.txt 2 | 3 | 4 | 5 | lora权重包含最终训练好的lora权重文件. 6 | 7 | -------------------------------------------------------------------------------- /llm-train/comment/lora权重/网盘权重链接.txt: -------------------------------------------------------------------------------- 1 | 链接:https://pan.baidu.com/s/1AM03P5oNjEzAvdRDxPj8hQ?pwd=v1u8 2 | 提取码:v1u8 -------------------------------------------------------------------------------- /llm-train/gpt2/README.md: -------------------------------------------------------------------------------- 1 | 1、下载数据集 2 | 3 | `python fineweb.py` 4 | 5 | 2、训练gpt2 6 | 7 | `python train_gpt2.py` 8 | 9 | 3、可视化分析结果 10 | 11 | `play.ipynb` -------------------------------------------------------------------------------- /llm-train/lora/README.md: -------------------------------------------------------------------------------- 1 | 如需数据 2 | 3 | 请联系公众号:程序锅锅。 -------------------------------------------------------------------------------- /paper/LLM_Survey_Chinese.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/paper/LLM_Survey_Chinese.pdf -------------------------------------------------------------------------------- /pic/awesome-ai-agents/assets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/assets/.DS_Store -------------------------------------------------------------------------------- /pic/awesome-ai-agents/assets/footer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/assets/footer.png -------------------------------------------------------------------------------- /pic/awesome-ai-agents/assets/landscape-latest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/assets/landscape-latest.png -------------------------------------------------------------------------------- /pic/awesome-ai-agents/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/image-1.png -------------------------------------------------------------------------------- /pic/awesome-ai-agents/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/image-2.png -------------------------------------------------------------------------------- /pic/awesome-ai-agents/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GuoCoder/ai-app/1f2ceb613fe251a5a16e49d887dec1ca5fe86aa3/pic/awesome-ai-agents/image.png --------------------------------------------------------------------------------