-
Notifications
You must be signed in to change notification settings - Fork 108
Neo4j message history #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexthomas93
merged 18 commits into
neo4j:main
from
alexthomas93:feature/message-history
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eac9f09
Added message history classes
alexthomas93 0074b1a
Updated Neo4jMessageHistoryModel
alexthomas93 2bb29d2
Fixed spelling error
alexthomas93 9d5914e
Fixed tests
alexthomas93 05299a1
Added test_graphrag_happy_path_with_neo4j_message_history
alexthomas93 cda7962
Updated LLMs
alexthomas93 a2cd518
Added missing copyright headers
alexthomas93 f248dbe
Refactored graphrag
alexthomas93 72c7070
Added docstrings to message history classes
alexthomas93 0f3c6de
Added message history examples
alexthomas93 c3e671c
Updated docs
alexthomas93 15bfd3f
Updated CHANGELOG
alexthomas93 c9e3c27
Removed Neo4jMessageHistory __del__ method
alexthomas93 5a873dc
Makes the build_query and chat_summary_prompt methods in the GraphRAG…
alexthomas93 cfcf324
Added a threading lock to InMemoryMessageHistory
alexthomas93 6e8e10d
Removed node_label parameter from Neo4jMessageHistory
alexthomas93 f56ba20
Updated CLEAR_SESSION_QUERY
alexthomas93 e84230a
Fixed CLEAR_SESSION_QUERY
alexthomas93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
"""This example illustrates the message_history feature | ||
of the LLMInterface by mocking a conversation between a user | ||
and an LLM about Tom Hanks. | ||
|
||
Neo4j is used as the database for storing the message history. | ||
|
||
OpenAILLM can be replaced by any supported LLM from this package. | ||
""" | ||
|
||
import neo4j | ||
from neo4j_graphrag.llm import LLMResponse, OpenAILLM | ||
from neo4j_graphrag.message_history import Neo4jMessageHistory | ||
|
||
# Define database credentials | ||
URI = "neo4j+s://demo.neo4jlabs.com" | ||
AUTH = ("recommendations", "recommendations") | ||
DATABASE = "recommendations" | ||
INDEX = "moviePlotsEmbedding" | ||
|
||
# set api key here on in the OPENAI_API_KEY env var | ||
api_key = None | ||
|
||
llm = OpenAILLM(model_name="gpt-4o", api_key=api_key) | ||
|
||
questions = [ | ||
"What are some movies Tom Hanks starred in?", | ||
"Is he also a director?", | ||
"Wow, that's impressive. And what about his personal life, does he have children?", | ||
] | ||
|
||
driver = neo4j.GraphDatabase.driver( | ||
URI, | ||
auth=AUTH, | ||
database=DATABASE, | ||
) | ||
|
||
history = Neo4jMessageHistory(session_id="123", driver=driver, window=10) | ||
|
||
for question in questions: | ||
res: LLMResponse = llm.invoke( | ||
question, | ||
message_history=history, | ||
) | ||
history.add_message( | ||
{ | ||
"role": "user", | ||
"content": question, | ||
} | ||
) | ||
history.add_message( | ||
{ | ||
"role": "assistant", | ||
"content": res.content, | ||
} | ||
) | ||
|
||
print("#" * 50, question) | ||
print(res.content) | ||
print("#" * 50) |
87 changes: 87 additions & 0 deletions
87
examples/question_answering/graphrag_with_neo4j_message_history.py
This file contains hidden or 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,87 @@ | ||
"""End to end example of building a RAG pipeline backed by a Neo4j database, | ||
simulating a chat with message history which is also stored in Neo4j. | ||
|
||
Requires OPENAI_API_KEY to be in the env var. | ||
""" | ||
|
||
import neo4j | ||
from neo4j_graphrag.embeddings.openai import OpenAIEmbeddings | ||
from neo4j_graphrag.generation import GraphRAG | ||
from neo4j_graphrag.llm import OpenAILLM | ||
from neo4j_graphrag.message_history import Neo4jMessageHistory | ||
from neo4j_graphrag.retrievers import VectorCypherRetriever | ||
|
||
# Define database credentials | ||
URI = "neo4j+s://demo.neo4jlabs.com" | ||
AUTH = ("recommendations", "recommendations") | ||
DATABASE = "recommendations" | ||
INDEX = "moviePlotsEmbedding" | ||
|
||
|
||
driver = neo4j.GraphDatabase.driver( | ||
URI, | ||
auth=AUTH, | ||
) | ||
|
||
embedder = OpenAIEmbeddings() | ||
|
||
retriever = VectorCypherRetriever( | ||
driver, | ||
index_name=INDEX, | ||
retrieval_query=""" | ||
WITH node as movie, score | ||
CALL(movie) { | ||
MATCH (movie)<-[:ACTED_IN]-(p:Person) | ||
RETURN collect(p.name) as actors | ||
} | ||
CALL(movie) { | ||
MATCH (movie)<-[:DIRECTED]-(p:Person) | ||
RETURN collect(p.name) as directors | ||
} | ||
RETURN movie.title as title, movie.plot as plot, movie.year as year, actors, directors | ||
""", | ||
embedder=embedder, | ||
neo4j_database=DATABASE, | ||
) | ||
|
||
llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0}) | ||
|
||
rag = GraphRAG( | ||
retriever=retriever, | ||
llm=llm, | ||
) | ||
|
||
history = Neo4jMessageHistory(session_id="123", driver=driver, window=10) | ||
|
||
questions = [ | ||
"Who starred in the Apollo 13 movies?", | ||
"Who was its director?", | ||
"In which year was this movie released?", | ||
] | ||
|
||
for question in questions: | ||
result = rag.search( | ||
question, | ||
return_context=False, | ||
message_history=history, | ||
) | ||
|
||
answer = result.answer | ||
print("#" * 50, question) | ||
print(answer) | ||
print("#" * 50) | ||
|
||
history.add_message( | ||
{ | ||
"role": "user", | ||
"content": question, | ||
} | ||
) | ||
history.add_message( | ||
{ | ||
"role": "assistant", | ||
"content": answer, | ||
} | ||
) | ||
|
||
driver.close() |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.