-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More on LLM message history and system instruction (#240)
* Add examples for LLM message history and system instructions * Fix for when system_message is None * Ruff * Better (?) system prompt for RAG * Update system_instruction * Mypy * Mypy * Fix ollama test * Fix anthropic test * Fix cohere test * Fix vertexai test * Fix mistralai test * Fix graphrag test * Ruff * Mypy * Variable is not used * Ruff... * Mypy + e2e tests * Ruffffffff * CHANGELOG * Fix examples * Remove useless commented api_key from examples
- Loading branch information
Showing
30 changed files
with
381 additions
and
299 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
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,42 @@ | ||
"""This example illustrates the message_history feature | ||
of the LLMInterface by mocking a conversation between a user | ||
and an LLM about Tom Hanks. | ||
OpenAILLM can be replaced by any supported LLM from this package. | ||
""" | ||
|
||
from neo4j_graphrag.llm import LLMResponse, OpenAILLM | ||
|
||
# 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?", | ||
] | ||
|
||
history: list[dict[str, str]] = [] | ||
for question in questions: | ||
res: LLMResponse = llm.invoke( | ||
question, | ||
message_history=history, # type: ignore | ||
) | ||
history.append( | ||
{ | ||
"role": "user", | ||
"content": question, | ||
} | ||
) | ||
history.append( | ||
{ | ||
"role": "assistant", | ||
"content": res.content, | ||
} | ||
) | ||
|
||
print("#" * 50, question) | ||
print(res.content) | ||
print("#" * 50) |
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,22 @@ | ||
"""This example illustrates how to set system instructions for LLM. | ||
OpenAILLM can be replaced by any supported LLM from this package. | ||
""" | ||
|
||
from neo4j_graphrag.llm import LLMResponse, OpenAILLM | ||
|
||
# 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, | ||
) | ||
|
||
question = "How fast is Santa Claus during the Christmas eve?" | ||
|
||
res: LLMResponse = llm.invoke( | ||
question, | ||
system_instruction="Answer with a serious tone", | ||
) | ||
print(res.content) |
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
85 changes: 85 additions & 0 deletions
85
examples/question_answering/graphrag_with_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,85 @@ | ||
"""End to end example of building a RAG pipeline backed by a Neo4j database, | ||
simulating a chat with message history feature. | ||
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.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, | ||
) | ||
|
||
questions = [ | ||
"Who starred in the Apollo 13 movies?", | ||
"Who was its director?", | ||
"In which year was this movie released?", | ||
] | ||
|
||
history: list[dict[str, str]] = [] | ||
for question in questions: | ||
result = rag.search( | ||
question, | ||
return_context=False, | ||
message_history=history, # type: ignore | ||
) | ||
|
||
answer = result.answer | ||
print("#" * 50, question) | ||
print(answer) | ||
print("#" * 50) | ||
|
||
history.append( | ||
{ | ||
"role": "user", | ||
"content": question, | ||
} | ||
) | ||
history.append( | ||
{ | ||
"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
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
Oops, something went wrong.