from langflow import load_flow
# Load your RAG flow configuration
flow = load_flow("rag_flow.json")
# Example document to process
docs = [
"LangFlow is a GUI for LangChain.",
"It enables rapid prototyping of LLM apps."
]
# Initialize the RAG pipeline
rag_chain = flow.get_chain()
# Query your documents
response = rag_chain.run(
query="What is LangFlow?",
documents=docs
)
print(response)
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
# Load and index documents
loader = TextLoader("data.txt")
docs = loader.load()
db = Chroma.from_documents(
docs,
OpenAIEmbeddings()
)
# Retrieve relevant docs
query = "How does RAG work?"
docs = db.similarity_search(query)from langchain.prompts import PromptTemplate
# Create prompt with context
template = """Use the following context to answer:
Context: {context}
Question: {question}
Answer:"""
prompt = PromptTemplate(
template=template,
input_variables=["context", "question"]
)
# Combine context with query
context = "\n".join([d.page_content for d in docs])
final_prompt = prompt.format(
context=context,
question=query
)from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
# Generate response
llm = ChatOpenAI()
response = llm([
HumanMessage(content=final_prompt)
])
print(response.content)# Example 1: Creating a basic RAG pipeline
from langflow import load_flow_from_json
# Load a predefined workflow
flow = load_flow_from_json("my_rag_flow.json")
# Components are automatically connected based on the visual design
loader = flow.get_component("PDFLoader")
embedder = flow.get_component("OpenAIEmbeddings")
vectorstore = flow.get_component("Chroma")
llm = flow.get_component("ChatOpenAI")
# Execute the flow
response = flow.execute(
input_data={"query": "What is RAG?"}
)
python --version
# Expected output: Python 3.10.0 or higherpip --version
# Expected output: pip 21.0.0 or higherpython -m ensurepip --upgrade$ pip install langflow --pre --force-reinstall
Collecting langflow
Downloading langflow-0.5.3-py3-none-any.whl (5.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.8/5.8 MB 4.2 MB/s eta 0:00:00
Installing collected packages: langflow
Successfully installed langflow-0.5.3$ langflow --version
LangFlow Version: 0.5.3
Python Version: 3.10.12
Platform: Linux-5.15.0-1041-azure-x86_64-with-glibc2.31langflow run# Launch on a specific port
langflow run --port 7861
# Launch on a specific host
langflow run --host 0.0.0.0
# Launch with both custom host and port
langflow run --host 0.0.0.0 --port 7861
# Launch in debug mode for troubleshooting
langflow run --debuglangflow statusfrom langflow import Canvas
# Create a new canvas
canvas = Canvas()
# Add components to specific positions
canvas.add_component("ChatOpenAI", position=(100, 100))
canvas.add_component("TextLoader", position=(300, 100))from langflow.components import load_component
# Access sidebar components
file_loader = load_component("FileLoader")
embeddings = load_component("OpenAIEmbeddings")
chat_model = load_component("ChatOpenAI")# Example component configuration
{
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 500,
"api_key": "your-api-key"
}from langflow import LangFlowProject
project = LangFlowProject(
name="My RAG Chatbot",
description="A chatbot using RAG architecture"
)project.save()
# Auto-save configuration
project.configure(
auto_save=True,
save_interval=300 # Save every 5 minutes
)workspace = project.get_workspace()
workspace.configure_layout(
components=[
"FileLoader",
"TextSplitter",
"Embeddings",
"VectorStore",
"ChatModel"
],
auto_arrange=True
)from langchain.document_loaders import TextLoader
# Load a text file
loader = TextLoader("data.txt")
documents = loader.load()
# For PDFs
from langchain.document_loaders import PyPDFLoader
pdf_loader = PyPDFLoader("document.pdf")from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
)
splits = text_splitter.split_documents(documents)from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
model="text-embedding-ada-002",
openai_api_key="your-api-key"
)documents = text_splitter.split_documents(raw_documents)
doc_embeddings = embeddings.embed_documents(
[doc.page_content for doc in documents]
)# Advanced configuration
embeddings = OpenAIEmbeddings(
model="text-embedding-ada-002",
chunk_size=1000, # Process in batches
max_retries=3, # Retry on API errors
timeout=30 # Seconds to wait for response
)from langchain.vectorstores import FAISS
# Create vector store from documents
vectorstore = FAISS.from_documents(
documents=text_chunks,
embedding=embeddings
)
# Save locally
vectorstore.save_local("faiss_index")from langchain.vectorstores import AstraDB
# Initialize with your credentials
vectorstore = AstraDB(
token="your_token",
api_endpoint="your_endpoint",
collection_name="your_collection"
)
# Add documents
vectorstore.add_documents(documents=text_chunks)# Chat Input Component
@app.route("/chat", methods=["POST"])
def chat_input():
user_message = request.json.get("message")
return process_message(user_message)
# Chat Memory Implementation
class ConversationMemory:
def __init__(self):
self.messages = []
def add_message(self, role, content):
self.messages.append({"role": role, "content": content})
def get_history(self):
return self.messages
# Chat Output Handler
def display_response(response):
return jsonify({
"message": response.content,
"timestamp": datetime.now().isoformat()
})[Chat Input] → [Query Processing] → [Vector DB Search] → [Context Assembly] → [Chat Model] → [Chat Output]# Initialize components
from langchain import LLMChain, VectorStore, PromptTemplate
from langchain.chat_models import ChatOpenAI
# Query Processing
def process_query(user_input: str) -> str:
return user_input.strip()
# Vector DB Search
def retrieve_context(query: str, vector_db: VectorStore) -> list:
return vector_db.similarity_search(query, k=3)
# Context Assembly
def assemble_context(relevant_docs: list) -> str:
return "\n".join([doc.page_content for doc in relevant_docs])
# Chat Model Integration
llm = ChatOpenAI(temperature=0.7)
prompt = PromptTemplate(
template="Context: {context}\nQuestion: {question}\nAnswer:",
input_variables=["context", "question"]
)
chain = LLMChain(llm=llm, prompt=prompt)
# Complete Pipeline
def rag_pipeline(user_query: str) -> str:
processed_query = process_query(user_query)
relevant_docs = retrieve_context(processed_query, vector_db)
context = assemble_context(relevant_docs)
response = chain.run(context=context, question=processed_query)
return response# Basic Q&A Template
template = """
You are an AI assistant. Use the context below to answer the question.
Context: {context}
Question: {question}
Answer: """
# Advanced Template with System Message
template_json = {
"system": "You are a helpful AI assistant that answers questions based on provided context.",
"messages": [
{"role": "system", "content": "Use this context: {context}"},
{"role": "user", "content": "{question}"}
]
}
# Python Implementation Example
from string import Template
class PromptTemplate:
def __init__(self, template_text):
self.template = Template(template_text)
def format(self, context, question):
return self.template.substitute(
context=context,
question=question
)import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
# Or load from .env file
from dotenv import load_dotenv
load_dotenv()from openai import OpenAI
client = OpenAI() # Automatically uses OPENAI_API_KEY from env
# Or specify directly:
client = OpenAI(api_key="your-api-key-here")response = client.chat.completions.create(
model="gpt-3.5-turbo", # or "gpt-4"
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is RAG?"}
],
temperature=0.7,
max_tokens=150
)
print(response.choices[0].message.content)from langflow import load_flow_from_json
# Load your flow
flow = load_flow_from_json("my_flow.json")
# Initialize and run
flow.build()
flow.run()response = flow.query({
"input": "What is the main topic of the document?",
"chat_history": []
})
print("Response:", response.get("output"))
print("Sources:", response.get("sources", []))# Enable debug logging
import logging
logging.basicConfig(level=logging.DEBUG)
# Inspect component outputs
embeddings = flow.get_component("embeddings")
vector_store = flow.get_component("vector_store")
# Check dimensions
print(f"Embedding dimension: {len(embeddings.embed_query('test'))}")
print(f"Vector store dimension: {vector_store.embedding_dimension}")from langchain import PromptTemplate, LLMChain
# Good naming practice
document_qa_prompt = PromptTemplate(
input_variables=["context", "question"],
template="Context: {context}\nQuestion: {question}\nAnswer:"
)
# Clear chain naming
document_qa_chain = LLMChain(
llm=llm,
prompt=document_qa_prompt,
verbose=True
)# Example of different prompt formats
factual_prompt = """
Given the context below, answer the question factually:
Context: {context}
Question: {question}
Factual answer:"""
creative_prompt = """
Based on the context, provide a creative explanation:
Context: {context}
Question: {question}
Creative response:"""from langchain.text_splitter import RecursiveCharacterTextSplitter
# Optimize chunk sizes and overlap
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
separators=["\n\n", "\n", " ", ""]
)from langchain.retrievers import MultiQueryRetriever
from langchain.llms import OpenAI
# Initialize the retriever
retriever = MultiQueryRetriever.from_llm(
llm=OpenAI(),
retriever=vector_store.as_retriever(),
num_queries=3 # Generate 3 variations
)
# Use the multi-query retriever
docs = retriever.get_relevant_documents(
"What are the key features of RAG?"
)from langchain.chains import MapReduceDocumentsChain
from langchain.chains.summarize import get_map_reduce_chain
# Initialize the map-reduce chain
chain = get_map_reduce_chain(
llm=OpenAI(),
token_max=1000,
reduce_llm_kwargs={"temperature": 0}
)
# Process documents
summary = chain.run(documents=docs)# Start LangFlow locally
langflow run --host 0.0.0.0 --port 7860
# Or using Docker
docker run -p 7860:7860 logspace/langflow
# Deploy to AWS ECS
aws ecs create-cluster --cluster-name langflow-cluster
# Create task definition
aws ecs register-task-definition \
--family langflow \
--container-definitions '[{
"name": "langflow",
"image": "logspace/langflow:latest",
"portMappings": [{
"containerPort": 7860,
"hostPort": 7860
}]
}]'
# Run service
aws ecs create-service \
--cluster langflow-cluster \
--service-name langflow-service \
--task-definition langflow:1 \
--desired-count 1
from langchain.document_loaders import DirectoryLoader
# Load multiple PDF files from a directory
loader = DirectoryLoader('./documents/', glob="**/*.pdf")
documents = loader.load()
# Split documents into chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
splits = text_splitter.split_documents(documents)
from langchain.embeddings import HuggingFaceEmbeddings
# Try different embedding models
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-mpnet-base-v2"
)
# Create and query vector store
vectorstore = Chroma.from_documents(
documents=splits,
embedding=embeddings
)
# Enhanced prompt template
template = """
Use the following pieces of context to answer the question.
If you don't know the answer, just say "I don't have enough information."
Context: {context}
Question: {question}
Answer: Let's think about this step by step:
"""
prompt = PromptTemplate(
template=template,
input_variables=["context", "question"]
)