|
import OpenAI from 'openai'; |
|
import { ChatOpenAI } from '@langchain/openai'; |
|
import { ConversationChain } from 'langchain/chains'; |
|
import { BufferMemory } from 'langchain/memory'; |
|
import { FileSystemChatMessageHistory } from '@langchain/community/stores/message/file_system'; |
|
import { HumanMessage, AIMessage } from '@langchain/core/messages'; |
|
|
|
const prompt = "What is 2+2? Answer in one word."; |
|
|
|
async function benchmarkDirectAPI() { |
|
const openai = new OpenAI({ |
|
apiKey: process.env.OPENAI_API_KEY, |
|
}); |
|
console.log('π Benchmarking Direct OpenAI API...'); |
|
const times = []; |
|
|
|
for (let i = 0; i < 50; i++) { |
|
const start = Date.now(); |
|
await openai.chat.completions.create({ |
|
model: 'gpt-4o-mini', |
|
messages: [{ role: 'user', content: prompt }], |
|
max_tokens: 10 |
|
}); |
|
const end = Date.now(); |
|
times.push(end - start); |
|
console.log(` Call ${i + 1}: ${end - start}ms`); |
|
} |
|
|
|
return times; |
|
} |
|
|
|
async function benchmarkLangChainWithMemory() { |
|
const llm = new ChatOpenAI({ |
|
modelName: 'gpt-4o-mini', |
|
openAIApiKey: process.env.OPENAI_API_KEY, |
|
}); |
|
|
|
// Create file-based memory for persistence |
|
const memory = new BufferMemory({ |
|
chatHistory: new FileSystemChatMessageHistory('./conversation_history.json'), |
|
returnMessages: true, |
|
memoryKey: 'chat_history' |
|
}); |
|
|
|
console.log('\nπ Benchmarking LangChain JS with Memory...'); |
|
const times = []; |
|
|
|
for (let i = 0; i < 50; i++) { |
|
const start = Date.now(); |
|
|
|
// Get conversation history from memory |
|
const chatHistory = await memory.chatHistory.getMessages(); |
|
|
|
// Create messages array with history + new prompt |
|
const messages = [ |
|
...chatHistory, |
|
new HumanMessage(prompt) |
|
]; |
|
|
|
// Call LLM with conversation history |
|
const response = await llm.invoke(messages); |
|
|
|
// Save the response to memory |
|
await memory.chatHistory.addMessage(new HumanMessage(prompt)); |
|
await memory.chatHistory.addMessage(response); |
|
|
|
const end = Date.now(); |
|
times.push(end - start); |
|
console.log(` Call ${i + 1}: ${end - start}ms`); |
|
} |
|
|
|
return times; |
|
} |
|
|
|
function calculateStats(times) { |
|
const avg = times.reduce((a, b) => a + b, 0) / times.length; |
|
const min = Math.min(...times); |
|
const max = Math.max(...times); |
|
return { avg: Math.round(avg), min, max }; |
|
} |
|
|
|
async function main() { |
|
if (!process.env.OPENAI_API_KEY) { |
|
console.error('β Please set OPENAI_API_KEY environment variable'); |
|
process.exit(1); |
|
} |
|
|
|
console.log('π GPT-4o-mini Latency Benchmark\n'); |
|
|
|
const directTimes = await benchmarkDirectAPI(); |
|
const langchainMemoryTimes = await benchmarkLangChainWithMemory(); |
|
|
|
const directStats = calculateStats(directTimes); |
|
const langchainMemoryStats = calculateStats(langchainMemoryTimes); |
|
|
|
console.log('\nπ Results:'); |
|
console.log(`Direct API - Avg: ${directStats.avg}ms, Min: ${directStats.min}ms, Max: ${directStats.max}ms`); |
|
console.log(`LangChain+Memory - Avg: ${langchainMemoryStats.avg}ms, Min: ${langchainMemoryStats.min}ms, Max: ${langchainMemoryStats.max}ms`); |
|
console.log(`\nOverhead:`); |
|
console.log(`LangChain+Memory vs Direct: ${langchainMemoryStats.avg - directStats.avg}ms (LangChain + Memory overhead)`); |
|
} |
|
|
|
main().catch(console.error); |