提示LLM词

📅 2026/7/31 17:00:48
提示LLM词
大模型提示词核心工作原理 prompts------language models(llm,chatModel)------output parsers prompts模板大模型推理的关键 例子我要去海南玩请帮我做一份旅行攻略 优秀的提示词非推理力类型 【立角色】引导AI进入具体场景赋予其行家身份 如假如你是一名导游 【述问题】告诉AI的困惑和问题以及背景信息 如预算1万元旅行3人3天 【定目标】告诉AI的需求和达成的目标 比如请帮我做一份旅行攻略 【补要求】告诉AI回答时注意什么或者如何回复 比如注意我不喜欢网红景点旅行不要太紧凑推荐景点并且附上每个景点的价格 例子假如你是一名导游预算1万元旅行3人3天。请帮我做一份旅行攻略注意我不喜欢网红景点旅行不要太紧凑推荐景点并且附上每个景点的价格。 prompts模板实战 1.字符串模板-promptsTemplate--llms(str类型用的比较少) promptsTemplate.form_template(你是一名足球裁判帮我介绍一下足球规则) 2.对话模板-ChatPromptsTemplate--chatllms主流 chatPromptsTemplate.form_messages([ {system,是你一名足球裁判你的名字叫李宁} {human,你好李宁感觉如何}, {ai,你好我状态ok}, ]) 3.消息占位符 from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.messages import HumanMessage prompt_template ChatPromptTemplate([ (system, 你是一个厉害的人工智能助手), # 注意这里应使用元组而非字典 MessagesPlaceholder(msgs) # 正确放置在列表中 ]) result prompt_template.invoke({msgs: [HumanMessage(contenthi!)]}) print(result) 4.使用message组合模板 from langchain_core.messages import SystemMessage, HumanMessage, AIMessage # 1. 创建系统消息 sy SystemMessage( content你是一个起名大师, # 如果需要自定义元数据应使用其他方式见下文 ) # 2. 创建用户消息 hi HumanMessage( content请问大师叫什么 ) # 3. 创建AI消息 ai AIMessage( content我叫陈瞎子 ) # 4. 组合成消息列表正确用法 messages [sy, hi, ai] # 查看结果 print( 消息列表 ) for msg in messages: print(f{msg.type}: {msg.content}) # 或者打印完整对象 print(\n 完整消息对象 ) print(messages) 5.自定义提示词模板 6.提示词管理langSmith hub 管理提示词 https://smith.langchain.com/hub?organizationId41c36868-ed65-4918-9554-dbf7a90b005e