Last active
May 15, 2023 22:42
-
-
Save goldengrape/91f55fb0e06de86fbcb56eb54ce21923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
貌似顺序记录memory以后,各位军师就失去独立思考了,可能还是应该前5个独立并行,最后一个总结
要分两个链,一个是分开讨论时用的,分开讨论时,不记录memory,单独将之前的memory从prompt里装入。一个是总结陈词用的,要记录本轮的memory
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
�
Finished chain
臣诸葛亮以为:攻击曹操可能是一个明智的选择,但是必须综合各军师的意见,结合当下情势,作出最合适的决定,以及最有效的战略部署。对文若、子敬、仲达、元直及公瑾所提出的意见,都必须认真考虑,以便引起重新思考,以便让决策更为完善。总之,只有以精