admin管理员组

文章数量:1530085

分类目录:《大模型从入门到应用》总目录

LangChain系列文章:

  • 基础知识
  • 快速入门
    • 安装与环境配置
    • 链(Chains)、代理(Agent:)和记忆(Memory)
    • 快速开发聊天模型
  • 模型(Models)
    • 基础知识
    • 大型语言模型(LLMs)
      • 基础知识
      • LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(Human Input LLM)
      • 缓存LLM的调用结果
      • 加载与保存LLM类、流式传输LLM与Chat Model响应和跟踪tokens使用情况
    • 聊天模型(Chat Models)
      • 基础知识
      • 使用少量示例和响应流式传输
    • 文本嵌入模型
      • Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
      • Embaas、Fake Embeddings、Google Vertex AI PaLM等
  • 提示(Prompts)
    • 基础知识
    • 提示模板
      • 基础知识
      • 连接到特征存储
      • 创建自定义提示模板和含有Few-Shot示例的提示模板
      • 部分填充的提示模板和提示合成
      • 序列化提示信息
    • 示例选择器(Example Selectors)
    • 输出解析器(Output Parsers)
  • 记忆(Memory)
    • 基础知识
    • 记忆的类型
      • 会话缓存记忆、会话缓存窗口记忆和实体记忆
      • 对话知识图谱记忆、对话摘要记忆和会话摘要缓冲记忆
      • 对话令牌缓冲存储器和基于向量存储的记忆
    • 将记忆添加到LangChain组件中
    • 自定义对话记忆与自定义记忆类
    • 聊天消息记录
    • 记忆的存储与应用
  • 索引(Indexes)
    • 基础知识
    • 文档加载器(Document Loaders)
    • 文本分割器(Text Splitters)
    • 向量存储器(Vectorstores)
    • 检索器(Retrievers)
  • 链(Chains)
    • 基础知识
    • 通用功能
      • 自定义Chain和Chain的异步API
      • LLMChain和RouterChain
      • SequentialChain和TransformationChain
      • 链的保存(序列化)与加载(反序列化)
    • 链与索引
      • 文档分析和基于文档的聊天
      • 问答的基础知识
      • 图问答(Graph QA)和带来源的问答(Q&A with Sources)
      • 检索式问答
      • 文本摘要(Summarization)、HyDE和向量数据库的文本生成
  • 代理(Agents)
    • 基础知识
    • 代理类型
    • 自定义代理(Custom Agent)
    • 自定义MRKL代理
    • 带有ChatModel的LLM聊天自定义代理和自定义多操作代理(Custom MultiAction Agent)
    • 工具
      • 基础知识
      • 自定义工具(Custom Tools)
      • 多输入工具和工具输入模式
      • 人工确认工具验证和Tools作为OpenAI函数
    • 工具包(Toolkit)
    • 代理执行器(Agent Executor)
      • 结合使用Agent和VectorStore
      • 使用Agents的异步API和创建ChatGPT克隆
      • 处理解析错误、访问中间步骤和限制最大迭代次数
      • 为代理程序设置超时时间和限制最大迭代次数和为代理程序和其工具添加共享内存
    • 计划与执行
  • 回调函数(Callbacks)

将提示信息存储为文件而不是Python代码通常更好。这样可以方便共享、存储和版本控制提示信息。本文介绍了如何在LangChain中进行提示信息的序列化,包括不同类型的提示信息和不同的序列化选项。

在高层次上,序列化遵循以下设计原则:

  • 支持JSON和YAML。LangChain希望支持在磁盘上易于阅读的序列化方法,而YAML和JSON是其中两种最受欢迎的方法。需要注意的是,此规则适用于提示信息。对于其他内容(例如示例),可能支持不同的序列化方法。
  • LangChain支持在一个文件中指定所有内容,或者将不同的组件(模板、示例等)存储在不同的文件中并进行引用。对于某些情况,将所有内容存储在一个文件中可能是最合适的,但对于其他情况,将某些内容拆分开(长模板、大型示例、可重用组件)可能更好。而LangChain同时支持这两种方式。

LangChain还提供了一个单一的入口点,用于从磁盘加载提示信息,从而轻松加载任何类型的提示信息。

# All prompts are loaded through the `load_prompt` function.
from langchain.prompts import load_prompt

PromptTemplate

本部分介绍了加载PromptTemplate

从YAML加载

下面是从YAML加载PromptTemplate的示例:
本地文件:

!cat simple_prompt.yaml
    _type: prompt
    input_variables:
        ["adjective", "content"]
    template: 
        Tell me a {adjective} joke about {content}.

输入:

prompt = load_prompt("simple_prompt.yaml")
print(prompt.format(adjective="funny", content="chickens"))

输出:

Tell me a funny joke about chickens.
从JSON加载

下面是从JSON加载PromptTemplate的示例:
本地文件:

!cat simple_prompt.json
    {
        "_type": "prompt",
        "input_variables": ["adjective", "content"],
        "template": "Tell me a {adjective} joke about {content}."
    }

输入:

prompt = load_prompt("simple_prompt.json")
print(prompt.format(adjective="funny", content="chickens"))

输出:

Tell me a funny joke about chickens.
从文件加载

下面是将模板存储在单独文件中,并在配置中引用该文件的示例。需要注意的是,键从template更改为template_path
本地文件:

!cat simple_template.txt
Tell me a {adjective} joke about {content}.

