MCPcopy Create free account
hub / github.com/MemTensor/MemOS / main

Function main

examples/core_memories/vllm_kv_cache_memory.py:11–132  ·  view source on GitHub ↗

Main function demonstrating VLLMKVCacheMemory usage.

()

Source from the content-addressed store, hash-verified

9
10
11def main():
12 """Main function demonstrating VLLMKVCacheMemory usage."""
13
14 print("=== VLLM KV Cache Memory Example ===\n")
15
16 # 1. Create config for VLLMKVCacheMemory (using vLLM backend)
17 config = MemoryConfigFactory(
18 backend="vllm_kv_cache", # Use the new vLLM KV cache backend
19 config={
20 "extractor_llm": {
21 "backend": "vllm",
22 "config": {
23 "model_name_or_path": "Qwen/Qwen3-0.6B",
24 "api_base": "http://localhost:8088/v1",
25 "temperature": 0.7,
26 "max_tokens": 1024,
27 "model_schema": "memos.configs.llm.VLLMLLMConfig",
28 },
29 },
30 },
31 )
32
33 # 2. Instantiate VLLMKVCacheMemory using the factory
34 print("Initializing VLLM KV Cache Memory...")
35 vllm_kv_mem = MemoryFactory.from_config(config)
36 print("✓ VLLM KV Cache Memory initialized successfully.\n")
37
38 # 3. Extract a VLLMKVCacheItem from a prompt
39 print("===== Extract VLLMKVCacheItem =====")
40 system_prompt = [
41 {"role": "system", "content": "You are a helpful AI assistant."},
42 {"role": "user", "content": "What is MemOS?"},
43 {"role": "assistant", "content": "MemOS is a memory operating system for LLMs."},
44 ]
45
46 try:
47 cache_item = vllm_kv_mem.extract(system_prompt)
48 print("✓ KV cache item extracted successfully")
49 print(f" ID: {cache_item.id}")
50 print(f" Memory (prompt): {cache_item.memory[:100]}...")
51 print(f" Metadata: {cache_item.metadata}")
52 print()
53 except Exception as e:
54 print(f"✗ Failed to extract KV cache item: {e}")
55 return
56
57 # 4. Add the extracted VLLMKVCacheItem
58 print("===== Add VLLMKVCacheItem =====")
59 vllm_kv_mem.add([cache_item])
60 all_items = vllm_kv_mem.get_all()
61 print(f"✓ Added cache item. Total items: {len(all_items)}")
62 print()
63
64 # 5. Get by id
65 print("===== Get VLLMKVCacheItem by id =====")
66 retrieved = vllm_kv_mem.get(cache_item.id)
67 if retrieved:
68 print(f"✓ Retrieved cache item: {retrieved.id}")

Callers 1

Calls 12

MemoryConfigFactoryClass · 0.90
preload_kv_cacheMethod · 0.80
getMethod · 0.65
from_configMethod · 0.45
extractMethod · 0.45
addMethod · 0.45
get_allMethod · 0.45
get_cacheMethod · 0.45
deleteMethod · 0.45
dumpMethod · 0.45
delete_allMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected