当使用思维链提示时,这个过程需要手工制作有效且多样化的例子。这种手动工作可能会导致次优解决方案。Zhang et al. (2022)(opens in a new tab) 提出了一种消除人工的方法,即利用 LLMs “让我们一步一步地思考” 提示来生成一个接一个的推理链。这种自动过程仍然可能在生成的链中出现错误。为了减轻错误的影响,演示的多样性很重要。这项工作提出了Auto-CoT,它对具有多样性的问题进行采样,并生成推理链来构建演示。
import bs4 from langchain import hub from langchain_community.document_loaders import WebBaseLoader from langchain_core.documents import Document from langchain_core.vectorstores import InMemoryVectorStore from langchain_text_splitters import RecursiveCharacterTextSplitter from langgraph.graph import START, StateGraph from typing_extensions import Annotated, List, TypedDict
# Load and chunk contents of the blog loader = WebBaseLoader( web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",), bs_kwargs=dict( parse_only=bs4.SoupStrainer( class_=("post-content", "post-title", "post-header") ) ), ) docs = loader.load()
for i, document inenumerate(all_splits): if i < third: document.metadata["section"] = "beginning" elif i < 2 * third: document.metadata["section"] = "middle" else: document.metadata["section"] = "end"
# Index chunks vector_store = InMemoryVectorStore(embeddings) _ = vector_store.add_documents(all_splits)
# Define schema for search classSearch(TypedDict): """Search query."""
query: Annotated[str, ..., "Search query to run."] section: Annotated[ Literal["beginning", "middle", "end"], ..., "Section to query.", ]
# Define prompt for question-answering prompt = hub.pull("rlm/rag-prompt")
# Define state for application classState(TypedDict): question: str query: Search context: List[Document] answer: str
for step in graph.stream( {"question": "What does the end of the post say about Task Decomposition?"}, stream_mode="updates", ): print(f"{step}\n\n----------------\n")
{'retrieve': {'context': [Document(id='d6cef137-e1e8-4ddc-91dc-b62bd33c6020', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 39221, 'section': 'end'}, page_content='Finite context length: The restricted context capacity limits the inclusion of historical information, detailed instructions, API call context, and responses. The design of the system has to work with this limited communication bandwidth, while mechanisms like self-reflection to learn from past mistakes would benefit a lot from long or infinite context windows. Although vector stores and retrieval can provide access to a larger knowledge pool, their representation power is not as powerful as full attention.\n\n\nChallenges in long-term planning and task decomposition: Planning over a lengthy history and effectively exploring the solution space remain challenging. LLMs struggle to adjust plans when faced with unexpected errors, making them less robust compared to humans who learn from trial and error.'), Document(id='d1834ae1-eb6a-43d7-a023-08dfa5028799', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 39086, 'section': 'end'}, page_content='}\n]\nChallenges#\nAfter going through key ideas and demos of building LLM-centered agents, I start to see a couple common limitations:'), Document(id='ca7f06e4-2c2e-4788-9a81-2418d82213d9', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 32942, 'section': 'end'}, page_content='}\n]\nThen after these clarification, the agent moved into the code writing mode with a different system message.\nSystem message:'), Document(id='1fcc2736-30f4-4ef6-90f2-c64af92118cb', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 35127, 'section': 'end'}, page_content='"content": "You will get instructions for code to write.\\nYou will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code.\\nMake sure that every detail of the architecture is, in the end, implemented as code.\\n\\nThink step by step and reason yourself to the right decisions to make sure we get it right.\\nYou will first lay out the names of the core classes, functions, methods that will be necessary, as well as a quick comment on their purpose.\\n\\nThen you will output the content of each file including ALL code.\\nEach file must strictly follow a markdown code block format, where the following tokens must be replaced such that\\nFILENAME is the lowercase file name including the file extension,\\nLANG is the markup code block language for the code\'s language, and CODE is the code:\\n\\nFILENAME\\n\`\`\`LANG\\nCODE\\n\`\`\`\\n\\nYou will start with the \\"entrypoint\\" file, then go to the ones that are imported by that file, and so on.\\nPlease')]}}
----------------
{'generate': {'answer': 'The end of the post highlights that task decomposition faces challenges in long-term planning and adapting to unexpected errors. LLMs struggle with adjusting their plans, making them less robust compared to humans who learn from trial and error. This indicates a limitation in effectively exploring the solution space and handling complex tasks.'}}
思维链(CoT)方法依赖于一组固定的人工注释范例。问题在于,这些范例可能不是不同任务的最有效示例。为了解决这个问题,Diao 等人(2023)(opens in a new tab)最近提出了一种新的提示方法,称为 Active-Prompt,以适应 LLMs 到不同的任务特定示例提示(用人类设计的 CoT 推理进行注释)。
下面是该方法的说明。第一步是使用或不使用少量 CoT 示例查询 LLM。对一组训练问题生成 k 个可能的答案。基于 k 个答案计算不确定度度量(使用不一致性)。选择最不确定的问题由人类进行注释。然后使用新的注释范例来推断每个问题。
具体来说,我们有兴趣创建一个功能,允许使用 LLM 回答需要日期理解的问题。我们将为 LLM 提供一个提示,其中包括一些示例,这些示例是从这里(opens in a new tab)采用的。
这是我们需要导入的包:
import openai from datetime import datetime from dateutil.relativedelta import relativedelta import os from langchain.llms import OpenAI from dotenv import load_dotenv
配置环境:
load_dotenv() # API configuration openai.api_key = os.getenv("OPENAI_API_KEY") # for LangChain os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?" DATE_UNDERSTANDING_PROMPT = """ # Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY? # If 2015 is coming in 36 hours, then today is 36 hours before. today = datetime(2015, 1, 1) - relativedelta(hours=36) # One week from today, one_week_from_today = today + relativedelta(weeks=1) # The answer formatted with %m/%d/%Y is one_week_from_today.strftime('%m/%d/%Y') # Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY? # If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later. today = datetime(2019, 1, 1) + relativedelta(days=6) # The answer formatted with %m/%d/%Y is today.strftime('%m/%d/%Y') # Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY? # If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later. today = datetime(1943, 6, 1) + relativedelta(days=1) # 10 days ago, ten_days_ago = today - relativedelta(days=10) # The answer formatted with %m/%d/%Y is ten_days_ago.strftime('%m/%d/%Y') # Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY? # It is 4/19/1969 today. today = datetime(1969, 4, 19) # 24 hours later, later = today + relativedelta(hours=24) # The answer formatted with %m/%d/%Y is today.strftime('%m/%d/%Y') # Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY? # If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/12/2002. today = datetime(2002, 3, 12) # 24 hours later, later = today + relativedelta(hours=24) # The answer formatted with %m/%d/%Y is later.strftime('%m/%d/%Y') # Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY? # If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later. today = datetime(2001, 2, 28) + relativedelta(years=16) # Yesterday, yesterday = today - relativedelta(days=1) # The answer formatted with %m/%d/%Y is yesterday.strftime('%m/%d/%Y') # Q: {question} """.strip() + '\n'
# If today is 27 February 2023 and I was born exactly 25 years ago, then I was born 25 years before. today = datetime(2023, 2, 27) # I was born 25 years before, born = today - relativedelta(years=25) # The answer formatted with %m/%d/%Y is born.strftime('%m/%d/%Y')