安装
# 基础安装
pip install langchain
# 全量安装
pip install langchain[all]
# 模块化安装
pip install langchain[vectorstores]
1.prompt
提示词类似于人类语言,将其输入模型后,模型会给出预测结果(即回答)。提示词主要分为以下三种:
- 系统提示词:用于全局性设定,比如将模型设定为 Java 工程师,或者添加行为约束。无论对话如何进行,该设定始终生效。
- 用户提示词:即用户输入的内容。
- 助手提示词:模型返回的结果。
格式如下:
{"role": "system", "content": "系统提示内容"},
{"role": "user", "content": "用户提示内容"},
{"role": "assistant", "content": "助手提示内容"}
2.提示词模板
提示词模板就是用于生成完整的提示词,比如,问模型 实现xx功能的python代码 ,这里xx功能可以多次使用使用模板替换下就好方便使用。
2.1 基础模板使用
from langchain.output_parsers import CommaSeparatedListOutputParser
# 1. 简单文本模板
# 定义一个简单的文本模板,input_variables 列出模板中使用的占位符名称
simple_template = PromptTemplate(
input_variables=["name"],
# 模板字符串,包含占位符 {name}
template="你好,我的名字是 {name}。",
# 在调用format 方法时 验证模板中的占位符是否与 input_variables 一致,默认 True
validate_template=True
# 模型返回内容格式化成逗号分割的形式 例如:让模型列出java常用框架
# 其它格式解析器
# DatetimeOutputParser
# NumberOutputParser
# StructuredOutputParser
# RegexParser
# BooleanOutputParser
# EnumOutputParser
# output_parser = CommaSeparatedListOutputParser()
# 使用jinja2模板格式化生成提示词
# template_format="jinja2"
)
# 使用 format 方法将占位符替换为具体值
specific_prompt = simple_template.format(name="Alice")
使用场景举例
代码生成
为不同的功能需求生成代码实现的提示。
from langchain.prompts import PromptTemplate
# 定义提示词模板
code_template = PromptTemplate(
input_variables=["function"],
template="实现 {function} 功能的Python代码"
)
# 使用模板生成具体提示词
specific_prompt = code_template.format(function="文件读写")
print(specific_prompt)
内容创作
生成不同主题的文章、故事等创作类提示。
from langchain.prompts import PromptTemplate
# 定义文章创作提示词模板
article_template = PromptTemplate(
input_variables=["topic", "word_count"],
template="写一篇关于 {topic} 的 {word_count} 字的科普文章"
)
# 使用模板生成具体提示词
article_prompt = article_template.format(topic="人工智能", word_count="1000")
print(article_prompt)
角色扮演
在角色扮演场景中,我们可以设定不同的角色和任务,让模型以特定角色的身份完成任务。
from langchain.prompts import PromptTemplate
# 定义猫娘女友角色扮演提示词模板
role_play_template = PromptTemplate(
input_variables=["scenario", "action"],
template="你现在扮演一位可爱的猫娘女友,处于 {scenario} 的场景中。{action}"
)
# 使用模板生成具体提示词
date_prompt = role_play_template.format(scenario="和男友在游乐园约会", action="对男友表达你对过山车的小害怕")
home_prompt = role_play_template.format(scenario="和男友在家中看电视", action="亲昵地靠在男友身上并说句甜言蜜语")
print(date_prompt)
print(home_prompt)
2.2 最小样本提示词模板(Few - shot Prompt Template)
最小样本提示词模板通过提供少量示例,帮助语言模型更好地理解任务并生成合适的输出。
使用例子:
文本情感分类
from langchain.prompts import PromptTemplate
# 定义最小样本提示词模板
few_shot_template = """
示例 1:
文本: "这部电影太精彩了,画面和剧情都超棒!"
情感: 积极
示例 2:
文本: "今天的晚餐太难吃了,完全不合口味。"
情感: 消极
示例 3:
文本: "天气一般,没有特别的感觉。"
情感: 中性
现在,请判断以下文本的情感:
文本: "{input_text}"
情感:
"""
# 创建 PromptTemplate 对象
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
# 生成具体提示词
specific_prompt = prompt_template.format(input_text="这家店的服务很周到,点赞!")
print(specific_prompt)
文本翻译
from langchain.prompts import PromptTemplate
few_shot_template = """
示例1:
原文: "Hello"
译文: "你好"
示例2:
原文: "Goodbye"
译文: "再见"
示例3:
原文: "Thank you"
译文: "谢谢"
现在,请翻译以下原文:
原文: "{input_text}"
译文:
"""
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
specific_prompt = prompt_template.format(input_text="How are you")
print(specific_prompt)
数学运算
from langchain.prompts import PromptTemplate
few_shot_template = """
示例1:
题目: "2 + 3"
答案: 5
示例2:
题目: "5 - 2"
答案: 3
示例3:
题目: "3 * 4"
答案: 12
现在,请计算以下题目:
题目: "{input_text}"
答案:
"""
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
specific_prompt = prompt_template.format(input_text="6 / 2")
print(specific_prompt)
词性标注
from langchain.prompts import PromptTemplate
few_shot_template = """
示例1:
单词: "apple"
词性: 名词
示例2:
单词: "run"
词性: 动词
示例3:
单词: "beautiful"
词性: 形容词
现在,请标注以下单词的词性:
单词: "{input_text}"
词性:
"""
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
specific_prompt = prompt_template.format(input_text="quickly")
print(specific_prompt)
故事续写
from langchain.prompts import PromptTemplate
few_shot_template = """
示例1:
开头: "小明走进森林,发现了一条神秘的小路。"
续写: "他好奇地沿着小路走去,不久后看到了一座古老的城堡。"
示例2:
开头: "小红在海边捡到了一个神奇的贝壳。"
续写: "当她把贝壳凑近耳边,听到了一阵悠扬的歌声。"
示例3:
开头: "小刚在山顶看到了一道绚丽的彩虹。"
续写: "他兴奋地朝着彩虹的方向跑去,想看看彩虹的尽头有什么。"
现在,请根据以下开头续写故事:
开头: "{input_text}"
续写:
"""
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
specific_prompt = prompt_template.format(input_text="小美在花园里种下了一颗魔法种子。")
print(specific_prompt)
非格式化内容提取成格式化
from langchain.prompts import PromptTemplate
# 定义最小样本提示词模板
few_shot_template = """
示例 1:
非格式化内容: "张三今年 25 岁,是一名程序员,住在北京。"
格式化内容: {
"name": "张三",
"age": 25,
"occupation": "程序员",
"location": "北京"
}
示例 2:
非格式化内容: "李四 30 岁,职业是医生,生活在上海。"
格式化内容: {
"name": "李四",
"age": 30,
"occupation": "医生",
"location": "上海"
}
示例 3:
非格式化内容: "王五 22 岁,还是个学生,待在广州。"
格式化内容: {
"name": "王五",
"age": 22,
"occupation": "学生",
"location": "广州"
}
现在,请将以下非格式化内容提取成格式化内容:
非格式化内容: "{input_text}"
格式化内容:
"""
# 创建 PromptTemplate 对象
prompt_template = PromptTemplate(
input_variables=["input_text"],
template=few_shot_template
)
# 生成具体提示词
input_text = "赵六 28 岁,是一名设计师,在深圳工作。"
specific_prompt = prompt_template.format(input_text=input_text)
print(specific_prompt)