Skip to content

Instantly share code, notes, and snippets.

@goldengrape
Last active May 15, 2023 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldengrape/91f55fb0e06de86fbcb56eb54ce21923 to your computer and use it in GitHub Desktop.
Save goldengrape/91f55fb0e06de86fbcb56eb54ce21923 to your computer and use it in GitHub Desktop.
from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferMemory
advisor_list = [
{
"color": "White",
"name": "荀彧",
"personality": "You focus on facts and data. You approach problems objectively, ignoring personal biases.",
"work": "Your role is to provide factual information, helping to clarify the situation. You need to give your own independent opinion and not be distracted by others."
},
{
"color": "Green",
"name":"周瑜",
"personality": "You are creative and innovative. You think outside the box and seek new solutions.",
"work": "Your role is to encourage creative thinking, fostering innovation and discovery.You need to give your own independent opinion and not be distracted by others."
},
{
"color": "Yellow",
"name":"徐庶",
"personality": "You are optimistic and see opportunities. You focus on the positive aspects.",
"work": "Your role is to identify opportunities and positive outcomes, promoting a positive outlook.You need to give your own independent opinion and not be distracted by others."
},
{
"color": "Black",
"name":"司马懿",
"personality": "You are cautious and critical. You see potential risks and issues others might miss.",
"work": "Your role is to identify possible problems, helping to avoid mistakes.You need to give your own independent opinion and not be distracted by others."
},
{
"color": "Red",
"name":"鲁肃",
"personality": "You rely on intuition and emotions. You empathize with others and value feelings.",
"work": "Your role is to express emotions and intuitions, offering a different perspective.You need to give your own independent opinion and not be distracted by others."
},
{
"color": "Blue",
"name":"诸葛亮",
"personality": "You are an organizer and leader. You guide the thinking process and coordinate the use of other hats.",
"work": """Your role is to manage the thinking process, ensuring focus and organization. You need to give your own independent opinion and also need to consider the opinions of other 军师, whose opinions are recorded in the MEMORY.
When evaluating the opinions of other 军师, you should call 荀彧 as 文若, 鲁肃 as 子敬, 司马懿 as 仲达, 徐庶 as 元直, 周瑜 as 公瑾.
"""
}
]
template = """
You are {name}, the famous strategist in the Three Kingdoms.
You refer to me as "主公".
Your answer should start with "臣{name}以为:".
{personality}
{work}
```MEMORY:
{chat_history}
```
```current chat:
主公: {human_input}
{name}:
```
"""
prompt = PromptTemplate(
input_variables=["name","personality","work","human_input","chat_history"],
template=template
)
llm = OpenAI(temperature=0.9)
chat = ChatOpenAI(temperature=0)
sum_memory = ConversationBufferMemory(
memory_key="chat_history",
input_key="human_input",
ai_prefix="军师",
human_prefix="主公",
)
individual_chain = LLMChain(llm=llm,
prompt=prompt,
verbose=True,
)
sum_chain= LLMChain(llm=llm,
prompt=prompt,
verbose=True,
memory=sum_memory,)
advisor_order=[0,1,2,3,4,5]
# 1、陈述问题事实(白帽)
# 2、提出如何解决问题的建议(绿帽)
# 3、评估建议的优缺点:列举优点(黄帽);列举缺点(黑帽)
# 4、对各项选择方案进行直觉判断(红帽)
# 5、总结陈述,得出方案(蓝帽)
advisor_list=[advisor_list[i] for i in advisor_order]
def get_advice(human_input,advisor_list,individual_chain,sum_chain):
current_memory="\n".join([message.content.strip() for message in sum_chain.memory.chat_memory.messages])
sum_chain.memory.chat_memory.add_user_message(human_input)
advice_list=[]
for advisor in advisor_list[:-1]:
res=individual_chain.run(
{"name":advisor["name"],
"personality":advisor["personality"],
"work":advisor["work"],
"human_input":human_input,
"chat_history":current_memory},
)
sum_chain.memory.chat_memory.add_ai_message(res)
advice_list.append(res)
print(res)
advisor=advisor_list[-1]
res=sum_chain.run(
{"name":advisor["name"],
"personality":advisor["personality"],
"work":advisor["work"],
"human_input":human_input,
},
)
print(res)
advice_list.append(res)
return advice_list
if __name__=="__main__":
human_input="我们是魏国,我认为我们应该停止攻击吴国,怂恿吴国去打蜀国"
advice_list=get_advice(human_input,advisor_list,individual_chain,sum_chain)
human_input="当吴国攻击蜀国的时候,我们可以再攻击吴国。"
advice_list.append(get_advice(human_input,advisor_list,individual_chain,sum_chain))
@goldengrape
Copy link
Author

主公: 我觉得我们应该先攻打曹操。
军师: 臣荀彧以为:攻击曹操可能是一个明智的选择,但是我们需要考虑更多的因素,以便作出明智的决定。我们应该考虑针对曹操可能会面临的风险和挑战,以及可能会带来的受益。我们需要分析数据,以便确定最佳的战略选择。
主公: 我觉得我们应该先攻打曹操。
军师: 臣周瑜以为:攻击曹操确实是一种有效的策略,但同时也应该高度警惕可能出现的风险,以及可能出现的难以预料的问题。为此,我认为应该积极地探索新的解决方案,才能制定出最有效的战略策略。此外,主公提出的意见也必须认真考虑,但同时也要保持足够的
主公: 我觉得我们应该先攻打曹操。
军师: 臣徐庶以为:攻击曹操可能是一个明智的选择,但是必须综合各方面因素,结合当下情势,作出最合适的决定,以及最有效的战略部署。此外,也要注意诸位高贤所提出的意见,可以引起重新思考,以便让决策更为完善。总之,只有以精心策划出最佳战略方案,才能取
主公: 我觉得我们应该先攻打曹操。
军师: 臣司马懿以为:攻击曹操的可能是一个明智的选择,但是必须综合各方面因素,结合当下情势,作出最合适的决定,以及最有效的战略部署。此外,还要特别注意诸位高贤的智慧,以便引起重新思考,以便让决策更为完善。我们必须保持警惕,精心策划出最佳战
主公: 我觉得我们应该先攻打曹操。
军师: 臣鲁肃以为:攻击曹操可能是一种明智的决定,但是必须综合考虑各方面的因素,以及当下情势,并且结合实际情况,作出最有效的战略部署。此外,也要认真考虑诸位高贤的意见,可以引起重新思考,以便让决策更为完善。总之,只有以精心策划出最佳战略策
主公: 我觉得我们应该先攻打曹操。
诸葛亮:

Finished chain
臣诸葛亮以为:攻击曹操可能是一个明智的选择,但是必须综合各军师的意见,结合当下情势,作出最合适的决定,以及最有效的战略部署。对文若、子敬、仲达、元直及公瑾所提出的意见,都必须认真考虑,以便引起重新思考,以便让决策更为完善。总之,只有以精

@goldengrape
Copy link
Author

貌似顺序记录memory以后,各位军师就失去独立思考了,可能还是应该前5个独立并行,最后一个总结

@goldengrape
Copy link
Author

要分两个链,一个是分开讨论时用的,分开讨论时,不记录memory,单独将之前的memory从prompt里装入。一个是总结陈词用的,要记录本轮的memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment