pythonintermediate
LlamaIndex Document Query Engine
Index and query documents with LlamaIndex's VectorStoreIndex for fast semantic search.
pythonPress ⌘/Ctrl + Shift + C to copy
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
Settings.llm = OpenAI(model='gpt-4o-mini')
Settings.embed_model = OpenAIEmbedding(model='text-embedding-3-small')
documents = SimpleDirectoryReader('./docs').load_data()
index = VectorStoreIndex.from_documents(documents)
index.storage_context.persist('./index_store')
query_engine = index.as_query_engine(similarity_top_k=5)
response = query_engine.query('What are the key findings?')
print(response)
for node in response.source_nodes:
print(f' Score: {node.score:.3f} | {node.text[:100]}...')Use Cases
- document RAG
- enterprise search
- knowledge Q&A
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
RAG Pipeline (Retrieve + Augment + Generate)
Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.
Best for: document Q&A
#rag#embeddings
pythonbeginner
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
Best for: prompt chaining
#langchain#openai
typescriptadvanced
LangChain RAG Chain Pipeline
Build a retrieval-augmented generation chain with LangChain using vector store retrieval and prompt templates.
Best for: Document Q&A
#langchain#rag
typescriptadvanced
RAG Pipeline Implementation
Build a retrieval-augmented generation pipeline that grounds LLM answers in your own documents.
Best for: Grounding LLM answers in private documents
#ai#rag