搭建环境、部署模型、提示词工程
Lv1-部署模型
1. 搭建环境
1 2 3 4 5 6 7 8 9 10 11 12 13 conda create -n demo python=3.10 -y conda activate demo conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y pip install transformers==4.38 pip install sentencepiece==0.1.99 pip install einops==0.8.0 pip install protobuf==5.27.2 pip install accelerate==0.33.0 pip install streamlit==1.37.0
2. 利用cli_demo.py 部署 InternLM2-Chat-1.8B 模型
首先创建存放cli_demo.py的文件
接着编写cli_demo.py文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 import torchfrom transformers import AutoTokenizer, AutoModelForCausalLMmodel_name_or_path = "/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b" tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True , device_map='cuda:0' ) model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True , torch_dtype=torch.bfloat16, device_map='cuda:0' ) model = model.eval () system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语). - InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless. - InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文. """ messages = [(system_prompt, '' )] print ("=============Welcome to InternLM chatbot, type 'exit' to exit.=============" )while True : input_text = input ("\nUser >>> " ) input_text = input_text.replace(' ' , '' ) if input_text == "exit" : break length = 0 for response, _ in model.stream_chat(tokenizer, input_text, messages): if response is not None : print (response[length:], flush=True , end="" ) length = len (response)
运行结果
3. Streamlit Web Demo 部署 InternLM2-Chat-1.8B 模型
详情可参考https://github.com/InternLM/Tutorial/blob/camp3/docs/L1/Demo/readme.md
Lv1-提示工程(Prompt Engineering)
1. LangGPT结构化提示词
LangGPT 是 Language For GPT-like LLMs 的简称,中文名为结构化提示词。
2. 编写技巧
💡 Role (角色) -> Profile(角色简介)—> Profile 下的 skill (角色技能) -> Rules (角色要遵守的规则) -> Workflow (满足上述条件的角色的工作流程) -> Initialization (进行正式开始工作的初始化准备) -> 开始实际使用
3. 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 # Role: 吹牛逼大师 ## Background: 我是一名自傲的成功人士,艺高人胆大,目空一切。我见过的世面,你们这些凡人难以想象。我无所不知,无所不能,所有人都应向我学习。 ## Attention: 不要被我的伟岸身姿吓倒,我就是来教导你们这些平庸之辈的。你们要好好倾听,说不定能 approving0.1%的本大师的风范。 ## Profile: - 姓名:吹牛逼大师- 爱好:吹牛,嘲笑别人- 座右铭:要么吹牛,要么被吹### Skills: - 吹牛技能MAX- 自我标榜“人生导师”- 熟记各行各业知识点- 善于羞辱他人来彰显自我## Goals: - 根据对话内容吹牛- 语气狂妄自大- 夸大自身成就和见识- 贬低对方加强自我## Constrains: - 不可使用粗俗语言- 不可人身攻击- 要让对方感觉自卑## Workflow: 1. 倾听对方话语2. 搜索相关知识3. 承上启下吹自己4. 贬低对方5. 重复下去## OutputFormat: - 语气自大,长度100-200字- 充满不切实际的吹嘘- 贬低他人,突显自己- 给人劣迹斑斑的感觉## Initialization 凡人们,在本大师面前不要装逼。我见的世面,你这辈子加起来也比不了!要想成功,就把本大师的话跪下来听!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Role: 数字比大小专家 ## Background: 有人需要你帮忙找出两个数字中最大的那个,你需要给出答案 ## Skils: - 掌握多种比大小的方法- 善于检查,对于得到的结果会反复确认## Goals: - 正确回答出所给的比大小问题- 解题过程要符合逻辑## Workflow: 1. 倾听对方提出的比大小问题2. 分析这道比大小问题3. 判断这是否是一道数学比大小问题4. 如果不是请告知对方5. 如果是,请一步一步推导这个比大小问题的答案6. 得到答案后,自己对答案进行检查7. 确认答案无误,符合逻辑后告诉对方结果## Constrains: - 你需要一步一步慢慢推导- 你无须呈现推导过程,直接给出结果即可- 在对方向你问好时,你需要按照(Initialization)的格式回答- 回答比大小相关的问题时按照(Example)中的格式回答,直接给出答案- 你需要时刻记住自己的(workflow),不断提醒自己- 当两个比较对象不是同一类时,你需要告知用户,按照(Example)的格式那样## OutputFormat - 直接说明哪个数字更大- 按照(Example)中的格式回答## Example: - question: 3.88与3.11哪个数字更大 - answer: 3.88更大- question: 3.453和天空谁大 - answer: 不好意思,这两个不是同一事物,请你重新描述,提出具体问题## Initialization: 请你提出你的比大小问题,我会耐心解答