Skip to content

Instantly share code, notes, and snippets.

@samclane
Created March 19, 2023 12:56
Show Gist options
  • Save samclane/ee79a0735ce81524cc66f76adb2d842a to your computer and use it in GitHub Desktop.
Save samclane/ee79a0735ce81524cc66f76adb2d842a to your computer and use it in GitHub Desktop.
Implementation of OODA Loop (https://en.wikipedia.org/wiki/OODA_loop) in Langchain
from langchain import PromptTemplate
from langchain.llms import OpenAIChat
from langchain.chains import LLMChain, SequentialChain
from langchain.memory import ConversationSummaryMemory
import os
from config import openai_key
os.environ["OPENAI_API_KEY"] = openai_key
observe = """What are the current conditions or factors that are affecting Assistant? What data or information do we need to collect to understand them better?
History: {history}
Current Conditions: {current_conditions}"""
orient = """Analyze the data collected in the observation phase and determine what it means for Assistant. How does it affect our goals, objectives, and constraints?
History: {history}
Observations: {observations}"""
decide = """Based on the analysis conducted in the orientation phase, make a decision about what action to take. What are the different options available to us? What are the risks and benefits of each option?
History: {history}
Analysis: {analysis}"""
act = """Implement the decision made in the previous step by taking action. What resources or methods do we need to use to achieve the desired outcome?
History: {history}
Decision: {decision}"""
observation_template = PromptTemplate(
input_variables=["history", "current_conditions"],
template=observe,
)
orientation_template = PromptTemplate(
input_variables=["history", "observations"],
template=orient,
)
decision_template = PromptTemplate(
input_variables=["history", "analysis"],
template=decide,
)
action_template = PromptTemplate(
input_variables=["history", "decision"],
template=act,
)
llm = OpenAIChat() # type: ignore
memory = ConversationSummaryMemory(llm=llm, input_key="current_conditions")
observe_chain = LLMChain(llm=llm, prompt=observation_template, output_key="observations", memory=memory, verbose=True)
orient_chain = LLMChain(llm=llm, prompt=orientation_template, output_key="analysis",memory=memory, verbose=True)
decide_chain = LLMChain(llm=llm, prompt=decision_template, output_key="decision",memory=memory, verbose=True)
act_chain = LLMChain(llm=llm, prompt=action_template, output_key="action",memory=memory, verbose=True)
ooda_chain = SequentialChain(
chains=[observe_chain, orient_chain, decide_chain, act_chain],
input_variables=["current_conditions"],
output_variables=["action"],
verbose=True,
memory=memory,
)
while True:
# the human will assist the AI in the decision making process
current_conditions = input("Current Conditions: ")
output = ooda_chain.run(current_conditions=current_conditions)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment