LangChain Plugin: Session
The FlotorchLangChainSession provides comprehensive session management for LangChain agents, enabling persistent storage of conversation history and session data through FloTorch Gateway. It implements LangChain’s BaseMemory interface and stores chat turns as FloTorch session events, exposing a single history string for prompts.
Prerequisites
Section titled “Prerequisites”Before using FlotorchLangChainSession, ensure you have completed the general prerequisites outlined in the LangChain Plugin Overview, including installation and environment configuration.
Configuration
Section titled “Configuration”Parameters
Section titled “Parameters”Configure your session service with the following parameters:
FlotorchLangChainSession( api_key: str, # FloTorch API key for authentication (required) base_url: str # FloTorch Gateway endpoint URL (required))Parameter Details:
api_key- Authentication key for accessing FloTorch Gateway (can be set via environment variable)base_url- The FloTorch Gateway endpoint URL (can be set via environment variable)
Features
Section titled “Features”BaseMemory Interface
Section titled “BaseMemory Interface”Fully implements LangChain’s BaseMemory interface:
- Memory Key - Uses
historyas the memory key - Event Persistence - Stores chat turns as FloTorch session events
- History Formatting - Exposes a single history string for prompts
- LangChain Compatibility - Works seamlessly with LangChain’s memory framework
Persistent Session Storage
Section titled “Persistent Session Storage”Provides comprehensive session persistence capabilities:
- Gateway Storage - Stores session data persistently in FloTorch Gateway
- Event History - Preserves complete event history across service restarts
- State Synchronization - Automatically synchronizes state management
- Format Conversion - Seamlessly converts events between LangChain and FloTorch formats
Short-Term Memory Integration
Section titled “Short-Term Memory Integration”Serves as short-term memory for LangChain agents:
- Conversation History - Maintains conversation history for the current session
- History String - Provides formatted history string for prompt injection
- Automatic Updates - Automatically updates history as conversations progress
Usage Example
Section titled “Usage Example”Basic Session Setup
Section titled “Basic Session Setup”from flotorch.langchain.agent import FlotorchLangChainAgentfrom flotorch.langchain.session import FlotorchLangChainSessionfrom langchain.agents import AgentExecutor
# Initialize session serviceshort_term_memory = FlotorchLangChainSession( api_key="your_api_key", base_url="https://gateway.flotorch.cloud")
# Initialize agent with memory enabledagent_manager = FlotorchLangChainAgent( agent_name="customer-support", enable_memory=True, # Enables memory placeholders in prompt base_url="https://gateway.flotorch.cloud", api_key="your_api_key")
# Get agent and toolsagent = agent_manager.get_agent()tools = agent_manager.get_tools()
# Use with AgentExecutorexecutor = AgentExecutor( agent=agent, tools=tools, memory=short_term_memory, verbose=False, handle_parsing_errors=True)Memory Key Access
Section titled “Memory Key Access”from flotorch.langchain.session import FlotorchLangChainSession
# Initialize session serviceshort_term_memory = FlotorchLangChainSession( api_key="your_api_key", base_url="https://gateway.flotorch.cloud")
# Access memory keymemory_key = short_term_memory.memory_key # Returns "history"
# Load memory variablesmemory_vars = short_term_memory.load_memory_variables({})print(memory_vars["history"])Best Practices
Section titled “Best Practices”- Environment Variables - Use environment variables for credentials to enhance security
- Session Management - Implement proper session lifecycle management for long-running applications
- Memory Key - Remember that the memory key is
historywhen accessing memory variables - Agent Memory Enablement - Set
enable_memory=TrueinFlotorchLangChainAgentto inject memory placeholders in the prompt - Error Handling - Implement robust error handling for network and storage operations
- Session Cleanup - Consider implementing session cleanup strategies for production environments
Next Steps
Section titled “Next Steps”- Agent Configuration - Learn how to configure agents with session management
- Memory Integration - Add memory capabilities alongside sessions
- LLM Configuration - Configure language models for your session-enabled agents