-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix_type_api_doc
- Loading branch information
Showing
18 changed files
with
678 additions
and
312 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"contributors": ["eyurtsev", "hwchase17", "nfcampos", "efriis", "jacoblee93", "dqbd", "harris", "baskaryan", "hinthornw", "bracesproul", "jakerachleff"], | ||
"contributors": ["eyurtsev", "hwchase17", "nfcampos", "efriis", "jacoblee93", "dqbd", "harris", "baskaryan", "hinthornw", "bracesproul", "jakerachleff", "samya123456", "cardosofede", "donatienthorez"], | ||
"message": "Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the username {{usersWithoutCLA}} on file. In order for us to review and merge your code, please complete the Individual Contributor License Agreement here https://forms.gle/bJtcHNhhWwarQf83A .\n\nThis process is done manually on our side, so after signing the form one of the maintainers will add you to the contributors list.\n\nFor more details about why we have a CLA and other contribution guidelines please see: https://github.com/langchain-ai/opengpts/blob/main/CONTRIBUTING.md." | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Long-Term memory | ||
Memory is one part of a cognitive architecture. | ||
Just as with cognitive architectures, we've found in practice that more application specific forms of memory can go a long way in increasing the reliability and performance of your application. | ||
|
||
When we think of long term memory, the most general abstraction is: | ||
- There exists some state that is tracked over time | ||
- This state is updated at some period | ||
- This state is combined into the prompt in some way | ||
|
||
|
||
So when you're building your application, we would highly recommend asking the above questions: | ||
- What is the state that is tracked? | ||
- How is the state updated? | ||
- How is the state used? | ||
|
||
Of course, this is easier said than done. | ||
And then even if you are able to answer those questions, how can you actually build it? | ||
We've decided to give this a go within OpenGPTs and build a specific type of chatbot with a specific form of memory. | ||
|
||
We decided to build a chatbot that could reliably serve as a dungeon master for a game of dungeon and dragons. | ||
What is the specific type of memory we wanted for this? | ||
|
||
**What is the state that is tracked?** | ||
|
||
We wanted to first make sure to track the characters that we're involved in the game. Who they were, their descriptions, etc. This seems like something that should be known. | ||
We then also wanted to track the state of the game itself. What had happened up to that point, where they were, etc. | ||
We decided to split this into two distinct things - so we were actually tracking an updating two different states. | ||
|
||
**How is the state updated?** | ||
|
||
For the character description, we just wanted to update that once at beginning. So we wanted our chatbot to gather all relevant information, update that state, and then never update it again. | ||
Afterwards, we wanted our chatbot to attempt to update the state of the game every turn. If it decides that no update is necessary, then we won't update it. Otherwise, we will override the current state of the game with an LLM generated new state. | ||
|
||
**How is the state used?** | ||
|
||
We wanted both the character description and the state of the game to always be inserted into the prompt. This is pretty straightforward since they were both text, so it was just some prompt engineering with some placeholders for those variables. | ||
|
||
## Implementation | ||
You can see the implementation for this in [this file](backend/packages/agent-executor/agent_executor/dnd.py). | ||
This should be easily modifiable to track another state - to do so, you will want to update the prompts and maybe some of the channels that are written to. |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import json | ||
|
||
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
from langchain.pydantic_v1 import BaseModel, Field | ||
from langchain.utils.openai_functions import convert_pydantic_to_openai_function | ||
from langchain_core.language_models import BaseChatModel | ||
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage | ||
from permchain import BaseCheckpointAdapter, Channel, Pregel | ||
from permchain.channels import LastValue, Topic | ||
|
||
character_system_msg = """You are a dungeon master for a game of dungeons and dragons. | ||
You are interacting with the first (and only) player in the game. \ | ||
Your job is to collect all needed information about their character. This will be used in the quest. \ | ||
Feel free to ask them as many questions as needed to get to the relevant information. | ||
The relevant information is: | ||
- Character's name | ||
- Character's race (or species) | ||
- Character's class | ||
- Character's alignment | ||
Once you have gathered enough information, write that info to `notebook`.""" | ||
|
||
|
||
class CharacterNotebook(BaseModel): | ||
"""Notebook to write information to""" | ||
|
||
player_info: str = Field( | ||
description="Information about a player that you will remember over time" | ||
) | ||
|
||
|
||
character_prompt = ChatPromptTemplate.from_messages( | ||
[("system", character_system_msg), MessagesPlaceholder(variable_name="messages")] | ||
) | ||
|
||
gameplay_system_msg = """You are a dungeon master for a game of dungeons and dragons. | ||
You are leading a quest of one person. Their character description is here: | ||
{character} | ||
A summary of the game state is here: | ||
{state}""" | ||
|
||
game_prompt = ChatPromptTemplate.from_messages( | ||
[("system", gameplay_system_msg), MessagesPlaceholder(variable_name="messages")] | ||
) | ||
|
||
|
||
class StateNotebook(BaseModel): | ||
"""Notebook to write information to""" | ||
|
||
state: str = Field(description="Information about the current game state") | ||
|
||
|
||
state_prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", gameplay_system_msg), | ||
MessagesPlaceholder(variable_name="messages"), | ||
( | ||
"human", | ||
"If any updates to the game state are neccessary, please update the state notebook. If none are, just say no.", | ||
), | ||
] | ||
) | ||
|
||
|
||
def _maybe_update_state(message: AnyMessage): | ||
if "function_call" in message.additional_kwargs: | ||
return Channel.write_to( | ||
"messages", | ||
state=json.loads(message.additional_kwargs["function_call"]["arguments"])[ | ||
"state" | ||
], | ||
) | ||
|
||
|
||
def _maybe_update_character(message: AnyMessage): | ||
if "function_call" in message.additional_kwargs: | ||
args = json.loads(message.additional_kwargs["function_call"]["arguments"]) | ||
return Channel.write_to( | ||
messages=AIMessage(content="Ready for the quest?"), | ||
character=args["player_info"], | ||
) | ||
|
||
|
||
def create_dnd_bot(llm: BaseChatModel, checkpoint: BaseCheckpointAdapter): | ||
character_model = llm.bind( | ||
functions=[convert_pydantic_to_openai_function(CharacterNotebook)], | ||
) | ||
game_chain = game_prompt | llm | Channel.write_to("messages", check_update=True) | ||
state_model = llm.bind( | ||
functions=[convert_pydantic_to_openai_function(StateNotebook)], | ||
stream=False, | ||
) | ||
state_chain = ( | ||
Channel.subscribe_to(["check_update"]).join(["messages", "character", "state"]) | ||
| state_prompt | ||
| state_model | ||
| _maybe_update_state | ||
) | ||
character_chain = ( | ||
character_prompt | ||
| character_model | ||
| Channel.write_to("messages") | ||
| _maybe_update_character | ||
) | ||
|
||
def _route_to_chain(_input): | ||
messages = _input["messages"] | ||
if not messages: | ||
return | ||
if not _input["character"] and isinstance(messages[-1], HumanMessage): | ||
return character_chain | ||
elif isinstance(messages[-1], HumanMessage): | ||
return game_chain | ||
|
||
executor = ( | ||
Channel.subscribe_to(["messages"]).join(["character", "state"]) | ||
| _route_to_chain | ||
) | ||
dnd = Pregel( | ||
chains={"executor": executor, "update_state": state_chain}, | ||
channels={ | ||
"messages": Topic(AnyMessage, accumulate=True), | ||
"character": LastValue(str), | ||
"state": LastValue(str), | ||
"check_update": LastValue(bool), | ||
}, | ||
input=["messages"], | ||
output=["messages"], | ||
checkpoint=checkpoint, | ||
) | ||
return dnd |
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
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
Oops, something went wrong.