forked from memvid/memvid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_memory_fixed.py
More file actions
132 lines (104 loc) · 4.46 KB
/
chat_memory_fixed.py
File metadata and controls
132 lines (104 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
"""
Example: Interactive conversation using MemvidChat (Fixed)
"""
import sys
import os
from memvid.config import VIDEO_FILE_TYPE
# Set environment variable before importing transformers
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from memvid import MemvidChat
import time
def print_search_results(results):
"""Pretty print search results"""
print("\nRelevant context found:")
print("-" * 50)
for i, result in enumerate(results[:3]):
print(f"\n[{i+1}] Score: {result['score']:.3f}")
print(f"Text: {result['text'][:150]}...")
print(f"Frame: {result['frame']}")
def main():
print("Memvid Example: Interactive Chat with Memory")
print("=" * 50)
# Check if memory files exist
video_file = F"output/memory.{VIDEO_FILE_TYPE}"
index_file = "output/memory_index.json"
if not os.path.exists(video_file) or not os.path.exists(index_file):
print("\nError: Memory files not found!")
print("Please run 'python examples/build_memory.py' first to create the memory.")
return
# Initialize chat
print(f"\nLoading memory from: {video_file}")
# You can set OPENAI_API_KEY environment variable or pass it here
api_key = os.getenv("OPENAI_API_KEY", "")
if not api_key:
print("\nNote: No OpenAI API key found. Chat will work in context-only mode.")
print("Set OPENAI_API_KEY environment variable to enable full chat capabilities.")
chat = MemvidChat(video_file, index_file, llm_api_key=api_key)
chat.start_session()
# Get stats
stats = chat.get_stats()
print(f"\nMemory loaded successfully!")
print(f" Total chunks: {stats['retriever_stats']['index_stats']['total_chunks']}")
print(f" LLM available: {stats['llm_available']}")
if stats['llm_available']:
print(f" LLM model: {stats['llm_model']}")
print("\nInstructions:")
print("- Type your questions to search the memory")
print("- Type 'search <query>' to see raw search results")
print("- Type 'stats' to see system statistics")
print("- Type 'export' to save conversation")
print("- Type 'exit' or 'quit' to end the session")
print("-" * 50)
# Interactive loop
while True:
try:
user_input = input("\nYou: ").strip()
if not user_input:
continue
# Handle commands
if user_input.lower() in ['exit', 'quit']:
print("\nGoodbye!")
break
elif user_input.lower() == 'stats':
stats = chat.get_stats()
print("\nSystem Statistics:")
print(f" Messages: {stats['message_count']}")
print(f" Cache size: {stats['retriever_stats']['cache_size']}")
print(f" Video frames: {stats['retriever_stats']['total_frames']}")
continue
elif user_input.lower() == 'export':
export_file = f"output/session_{chat.session_id}.json"
chat.export_session(export_file)
print(f"Session exported to: {export_file}")
continue
elif user_input.lower().startswith('search '):
query = user_input[7:]
print(f"\nSearching for: '{query}'")
start_time = time.time()
results = chat.search_context(query, top_k=5)
elapsed = time.time() - start_time
print(f"Search completed in {elapsed:.3f} seconds")
print_search_results(results)
continue
# Regular chat
print("\nAssistant: ", end="", flush=True)
start_time = time.time()
response = chat.chat(user_input)
elapsed = time.time() - start_time
print(response)
print(f"\n[Response time: {elapsed:.2f}s]")
except KeyboardInterrupt:
print("\n\nInterrupted. Goodbye!")
break
except Exception as e:
print(f"\nError: {e}")
continue
# Export session on exit
if chat.get_history():
export_file = f"output/session_{chat.session_id}.json"
chat.export_session(export_file)
print(f"\nSession saved to: {export_file}")
if __name__ == "__main__":
main()