2025-06-23 13:18:38 +05:30
"""
Example of using vLLM with mem0 for high - performance memory operations .
SETUP INSTRUCTIONS :
1. Install vLLM :
pip install vllm
2. Start vLLM server ( in a separate terminal ) :
vllm serve microsoft / DialoGPT - small - - port 8000
Wait for the message : " Uvicorn running on http://0.0.0.0:8000 "
( Small model : ~ 500 MB download , much faster ! )
3. Verify server is running :
curl http : / / localhost : 8000 / health
4. Run this example :
python examples / misc / vllm_example . py
Optional environment variables :
export VLLM_BASE_URL = " http://localhost:8000/v1 "
export VLLM_API_KEY = " vllm-api-key "
"""
from mem0 import Memory
# Configuration for vLLM integration
config = {
" llm " : {
" provider " : " vllm " ,
" config " : {
" model " : " Qwen/Qwen2.5-32B-Instruct " ,
" vllm_base_url " : " http://localhost:8000/v1 " ,
" api_key " : " vllm-api-key " ,
" temperature " : 0.7 ,
" max_tokens " : 100 ,
2025-07-03 18:52:50 -07:00
} ,
2025-06-23 13:18:38 +05:30
} ,
2025-07-03 18:52:50 -07:00
" embedder " : { " provider " : " openai " , " config " : { " model " : " text-embedding-3-small " } } ,
2025-06-23 13:18:38 +05:30
" vector_store " : {
" provider " : " qdrant " ,
2025-07-03 18:52:50 -07:00
" config " : { " collection_name " : " vllm_memories " , " host " : " localhost " , " port " : 6333 } ,
} ,
2025-06-23 13:18:38 +05:30
}
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
def main ( ) :
"""
Demonstrate vLLM integration with mem0
"""
print ( " --> Initializing mem0 with vLLM... " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
# Initialize memory with vLLM
memory = Memory . from_config ( config )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
print ( " --> Memory initialized successfully! " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
# Example conversations to store
conversations = [
{
" messages " : [
{ " role " : " user " , " content " : " I love playing chess on weekends " } ,
2025-07-03 18:52:50 -07:00
{
" role " : " assistant " ,
" content " : " That ' s great! Chess is an excellent strategic game that helps improve critical thinking. " ,
} ,
2025-06-23 13:18:38 +05:30
] ,
2025-07-03 18:52:50 -07:00
" user_id " : " user_123 " ,
2025-06-23 13:18:38 +05:30
} ,
{
" messages " : [
{ " role " : " user " , " content " : " I ' m learning Python programming " } ,
2025-07-03 18:52:50 -07:00
{
" role " : " assistant " ,
" content " : " Python is a fantastic language for beginners! What specific areas are you focusing on? " ,
} ,
2025-06-23 13:18:38 +05:30
] ,
2025-07-03 18:52:50 -07:00
" user_id " : " user_123 " ,
2025-06-23 13:18:38 +05:30
} ,
{
" messages " : [
{ " role " : " user " , " content " : " I prefer working late at night, I ' m more productive then " } ,
2025-07-03 18:52:50 -07:00
{
" role " : " assistant " ,
" content " : " Many people find they ' re more creative and focused during nighttime hours. It ' s important to maintain a consistent schedule that works for you. " ,
} ,
2025-06-23 13:18:38 +05:30
] ,
2025-07-03 18:52:50 -07:00
" user_id " : " user_123 " ,
} ,
2025-06-23 13:18:38 +05:30
]
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
print ( " \n --> Adding memories using vLLM... " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
# Add memories - now powered by vLLM's high-performance inference
for i , conversation in enumerate ( conversations , 1 ) :
2025-07-03 18:52:50 -07:00
result = memory . add ( messages = conversation [ " messages " ] , user_id = conversation [ " user_id " ] )
2025-06-23 13:18:38 +05:30
print ( f " Memory { i } added: { result } " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
print ( " \n 馃攳 Searching memories... " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
# Search memories - vLLM will process the search and memory operations
search_queries = [
" What does the user like to do on weekends? " ,
" What is the user learning? " ,
2025-07-03 18:52:50 -07:00
" When is the user most productive? " ,
2025-06-23 13:18:38 +05:30
]
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
for query in search_queries :
print ( f " \n Query: { query } " )
2025-07-03 18:52:50 -07:00
memories = memory . search ( query = query , user_id = " user_123 " )
2025-06-23 13:18:38 +05:30
for memory_item in memories :
print ( f " - { memory_item [ ' memory ' ] } " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
print ( " \n --> Getting all memories for user... " )
all_memories = memory . get_all ( user_id = " user_123 " )
print ( f " Total memories stored: { len ( all_memories ) } " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
for memory_item in all_memories :
print ( f " - { memory_item [ ' memory ' ] } " )
2025-07-03 18:52:50 -07:00
2025-06-23 13:18:38 +05:30
print ( " \n --> vLLM integration demo completed successfully! " )
print ( " \n Benefits of using vLLM: " )
print ( " -> 2.7x higher throughput compared to standard implementations " )
print ( " -> 5x faster time-per-output-token " )
print ( " -> Efficient memory usage with PagedAttention " )
print ( " -> Simple configuration, same as other providers " )
if __name__ == " __main__ " :
try :
main ( )
except Exception as e :
print ( f " => Error: { e } " )
print ( " \n Troubleshooting: " )
print ( " 1. Make sure vLLM server is running: vllm serve microsoft/DialoGPT-small --port 8000 " )
print ( " 2. Check if the model is downloaded and accessible " )
print ( " 3. Verify the base URL and port configuration " )
print ( " 4. Ensure you have the required dependencies installed " )