forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added message history classes * Updated Neo4jMessageHistoryModel * Fixed spelling error * Fixed tests * Added test_graphrag_happy_path_with_neo4j_message_history * Updated LLMs * Added missing copyright headers * Refactored graphrag * Added docstrings to message history classes * Added message history examples * Updated docs * Updated CHANGELOG * Removed Neo4jMessageHistory __del__ method * Makes the build_query and chat_summary_prompt methods in the GraphRAG class private * Added a threading lock to InMemoryMessageHistory * Removed node_label parameter from Neo4jMessageHistory * Updated CLEAR_SESSION_QUERY * Fixed CLEAR_SESSION_QUERY
- Loading branch information
1 parent
4ce3b56
commit b893584
Showing
23 changed files
with
908 additions
and
81 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
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,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 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 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.