-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent short & long term memory with langgraph. (#851)
* draft a demo code for memory. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add agent short-term memory with langgraph checkpoint. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add save long-term memory func. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add save long-term memory func. * add timeout for llm response. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix ut with adding -e HABANA_VISIBLE_DEVICES=all. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
24b9f03
commit e39b08f
Showing
6 changed files
with
120 additions
and
20 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
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,68 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import json | ||
import uuid | ||
from datetime import datetime | ||
from typing import List, Optional | ||
|
||
from langchain_core.runnables import RunnableConfig | ||
from langgraph.checkpoint.memory import MemorySaver | ||
from langgraph.graph import StateGraph | ||
from langgraph.store.memory import InMemoryStore | ||
from pydantic import BaseModel | ||
|
||
|
||
class PersistenceConfig(BaseModel): | ||
checkpointer: bool = False | ||
store: bool = False | ||
|
||
|
||
class PersistenceInfo(BaseModel): | ||
user_id: str = None | ||
thread_id: str = None | ||
started_at: datetime | ||
|
||
|
||
class AgentPersistence: | ||
def __init__(self, config: PersistenceConfig): | ||
# for short-term memory | ||
self.checkpointer = None | ||
# for long-term memory | ||
self.store = None | ||
self.config = config | ||
print(f"Initializing AgentPersistence: {config}") | ||
self.initialize() | ||
|
||
def initialize(self) -> None: | ||
if self.config.checkpointer: | ||
self.checkpointer = MemorySaver() | ||
if self.config.store: | ||
self.store = InMemoryStore() | ||
|
||
def save( | ||
self, | ||
config: RunnableConfig, | ||
content: str, | ||
context: str, | ||
memory_id: Optional[str] = None, | ||
): | ||
"""This function is only for long-term memory.""" | ||
mem_id = memory_id or uuid.uuid4() | ||
user_id = config["configurable"]["user_id"] | ||
self.store.put( | ||
("memories", user_id), | ||
key=str(mem_id), | ||
value={"content": content, "context": context}, | ||
) | ||
return f"Stored memory {content}" | ||
|
||
def get(self, config: RunnableConfig): | ||
"""This function is only for long-term memory.""" | ||
user_id = config["configurable"]["user_id"] | ||
namespace = ("memories", user_id) | ||
memories = self.store.search(namespace) | ||
return memories | ||
|
||
def update_state(self, config, graph: StateGraph): | ||
pass |
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