Skip to content

Commit f3c5296

Browse files
committed
Added message history examples
1 parent 8ec3076 commit f3c5296

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ are listed in [the last section of this file](#customize).
5252

5353
- [End to end GraphRAG](./answer/graphrag.py)
5454
- [GraphRAG with message history](./question_answering/graphrag_with_message_history.py)
55-
55+
- [GraphRAG with Neo4j message history](./question_answering/graphrag_with_neo4j_message_history.py)
5656

5757
## Customize
5858

@@ -75,6 +75,7 @@ are listed in [the last section of this file](#customize).
7575
- [Custom LLM](./customize/llms/custom_llm.py)
7676

7777
- [Message history](./customize/llms/llm_with_message_history.py)
78+
- [Message history with Neo4j](./customize/llms/llm_with_neo4j_message_history.py)
7879
- [System Instruction](./customize/llms/llm_with_system_instructions.py)
7980

8081

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""This example illustrates the message_history feature
2+
of the LLMInterface by mocking a conversation between a user
3+
and an LLM about Tom Hanks.
4+
5+
Neo4j is used as the database for storing the message history.
6+
7+
OpenAILLM can be replaced by any supported LLM from this package.
8+
"""
9+
10+
import neo4j
11+
from neo4j_graphrag.llm import LLMResponse, OpenAILLM
12+
from neo4j_graphrag.message_history import Neo4jMessageHistory
13+
14+
# Define database credentials
15+
URI = "neo4j+s://demo.neo4jlabs.com"
16+
AUTH = ("recommendations", "recommendations")
17+
DATABASE = "recommendations"
18+
INDEX = "moviePlotsEmbedding"
19+
20+
# set api key here on in the OPENAI_API_KEY env var
21+
api_key = None
22+
23+
llm = OpenAILLM(model_name="gpt-4o", api_key=api_key)
24+
25+
questions = [
26+
"What are some movies Tom Hanks starred in?",
27+
"Is he also a director?",
28+
"Wow, that's impressive. And what about his personal life, does he have children?",
29+
]
30+
31+
driver = neo4j.GraphDatabase.driver(
32+
URI,
33+
auth=AUTH,
34+
database=DATABASE,
35+
)
36+
37+
history = Neo4jMessageHistory(
38+
session_id="123", driver=driver, node_label="Message", window=10
39+
)
40+
41+
for question in questions:
42+
res: LLMResponse = llm.invoke(
43+
question,
44+
message_history=history,
45+
)
46+
history.add_message(
47+
{
48+
"role": "user",
49+
"content": question,
50+
}
51+
)
52+
history.add_message(
53+
{
54+
"role": "assistant",
55+
"content": res.content,
56+
}
57+
)
58+
59+
print("#" * 50, question)
60+
print(res.content)
61+
print("#" * 50)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""End to end example of building a RAG pipeline backed by a Neo4j database,
2+
simulating a chat with message history which is also stored in Neo4j.
3+
4+
Requires OPENAI_API_KEY to be in the env var.
5+
"""
6+
7+
import neo4j
8+
from neo4j_graphrag.embeddings.openai import OpenAIEmbeddings
9+
from neo4j_graphrag.generation import GraphRAG
10+
from neo4j_graphrag.llm import OpenAILLM
11+
from neo4j_graphrag.message_history import Neo4jMessageHistory
12+
from neo4j_graphrag.retrievers import VectorCypherRetriever
13+
14+
# Define database credentials
15+
URI = "neo4j+s://demo.neo4jlabs.com"
16+
AUTH = ("recommendations", "recommendations")
17+
DATABASE = "recommendations"
18+
INDEX = "moviePlotsEmbedding"
19+
20+
21+
driver = neo4j.GraphDatabase.driver(
22+
URI,
23+
auth=AUTH,
24+
)
25+
26+
embedder = OpenAIEmbeddings()
27+
28+
retriever = VectorCypherRetriever(
29+
driver,
30+
index_name=INDEX,
31+
retrieval_query="""
32+
WITH node as movie, score
33+
CALL(movie) {
34+
MATCH (movie)<-[:ACTED_IN]-(p:Person)
35+
RETURN collect(p.name) as actors
36+
}
37+
CALL(movie) {
38+
MATCH (movie)<-[:DIRECTED]-(p:Person)
39+
RETURN collect(p.name) as directors
40+
}
41+
RETURN movie.title as title, movie.plot as plot, movie.year as year, actors, directors
42+
""",
43+
embedder=embedder,
44+
neo4j_database=DATABASE,
45+
)
46+
47+
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})
48+
49+
rag = GraphRAG(
50+
retriever=retriever,
51+
llm=llm,
52+
)
53+
54+
history = Neo4jMessageHistory(
55+
session_id="123", driver=driver, node_label="Message", window=10
56+
)
57+
58+
questions = [
59+
"Who starred in the Apollo 13 movies?",
60+
"Who was its director?",
61+
"In which year was this movie released?",
62+
]
63+
64+
for question in questions:
65+
result = rag.search(
66+
question,
67+
return_context=False,
68+
message_history=history,
69+
)
70+
71+
answer = result.answer
72+
print("#" * 50, question)
73+
print(answer)
74+
print("#" * 50)
75+
76+
history.add_message(
77+
{
78+
"role": "user",
79+
"content": question,
80+
}
81+
)
82+
history.add_message(
83+
{
84+
"role": "assistant",
85+
"content": answer,
86+
}
87+
)
88+
89+
driver.close()

0 commit comments

Comments
 (0)