【LangChain系列】第五篇:大语言模型中的提示词,模型及输出简介及实践

本节我们将探索 LangChain 的内部工作原理,LangChain 是一个有用的 Python 库,可以简化使用 LLM 的过程。您将能够创建直观、可重用且可扩展的应用程序,这些应用程序可以改变您与 LLM 的交互方式。

一、大语言模型(LLM)

大型语言模型 (LLM) 是在大量数据上预先训练的非常大的深度学习模型。底层转换器是一组神经网络,由具有自注意力功能的编码器和解码器组成,使其能够理解和生成类似人类的文本。这些模型是构建应用程序的基础。其中包括GPT-3.5-turbo,这是由 OpenAI 开发的 LLM。

ChatOpenAI 类允许您通过 OpenAI API 与 GPT-3.5-turbo 模型进行交互。

from langchain_openai import ChatOpenAI

# Create a ChatOpenAI instance
chat = ChatOpenAI(temperature=0.0, model="gpt-3.5-turbo")

通过将温度参数设置为 0.0,我们指示模型生成确定性输出,确保多次运行的一致性。这在构建需要可靠和可重复结果的应用程序时特别有用。

二、提示词

模型提供底层智能,提示充当指挥,指导语言模型执行特定任务或生成所需的输出。提示词本质上是我们提供给模型的指令,塑造其行为并根据我们的需求定制其响应。LangChain引入了ChatPromptTemplate的概念,可以简化创建和管理提示的过程。使用此工具,您可以定义可重用的模板,这些模板可以在整个应用程序中轻松共享和调整。

from langchain.prompts import ChatPromptTemplate

template_string = """Translate the text that is delimited by double quotes into a style that is {style}. text: ''{text}''"""

prompt_template = ChatPromptTemplate.from_template(template_string)

print(prompt_template.messages[0].prompt)

# Output
PromptTemplate(input_variables=['style', 'text'], template='Translate the text that is delimited by triple quotes into a style that is {style}. text: """{text}"""\n')

在上面的示例中,我们定义了一个提示词模板,该模板指示语言模型将给定文本转换为指定的样式。通过使用提示,我们可以执行广泛的任务,从语言翻译到内容生成,甚至是复杂的分析任务。如果您需要以更动态和个性化的方式与语言模型进行交互,可以借助使用 prompt_template.format_messages 方法设置消息格式的功能,您可以轻松地将用户输入、上下文信息或任何其他相关数据用于提示,从而确保与语言模型的交互针对每个独特的方案进行定制。

customer_style = "American English in a calm and respectful tone"
customer_email = "Arrr, I be fuming that me blender lid flew off and splattered me kitchen walls with smoothie! ..."

# Call the LLM to translate to the style of the customer message
customer_messages = prompt_template.format_messages(style=customer_style, text=customer_email)
customer_response = chat.invoke(customer_messages)
print(customer_response.content)

# Output
I am really frustrated that my blender lid flew off and splattered my kitchen walls with smoothie! And to make matters worse, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help right now, friend.

通过提供所需的样式和客户电子邮件作为输入,我们可以生成一个提示词,指示语言模型将客户的信息从“盗版语”翻译成礼貌、易于理解的格式。这个简单而强大的示例展示了LangChain提示系统的多功能性,使您能够与语言模型创建动态和上下文感知的交互。

三、输出解析器

虽然提示词会引导语言模型的输入,但输出解析器在解释和构建其响应方面起着至关重要的作用。这些分析器将语言模型生成的原始文本转换为可由应用程序轻松使用和处理的结构化格式。想象一下,您正在构建一个电子商务应用程序,该应用程序依赖于客户评论来提供见解。使用 LangChain 的 ResponseSchema 和 StructuredOutputParser,您可以定义语言模型的预期输出格式,并从客户评论中提取相关信息。

from langchain.output_parsers import ResponseSchema, StructuredOutputParser

# Define the expected output schema
gift_schema = ResponseSchema(name="gift", description="Was the item purchased as a gift?")
delivery_days_schema = ResponseSchema(name="delivery_days", description="How many days did it take for the product to arrive?")
price_value_schema = ResponseSchema(name="price_value", description="Extract any sentences about the value or price.")

response_schemas = [gift_schema, delivery_days_schema, price_value_schema]

# Create the output parser
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
print(format_instructions)

# Output 
The output should be a markdown code snippet formatted in the following schema, 
{
   
    "gift": string  // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.
    "delivery_days": string  // How many days did it take for the product to arrive? If this information is not found, output -1.
    "price_value": string  // Extract any sentences about the value or price, and output them as a comma separated Python list.
}

通过定义所需的输出模式,LangChain会生成格式指令,指导语言模型以特定格式生成其输出。然后,可以使用 output_parser.parse 方法轻松将此结构化输出解析为 Python 字典,从而允许您提取信息,例如该物品是否是礼物、交货时间以及有关产品价值或价格的任何评论。

review_template_2 = """\
For the following text, extract the following information:

gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.

delivery_days: How many days did it take for the product\
to arrive? If this information is not found, output -1.

price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.

text: {text}

{format_instructions}
"""

prompt = ChatPromptTemplate.from_template(template=review_template_2)

messages = prompt.format_messages(
    text=customer_review, format_instructions=format_instructions
)

response = chat.invoke(messages)
print(response.content)

