提示LLM词

📅 2026/7/31 17:03:41 👁️ 阅读次数 📝 编程学习
提示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(content="hi!")]}) 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?organizationId=41c36868-ed65-4918-9554-dbf7a90b005e