-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_connection.py
More file actions
146 lines (115 loc) Β· 4.34 KB
/
debug_connection.py
File metadata and controls
146 lines (115 loc) Β· 4.34 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Debug Connection Script
Test Qdrant and OpenAI connections independently to diagnose issues.
"""
import os
from qdrant_client import QdrantClient
from openai import OpenAI
from app.core.config import settings
def test_qdrant_connection():
"""Test Qdrant connection."""
print("π Testing Qdrant Connection")
print("-" * 40)
try:
print(f"URL: {settings.qdrant_url}")
print(f"Port: {settings.qdrant_port}")
print(f"HTTPS: {settings.qdrant_use_https}")
# Use the same pattern as the working test files
protocol = "https" if settings.qdrant_use_https else "http"
qdrant_url = f"{protocol}://{settings.qdrant_url}"
print(f"Connecting to: {qdrant_url}")
# Create Qdrant client using the proven working pattern
# Critical: port=None prevents :6333 from being appended to URL
client = QdrantClient(
url=qdrant_url,
port=None, # Critical: prevents :6333 from being appended to URL
timeout=30,
prefer_grpc=False # Force REST API usage
)
# Test connection
collections = client.get_collections()
print(f"β
Connected successfully!")
print(f"Collections found: {len(collections.collections)}")
for collection in collections.collections:
print(f" - {collection.name}")
return True, client
except Exception as e:
print(f"β Connection failed: {str(e)}")
return False, None
def test_openai_connection():
"""Test OpenAI connection."""
print("\nπ Testing OpenAI Connection")
print("-" * 40)
try:
print(f"API Key: {settings.openai_api_key[:10]}...")
client = OpenAI(api_key=settings.openai_api_key)
# Test with a simple model list call
models = client.models.list()
print(f"β
Connected successfully!")
print(f"Models available: {len(models.data)}")
return True
except Exception as e:
print(f"β Connection failed: {str(e)}")
return False
def test_mem0_initialization():
"""Test Mem0 initialization."""
print("\nπ Testing Mem0 Initialization")
print("-" * 40)
try:
from mem0 import Memory
# Test Qdrant connection first
qdrant_ok, qdrant_client = test_qdrant_connection()
if not qdrant_ok:
print("β Cannot initialize Mem0 - Qdrant connection failed")
return False
# Test OpenAI connection
openai_ok = test_openai_connection()
if not openai_ok:
print("β Cannot initialize Mem0 - OpenAI connection failed")
return False
# Configure Mem0
config = {
"llm": {
"provider": "openai",
"config": {
"model": settings.ai_model,
"temperature": settings.ai_temperature,
"max_tokens": settings.ai_max_tokens
}
},
"vector_store": {
"provider": "qdrant",
"config": {
"collection_name": settings.mem0_collection_name,
"client": qdrant_client,
"embedding_model_dims": 1536,
"on_disk": False
}
}
}
print(f"Collection: {settings.mem0_collection_name}")
# Initialize Mem0
memory = Memory.from_config(config)
print("β
Mem0 initialized successfully!")
return True
except Exception as e:
print(f"β Mem0 initialization failed: {str(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
print("π AI Agent Connection Debug")
print("=" * 50)
# Test individual components
qdrant_ok, _ = test_qdrant_connection()
openai_ok = test_openai_connection()
if qdrant_ok and openai_ok:
mem0_ok = test_mem0_initialization()
if mem0_ok:
print("\nπ All connections successful!")
else:
print("\nβ Mem0 initialization failed")
else:
print("\nβ Basic connections failed")
print("\n" + "=" * 50)