# Output
{
   
    "gift": "True",
    "delivery_days": "2",
    "price_value": "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features."
}

output_dict = output_parser.parse(response_content)
print(output_dict["gift"])  # Output: True
print(output_dict["delivery_days"])  # Output: 2
print(output_dict["price_value"])  # Output: "It's slightly more expensive than other leaf blowers, but worth it for the extra features."

有了结构化输出,您可以轻松地将提取的信息集成到您的应用程序中,从而能够提供个性化建议、执行数据分析或增强整体用户体验。

四、优势

通过使用LangChain的模型、提示和输出解析器抽象,您可以获得几个关键优势:

  • 可重用性:LangChain允许您定义可重用的组件,这些组件可以在应用程序之间共享,甚至可以与团队或社区中的其他开发人员共享,而不是从头开始为每个新任务重新创建提示和解析器。
  • 一致性:通过集中提示和输出格式,无论使用何种任务或语言模型,都可以确保为用户提供一致且可预测的体验。
  • 可扩展性:随着应用程序的增长和演变,LangChain的模块化方法可以更轻松地适应不断变化的需求,合并新的语言模型,或与其他数据源或API集成。
  • 内置库:LangChain为摘要、问答和数据库交互等常见任务提供了丰富的预构建提示和解析器库,使您能够启动开发过程并专注于构建独特的功能,使您的应用程序与众不同。
  • 无缝集成:LangChain的抽象促进了应用程序、语言模型和解析输出之间的平滑交互,使您能够构建使用LLM的端到端解决方案,同时保持干净且可维护的代码库。

五、小结

在快速发展的机器学习和自然语言处理世界中,掌握大型语言模型是必要的。通过使用LangChain的模型、提示和输出解析器,您可以解锁一个充满可能性的世界,并构建复杂的应用程序,将语言模型无缝集成到您的工作流程中。

如何系统的去学习大模型LLM ?

作为一名热心肠的互联网老兵,我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。

但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的 AI大模型资料 包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来

所有资料 ⚡️ ,朋友们如果有需要全套 《LLM大模型入门+进阶学习资源包》,扫码获取~

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

在这里插入图片描述

四、AI大模型商业化落地方案

img

阶段1:AI大模型时代的基础理解

  • 目标:了解AI大模型的基本概念、发展历程和核心原理。
  • 内容
    • L1.1 人工智能简述与大模型起源
    • L1.2 大模型与通用人工智能
    • L1.3 GPT模型的发展历程
    • L1.4 模型工程
      - L1.4.1 知识大模型
      - L1.4.2 生产大模型
      - L1.4.3 模型工程方法论
      - L1.4.4 模型工程实践
    • L1.5 GPT应用案例

阶段2:AI大模型API应用开发工程

  • 目标:掌握AI大模型API的使用和开发,以及相关的编程技能。
  • 内容
    • L2.1 API接口
      - L2.1.1 OpenAI API接口
      - L2.1.2 Python接口接入
      - L2.1.3 BOT工具类框架
      - L2.1.4 代码示例
    • L2.2 Prompt框架
      - L2.2.1 什么是Prompt
      - L2.2.2 Prompt框架应用现状
      - L2.2.3 基于GPTAS的Prompt框架
      - L2.2.4 Prompt框架与Thought
      - L2.2.5 Prompt框架与提示词
    • L2.3 流水线工程
      - L2.3.1 流水线工程的概念
      - L2.3.2 流水线工程的优点
      - L2.3.3 流水线工程的应用
    • L2.4 总结与展望

阶段3:AI大模型应用架构实践

  • 目标:深入理解AI大模型的应用架构,并能够进行私有化部署。
  • 内容
    • L3.1 Agent模型框架
      - L3.1.1 Agent模型框架的设计理念
      - L3.1.2 Agent模型框架的核心组件
      - L3.1.3 Agent模型框架的实现细节
    • L3.2 MetaGPT
      - L3.2.1 MetaGPT的基本概念
      - L3.2.2 MetaGPT的工作原理
      - L3.2.3 MetaGPT的应用场景
    • L3.3 ChatGLM
      - L3.3.1 ChatGLM的特点
      - L3.3.2 ChatGLM的开发环境
      - L3.3.3 ChatGLM的使用示例
    • L3.4 LLAMA
      - L3.4.1 LLAMA的特点
      - L3.4.2 LLAMA的开发环境
      - L3.4.3 LLAMA的使用示例
    • L3.5 其他大模型介绍

阶段4:AI大模型私有化部署

  • 目标:掌握多种AI大模型的私有化部署,包括多模态和特定领域模型。
  • 内容
    • L4.1 模型私有化部署概述
    • L4.2 模型私有化部署的关键技术
    • L4.3 模型私有化部署的实施步骤
    • L4.4 模型私有化部署的应用场景

学习计划:

  • 阶段1:1-2个月,建立AI大模型的基础知识体系。
  • 阶段2:2-3个月,专注于API应用开发能力的提升。
  • 阶段3:3-4个月,深入实践AI大模型的应用架构和私有化部署。
  • 阶段4:4-5个月,专注于高级模型的应用和部署。
这份完整版的所有 ⚡️ 大模型 LLM 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

全套 《LLM大模型入门+进阶学习资源包↓↓↓ 获取~

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

  • 25
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值