-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ollama.php
More file actions
131 lines (106 loc) Β· 4.51 KB
/
test_ollama.php
File metadata and controls
131 lines (106 loc) Β· 4.51 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
<?php
/**
* Ollama Connection Test Script
* This script helps debug Ollama integration issues
*/
echo "<h1>π Ollama Connection Test</h1>";
echo "<hr>";
// Test 1: Check if Ollama is running
echo "<h2>1. Checking Ollama Server...</h2>";
$ollama_url = "http://localhost:11434/api/tags";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $ollama_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if($curl_error) {
echo "<p style='color:red'>β Connection Error: $curl_error</p>";
echo "<p><strong>Solution:</strong> Make sure Ollama is running. Download from <a href='https://ollama.ai'>ollama.ai</a> and run it.</p>";
} elseif($http_code === 200) {
echo "<p style='color:green'>β
Ollama Server is Running!</p>";
$tags = json_decode($response, true);
echo "<h3>Available Models:</h3>";
if(isset($tags['models']) && is_array($tags['models'])) {
echo "<ul>";
foreach($tags['models'] as $model) {
$name = $model['name'] ?? 'Unknown';
$size = isset($model['size']) ? round($model['size'] / (1024*1024*1024), 2) . ' GB' : 'Unknown';
echo "<li><strong>$name</strong> ($size)</li>";
}
echo "</ul>";
} else {
echo "<p style='color:orange'>β οΈ No models installed</p>";
echo "<p><strong>Install models with:</strong></p>";
echo "<pre>ollama pull mistral
ollama pull llama3
ollama pull phi3</pre>";
}
} else {
echo "<p style='color:red'>β Ollama returned HTTP $http_code</p>";
echo "<p>Response: " . htmlspecialchars($response) . "</p>";
}
echo "<hr>";
// Test 2: Test with a specific model
echo "<h2>2. Testing Model Generation...</h2>";
$test_model = "mistral"; // Change this to test different models
echo "<p>Testing model: <strong>$test_model</strong></p>";
$payload = json_encode([
'model' => $test_model,
'prompt' => 'In one sentence, what is PHP?',
'stream' => false,
'temperature' => 0.3
]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "http://localhost:11434/api/generate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => ['Content-Type: application/json']
]);
echo "<p>Sending request (this may take 10-30 seconds)...</p>";
$start_time = microtime(true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
$elapsed = round(microtime(true) - $start_time, 2);
if($curl_error) {
echo "<p style='color:red'>β Generation Error: $curl_error</p>";
} elseif($http_code !== 200) {
echo "<p style='color:red'>β HTTP Error $http_code</p>";
echo "<p>Response: " . htmlspecialchars($response) . "</p>";
} else {
echo "<p style='color:green'>β
Generation Successful! (${elapsed}s)</p>";
$decoded = json_decode($response, true);
echo "<h3>Raw Response Structure:</h3>";
echo "<pre>" . htmlspecialchars(json_encode($decoded, JSON_PRETTY_PRINT)) . "</pre>";
if(isset($decoded['response'])) {
echo "<h3>Generated Text:</h3>";
echo "<p>" . htmlspecialchars($decoded['response']) . "</p>";
}
}
echo "<hr>";
// Test 3: Configuration Check
echo "<h2>3. Configuration Check</h2>";
require_once 'includes/.env.php';
echo "<table style='border-collapse: collapse; width: 100%;'>";
echo "<tr style='background: #f0f0f0;'><th style='border: 1px solid #ccc; padding: 8px;'>Setting</th><th style='border: 1px solid #ccc; padding: 8px;'>Value</th></tr>";
echo "<tr><td style='border: 1px solid #ccc; padding: 8px;'>USE_OLLAMA</td>";
echo "<td style='border: 1px solid #ccc; padding: 8px;'>" . (defined('USE_OLLAMA') ? (USE_OLLAMA ? 'β
TRUE' : 'β FALSE') : 'Not defined') . "</td></tr>";
echo "<tr><td style='border: 1px solid #ccc; padding: 8px;'>OLLAMA_MODEL</td>";
echo "<td style='border: 1px solid #ccc; padding: 8px;'>" . (defined('OLLAMA_MODEL') ? OLLAMA_MODEL : 'Not defined') . "</td></tr>";
echo "<tr><td style='border: 1px solid #ccc; padding: 8px;'>OLLAMA_BASE_URL</td>";
echo "<td style='border: 1px solid #ccc; padding: 8px;'>" . (defined('OLLAMA_BASE_URL') ? OLLAMA_BASE_URL : 'Not defined') . "</td></tr>";
echo "</table>";
echo "<hr>";
echo "<h2>β
Test Complete</h2>";
echo "<p><a href='admin/descriptive_test_evaluation.php'>Back to Descriptive Test Evaluation</a></p>";
?>