-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_api.py
More file actions
150 lines (124 loc) · 5.34 KB
/
debug_api.py
File metadata and controls
150 lines (124 loc) · 5.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
147
148
149
150
#!/usr/bin/env python
"""
Debug script for testing API connectivity and question generation
"""
import requests
import json
import time
import sys
def test_api_connection():
"""Test basic connection to the qewertyy.dev API"""
print("Testing connection to qewertyy.dev API...")
try:
api_url = "https://api.qewertyy.dev/models"
payload = {
"model_id": 5, # GPT-3.5 Turbo
"messages": [
{
"role": "user",
"content": "Hello, this is a test message. Please respond with 'API connection successful'."
}
]
}
headers = {"Content-Type": "application/json"}
print("Sending test request...")
start_time = time.time()
response = requests.post(api_url, json=payload, headers=headers, timeout=10)
end_time = time.time()
print(f"Response received in {end_time - start_time:.2f} seconds")
print(f"Status code: {response.status_code}")
if response.status_code == 200:
print("API connection successful!")
try:
data = response.json()
content = data.get("content", "No content received")
print(f"Response content: {content}")
return True
except json.JSONDecodeError:
print("Error: Could not parse JSON response")
print(f"Raw response: {response.text[:500]}")
return False
else:
print(f"API request failed with status code: {response.status_code}")
print(f"Response: {response.text[:500]}")
return False
except Exception as e:
print(f"Error connecting to API: {str(e)}")
return False
def test_question_generation():
"""Test question generation with a small sample text"""
print("\nTesting question generation API...")
sample_text = """
The Python programming language was created by Guido van Rossum in the late 1980s.
It is named after the British comedy group Monty Python.
Python is known for its simple syntax and readability, making it a popular choice for beginners.
Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
"""
try:
api_url = "https://api.qewertyy.dev/models"
# Create a simplified prompt for testing
prompt = f"""Generate 2 questions and answers based on this text:
{sample_text}
Format each as "Question: [question]" and "Answer: [answer]".
"""
payload = {
"model_id": 5, # GPT-3.5 Turbo
"messages": [
{
"role": "user",
"content": prompt
}
]
}
headers = {"Content-Type": "application/json"}
print("Sending question generation request...")
start_time = time.time()
response = requests.post(api_url, json=payload, headers=headers, timeout=20)
end_time = time.time()
print(f"Response received in {end_time - start_time:.2f} seconds")
print(f"Status code: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
content = data.get("content", "No content received")
if not content:
print("Error: Empty content received")
return False
print("\nGenerated questions and answers:")
print("-" * 50)
print(content)
print("-" * 50)
if "Question:" in content and "Answer:" in content:
print("\nSuccess! The API generated questions and answers correctly.")
return True
else:
print("\nWarning: The API returned content but it doesn't contain the expected Question/Answer format.")
return False
except json.JSONDecodeError:
print("Error: Could not parse JSON response")
print(f"Raw response: {response.text[:500]}")
return False
else:
print(f"API request failed with status code: {response.status_code}")
print(f"Response: {response.text[:500]}")
return False
except Exception as e:
print(f"Error testing question generation: {str(e)}")
return False
if __name__ == "__main__":
print("=" * 50)
print("ASKPDF-AI API Connection Tester")
print("=" * 50)
connection_result = test_api_connection()
if connection_result:
print("\nBasic API connection successful, testing question generation...")
question_result = test_question_generation()
if question_result:
print("\nAll tests passed! The API is working correctly for question generation.")
sys.exit(0)
else:
print("\nQuestion generation test failed. The API may be working but not returning the expected format.")
sys.exit(1)
else:
print("\nFailed to connect to the API. Please check your internet connection and try again.")
sys.exit(1)