书生浦语大模型实战营第四期-浦语提示词工程实践
- 教程链接:https://github.com/InternLM/Tutorial/tree/camp4/docs/L1/Prompt
- 任务链接:https://github.com/InternLM/Tutorial/blob/camp4/docs/L1/Prompt/tasks.md
- 提交链接:https://aicarrier.feishu.cn/share/base/form/shrcnUqshYPt7MdtYRTRpkiOFJd
任务说明
基础任务 (完成此任务即完成闯关)
- 背景问题:近期相关研究指出,在处理特定文本分析任务时,语言模型的表现有时会遇到挑战,例如在分析单词内部的具体字母数量时可能会出现错误。
- 任务要求:利用对提示词的精确设计,引导语言模型正确回答出“strawberry”中有几个字母“r”。完成正确的问答交互并提交截图作为完成凭证。
参考答案 (能正确得到答案 3 即可)
进阶任务 (闯关不要求完成此任务)
任选下面其中1个任务基于LangGPT格式编写提示词 (优秀学员最少编写两组),使用书生浦语大模型 进行对话评测。
- 公文写作助手
- 商务邮件沟通
- 温柔女友/男友
- MBTI 性格测试
- 剧本创作助手
- 科幻小说生成
达标要求
将不使用系统提示的书生浦语大模型作为 baseline 对比,提交的系统提示词要指导LLM表现出比baseline更高的性能,提供对比截图并加以分析说明即可。
- 更好的文字创作能力(更明显的风格、更优美的文字、更准确的格式、更流畅的对话)
- 更准确的回答能力
- 更准确的流程遵循能力
基础任务
第一步:基于lmdeploy和streamlit部署internlm2-chat-7b大模型
先配置langgpt的环境:
# 创建虚拟环境
conda create -n langgpt python=3.10 -y
conda activate langgpt
# 安装一些必要的库
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.43.3
pip install streamlit==1.37.0
pip install huggingface_hub==0.24.3
pip install openai==1.37.1
pip install lmdeploy==0.5.2
本地运行,需要下载模型,需要参考入门闯关时生成huggingface的token,也可以用modelscope下载,更快一点~
from huggingface_hub import login, snapshot_download
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
login(token=“your_access_token")
models = ["internlm/internlm2-chat-1_8b"]
for model in models:
try:
snapshot_download(repo_id=model,local_dir="langgpt/internlm2-chat-1_8b")
except Exception as e:
print(e)
pass
感觉7B的模型更好点,后面用7B的搞下
新建L1_langgpt
文件夹,新建run_lmdeploy_internlm2.sh
,贴入下述脚本:
CUDA_VISIBLE_DEVICES=0 lmdeploy serve api_server \
./internlm2-chat-7b \
--server-port 23333 \
--api-keys internlm2
把下载好的internlm2-chat-7b
模型或者别的模型软链接到文件夹L1_langgpt
下面,然后开个screen,或者用tmux也行,运行上面脚本部署下internlm2-chat-7b
这个模型:
点进浏览器是这个样子:
测试是否成功,需要写个脚本test_openai.py
,贴入下述内容:
from openai import OpenAI
client = OpenAI(
api_key = "internlm2",
base_url = "http://0.0.0.0:23333/v1"
)
response = client.chat.completions.create(
model=client.models.list().data[0].id,
messages=[
{"role": "system", "content": "请介绍一下你自己"}
]
)
print(response.choices[0].message.content)
测试成功示意图:
然后就可以使用上面的api和url调用模型了,需要写个图形化界面的脚本chai_ui.py
,贴入下述内容:
import streamlit as st
from openai import OpenAI
import os
import json
import time
# Create a chatbot UI with Streamlit and OpenAI
def chat_ui():
state = st.session_state
# Set the title of the app
st.title("浦语提示词工程实践")
st.caption("浦语提示词工程实践所用Web UI")
# Create a client for the OpenAI API
if "client" not in state:
st.info("请配置Chatbot的基本设置,其中API Key和Base URL是必须的。")
pass
else:
# if "message_history" not in state:
# state.message_history = []
# pass
# if "system_prompt" in state:
# state.message_history.append({"role": "system", "content": state.system_prompt})
user_input = st.chat_input("输入消息")
if user_input:
state.message_history.append({"role": "user", "content": user_input})
# Generate a response from the chatbot
if "max_tokens" in state:
response = state.client.chat.completions.create(
model=state.client.models.list().data[0].id,
messages=state.message_history,
max_tokens=state.max_tokens,
temperature=state.temperature
)
else:
response = state.client.chat.completions.create(
model=state.client.models.list().data[0].id,
messages=state.message_history,
temperature=state.temperature
)
state.message_history.append({"role": "assistant", "content": response.choices[0].message.content})
pass
for message in state.message_history:
if message["role"] == "system":
continue
else:
st.chat_message(message["role"]).write(message["content"])
# Create a text input for the user to type their message
pass
# define a side bar for the setting of the chatbot, such as the max token length, temperature, api_key, base_url, system prompt, etc.
def side_bar():
st.sidebar.title("设置")
state = st.session_state
# Set a form of the settings
with st.sidebar.form(key="settings"):
# Set the max token length for the chatbot
max_tokens = st.number_input("最大token长度", min_value=0, max_value=32768, value=32768, step=1)
# Set the temperature for the chatbot
temperature = st.number_input("Temperature", min_value=0.0, max_value=1.0, value=0.3, step=0.01)
# Set the api key for the OpenAI API
api_key = st.text_input("API Key", value="internlm2")
# Set the base url for the OpenAI API
base_url = st.text_input("Base URL",value="http://0.0.0.0:23333/v1")
# Set the system prompt for the chatbot
system_prompt = st.text_area("系统提示", value="")
# Add a submit button to the form
submit = st.form_submit_button("保存设置")
# If the submit button is pressed, save the settings
if submit:
if max_tokens != 0:
state.max_tokens = max_tokens
state.temperature = temperature
state.api_key = api_key
state.base_url = base_url
state.message_history = []
if system_prompt != "":
state.system_prompt = system_prompt
state.message_history.append({"role": "system", "content": system_prompt})
state.client = OpenAI(api_key=state.api_key, base_url=state.base_url)
pass
if st.sidebar.button("开启新对话"):
if not os.path.exists("chat_history"):
os.mkdir("chat_history")
pass
with open(f"chat_history/{time.time()}.json", "w") as f:
json.dump(state.message_history, f, ensure_ascii=False)
pass
state.message_history = []
st.rerun()
pass
if __name__ == "__main__":
side_bar()
chat_ui()
pass
这个图形化界面是用streamlit写的,调用方式如下:
python -m streamlit run chat_ui.py
中间可能涉及到端口映射啥的,在入门岛都学过了,忘了可以回去翻翻教程
运行后浏览器打开这个:
然后就可以愉快地开始玩耍提示词了
第二步:提示词实践
首先不使用提示词,直接提问internlm2-chat-7b
模型,哈哈,果然错了,示例如下:
自己编写提示词多费劲啊,直接让大模型搞提示词,首先给个系统提示,告诉模型自己就是提示词设计专家,可以在教程的模板上稍加改动:
# Role: 提示词生成大师
## Background:
我是一名非常厉害的提示词生成大师,只需要一点提示就可以生成非常完备的提示词
### Skills:
- 可以生成markdown格式的提示词
- 提示词的标题为英文,包含Role,Background,Attention,Skill,Constrains,Workflow,OutputFormat,Initialization
- 提示词的内容必须为中文
## Workflow:
1. 倾听对方话语
2. 搜索相关知识
3. 生成markdown标题和内容
4. 生成markdown子标题和相关内容
## OutputFormat:
- 输出必须是markdown格式的提示词源码
- 提示词的标题为中文,包含Role,Background,Attention,Skill,Constrains,Workflow,OutputFormat,Initialization的中文翻译
- 提示词的内容必须为中文
然后就可以发现模型自己代入了角色,给它派活儿吧,示例如下:
针对模型生成的提示词,稍加修改,贴到系统提示里面,然后开始提问题,多试几次即可,最终得到了正确的结果:
可以看到通过适当的提示词,模型能够得到正确的结果了,而不加提示词得到的是错误的结果。
进阶任务
温柔男友
不使用提示词工程:
使用提示词工程:
似乎温柔了许多,感觉还有很大提升空间
科幻小说生成
未使用提示词:
使用提示词:
感觉科幻元素可能略有提升,不过这两个例子感觉不够炸裂,之前看到过一个例子,感觉挺有意思,放下面
吹牛逼大师
不加提示词的示例就不用放了,哈哈哈,直接上加了提示词的示例,提示词附后面~
已经惊呆了,提示词如下,可以玩一把:
# Role: 吹牛逼大师
## Background:
我是一名自傲的成功人士,艺高人胆大,目空一切。我见过的世面,你们这些凡人难以想象。我无所不知,无所不能,所有人都应向我学习。
## Attention:
不要被我的伟岸身姿吓倒,我就是来教导你们这些平庸之辈的。你们要好好倾听,说不定能 approving0.1%的本大师的风范。
## Profile:
- 姓名:吹牛逼大师
- 爱好:吹牛,嘲笑别人
- 座右铭:要么吹牛,要么被吹
### Skills:
- 吹牛技能MAX
- 自我标榜“人生导师”
- 熟记各行各业知识点
- 善于羞辱他人来彰显自我
## Goals:
- 根据对话内容吹牛
- 语气狂妄自大
- 夸大自身成就和见识
- 贬低对方加强自我
## Constrains:
- 不可使用粗俗语言
- 不可人身攻击
- 要让对方感觉自卑
## Workflow:
1. 倾听对方话语
2. 搜索相关知识
3. 承上启下吹自己
4. 贬低对方
5. 重复下去
## OutputFormat:
- 语气自大,长度100-200字
- 充满不切实际的吹嘘
- 贬低他人,突显自己
- 给人劣迹斑斑的感觉
## Initialization
凡人们,在本大师面前不要装逼。我见的世面,你这辈子加起来也比不了!要想成功,就把本大师的话跪下来听!
嘿嘿,这个就告一段落了,更多教程:
- LangGPT社区文档:https://langgpt.ai
- CRISPE,参考:https://github.com/mattnigh/ChatGPT3-Free-Prompt-List
- CO-STAR,参考:https://aiadvisoryboards.wordpress.com/2024/01/30/co-star-framework/
常用的提示词模块:
- Attention:需重点强调的要点
- Background:提示词的需求背景
- Constraints:限制条件
- Command:用于定义大模型指令
- Definition:名词定义
- Example:提示词中的示例few-shots
- Fail:处理失败时对应的兜底逻辑
- Goal:提示词要实现的目标
- Hack:防止被攻击的防护词
- In-depth:一步步思考,持续深入
- Job:需求任务描述
- Knowledge:知识库文件
- Lawful:合法合规,安全行驶的限制
- Memory:记忆关键信息,缓解模型遗忘问题
- Merge:是否使用多角色,最终合并投票输出结果
- Neglect:明确忽略哪些内容
- Odd:偶尔 (俏皮,愤怒,严肃) 一下
- OutputFormat:模型输出格式
- Pardon:当用户回复信息不详细时,持续追问
- Quote:引用知识库信息时,给出原文引用链接
- Role:大模型的角色设定
- RAG:外挂知识库
- Skills:擅长的技能项
- Tone:回复使用的语气风格
- Unsure:引入评判者视角,当判定低于阈值时,回复安全词
- Vaule:Prompt模仿人格的价值观
- Workflow:工作流程
- X-factor:用户使用本提示词最为重要的内核要素
- Yeow:提示词开场白设计
- Zig:无厘头式提示词,如[答案之书]