使用LangChain和SmartLLMChain自我批评LLM链

2023年09月26日 由 alex 发表 393 0

问题:


If it takes one man three days to dig a hole of one cubic metres, how long will it take 30 men to dig 30 such holes?


对于我们人类说,显然这个问题答案是3天。


然而,当提出问题时gpt-3.5-turbo给出了错误的答案。如下图所示:


1


这个问题可以通过访问相同的LLM并按照一定的步骤进行解决,其中使用gpt-3.5-turbo模型来进行问题构思,批判发现并提出解决方案。


考虑下面定义 的代码chain,可以为链中的每个步骤定义不同的模型。ideation_llm、critique_llm和resolver_llm可以分别定义。


chain = SmartLLMChain(
    ideation_llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo"),0.9, model_name="gpt-3.5-turbo"),
    critique_llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo"),
    resolver_llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo"),
    prompt=prompt,
    n_ideas=3,
    verbose=True,
)


方便的是,不同的模型可以分配给三个步骤中的每一个;因此,特定的模型可以分配给构思、批判和解决步骤。


考虑到 SmartLLMChain 在相同的 gpt-3.5-turbo 模型下生成的正确而全面的回答。该改进后的回答在考虑了各种变量的情况下非常令人震惊。这个回答与在 OpenAI 游乐场中直接提问的回答形成鲜明对比。


这再次说明了LLMs的多才多艺和prompt工程的重要性。


以下是链的输出:


Step 3: The improved answer taking into account the modifier factor is as 
follows:
Let's assume that the rate of digging scales with the number of people, 
but not linearly. Let's introduce a modifier factor of K to account for 
this. This means that the rate of digging for N men can be represented as 
K * (1/3) cubic meters per day.
Now, if 30 men work together, their combined rate of digging can be 
calculated as 30 * (K * 1/3) = 10K cubic meters per day.
Since we want to dig 30 holes, we need to determine how many days it would 
take to dig those 30 holes at a rate of 10K cubic meters per day. To find 
this, we divide the total volume (30 cubic meters) by the rate of digging 
(10K cubic meters per day):
30 / (10K) = 3 / K
Therefore, it will take 30 men 3/K days to dig 30 holes of one cubic meter 
each, considering the modifier factor K.
Please note that without additional information or context, we cannot 
determine the specific value of the modifier factor K. It would depend on 
factors such as the efficiency, coordination, and skill level of the 
individuals involved.
Please output the full answer from Step 3.


分解SmartLLMChain的流程如下所示:


1. 根据下面图像,【1】创意阶段从LLM(低达最大值)获得一定数量的输出提案(创意)。


2. 然后,LLM【2】对所有创意进行评估,寻找可能的缺陷并选择最合适的建议。


3. 最后,【3】在解决阶段,LLM试图改进[2]评估阶段中的最佳创意。这里的输出构成了最终答案。


2


需要注意的是,SmartLLMChains使用多个LLM传递,这可能使其成为一项昂贵且延迟较高的链。


所提到的LLM需要具有聊天终端(在OpenAI的情况下),并且必须具备反射能力,而某些模型无法做到这一点。


以下是Colab笔记本的完整输出,请注意蓝色部分的思路部分,接着是黄色的批评部分。最后是绿色的解决方案部分。


3

4


以下是应用程序的最简单形式,您可以复制并粘贴到笔记本中。


您需要输入您自己的OpenAI API密钥。


pip install langchain
pip install openai
pip install langchain_experimental
import os
os.environ["OPENAI_API_KEY"] = "xxxxxxxxxxxxxxxx"
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain_experimental.smart_llm import SmartLLMChain
hard_question = "If it takes one man three days to dig a hole of one cubic metres, how long will it take 30 men to dig 30 such holes?"
prompt = PromptTemplate.from_template(hard_question)
llm = ChatOpenAI(temperature=0, model_name="gpt-4")
chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=3, verbose=True)
chain.run({})


