实现程序辅助语言模型,参考 https://arxiv.org/pdf/2211.10435.pdf%E3%80%82
# 导入 PALChain 类
from langchain_experimental.pal_chain import PALChain
# 导入 OpenAI 类
from langchain_openai import OpenAI
# 创建OpenAI对象,并设置temperature为0,max_tokens为512
llm = OpenAI(temperature=0, max_tokens=512)
# 从数学提示创建PALChain对象,并打印详细信息
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
question = "Jan有Marcia的宠物数量的三倍。Marcia比Cindy多两只宠物。如果Cindy有四只宠物,这三个人总共有多少只宠物?"
# 运行 pal_chain 对象的 run 方法,并传入 question 参数
pal_chain.run(question)
> Entering new PALChain chain... def solution(): """Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?""" cindy_pets = 4 marcia_pets = cindy_pets + 2 jan_pets = marcia_pets * 3 total_pets = cindy_pets + marcia_pets + jan_pets result = total_pets return result > Finished chain.
'28'
# 创建一个PALChain对象,并从colored_object_prompt函数中获取输入参数llm,同时打开verbose模式
pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)
# 定义问题字符串
question = "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"
# 运行 pal_chain 的 run 方法,并传入 question 参数
pal_chain.run(question)
> Entering new PALChain chain... # Put objects into a list to record ordering objects = [] objects += [('booklet', 'blue')] * 2 objects += [('booklet', 'purple')] * 2 objects += [('sunglasses', 'yellow')] * 2 # Remove all pairs of sunglasses objects = [object for object in objects if object[0] != 'sunglasses'] # Count number of purple objects num_purple = len([object for object in objects if object[1] == 'purple']) answer = num_purple > Finished PALChain chain.
'2'
您还可以使用中间步骤标志来返回生成答案的执行代码。
# 从彩色对象提示创建PALChain对象
pal_chain = PALChain.from_colored_object_prompt(
llm, verbose=True, return_intermediate_steps=True
)
# 定义问题字符串
question = "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"
# 调用pal_chain函数,并传入一个字典作为参数
result = pal_chain({"question": question})
> Entering new PALChain chain... # Put objects into a list to record ordering objects = [] objects += [('booklet', 'blue')] * 2 objects += [('booklet', 'purple')] * 2 objects += [('sunglasses', 'yellow')] * 2 # Remove all pairs of sunglasses objects = [object for object in objects if object[0] != 'sunglasses'] # Count number of purple objects num_purple = len([object for object in objects if object[1] == 'purple']) answer = num_purple > Finished chain.
# 获取 result 字典中的 "intermediate_steps" 键对应的值
result["intermediate_steps"]
"# Put objects into a list to record ordering\nobjects = []\nobjects += [('booklet', 'blue')] * 2\nobjects += [('booklet', 'purple')] * 2\nobjects += [('sunglasses', 'yellow')] * 2\n\n# Remove all pairs of sunglasses\nobjects = [object for object in objects if object[0] != 'sunglasses']\n\n# Count number of purple objects\nnum_purple = len([object for object in objects if object[1] == 'purple'])\nanswer = num_purple"