Langchain Integration
Learn about langchain integration and how to implement it effectively.
LangChain Integration
LangChain is a powerful framework for developing applications with large language models (LLMs). Forjinn provides comprehensive LangChain integration, enabling sophisticated AI workflows with chains, agents, and tools.
Overview
Forjinn's LangChain integration includes:
- Pre-built Chains: Ready-to-use chain templates
- Custom Chain Development: Build complex multi-step workflows
- agent Framework: Intelligent agents with tool usage
- Memory Management: Persistent conversation context
- Tool Integration: Extensive tool ecosystem
- Prompt Engineering: Advanced prompt templates and management
Core Components
Chains
Chains are sequences of operations that process inputs through multiple steps.
Conversation Chain
Basic conversational AI with memory.
{
name: 'ConversationChain',
description: 'Simple conversation with memory',
inputs: ['llm', 'memory'],
features: [
'Maintains conversation context',
'Customizable prompt templates',
'Memory persistence'
]
}
LLM Chain
Direct LLM interaction with prompt templates.
{
name: 'LLMChain',
description: 'Direct LLM interaction with prompts',
inputs: ['llm', 'prompt'],
features: [
'Custom prompt templates',
'Variable substitution',
'Output parsing'
]
}
Retrieval QA Chain
Question-answering with document retrieval.
{
name: 'RetrievalQAChain',
description: 'QA with document retrieval',
inputs: ['llm', 'retriever'],
features: [
'Document-based QA',
'Similarity search',
'Source attribution'
]
}
Conversational Retrieval QA Chain
Combines conversation memory with document retrieval.
{
name: 'ConversationalRetrievalQAChain',
description: 'Conversational QA with retrieval',
inputs: ['llm', 'retriever', 'memory'],
features: [
'Conversation-aware retrieval',
'Follow-up questions',
'Context preservation'
]
}
SQL Database Chain
Natural language to SQL query execution.
{
name: 'SqlDatabaseChain',
description: 'Natural language to SQL',
inputs: ['llm', 'database'],
features: [
'SQL query generation',
'Database schema awareness',
'Query validation'
]
}
API Chain
HTTP API interactions with natural language.
{
name: 'APIChain',
description: 'Natural language API interactions',
inputs: ['llm', 'api_spec'],
features: [
'OpenAPI specification support',
'GET/POST operations',
'Response parsing'
]
}
Agents
Agents are autonomous entities that can reason and use tools to accomplish tasks.
Conversational agent
General-purpose conversational agent with tool access.
{
name: 'ConversationalAgent',
description: 'Conversational agent with tools',
capabilities: [
'Multi-turn conversations',
'Tool selection and usage',
'Reasoning and planning'
],
tools: ['search', 'calculator', 'custom_tools']
}
ReAct agent
Reasoning and Acting agent for complex problem-solving.
{
name: 'ReActAgent',
description: 'Reasoning and Acting agent',
capabilities: [
'Step-by-step reasoning',
'Action planning',
'Observation processing'
],
workflow: [
'Thought: Analyze the problem',
'Action: Execute tool or operation',
'Observation: Process results',
'Repeat until complete'
]
}
Tool agent
Specialized agent for tool-heavy workflows.
{
name: 'ToolAgent',
description: 'Tool-focused agent',
capabilities: [
'Advanced tool selection',
'Tool chaining',
'Error handling and recovery'
]
}
OpenAI Assistant agent
Integration with OpenAI's Assistant API.
{
name: 'OpenAIAssistant',
description: 'OpenAI Assistant integration',
capabilities: [
'Code interpreter',
'File handling',
'Function calling'
]
}
Memory Systems
Memory components maintain context across interactions.
Buffer Memory
Simple conversation buffer with configurable size.
{
name: 'BufferMemory',
description: 'Simple conversation buffer',
features: [
'Configurable buffer size',
'Message truncation',
'Fast access'
]
}
Buffer Window Memory
Sliding window of recent conversations.
{
name: 'BufferWindowMemory',
description: 'Sliding window memory',
features: [
'Fixed window size',
'Automatic cleanup',
'Recent context focus'
]
}
Summary Memory
Conversation summarization for long contexts.
{
name: 'ConversationSummaryMemory',
description: 'Summarized conversation memory',
features: [
'Automatic summarization',
'Context compression',
'Long conversation support'
]
}
Vector Store Memory
Semantic memory using vector embeddings.
{
name: 'VectorStoreMemory',
description: 'Semantic memory with vectors',
features: [
'Semantic similarity',
'Scalable storage',
'Relevant context retrieval'
]
}
Tools Integration
LangChain provides extensive tool integration capabilities.
Built-in Tools
- Search Tools: Google Search, SerpAPI, Brave Search
- Calculation: Calculator, Math operations
- Code Execution: Python REPL, Code interpreter
- File Operations: Read/Write files
- Web Tools: Web scraping, HTTP requests
- Database Tools: SQL queries, NoSQL operations
Custom Tools
from langchain.tools import BaseTool
class CustomAnalysisTool(BaseTool):
name = "data_analysis"
description = "Analyze data and generate insights"
def _run(self, query: str) -> str:
# Custom analysis logic
return analysis_result
Tool Configuration
{
tools: [
{
name: 'google_search',
description: 'Search the web for information',
parameters: {
query: 'string',
num_results: 'number'
}
},
{
name: 'calculator',
description: 'Perform mathematical calculations',
parameters: {
expression: 'string'
}
}
]
}
Advanced Features
Prompt Engineering
Prompt Templates
from langchain.prompts import PromptTemplate
template = """
You are a helpful AI assistant. Given the following context and question,
provide a comprehensive answer.
Context: {context}
Question: {question}
Answer:
"""
prompt = PromptTemplate(
template=template,
input_variables=["context", "question"]
)
Few-Shot Prompting
from langchain.prompts import FewShotPromptTemplate
examples = [
{
"question": "What is the capital of France?",
"answer": "The capital of France is Paris."
},
{
"question": "What is 2+2?",
"answer": "2+2 equals 4."
}
]
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="Answer the following questions:",
suffix="Question: {input}\nAnswer:",
input_variables=["input"]
)
Output Parsing
Structured Output
from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
class PersonInfo(BaseModel):
name: str = Field(description="person's name")
age: int = Field(description="person's age")
occupation: str = Field(description="person's job")
parser = PydanticOutputParser(pydantic_object=PersonInfo)
JSON Output
from langchain.output_parsers import OutputFixingParser
from langchain.output_parsers import PydanticOutputParser
# Automatically fix malformed JSON
fixing_parser = OutputFixingParser.from_llm(
parser=PydanticOutputParser(pydantic_object=PersonInfo),
llm=llm
)
Document Processing
Text Splitters
from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
separators=["\n\n", "\n", " ", ""]
)
Document Loaders
from langchain.document_loaders import (
PDFLoader,
TextLoader,
CSVLoader,
WebBaseLoader
)
# Load various document types
pdf_loader = PDFLoader("document.pdf")
web_loader = WebBaseLoader("https://example.com")
Vector Stores
Supported Vector Stores
- Pinecone: Cloud-native vector database
- Chroma: Open-source embedding database
- Weaviate: Vector search engine
- Qdrant: Vector similarity search
- FAISS: Facebook AI Similarity Search
Vector Store Usage
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
documents=docs,
embedding=embeddings,
persist_directory="./chroma_db"
)
Configuration Examples
Basic Chain Setup
{
"chain_type": "conversation",
"llm": {
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 1000
},
"memory": {
"type": "buffer",
"max_token_limit": 2000
},
"prompt": {
"template": "You are a helpful assistant. {history}\nHuman: {input}\nAI:"
}
}
agent Configuration
{
"agent_type": "conversational-react-description",
"llm": {
"model": "gpt-4",
"temperature": 0
},
"tools": [
"google_search",
"calculator",
"python_repl"
],
"memory": {
"type": "conversation_buffer_window",
"k": 5
},
"max_iterations": 10,
"early_stopping_method": "generate"
}
RAG System Setup
{
"retrieval_qa": {
"llm": {
"model": "gpt-3.5-turbo",
"temperature": 0.1
},
"retriever": {
"vectorstore": "chroma",
"search_type": "similarity",
"search_kwargs": {
"k": 4
}
},
"chain_type": "stuff",
"return_source_documents": true
}
}
Best Practices
Performance Optimization
- Caching: Implement LLM response caching
- Batch Processing: Process multiple requests together
- Streaming: Use streaming for real-time responses
- Memory Management: Optimize memory usage for long conversations
Error Handling
from langchain.callbacks import get_openai_callback
import logging
try:
with get_openai_callback() as cb:
result = chain.run(input_text)
logging.info(f"Tokens used: {cb.total_tokens}")
except Exception as e:
logging.error(f"Chain execution failed: {e}")
# Implement fallback logic
Security Considerations
- Input Validation: Sanitize user inputs
- Output Filtering: Filter sensitive information
- Rate Limiting: Implement request rate limits
- Access Control: Secure API endpoints
Monitoring and Debugging
from langchain.callbacks import StdOutCallbackHandler
from langchain.callbacks import FileCallbackHandler
# Add debugging callbacks
callbacks = [
StdOutCallbackHandler(),
FileCallbackHandler("langchain.log")
]
chain.run(input_text, callbacks=callbacks)
Integration with Forjinn
Workflow Integration
LangChain components integrate seamlessly with Forjinn workflows:
- Drag-and-Drop: Visual chain building
- Component Library: Pre-built LangChain nodes
- Custom Nodes: Create custom LangChain components
- State Management: Maintain state across workflow steps
- Error Handling: Built-in error recovery
Deployment Options
- Cloud Deployment: Scalable cloud infrastructure
- On-Premise: Local deployment for security
- Hybrid: Mix of cloud and on-premise components
- Air-Gapped: Completely offline deployment
Monitoring and Analytics
- Performance Metrics: Track chain execution times
- Usage Analytics: Monitor tool and model usage
- Error Tracking: Comprehensive error logging
- Cost Optimization: Track and optimize API costs
Troubleshooting
Common Issues
- Memory Overflow: Optimize memory configuration
- Token Limits: Implement proper text chunking
- API Rate Limits: Add retry logic with backoff
- Tool Failures: Implement fallback mechanisms
Debug Techniques
- Verbose Logging: Enable detailed logging
- Step-by-Step Execution: Debug individual chain steps
- Input/Output Inspection: Examine data flow
- Performance Profiling: Identify bottlenecks
This comprehensive LangChain integration enables powerful AI application development within the Forjinn platform, providing flexibility, scalability, and enterprise-grade reliability.