当前位置:   article > 正文

Langchain使用介绍之outparser 和memory_langchain 的 outer_parser

langchain 的 outer_parser

  上一篇博客中对Langchain中prompt进行了详细的介绍,此篇博客将介绍Langchain中的outparser和memory。当调用大模型生成内容时,返回的内容默认是string类型,这对于我们获取response中的某些内容信息可能会带来障碍,例如返回的内容本身是json string内容,如果能返回json对象的话,那么获取response中某个对象的值就更加容易,那么如何通过Langchain提供的能力,将输出的内容转换成JSON类型呢?来看看下面的例子。

  1. import openai
  2. import os
  3. from langchain.prompts import (
  4. HumanMessagePromptTemplate, SystemMessagePromptTemplate, ChatPromptTemplate)
  5. from langchain.chat_models import ChatOpenAI
  6. from langchain.output_parsers import ResponseSchema
  7. from langchain.output_parsers import StructuredOutputParser
  8. openai.api_key = os.environ.get("OPEN_AI_KEY")
  9. customer_review = """\
  10. This leaf blower is pretty amazing. It has four settings:\
  11. candle blower, gentle breeze, windy city, and tornado. \
  12. It arrived in two days, just in time for my wife's \
  13. anniversary present. \
  14. I think my wife liked it so much she was speechless. \
  15. So far I've been the only one using it, and I've been \
  16. using it every other morning to clear the leaves on our lawn. \
  17. It's slightly more expensive than the other leaf blowers \
  18. out there, but I think it's worth it for the extra features.
  19. """
  20. review_template = """\
  21. For the following text, extract the following information:
  22. gift: Was the item purchased as a gift for someone else? \
  23. Answer True if yes, False if not or unknown.
  24. delivery_days: How many days did it take for the product\
  25. to arrive? If this information is not found, output -1.
  26. price_value: Extract any sentences about the value or price,\
  27. and output them as a comma separated Python list.
  28. text: {text}
  29. """
  30. human_template_message = HumanMessagePromptTemplate.from_template(
  31. review_template)
  32. chat_template = ChatPromptTemplate.from_messages([human_template_message])
  33. message = chat_template.format_messages(text=customer_review)
  34. chat = ChatOpenAI(model_name="gpt-3.5-turbo")
  35. response = chat(message)
  36. print(response.content)

  上面的代码生成的结果如下图所示,从一段文本中提取出了gift,delivery_days和price_value.但是因为不是JSON对象,所以,如果要获取提取出来的某个值是不行的。

  如果想输出JSON格式的数据,应该如何处理呢?首先在prompt增加了{formact_instruction},通过langchain提供的ResponseSchema定义要提取的字段名称,描述信息;接着通过StructureOutputParser生成parser,再调用parser的get_format_instruction方法生成format_instruction.

  1. review_template_2 = """\
  2. For the following text, extract the following information:
  3. gift: Was the item purchased as a gift for someone else? \
  4. Answer True if yes, False if not or unknown.
  5. delivery_days: How many days did it take for the product\
  6. to arrive? If this information is not found, output -1.
  7. price_value: Extract any sentences about the value or price,\
  8. and output them as a comma separated Python list.
  9. text: {text}
  10. {format_instructions}
  11. """
  12. gift_schema = ResponseSchema(name="gift",
  13. description="Was the item purchased\
  14. as a gift for someone else? \
  15. Answer True if yes,\
  16. False if not or unknown.")
  17. delivery_days_schema = ResponseSchema(name="delivery_days",
  18. description="How many days\
  19. did it take for the product\
  20. to arrive? If this \
  21. information is not found,\
  22. output -1.")
  23. price_value_schema = ResponseSchema(name="price_value",
  24. description="Extract any\
  25. sentences about the value or \
  26. price, and output them as a \
  27. comma separated Python list.")
  28. response_schema = [gift_schema, delivery_days_schema, price_value_schema]
  29. out_parser = StructuredOutputParser.from_response_schemas(response_schema)
  30. format_instruction = out_parser.get_format_instructions()
  31. # print(format_instrucation)
  32. human_prompt = HumanMessagePromptTemplate.from_template(review_template_2)
  33. chat_prompt = ChatPromptTemplate.from_messages([human_prompt])
  34. message = chat_prompt.format_messages(
  35. text=customer_review, format_instructions=format_instruction)
  36. chat = ChatOpenAI(model_name="gpt-3.5-turbo")
  37. response = chat(message)
  38. print(type(response.content))
  39. result = out_parser.parse(response.content)
  40. print(result)
  41. print(result.get('delivery_days'))

  生成的format_instruction如下所示,可以看到实际是一个包含字段名称以及字段类型说明的json对象。

  执行完上面的整体代码,结果如下图所示,可以看到返回的是一个json对象,可以单独打印比如delivery_days的值。

  以上就是如何让输出的内容是JSON对象的实现说明。

  当使用 LangChain 中的储存(Memory)模块时,它可以帮助保存和管理历史聊天消息,以及构建关于特定实体的知识。这些组件可以跨多轮对话储存信息,并允许在对话期间跟踪特定信息和上下文。LangChain 提供了多种储存类型。其中,缓冲区储存允许保留最近的聊天消息,摘要储存则提供了对整个对话的摘要。实体储存 则允许在多轮对话中保留有关特定实体的信息。这些记忆组件都是模块化的,可与其他组件组合使用,从而增强机器人的对话管理能力。储存模块可以通过简单的API调用来访问和更新,允许开发人员更轻松地实现对话历史记录的管理和维护。接下来主要介绍四种类型的Memory,具体如下所示:

  • 对话缓存储存 (ConversationBufferMemory)
  • 对话缓存窗口储存 (ConversationBufferWindowMemory)
  • 对话令牌缓存储存 (ConversationTokenBufferMemory)
  • 对话摘要缓存储存 (ConversationSummaryBufferMemory)
  1. import openai
  2. import os
  3. from langchain.chains import ConversationChain
  4. from langchain.chat_models import ChatOpenAI
  5. from langchain.memory import ConversationBufferMemory
  6. from langchain.prompts import (
  7. SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, ChatMessagePromptTemplate)
  8. openai.api_key = os.environ.get("OPENAI_API_KEY")
  9. model = ChatOpenAI(model_name="gpt-3.5-turbo")
  10. memory = ConversationBufferMemory()
  11. chat = ConversationChain(llm=model, memory=memory, verbose=True)
  12. chat.predict(input="HI, my name is taoli?")
  13. chat.predict(input="What is 1+1?")
  14. chat.predict(input="what is my name?")
  15. print(memory.buffer)

