Skip to content

Commit

Permalink
FalkorDB Multi Graph ability (#61)
Browse files Browse the repository at this point in the history
* multi-graph

* update-def-database

* update-app

* update-toml

* update-docs

---------

Co-authored-by: Gal Shubeli <gal@falkordb.com>
  • Loading branch information
galshubeli and galshubeli authored Oct 18, 2024
1 parent 9855cce commit b2331a2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ memary will default to the locally run models unless explicitly specified. Addit
4. Update user persona which can be found in `streamlit_app/data/user_persona.txt` using the user persona template which can be found in `streamlit_app/data/user_persona_template.txt`. Instructions have been provided - replace the curly brackets with relevant information.

5. [Optional] Update system persona, if needed, which can be found in `streamlit_app/data/system_persona.txt`.

6. Run:

6. [Optional] Multi Graphs - Users who are using FalkorDB can generate multiple graphs and switch between their IDs, which correspond to different agents. This enables seamless transitions and management of different agents' memory and knowledge contexts.

7. Run:

```
cd streamlit_app
Expand All @@ -115,6 +117,33 @@ chat_agent = ChatAgent(
```
Pass in subset of `['search', 'vision', 'locate', 'stocks']` as `include_from_defaults` for different set of default tools upon initialization.

### Multi-Graph
When using FalkorDB database, you can create multi-agents. Here is an example of how to set up personal agents for different users:

```python
# User A personal agent
chat_agent_user_a = ChatAgent(
"Personal Agent",
memory_stream_json_user_a,
entity_knowledge_store_json_user_a,
system_persona_txt_user_a,
user_persona_txt_user_a,
past_chat_json_user_a,
user_id='user_a_id'
)

# User B personal agent
chat_agent_user_b = ChatAgent(
"Personal Agent",
memory_stream_json_user_b,
entity_knowledge_store_json_user_b,
system_persona_txt_user_b,
user_persona_txt_user_b,
past_chat_json_user_b,
user_id='user_b_id'
)
```

### Adding Custom Tools
```python
def multiply(a: int, b: int) -> int:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies = [
"neo4j==5.17.0",
"python-dotenv==1.0.1",
"pyvis==0.3.2",
"streamlit==1.31.1",
"streamlit==1.38.0",
"llama-index==0.11.4",
"llama-index-agent-openai==0.3.0",
"llama-index-core==0.11.4",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FalkorDB==1.0.8
neo4j==5.17.0
python-dotenv==1.0.1
pyvis==0.3.2
streamlit==1.31.1
streamlit==1.38.0
llama-index==0.11.4
llama-index-agent-openai==0.3.0
llama-index-core==0.11.4
Expand Down
7 changes: 4 additions & 3 deletions src/memary/agent/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ class Agent(object):

def __init__(
self,
name,
agent_name,
memory_stream_json,
entity_knowledge_store_json,
system_persona_txt,
user_persona_txt,
past_chat_json,
user_id='falkor',
llm_model_name="llama3",
vision_model_name="llava",
include_from_defaults=["search", "locate", "vision", "stocks"],
debug=True,
):
load_dotenv()
self.name = name
self.name = agent_name
self.model = llm_model_name

googlemaps_api_key = os.getenv("GOOGLEMAPS_API_KEY")
Expand All @@ -83,7 +84,7 @@ def __init__(
if self.falkordb_url is not None:
from llama_index.graph_stores.falkordb import FalkorDBGraphStore
# initialize FalkorDB graph resources
self.graph_store = FalkorDBGraphStore(self.falkordb_url, decode_responses=True)
self.graph_store = FalkorDBGraphStore(self.falkordb_url, database=user_id, decode_responses=True)
else:
from llama_index.graph_stores.neo4j import Neo4jGraphStore
# Neo4j credentials
Expand Down
6 changes: 4 additions & 2 deletions src/memary/agent/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ class ChatAgent(Agent):

def __init__(
self,
name,
agent_name,
memory_stream_json,
entity_knowledge_store_json,
system_persona_txt,
user_persona_txt,
past_chat_json,
user_id='falkor',
llm_model_name="llama3",
vision_model_name="llava",
include_from_defaults=["search", "locate", "vision", "stocks"],
):
super().__init__(
name,
agent_name,
memory_stream_json,
entity_knowledge_store_json,
system_persona_txt,
user_persona_txt,
past_chat_json,
user_id,
llm_model_name,
vision_model_name,
include_from_defaults,
Expand Down
29 changes: 18 additions & 11 deletions streamlit_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@
from memary.agent.chat_agent import ChatAgent

load_dotenv()

system_persona_txt = "data/system_persona.txt"
user_persona_txt = "data/user_persona.txt"
past_chat_json = "data/past_chat.json"
memory_stream_json = "data/memory_stream.json"
entity_knowledge_store_json = "data/entity_knowledge_store.json"
chat_agent = ChatAgent(
"Personal Agent",
memory_stream_json,
entity_knowledge_store_json,
system_persona_txt,
user_persona_txt,
past_chat_json,
)


def create_graph(nodes, edges):
g = Network(
Expand Down Expand Up @@ -69,7 +59,7 @@ def fill_graph(nodes, edges, cypher_query):

if chat_agent.falkordb_url is not None:
falkordb = FalkorDB.from_url(chat_agent.falkordb_url)
session = falkordb.select_graph('falkor')
session = falkordb.select_graph(user_id)
result = session.query(cypher_query).result_set
for record in result:
path = record[0]
Expand Down Expand Up @@ -117,7 +107,23 @@ def get_models(llm_models, vision_models):
answer = ""
external_response = ""
st.title("memary")
# Create a two column layout (HTML table)
col1, col2 = st.columns([3, 1], vertical_alignment="bottom")

# Create a text input field for user ID
with col1:
user_id = st.text_input(
"Enter a user ID (Available only with FalkorDB)",
)
# Create a button to apply the switch user ID
with col2:
st.write("")
submit = st.button("Switch Agent ID")

# If the user ID is empty, set it to "falkor"
if len(user_id) == 0:
user_id = "falkor"

llm_models = ["gpt-3.5-turbo"]
vision_models = ["gpt-4-vision-preview"]
get_models(llm_models, vision_models)
Expand All @@ -143,6 +149,7 @@ def get_models(llm_models, vision_models):
system_persona_txt,
user_persona_txt,
past_chat_json,
user_id,
selected_llm_model,
selected_vision_model,
)
Expand Down

0 comments on commit b2331a2

Please sign in to comment.