Chapter 15 of 16
APPENDIX: Quick Reference
(This appendix has the code snippets you'll actually copy-paste when building systems. Bookmark this page. You'll be back here constantly until this stuff becomes muscle memory.)
Common Code Patterns
1. Basic RAG Query
def rag_query(question, vector_db, llm):
# Retrieve
docs = vector_db.query(get_embedding(question), top_k=5)
# Generate
context = "\n".join([d['content'] for d in docs])
prompt = f"Context: {context}\n\nQuestion: {question}\nAnswer:"
answer = llm.generate(prompt)
return answer
2. KG Entity Lookup
MATCH (e:Entity {name: $entity_name})
RETURN e, properties(e)
3. Hybrid Retrieval
# Get from both sources
kg_facts = graph_db.query(cypher_query)
rag_docs = vector_db.query(embedding)
# Fuse
context = format_kg(kg_facts) + format_docs(rag_docs)
answer = llm.generate(context + question)