上面的代码使用ConversationBufferMemory来记录整个历史对话,打印memory.buffer的值,结果如下所示:可以看到当第三个问题问AI,what is my name时,AI可以正确回答,应为Memory中存入了整个对话History信息。

  除了在ConversationChain中增加memory对象对memory赋值外,实际也可以对memory对象直接调用save_context()进行赋值,input就是human的输入,output模拟AI的返回。

  1. memoryTwo = ConversationBufferMemory()
  2. memoryTwo.save_context({'input': 'Hi I came from china'}, {
  3. 'output': 'china is beatiful'})
  4. memoryTwo.save_context({"input": "what is your name"}, {
  5. "output": "my name is chatbot"})
  6. print(memoryTwo.buffer)
  7. print(memoryTwo.load_memory_variables({}))

  打印的信息如下图所示,buffer中的信息和ConversionChain生成的一样,调用load_memory_variables({})方法,把整个对话信息连接起来,赋值给history变量。

  如果把所有的history都记录下来,那么每次传入给LLM的文字太多,token消耗很大,所以,langchain还提供了ConversationBufferWindowMemory,通过定义窗口大小,即K的值来控制记录最新的K轮对话历史信息。memory = ConversationBufferWindowMemory(k=1)。

  如果是有大量的文字描述内容,为了节省token以及保证单次对话的token不要超过限制,那么还可以使用ConversationSummaryBufferMemory,使用方式上和ConversationBufferMemory相同。以上就是对Langchain提供的memory的使用介绍。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/292201
推荐阅读
相关标签
  

闽ICP备14008679号