代码的输出是:


> Entering new SmartLLMChain chain...
Prompt after formatting:
If it takes one man three days to dig a hole of one cubic metres, how long will it take 30 men to dig 30 such holes?
Idea 1:
Step 1: If one man can dig a hole of one cubic meter in three days, it means that the rate of digging for one man is 1/3 cubic meters per day.
Step 2: Now, we need to find the total rate of digging for 30 men. Since each man has a rate of 1/3 cubic meters per day, the total rate for 30 men would be 30 times the rate of one man.
Total rate for 30 men = 30 * (1/3) = 10 cubic meters per day.
Step 3: Finally, we need to find how long it will take 30 men to dig 30 holes, each of one cubic meter. Since the total rate of digging for 30 men is 10 cubic meters per day, we can divide the total number of holes (30) by the rate to find the time.
Time = Total number of holes / Total rate of digging
Time = 30 / 10 = 3 days.
Therefore, it will take 30 men 3 days to dig 30 holes, each of one cubic meter.
Idea 2:
Step 1: If one man can dig a hole of one cubic meter in three days, it means that the rate of digging for one man is 1/3 cubic meters per day.
Step 2: Now, we need to find the total rate of digging for 30 men. Since each man has a rate of 1/3 cubic meters per day, the total rate for 30 men would be 30 times the rate of one man.
Total rate for 30 men = 30 * (1/3) = 10 cubic meters per day.
Step 3: Finally, we need to find how long it will take 30 men to dig 30 holes, each of one cubic meter. Since the total rate of digging for 30 men is 10 cubic meters per day, we can divide the total number of holes (30) by the rate to find the time.
Time = Total number of holes / Total rate of digging
Time = 30 / 10 = 3 days.
Therefore, it will take 30 men 3 days to dig 30 holes, each of one cubic meter.
Idea 3:
Step 1: If one man can dig a hole of one cubic meter in three days, it means that the rate of digging for one man is 1/3 cubic meters per day.
Step 2: Now, we need to find the total rate of digging for 30 men. Since each man has a rate of 1/3 cubic meters per day, the total rate for 30 men would be 30 times the rate of one man.
Total rate for 30 men = 30 * (1/3) = 10 cubic meters per day.
Step 3: Finally, we need to find how long it will take 30 men to dig 30 holes, each of one cubic meter. Since the total rate of digging for 30 men is 10 cubic meters per day, we can divide the total number of holes (30) by the rate to find the time.
Time = Total number of holes / Total rate of digging
Time = 30 / 10 = 3 days.
Therefore, it will take 30 men 3 days to dig 30 holes, each of one cubic meter.
Critique:
Idea 1: 
Flaws and faulty logic:
1. The answer assumes that the rate of digging for one man remains constant regardless of the number of men working. However, in reality, the rate of digging may vary depending on factors such as coordination, efficiency, and physical limitations.
2. The answer assumes that the time taken to dig one hole is the same as the time taken to dig 30 holes. This may not be accurate as the efficiency of digging may decrease when multiple holes are being dug simultaneously.
3. The answer does not consider any potential limitations or constraints that may arise when multiple men are working together, such as space constraints or coordination issues.
Idea 2: 
Flaws and faulty logic:
1. The answer assumes that the rate of digging for one man remains constant regardless of the number of men working. However, in reality, the rate of digging may vary depending on factors such as coordination, efficiency, and physical limitations.
2. The answer assumes that the time taken to dig one hole is the same as the time taken to dig 30 holes. This may not be accurate as the efficiency of digging may decrease when multiple holes are being dug simultaneously.
3. The answer does not consider any potential limitations or constraints that may arise when multiple men are working together, such as space constraints or coordination issues.
Idea 3: 
Flaws and faulty logic:
1. The answer assumes that the rate of digging for one man remains constant regardless of the number of men working. However, in reality, the rate of digging may vary depending on factors such as coordination, efficiency, and physical limitations.
2. The answer assumes that the time taken to dig one hole is the same as the time taken to dig 30 holes. This may not be accurate as the efficiency of digging may decrease when multiple holes are being dug simultaneously.
3. The answer does not consider any potential limitations or constraints that may arise when multiple men are working together, such as space constraints or coordination issues.
In conclusion, all three answer options have flaws and faulty logic as they do not account for potential variations in the rate of digging and the efficiency of multiple men working together. They also overlook any limitations or constraints that may affect the overall time taken to dig the holes.
Resolution:
Step 1: Finding the best answer option chosen by the researcher.
Step 2: Improving the chosen answer option.
Step 3: Printing the full answer.
Answer: 
It seems that the researcher did not provide any indication of which answer option they considered to be the best. Therefore, we cannot determine the chosen answer option.
However, based on the flaws and faulty logic identified in all three answer options, it is necessary to provide an improved and accurate answer.
Improved Answer:
To determine the time it will take for 30 men to dig 30 holes, we need to consider the rate of digging and the efficiency of multiple men working together. 
Given that one man can dig a hole of one cubic meter in three days, we can calculate the rate of digging for one man as 1/3 cubic meters per day.
To find the total rate of digging for 30 men, we multiply the rate of one man by the number of men:
Total rate for 30 men = 30 * (1/3) = 10 cubic meters per day.
Now, we need to find the time it will take for 30 men to dig 30 holes, each of one cubic meter. Since the total rate of digging for 30 men is 10 cubic meters per day, we can divide the total number of holes (30) by the rate to find the time:
Time = Total number of holes / Total rate of digging
Time = 30 / 10 = 3 days.
Therefore, it will take 30 men 3 days to dig 30 holes, each of one cubic meter.
This answer takes into account the rate of digging, the efficiency of multiple men working together, and provides a clear and accurate calculation of the time required.
> Finished chain.
Step 1: Finding the best answer option chosen by the researcher.\n\nStep 2: Improving the chosen answer option.\n\nStep 3: Printing the full answer.\n\nAnswer: \n\nIt seems that the researcher did not provide any indication of which answer option they considered to be the best. Therefore, we cannot determine the chosen answer option.\n\nHowever, based on the flaws and faulty logic identified in all three answer options, it is necessary to provide an improved and accurate answer.\n\nImproved Answer:\n\nTo determine the time it will take for 30 men to dig 30 holes, we need to consider the rate of digging and the efficiency of multiple men working together. \n\nGiven that one man can dig a hole of one cubic meter in three days, we can calculate the rate of digging for one man as 1/3 cubic meters per day.\n\nTo find the total rate of digging for 30 men, we multiply the rate of one man by the number of men:\n\nTotal rate for 30 men = 30 * (1/3) = 10 cubic meters per day.\n\nNow, we need to find the time it will take for 30 men to dig 30 holes, each of one cubic meter. Since the total rate of digging for 30 men is 10 cubic meters per day, we can divide the total number of holes (30) by the rate to find the time:\n\nTime = Total number of holes / Total rate of digging\nTime = 30 / 10 = 3 days.\n\nTherefore, it will take 30 men 3 days to dig 30 holes, each of one cubic meter.\n\nThis answer takes into account the rate of digging, the efficiency of multiple men working together, and provides a clear and accurate calculation of the time required.


