Pinecall offers seamless integration with LangChain, allowing you to leverage your existing LangChain agents for voice interactions. Our plugin acts as an interceptor through WebSockets, enabling real-time voice communication with your LangChain workflows.
First, install the Pinecall LangChain plugin in your project:
# For JavaScript/TypeScriptnpm install @pinecall/langchain-plugin
# For Pythonpip install pinecall-langchain
The Pinecall LangChain plugin works as a WebSocket interceptor that sits between your LangChain agents and Pinecall's voice AI infrastructure:
LangChain Agent
Pinecall WebSocket Interceptor
Pinecall Voice AI
This architecture provides several benefits:
Here's how to integrate your LangChain agent with Pinecall using our WebSocket interceptor:
import { Pinecall } from '@pinecall/sdk';import { PinecallLangChainPlugin } from '@pinecall/langchain-plugin';import { ConversationChain } from 'langchain/chains';import { ChatOpenAI } from 'langchain/chat_models/openai';import { BufferMemory } from 'langchain/memory';
// Initialize Pinecallconst pinecall = new Pinecall({ apiKey: process.env.PINECALL_API_KEY});
// Create your LangChain agentconst model = new ChatOpenAI({ temperature: 0, modelName: 'gpt-4'});
const memory = new BufferMemory();const chain = new ConversationChain({ llm: model, memory });
// Initialize the Pinecall LangChain pluginconst plugin = new PinecallLangChainPlugin({ pinecall, agent: chain, voiceConfig: { voice: 'sophia', language: 'en-US' }, // Optional: Configure how to handle the conversation config: { interceptMode: 'realtime', // 'realtime' or 'turnbased' contextPreservation: true, transcriptHandling: 'memory' // Add transcript to agent memory }});
// Start the WebSocket serverplugin.listen(3000);console.log('LangChain interceptor running on ws://localhost:3000');
// Connect the plugin to a phone number (optional)async function connectToPhoneNumber() { const phoneNumber = await pinecall.phoneNumbers.update('pn_12345', { agentConfig: { type: 'websocket', endpoint: 'ws://your-public-endpoint:3000' } }); console.log(`Phone number ${phoneNumber.number} connected to LangChain agent`);}
connectToPhoneNumber();
from pinecall import Pinecallfrom pinecall_langchain import PinecallLangChainPluginfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.chains import ConversationChainimport os
# Initialize Pinecallpinecall = Pinecall(api_key=os.environ["PINECALL_API_KEY"])
# Create your LangChain agentllm = ChatOpenAI(temperature=0, model_name="gpt-4")memory = ConversationBufferMemory()conversation = ConversationChain(llm=llm, memory=memory)
# Initialize the Pinecall LangChain pluginplugin = PinecallLangChainPlugin( pinecall=pinecall, agent=conversation, voice_config={ "voice": "sophia", "language": "en-US" }, # Optional: Configure how to handle the conversation config={ "intercept_mode": "realtime", # 'realtime' or 'turnbased' "context_preservation": True, "transcript_handling": "memory" # Add transcript to agent memory })
# Start the WebSocket serverplugin.listen(3000)print("LangChain interceptor running on ws://localhost:3000")
# Connect the plugin to a phone number (optional)async def connect_to_phone_number(): phone_number = await pinecall.phone_numbers.update("pn_12345", { "agent_config": { "type": "websocket", "endpoint": "ws://your-public-endpoint:3000" } }) print(f"Phone number {phone_number.number} connected to LangChain agent")
import asyncioasyncio.run(connect_to_phone_number())
The Pinecall LangChain plugin offers several configuration options to fine-tune how it interacts with your LangChain agent:
Option | Description | Values | Default |
---|---|---|---|
interceptMode | How the agent interacts with the conversation | 'realtime', 'turnbased' | 'realtime' |
contextPreservation | Whether to maintain conversation context | true, false | true |
transcriptHandling | How to handle call transcripts | 'memory', 'none', 'function' | 'memory' |
streaming | Whether to use streaming responses | true, false | true |
You can integrate LangChain tools with Pinecall to enable your voice agent to perform actions like querying databases, accessing APIs, and more:
import { Pinecall } from '@pinecall/sdk';import { PinecallLangChainPlugin } from '@pinecall/langchain-plugin';import { ChatOpenAI } from 'langchain/chat_models/openai';import { ConversationChain } from 'langchain/chains';import { BufferMemory } from 'langchain/memory';import { SerpAPI } from 'langchain/tools';import { Calculator } from 'langchain/tools/calculator';import { initializeAgentExecutorWithOptions } from "langchain/agents";
// Initialize Pinecallconst pinecall = new Pinecall({ apiKey: process.env.PINECALL_API_KEY});
// Set up LangChain toolsconst tools = [ new SerpAPI(process.env.SERPAPI_API_KEY, { location: "San Francisco,California,United States", hl: "en", gl: "us", }), new Calculator(),];
// Create an agent with toolsconst model = new ChatOpenAI({ temperature: 0, modelName: 'gpt-4'});
const executor = await initializeAgentExecutorWithOptions( tools, model, { agentType: "chat-conversational-react-description", verbose: true, memory: new BufferMemory({ returnMessages: true, memoryKey: "chat_history", }), });
// Initialize the Pinecall LangChain plugin with the agent executorconst plugin = new PinecallLangChainPlugin({ pinecall, agent: executor, voiceConfig: { voice: 'sophia', language: 'en-US' }});
// Start the WebSocket serverplugin.listen(3000);console.log('LangChain agent with tools running on ws://localhost:3000');
When using the WebSocket interceptor, keep these points in mind:
The plugin includes debugging capabilities to help you monitor and troubleshoot your integration:
// Enable debug modeconst plugin = new PinecallLangChainPlugin({ // ... configuration options debug: true, logLevel: 'verbose' // 'error', 'warn', 'info', 'verbose', 'debug'});
// Add event listeners for monitoringplugin.on('connect', (connectionInfo) => { console.log('New connection established:', connectionInfo);});
plugin.on('message', (message) => { console.log('Message received:', message);});
plugin.on('error', (error) => { console.error('Error occurred:', error);});
plugin.on('disconnect', (reason) => { console.log('Connection closed:', reason);});
Now that you've integrated LangChain with Pinecall, you can: