FalkorDBCypherRetriever
FalkorDBCypherRetriever
FalkorDB Document Store에 대해 임의의 OpenCypher 쿼리를 실행하는 리트리버예요.
출처: 문서
본문
- 파이프라인에서의 일반적인 위치: GraphRAG 파이프라인에서 쿼리 빌드 컴포넌트 뒤,
PromptBuilder앞에 사용해요. - 필수 초기화 변수:
document_store(FalkorDBDocumentStore 인스턴스) - 필수 실행 변수:
query(OpenCypher 쿼리 문자열. 또는 초기화 시custom_cypher_query설정) - 출력 변수:
documents(문서 리스트)
개요 (Overview)
FalkorDBCypherRetriever는 FalkorDBDocumentStore에 대해 임의의 OpenCypher 쿼리를 실행해요. 그래프 탐색과 다중 홉(multi-hop) 쿼리가 필요한 GraphRAG 파이프라인에 적합해요. 쿼리는 Haystack Document 필드에 매핑되는 노드 또는 딕셔너리를 반환해야 해요.
custom_cypher_query를 초기화 시 설정할 수 있고, run()에 query를 전달해 런타임에 선택적으로 덮어쓸 수도 있어요. 문자열 보간 대신 파라미터화된 쿼리(Cypher에서 $param_name, parameters로 전달)를 사용해서 주입(injection) 취약점을 피하세요.
보안 (Security)
원시 Cypher 쿼리는 반드시 신뢰할 수 있는 소스에서만 와야 해요. 쿼리 문자열에 정제되지 않은 사용자 입력을 직접 전달하지 마세요. parameters를 사용하세요.
설치 (Installation)
pip install falkordb-haystack
FalkorDB가 실행 중인지 확인하세요. 예를 들어 Docker로 실행할 수 있어요.
docker run -d -p 6379:6379 falkordb/falkordb:latest
이 페이지의 예시는 transformers-haystack 패키지의 Transformers 컴포넌트를 사용해요. 예시를 실행하려면 설치하세요.
pip install transformers-haystack
사용법 (Usage)
단독 사용 (On its own)
from haystack import Document
from haystack_integrations.document_stores.falkordb import FalkorDBDocumentStore
from haystack_integrations.components.retrievers.falkordb import FalkorDBCypherRetriever
document_store = FalkorDBDocumentStore(
host="localhost",
port=6379,
recreate_graph=True,
)
document_store.write_documents(
[
Document(
content="There are over 7,000 languages spoken around the world today.",
meta={"topic": "linguistics"},
),
Document(
content="Elephants have been observed to recognize themselves in mirrors.",
meta={"topic": "biology"},
),
],
)
retriever = FalkorDBCypherRetriever(
document_store=document_store,
custom_cypher_query="MATCH (d:Document {topic: $topic}) RETURN d",
)
result = retriever.run(parameters={"topic": "linguistics"})
print(result["documents"][0].content)
파이프라인에서 사용 (In a pipeline)
from haystack import Document, Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack_integrations.components.generators.transformers import (
TransformersChatGenerator,
)
from haystack.dataclasses import ChatMessage
from haystack_integrations.document_stores.falkordb import FalkorDBDocumentStore
from haystack_integrations.components.retrievers.falkordb import FalkorDBCypherRetriever
document_store = FalkorDBDocumentStore(
host="localhost",
port=6379,
recreate_graph=True,
)
document_store.write_documents(
[
Document(
content="There are over 7,000 languages spoken around the world today.",
meta={"topic": "linguistics"},
),
Document(
content="Elephants have been observed to recognize themselves in mirrors.",
meta={"topic": "biology"},
),
],
)
prompt_template = [
ChatMessage.from_user(
"""Given these documents, answer the question.
Documents:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
Question: {{ question }}""",
),
]
pipeline = Pipeline()
pipeline.add_component(
"retriever",
FalkorDBCypherRetriever(
document_store=document_store,
custom_cypher_query="MATCH (d:Document {topic: $topic}) RETURN d",
),
)
pipeline.add_component("prompt_builder", ChatPromptBuilder(template=prompt_template))
pipeline.add_component(
"llm",
TransformersChatGenerator(model="HuggingFaceTB/SmolLM2-135M-Instruct"),
)
pipeline.connect("retriever.documents", "prompt_builder.documents")
pipeline.connect("prompt_builder.prompt", "llm.messages")
result = pipeline.run(
{
"retriever": {"parameters": {"topic": "linguistics"}},
"prompt_builder": {"question": "How many languages are there?"},
},
)
print(result["llm"]["replies"][0].text)