正如本文前面所提到的,每个步骤的链条可以定义不同的模型。


chain = SmartLLMChain(
    ideation_llm=ChatOpenAI(temperature=0.9, model_name="gpt-4"),0.9, model_name="gpt-4"),
    critique_llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo-16k-0613"),
    resolver_llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo"),
    prompt=prompt,
    n_ideas=3,
    verbose=True,
)


而且链条可以再次运行。


chain.run({})


输出:以上文本


at the same rate as the one man. However, it is possible that the efficiency or skill level of the additional men may differ, resulting in a different overall rate of digging.
Idea 2:
- The answer assumes that the rate of digging remains the same when 30 men work together. However, as mentioned before, the rate of digging may not scale linearly with the number of people, and there could be diminishing returns or coordination issues.
- The answer divides the total volume (30 cubic meters) by the rate of digging (10 cubic meters per day) to determine the number of days. This assumes that the 30 men will work continuously without any breaks or variations in their rate. In reality, there could be limitations or variations in their working hours or productivity.
Idea 3:
- The answer assumes that the rate of digging remains constant regardless of the number of people involved, similar to the first idea. Again, this may not reflect reality.
- The answer states that 30 men can dig 30 times the amount that one man can dig in one day. However, this assumes that the efficiency or skill level of the additional men is the same as the one man. As mentioned before, there could be variations in productivity among the individuals, leading to a different overall rate of digging.
In summary, all three answer options make assumptions about the rate of digging and the efficiency of the people involved without considering potential variations or limitations in real-life scenarios. Therefore, they may not accurately determine the time required for 30 men to dig 30 holes.
Resolution:
Step 1: The researcher thought that Idea 3 was the best answer option.
Step 2: To improve Idea 3, we need to consider the potential variations in the rate of digging when multiple people are involved. We can account for this by assuming that the rate of digging scales with the number of people, but not necessarily linearly. We can introduce a modifier factor to reflect this.
Step 3: The improved answer taking into account the modifier factor is as follows:
Let's assume that the rate of digging scales with the number of people, but not linearly. Let's introduce a modifier factor of K to account for this. This means that the rate of digging for N men can be represented as K * (1/3) cubic meters per day.
Now, if 30 men work together, their combined rate of digging can be calculated as 30 * (K * 1/3) = 10K cubic meters per day.
Since we want to dig 30 holes, we need to determine how many days it would take to dig those 30 holes at a rate of 10K cubic meters per day. To find this, we divide the total volume (30 cubic meters) by the rate of digging (10K cubic meters per day):
30 / (10K) = 3 / K
Therefore, it will take 30 men 3/K days to dig 30 holes of one cubic meter each, considering the modifier factor K.
Please note that without additional information or context, we cannot determine the specific value of the modifier factor K. It would depend on factors such as the efficiency, coordination, and skill level of the individuals involved.
Please output the full answer from Step 3.
> Finished chain.
Step 1: The researcher thought that Idea 3 was the best answer option.\n\nStep 2: To improve Idea 3, we need to consider the potential variations in the rate of digging when multiple people are involved. We can account for this by assuming that the rate of digging scales with the number of people, but not necessarily linearly. We can introduce a modifier factor to reflect this.\n\nStep 3: The improved answer taking into account the modifier factor is as follows:\n\nLet's assume that the rate of digging scales with the number of people, but not linearly. Let's introduce a modifier factor of K to account for this. This means that the rate of digging for N men can be represented as K * (1/3) cubic meters per day.\n\nNow, if 30 men work together, their combined rate of digging can be calculated as 30 * (K * 1/3) = 10K cubic meters per day.\n\nSince we want to dig 30 holes, we need to determine how many days it would take to dig those 30 holes at a rate of 10K cubic meters per day. To find this, we divide the total volume (30 cubic meters) by the rate of digging (10K cubic meters per day):\n\n30 / (10K) = 3 / K\n\nTherefore, it will take 30 men 3/K days to dig 30 holes of one cubic meter each, considering the modifier factor K.\n\nPlease note that without additional information or context, we cannot determine the specific value of the modifier factor K. It would depend on factors such as the efficiency, coordination, and skill level of the individuals involved.\n\nPlease output the full answer from Step 3.


文章来源:https://medium.com/@cobusgreyling/self-critique-llm-chain-using-langchain-smartllmchain-d67c42a4fa83
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
写评论取消
回复取消