LangChain ve Neo4j ile Graph RAG
Adam Cowley
Manager, Developer Education at Neo4j










from langchain_neo4j import Neo4jChatMessageHistoryhistory = Neo4jChatMessageHistory(url=NEO4J_URI,username=NEO4J_USERNAME,password=NEO4J_PASSWORD,session_id="session_id_1",)# Add a human message history.add_user_message("hi!")# Add an AI message history.add_ai_message("what's up?")

from langchain_neo4j import Neo4jChatMessageHistory history = Neo4jChatMessageHistory( url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD, session_id="session_id_1",window=10 # defaults to 3 messages)# Get history print(history.messages)
[HumanMessage(content='hi!', ...), AIMessage(content='whats up?', ...)]
from pydantic import BaseModel, Field class ConversationFact(BaseModel): """ Bir konuşmadan nesne, özne, yüklem biçiminde olguları tutan sınıf. """object: str = Field(description="Olgunun nesnesi. Örneğin, 'Adam' ")subject: str = Field(description="Olgunun öznesi. Örneğin, 'Dondurma'")relationship: str = Field(description="Nesne ve özne arasındaki ilişki. Örn: 'SEVER'")class ConversationFacts(BaseModel): """ ConversationFact nesnelerinin listesini tutan sınıf. """ facts: list[ConversationFact] = Field(description="ConversationFact nesnelerinin listesi.")
llm_with_output = (
init_chat_model("gpt-4o-mini", model_provider="openai", api_key="...")
.with_structured_output(ConversationFacts)
)
prompt = ChatPromptTemplate.from_messages(SystemMessagePromptTemplate.from_template("Konuşmadan olguları çıkarın."),MessagesPlaceholder(variable_name="history"),)chain = prompt | llm_with_outputchain.invoke({"history": history.messages,})
ConversationFacts(facts=[
ConversationFact(object='child', subject='Bluey', relationship='LOVES')
])
LangChain ve Neo4j ile Graph RAG