• 生成式AI - Knowledge Graph Prompting:一种基于大模型的多文档问答方法


    大型语言模型(LLM)已经彻底改变了自然语言处理(NLP)任务。它们改变了我们与文本数据交互和处理的方式。这些强大的AI模型,如OpenAI的GPT-4,改变了理解、生成人类类似文本的方式,导致各种行业出现了众多突破性应用。

    LangChain是一个用于构建基于大型语言模型(如GPT)的应用程序的开源框架。它使应用程序能够将语言模型连接到其他数据源,并允许语言模型与其环境进行交互。

        在这篇博客中,我们将讨论LangChain在基于LLM的应用开发中的应用。通过提示LLM,现在比以前任何时候都可以更快地开发AI应用程序。基于LLM的应用需要多次提示和输出解析,因此我们需要为此编写大量代码。LangChain通过利用NLP应用开发中发现的基本抽象,使得这一开发过程变得更加容易。本博客的内容主要基于短课程LangChain用于LLM应用开发。

    LangChain框架概述LangChain是一个用于开发应用程序的开源框架。它将大型语言模型(如GPT-4)与外部数据相结合。LangChain有Python或JavaScript(TypeScript)包可用。LangChain注重组合和模块化。它具有模块化组件,其中单个组件可以相互结合使用,也可以单独使用。LangChain可以应用于多个用例,并可以组合其模块化组件以实现更完整的端到端应用程序。

    LangChain的关键组件LangChain强调灵活性和模块化。它将自然语言处理流程划分为独立的模块化组件,使开发人员能够根据需要定制工作流程。LangChain框架可以分为六个模块,每个模块允许与LLM进行不同方面的交互。

    模型:

    • LLMs — 20+集成

    • Chat Models

    • Text Embedding Models — 10+集成 提示:

    • 提示模板

    • 输出解析器 — 5+集成

    • 示例选择器 — 10+集成 索引:

    • 文档加载器: 50+集成

    • 文本拆分器: 10+集成

    • 向量空间: 10+集成

    • 检索器: 5+集成/实现 链:

    • Prompt + LLM + Output parsing

    • 可用作更长链的构建块

    • 更多特定于应用的链:20+类型

    • 检索器: 5+集成/实现 代理:

    • 代理是一种端到端用例类型,将模型用作推理引擎

    • 代理类型: 5+类型

    • 代理工具包: 10+实现

    模型模型是任何语言模型应用的核心元素。模型指的是支持LLM的语言模型。LangChain提供了与任何语言模型接口和集成的构建块。LangChain为两种类型的模型提供接口和集成:

    LLMs — 以文本字符串作为输入并返回文本字符串的模型Chat Models — 由语言模型支持但以聊天消息列表作为输入并返回聊天消息的模型。

    # This is langchain's abstraction for chatGPT API Endpointfrom langchain.chat_models import ChatOpenAI​​​​​​
    # To control the randomness and creativity of the generated text by an LLM, # use temperature = 0.0chat = ChatOpenAI(temperature=0.0)

    Prompts是编程模型的新方式。提示是指创建输入以传递到模型的风格。提示通常由多个组件构成。提示模板和示例选择器提供了主要类和函数,以便轻松构建和使用提示。

    我们将定义一个模板字符串,并使用该模板字符串和ChatPromptTemplate从LangChain创建提示模板。

    提示模板

    1. # Define a template string
    2. template_string = """Translate the text that is delimited by triple backticks \
    3. into a style that is {style}. text: ```{text}```
    4. """
    # Create a prompt template using above template stringfrom langchain.prompts import ChatPromptTemplateprompt_template = ChatPromptTemplate.from_template(template_string

    上述的prompt_template有两个字段,即style和text。我们也可以从此提示模板中提取原始模板字符串。现在,如果我们想要将文本翻译为某种其他样式,我们需要定义我们的翻译样式和文本。

    1. customer_style = """American English in a calm and respectful tone
    2. """
    3. customer_email = """
    4. Arrr, I be fuming that me blender lid flew off and splattered me kitchen walls \
    5. with smoothie! And to make matters worse, the warranty don't cover the cost of \
    6. cleaning up me kitchen. I need yer help right now, matey!
    7. """

    在这里,我们将风格设置为美国英语,语气平静且尊重。我们使用带有将被三个反引号括起来的文本翻译为特定风格的f-string指令来指定提示,然后将上述样式(客户风格)和文本(客户电子邮件)传递给LLM进行文本翻译。

    1. # customer_message will generate the prompt and it will be passed into
    2. # the llm to get a response.
    3. customer_messages = prompt_template.format_messages(
    4. style=customer_style,
    5. text=customer_email)
    6. # Call the LLM to translate to the style of the customer message.
    7. customer_response = chat(customer_messages)

    当我们构建复杂的应用程序时,提示可以变得相当长和详细。我们不使用f字符串,而是使用提示模板,因为提示模板是有用的抽象,可以帮助我们重用好的提示。我们可以创建提示模板并重用这些提示模板,并为模型指定输出样式和文本以供工作。

    LangChain提供了一些常见操作的提示,例如摘要或回答问题,或者连接到SQL数据库或连接到不同的API。因此,通过使用LangChain的一些内置提示,我们可以快速获得一个正在运行的应用程序,而无需自行设计提示。

    输出解析器LangChain的提示库的另一个方面是它还支持输出解析。输出解析器有助于从语言模型的输出中获取结构化信息。输出解析器涉及将模型的输出解析为更结构化的格式,以便我们可以使用输出执行下游任务。

    当我们使用LLMs构建复杂应用程序时,我们经常指示LLM以特定格式生成其输出,例如使用特定的关键字。LangChain的库函数假设LLM将使用某些关键字来解析其输出。

    我们可以有一个LLM输出JSON,我们将使用LangChain解析该输出,如下所示:

    我们需要首先定义我们希望如何格式化LLM输出。在这种情况下,我们定义了一个具有提及产品是否为礼物、交付所需的天数以及价格是否可负担的字段的Python字典。

    1. # Following is one example of the desired output.
    2. {
    3. "gift": False,
    4. "delivery_days": 5,
    5. "price_value": "pretty affordable!"
    6. }

    我们可以在下面提到的三个反引号中包含客户评论。我们可以定义以下评论模板:​​​​​​​​​​​​​​

    1. # This is an example of customer review and a template that try to get the desired output
    2. customer_review = """\
    3. Need to be actual review
    4. """
    5. review_template = """\
    6. For the following text, extract the following information:
    7. gift: Was the item purchased as a gift for someone else? \
    8. Answer True if yes, False if not or unknown.
    9. delivery_days: How many days did it take for the product \
    10. to arrive? If this information is not found, output -1.
    11. price_value: Extract any sentences about the value or price,\
    12. and output them as a comma separated Python list.
    13. Format the output as JSON with the following keys:
    14. gift
    15. delivery_days
    16. price_value
    17. text: {text}
    18. """
    19. # This is an example of customer review and a template that try to get the desired output
    20. customer_review = """\
    21. Need to be actual review
    22. """
    23. review_template = """\
    24. For the following text, extract the following information:
    25. gift: Was the item purchased as a gift for someone else? \
    26. Answer True if yes, False if not or unknown.
    27. delivery_days: How many days did it take for the product \
    28. to arrive? If this information is not found, output -1.
    29. price_value: Extract any sentences about the value or price,\
    30. and output them as a comma separated Python list.
    31. Format the output as JSON with the following keys:
    32. gift
    33. delivery_days
    34. price_value
    35. text: {text}
    36. """
    37. # We will wrap all review template, customer review in langchain to get output
    38. # in desired format. We will have prompt template created from review template.
    39. from langchain.prompts import ChatPromptTemplate
    40. prompt_template = ChatPromptTemplate.from_template(review_template)
    41. print(prompt_template)
    42. # Create messages using prompt templates created earlier and customer review.
    43. # Finally, we pass messgaes to OpenAI endpoint to get response.
    44. messages = prompt_template.format_messages(text=customer_review)
    45. chat = ChatOpenAI(temperature=0.0)
    46. response = chat(messages)
    47. print(response.content)

    上述响应仍然不是字典,而是字符串。我们需要使用Python字典将LLM输出字符串解析为字典。我们需要为Python字典中的每个字段项定义ResponseSchema。为了简洁起见,我没有提供这些代码片段。它们可以在我的GitHub笔记本中找到。这是一种非常有效的方法,可以将LLM输出解析为Python字典,使其更易于在下游处理中使用。

    ReAct框架

    图片

    在上述示例中,LLM使用诸如“思想”、“行动”和“观察”等关键词,使用名为ReAct的框架执行思维推理链。“思想”是LLM所想的,通过给LLM思考的空间,LLM可以得到更准确的结论。“行动”是一个关键词来执行特定的行动,而“观察”是一个关键词来展示LLM从特定行动中所学到的内容。如果我们有一个指示LLM使用这些特定关键词(如思想、行动和观察)的提示,那么这些关键词可以与解析器结合使用,以提取标记有这些关键词的文本。

    记忆大型语言模型无法记住之前的对话。

    当你与这些模型互动时,它们自然不会记得你之前说的话或之前的所有对话,这在你构建一些应用程序(如聊天机器人)并希望与它们进行对话时是一个问题。

    通过模型、提示和解析器,我们可以重用我们自己的提示模板,与他人共享提示模板,或使用LangChain内置的提示模板,这些模板可以与输出解析器结合使用,以便我们获得特定格式的输出,并让解析器解析该输出并将其存储在特定字典或其他数据结构中,从而使下游处理更容易。

    我将在下一篇博客中讨论链和代理。我还将在我的另一篇博客中讨论如何在我们的数据中进行问题回答。最后,我们可以看到,通过提示LLM或大型语言模型,现在比以前任何时候都更有可能开发出更快的AI应用程序。但是一个应用程序可能需要多次提示LLM并解析其输出,因此需要编写大量的粘合代码。Langchain有助于简化这个过程。

  • 相关阅读:
    CYEZ 模拟赛 5
    开源 Golang 微服务入门一: HTTP 框架 Hertz
    AWS认证SAA-C03每日一题
    网络安全笔记-TCP/IP
    【快速上手系列】使用MD5加密对密码进行加密
    卷积、卷积图像操作和卷积神经网络
    手记系列之三 ----- 关于使用Nginx的一些使用方法和经验
    【linux命令讲解大全】014.Git:分布式版本控制系统的先驱和常用命令清单(三)
    R中的min()函数 和max()函数
    MySQL---DML+DQL+DCL
  • 原文地址:https://blog.csdn.net/u011537073/article/details/134368187