prompt第四讲-fewshot

文章目录

前提回顾

前面已经实现了一个翻译助手了[prompt第三讲-PromptTemplate],prompt模板设计中,有说明、案例、和实际的问题

python 复制代码
# -*- coding: utf-8 -*-
"""
@Time : 2024/7/8 9:44
@Auth : leon
"""
from langchain_core.prompts import PromptTemplate
# 4. 定义部分变量
prompt_template = PromptTemplate.from_template("""
你是一个翻译助手,你擅长将{source_language}翻译为{dst_language},请将我发送给你的question的内容翻译为{dst_language},不要返回无关的内容,只需返回最终翻译结果,下面的history examples中提供了一些具体的案例,为你提供一些参考:

## history examples:
question:美丽->answer:beautiful;
question:男孩->answer:boy;
question:男人->answer:man;
question:456->answer:four hundred and fifty-six;
question:1->answer:one;
question:34->answer:thirty-four;

## user true task:
question:{user_input_words}->answer:
""")
lag2lag = input("你想我成为什么翻译助手(格式如:中文-英文):")
source_language,dst_language = lag2lag.split('-')
new_prompt_template = prompt_template.partial(source_language=source_language,dst_language=dst_language)
print("助手初始化完毕,您的翻译助手上线!!!")
# 2. llm定义
from langchain_community.llms import Tongyi
from pydantic_settings import BaseSettings,SettingsConfigDict

"""
2,1 获取千问的key
我这么写的原因是因为方便我上传项目到github的同时,不暴露我的key,所以我把可以key保存到了最外部的一个.env文件中
这样我每一次同步到github的时候就不会把key也推出去,你们测试的时候,可以直接写成
qwen_key="sk-cc2209cec48c4bc966fb4acda169e",这样省事。
"""
class ModelConfig(BaseSettings):
    model_config = SettingsConfigDict(env_file="../../../.env",env_file_encoding="utf-8")
    qwen_key:str
    deepseek_key:str
    deepseek_base_url:str

model_config = ModelConfig()
qwen_key = model_config.qwen_key
# 1. 读取配置信息,获取模型key
llm = Tongyi(dashscope_api_key=qwen_key)


while(True):
    user_input_word = input(f"请输入需要翻译的{source_language}:")
    if user_input_word.lower() =="quit":
        break
    else:
        prompt = new_prompt_template.invoke({"user_input_words":user_input_word})
        print(llm.invoke(prompt))

FewShotPromptTemplate

下面我们换一种更加优雅的方式来实现上面的prompt模板

python 复制代码
# -*- coding: utf-8 -*-
"""
@Time : 2024/7/9 9:44
@Auth : leon
"""
from langchain_core.prompts import PromptTemplate,FewShotPromptTemplate
example_prompt = PromptTemplate.from_template("question: {question}->answer:{answer}")
examples = [
    {'question':'美丽',"answer":'beautiful'},
    {'question':'男孩',"answer":'boy'},
    {'question':'男人',"answer":'man'},
    {'question':'456',"answer":'four'},
    {'question':'456',"answer":'four hundred and fifty-six'},
    {'question':'1',"answer":'one'},
    {'question':'34',"answer":'thirty-four'}
]
prefix = """
你是一个翻译助手,你擅长将{source_language}翻译为{dst_language},请将我发送给你的question的内容翻译为{dst_language},不要返回无关的内容,只需返回最终翻译结果,下面的history examples中提供了一些具体的案例,为你提供一些参考:
## history examples:
"""
suffix = """
## user true task:
question:{user_input_words}->answer:
"""
prompt_template = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix=prefix,
    suffix=suffix,
    input_variables=['user_input_words','source_language','dst_language']
)

lag2lag = input("你想我成为什么翻译助手(格式如:中文-英文):")
source_language,dst_language = lag2lag.split('-')
new_prompt_template = prompt_template.partial(source_language=source_language,dst_language=dst_language)


from langchain_community.llms import Tongyi
from pydantic_settings import BaseSettings,SettingsConfigDict