!cat simple_prompt_with_template_file.json
    {
        "_type": "prompt",
        "input_variables": ["adjective", "content"],
        "template_path": "simple_template.txt"
    }

输入:

prompt = load_prompt("simple_prompt_with_template_file.json")
print(prompt.format(adjective="funny", content="chickens"))

输出:

Tell me a funny joke about chickens.

FewShotPromptTemplate

本部分介绍加载FewShotPromptTemplate的示例。下面是SON本地文件的示例:
本地文件:

!cat examples.json
    [
        {"input": "happy", "output": "sad"},
        {"input": "tall", "output": "short"}
    ]

以下是将相同示例存储为YAML格式的示例:

!cat examples.yaml
    - input: happy
      output: sad
    - input: tall
      output: short
从YAML加载

这是一个从YAML加载Few-Shot Example的示例。
本地文件:

!cat few_shot_prompt.yaml
    _type: few_shot
    input_variables:
        ["adjective"]
    prefix: 
        Write antonyms for the following words.
    example_prompt:
        _type: prompt
        input_variables:
            ["input", "output"]
        template:
            "Input: {input}\nOutput: {output}"
    examples:
        examples.json
    suffix:
        "Input: {adjective}\nOutput:"

输入:

prompt = load_prompt("few_shot_prompt.yaml")
print(prompt.format(adjective="funny"))

输出:

    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

如果我们从YAML文件中加载示例,同样的方法也适用。
本地文件:

!cat few_shot_prompt_yaml_examples.yaml
    _type: few_shot
    input_variables:
        ["adjective"]
    prefix: 
        Write antonyms for the following words.
    example_prompt:
        _type: prompt
        input_variables:
            ["input", "output"]
        template:
            "Input: {input}\nOutput: {output}"
    examples:
        examples.yaml
    suffix:
        "Input: {adjective}\nOutput:"

输入:

prompt = load_prompt("few_shot_prompt_yaml_examples.yaml")
print(prompt.format(adjective="funny"))
    Write antonyms for the following words.

输出:

    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:
从JSON加载

这是一个从JSON加载Few-Shot Example的示例。
本地文件:

!cat few_shot_prompt.json
    {
        "_type": "few_shot",
        "input_variables": ["adjective"],
        "prefix": "Write antonyms for the following words.",
        "example_prompt": {
            "_type": "prompt",
            "input_variables": ["input", "output"],
            "template": "Input: {input}\nOutput: {output}"
        },
        "examples": "examples.json",
        "suffix": "Input: {adjective}\nOutput:"
    }   

输入:

prompt = load_prompt("few_shot_prompt.json")
print(prompt.format(adjective="funny"))

输出:

    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:
配置中的示例

这是一个直接在配置中引用示例的示例。
本地文件:

!cat few_shot_prompt_examples_in.json
    {
        "_type": "few_shot",
        "input_variables": ["adjective"],
        "prefix": "Write antonyms for the following words.",
        "example_prompt": {
            "_type": "prompt",
            "input_variables": ["input", "output"],
            "template": "Input: {input}\nOutput: {output}"
        },
        "examples": [
            {"input": "happy", "output": "sad"},
            {"input": "tall", "output": "short"}
        ],
        "suffix": "Input: {adjective}\nOutput:"
    } 

输入:

prompt = load_prompt("few_shot_prompt_examples_in.json")
print(prompt.format(adjective="funny"))

输出:

    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:
从文件加载示例提示

这是一个从单独的文件加载用于格式化示例的PromptTemplate的示例。需要注意的是,键名从example_prompt更改为example_prompt_path
本地文件:

!cat example_prompt.json
    {
        "_type": "prompt",
        "input_variables": ["input", "output"],
        "template": "Input: {input}\nOutput: {output}" 
    }
!cat few_shot_prompt_example_prompt.json 
    {
        "_type": "few_shot",
        "input_variables": ["adjective"],
        "prefix": "Write antonyms for the following words.",
        "example_prompt_path": "example_prompt.json",
        "examples": "examples.json",
        "suffix": "Input: {adjective}\nOutput:"
    }   

输入:

prompt = load_prompt("few_shot_prompt_example_prompt.json")
print(prompt.format(adjective="funny"))

输出:

    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

带有OutputParser的PromptTemplate

这是一个从文件加载PromptTemplateOutputParser的示例。
本地文件:

! cat prompt_with_output_parser.json
    {
        "input_variables": [
            "question",
            "student_answer"
        ],
        "output_parser": {
            "regex": "(.*?)\\nScore: (.*)",
            "output_keys": [
                "answer",
                "score"
            ],
            "default_output_key": null,
            "_type": "regex_parser"
        },
        "partial_variables": {},
        "template": "Given the following question and student answer, provide a correct answer and score the student answer.\nQuestion: {question}\nStudent Answer: {student_answer}\nCorrect Answer:",
        "template_format": "f-string",
        "validate_template": true,
        "_type": "prompt"
    }

输入:

prompt = load_prompt("prompt_with_output_parser.json")
prompt.output_parser.parse("George Washington was born in 1732 and died in 1799.\nScore: 1/2")
    {'answer': 'George Washington was born in 1732 and died in 1799.',
     'score': '1/2'}

参考文献:
[1] LangChain官方网站:https://www.langchain/
[2] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain/
[3] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://wwwlangchain/

本文标签: 提示提示信息入门模型模板