"""
2,1 获取千问的key
我这么写的原因是因为方便我上传项目到github的同时,不暴露我的key,所以我把可以key保存到了最外部的一个.env文件中
这样我每一次同步到github的时候就不会把key也推出去,你们测试的时候,可以直接写成
qwen_key="sk-cc2209cec48c4bc966fb4acda169e",这样省事。
"""
class ModelConfig(BaseSettings):
    model_config = SettingsConfigDict(env_file="../../../.env",env_file_encoding="utf-8")
    qwen_key:str
    deepseek_key:str
    deepseek_base_url:str

model_config = ModelConfig()
qwen_key = model_config.qwen_key
# 1. 读取配置信息,获取模型key
llm = Tongyi(dashscope_api_key=qwen_key)

while(True):
    user_input_word = input(f"请输入需要翻译的{source_language}:")
    if user_input_word.lower() =="quit":
        break
    else:
        prompt = new_prompt_template.invoke({"user_input_words":user_input_word})
        print(llm.invoke(prompt))

着重看一下FewShotPromptTemplate定义模板部分,他没有什么方法可以实例化对象,只支持直接实例化,而实例化

要传入的参数也不用咋说,格式一目了然
参数讲解

  1. example_prompt:你想要案例遵守的prompt模板格式
  2. examples一个案例列表,里面是多个字典,字典的key必须和example_prompt中定义的变量是统一的
  3. prefix:你想要在案例前面插入的内容,如果是接着前面的翻译助手,那这里通常就是这个助手的能力说明
  4. suffix:通常就是你想要最后插入的实际的问题的prompt模板
  5. input_variables:变量说明,这个变量来自prefix和suffix

foramt格式化

因为FewShotPromptTemplate也是继承自runnable的,所以他有的方法和变量基本和前面讲的PromptTemplate差不多,

变量可能会有些变化,但是方法基本是统一的,也是遵从(invoke,batch,stream那一套的),而invoke最底层是

调用了format,所以我只需要讲解一下format,其他的都懂了

format的原理如下:

  1. 遍历examples列表,根据example_prompt模板格式,实例化出一个prompt列表,并且以空格的形式进行拼接成一个字符串
  2. 将prefix添加到第一步得到prompt字符串前面,将suffix添加到prompt字符串后面
  3. 将输入的变量填入新的模板中,得到格式化后的prompt

其他支持的方法,和前面的PromptTemplate是一样的invoke,batch,stream等,需要注意的是,它也提供了save功能,但是
没有提供加载功能,这很奇怪。

附上筋斗云,会有完整教程和代码:https://github.com/traveler-leon/langchain-learning.git

相关推荐
lqjun082710 分钟前
Focal Loss 原理详解及 PyTorch 代码实现
人工智能·pytorch·python
科技小E10 分钟前
WebRTC技术EasyRTC嵌入式音视频通信SDK打造远程实时视频通话监控巡检解决方案
人工智能·音视频
风虎云龙科研服务器16 分钟前
英伟达Blackwell架构重构未来:AI算力革命背后的技术逻辑与产业变革
人工智能·重构·架构
契合qht53_shine35 分钟前
深度学习 自然语言处理(RNN) day_02
rnn·深度学习·自然语言处理
白熊18840 分钟前
【计算机视觉】OpenCV实战项目:基于Tesseract与OpenCV的字符识别系统深度解析
人工智能·opencv·计算机视觉
kovlistudio1 小时前
机器学习第三讲:监督学习 → 带答案的学习册,如预测房价时需要历史价格数据
人工智能·机器学习
嵌入式仿真实验教学平台1 小时前
「国产嵌入式仿真平台:高精度虚实融合如何终结Proteus时代?」——从教学实验到低空经济,揭秘新一代AI赋能的产业级教学工具
人工智能·学习·proteus·无人机·低空经济·嵌入式仿真·实验教学
正在走向自律2 小时前
Python 数据分析与可视化:开启数据洞察之旅(5/10)
开发语言·人工智能·python·数据挖掘·数据分析
LuvMyLife2 小时前
基于Win在VSCode部署运行OpenVINO模型
人工智能·深度学习·计算机视觉·openvino
fancy1661662 小时前
力扣top100 矩阵置零
人工智能·算法·矩阵