Skip to content

Commit 57db085

Browse files
committed
Merge branch 'main' into feature/ai
2 parents 16c76dc + d535f50 commit 57db085

14 files changed

+2007
-12
lines changed

Tests/AnthropicChat.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use Joomla\AI\Provider\AnthropicProvider;
6+
7+
echo "Testing Real Anthropic API Calls...\n\n";
8+
9+
$configFile = __DIR__ . '/../config.json';
10+
$config = json_decode(file_get_contents($configFile), true);
11+
$api_key = $config['anthropic_api_key'] ?? null;
12+
13+
try {
14+
// Create provider with your API key
15+
$provider = new AnthropicProvider([
16+
'api_key' => $api_key
17+
]);
18+
19+
echo "Provider created with API key\n";
20+
echo "Provider name: " . $provider->getName() . "\n\n";
21+
22+
// Test 1: Simple prompt
23+
echo "Test 1: Simple prompt\n";
24+
echo str_repeat('-', 50) . "\n";
25+
26+
$response = $provider->chat("Translate the following sentence into French: Joomla makes website building easy and fun.", ['model' => 'claude-3-haiku-20240307']);
27+
28+
echo "API call successful!\n";
29+
echo "Response: " . $response->getContent() . "\n";
30+
echo "Provider: " . $response->getProvider() . "\n";
31+
echo "Status: " . $response->getStatusCode() . "\n";
32+
33+
$metadata = $response->getMetadata();
34+
echo "Model used: " . ($metadata['model']) . "\n";
35+
echo "Input Tokens used: " . ($metadata['input_tokens']) . "\n";
36+
echo "Output Tokens used: " . ($metadata['output_tokens']) . "\n";
37+
echo "\n";
38+
39+
echo "\n" . str_repeat('=', 60) . "\n";
40+
echo "All Messages endpoint tests completed successfully!\n";
41+
} catch (Exception $e) {
42+
echo "Error: " . $e->getMessage() . "\n";
43+
}

Tests/AnthropicModelManagement.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use Joomla\AI\Provider\AnthropicProvider;
6+
7+
echo "=== Anthropic Provider Comprehensive Test ===\n\n";
8+
9+
$configFile = __DIR__ . '/../config.json';
10+
$config = json_decode(file_get_contents($configFile), true);
11+
$api_key = $config['anthropic_api_key'] ?? null;
12+
13+
try {
14+
// Create provider
15+
$provider = new AnthropicProvider([
16+
'api_key' => $api_key
17+
]);
18+
19+
echo "Provider: " . $provider->getName() . "\n";
20+
echo "Provider created successfully!\n\n";
21+
22+
// Test 1: Get all available models
23+
echo "=== Test 1: Get Available Models ===\n";
24+
25+
$availableModels = $provider->getAvailableModels();
26+
echo "Available Models: " . implode(', ', $availableModels) . "\n";
27+
echo "Total: " . count($availableModels) . " models\n";
28+
29+
echo "\n" . str_repeat("=", 60) . "\n\n";
30+
31+
// Test 2: Test with a known model ID (if different from available models)
32+
echo "=== Test 2: Get Known Model (claude-3-haiku-20240307) ===\n";
33+
34+
try {
35+
$validModelResponse = $provider->getModel('claude-3-haiku-20240307');
36+
37+
echo "Model Information:\n";
38+
echo $validModelResponse->getContent() . "\n";
39+
40+
} catch (Exception $e) {
41+
echo "Error getting known model: " . $e->getMessage() . "\n";
42+
}
43+
44+
echo "\n" . str_repeat("=", 60) . "\n\n";
45+
46+
// Test 3: Test with invalid model ID (error handling)
47+
echo "=== Test 3: Error Handling - Invalid Model ID ===\n";
48+
49+
try {
50+
$invalidModelResponse = $provider->getModel('invalid-model-12345');
51+
echo "Unexpected success with invalid model\n";
52+
} catch (Exception $e) {
53+
echo "Expected error caught: " . $e->getMessage() . "\n";
54+
}
55+
56+
echo "\n" . str_repeat("=", 60) . "\n\n";
57+
58+
// Test 4: Basic Chat with saveFile
59+
echo "=== Test 4: Basic Chat with saveFile ===\n";
60+
echo str_repeat("-", 50) . "\n";
61+
62+
$chatResponse = $provider->chat(
63+
"Hi",
64+
[
65+
'model' => 'claude-3-haiku-20240307',
66+
'max_tokens' => 150
67+
]
68+
);
69+
70+
echo "Chat Response:\n";
71+
echo $chatResponse->getContent() . "\n";
72+
echo "Status: " . $chatResponse->getStatusCode() . "\n";
73+
echo "Provider: " . $chatResponse->getProvider() . "\n";
74+
75+
$metadata = $chatResponse->getMetadata();
76+
echo "Model: " . $metadata['model'] . "\n";
77+
echo "Input Tokens: " . $metadata['input_tokens'] . "\n";
78+
echo "Output Tokens: " . $metadata['output_tokens'] . "\n";
79+
echo "Stop Reason: " . $metadata['stop_reason'] . "\n";
80+
81+
echo "\n" . str_repeat("=", 60) . "\n\n";
82+
83+
// Test 5: Vision Analysis
84+
echo "=== Test 5: Vision Analysis ===\n";
85+
echo str_repeat("-", 50) . "\n";
86+
87+
$testImage = "test_files/fish.png";
88+
$visionResponse = $provider->vision(
89+
"What do you see in this image? Describe in one line.",
90+
$testImage,
91+
);
92+
93+
echo "Vision Response:\n";
94+
echo $visionResponse->getContent() . "\n";
95+
echo "Status: " . $visionResponse->getStatusCode() . "\n";
96+
97+
$visionMetadata = $visionResponse->getMetadata();
98+
echo "Model: " . $visionMetadata['model'] . "\n";
99+
echo "Input Tokens: " . $visionMetadata['input_tokens'] . "\n";
100+
echo "Output Tokens: " . $visionMetadata['output_tokens'] . "\n";
101+
102+
echo "\n" . str_repeat("=", 60) . "\n\n";
103+
104+
// Test 6: Advanced Chat with Options
105+
echo "=== Test 6: Advanced Chat with Options ===\n";
106+
echo str_repeat("-", 50) . "\n";
107+
108+
$advancedResponse = $provider->chat(
109+
"What is the full form of AI?",
110+
[
111+
'model' => 'claude-3-haiku-20240307',
112+
'max_tokens' => 200,
113+
'temperature' => 0.7,
114+
'top_p' => 0.9,
115+
'stop_sequences' => ['\n\n\n']
116+
]
117+
);
118+
119+
echo "Advanced Chat Response:\n";
120+
echo $advancedResponse->getContent() . "\n";
121+
122+
$advancedMetadata = $advancedResponse->getMetadata();
123+
echo "Model: " . $advancedMetadata['model'] . "\n";
124+
echo "Input Tokens: " . $advancedMetadata['input_tokens'] . "\n";
125+
echo "Output Tokens: " . $advancedMetadata['output_tokens'] . "\n";
126+
echo "Stop Reason: " . $advancedMetadata['stop_reason'] . "\n";
127+
128+
echo "\n" . str_repeat("=", 60) . "\n\n";
129+
130+
echo "\nAll Anthropic tests completed successfully!\n";
131+
132+
} catch (Exception $e) {
133+
echo "Test failed with error: " . $e->getMessage() . "\n";
134+
}

Tests/AnthropicVision.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use Joomla\AI\Provider\AnthropicProvider;
6+
7+
echo "Testing Anthropic Vision API Calls...\n\n";
8+
9+
$configFile = __DIR__ . '/../config.json';
10+
$config = json_decode(file_get_contents($configFile), true);
11+
$api_key = $config['anthropic_api_key'] ?? null;
12+
13+
try {
14+
// Create provider with your API key
15+
$provider = new AnthropicProvider([
16+
'api_key' => $api_key
17+
]);
18+
19+
echo "Provider created with API key\n";
20+
echo "Provider name: " . $provider->getName() . "\n\n";
21+
22+
// Test vision capability
23+
echo "Test: Vision with image description\n";
24+
echo str_repeat('-', 50) . "\n";
25+
26+
// You can use a base64 image or URL
27+
$imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg";
28+
29+
$response = $provider->vision(
30+
"What do you see in this image? Describe it in detail.",
31+
$imageUrl,
32+
[
33+
'model' => 'claude-3-5-sonnet-20241022',
34+
]
35+
);
36+
37+
echo "Vision API call successful!\n";
38+
echo "Response: " . $response->getContent() . "\n";
39+
echo "Provider: " . $response->getProvider() . "\n";
40+
echo "Status: " . $response->getStatusCode() . "\n";
41+
42+
$metadata = $response->getMetadata();
43+
echo "Model used: " . ($metadata['model']) . "\n";
44+
echo "Input Tokens used: " . ($metadata['input_tokens']) . "\n";
45+
echo "Stop reason: " . ($metadata['stop_reason']) . "\n";
46+
echo "\n";
47+
48+
echo "\n" . str_repeat('=', 60) . "\n";
49+
echo "Anthropic Vision tests completed successfully!\n";
50+
} catch (Exception $e) {
51+
echo "Error: " . $e->getMessage() . "\n";
52+
}

Tests/DefaultModels.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// and the default model is set
3434
echo "Test 1: Simple prompt- Will use default model gpt-3.5-turbo\n";
3535
echo str_repeat('-', 50) . "\n";
36+
3637
$response = $provider->chat("Hello! How are you?");
3738
echo "Model: " . $response->getMetadata()['model'] . "\n";
3839
echo "Response: " . $response->getContent() . "\n";
@@ -64,6 +65,7 @@
6465

6566
$metadata = $response->getMetadata();
6667
echo "Model: " . $response->getMetadata()['model'] . "\n";
68+
6769
if (isset($metadata['choices'][0]['message']['audio']['data'])) {
6870
$audioData = $metadata['choices'][0]['message']['audio']['data'];
6971
$audioDatab64 = base64_decode($audioData, true);
@@ -119,6 +121,7 @@
119121
$response->saveFile("output/test6_image.png");
120122
echo "Model: " . ($response->getMetadata()['model']) . "\n";
121123
echo "File saved to: output/test6_image.png\n";
124+
122125
echo "\n";
123126

124127
echo "\n" . str_repeat('=', 60) . "\n";

Tests/Ollama.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Joomla\AI\Provider\OllamaProvider;
6+
7+
echo "Testing Ollama Provider Integration...\n\n";
8+
9+
try {
10+
// Test 1: Check if Ollama is supported (server is running)
11+
echo "Test 1: Checking if Ollama is supported\n";
12+
echo str_repeat('-', 50) . "\n";
13+
14+
$isSupported = OllamaProvider::isSupported();
15+
echo "Is Ollama supported? " . ($isSupported ? "Yes" : "No") . "\n\n";
16+
17+
if (!$isSupported) {
18+
throw new Exception("Ollama server is not running. Please start it with 'ollama serve'");
19+
}
20+
21+
// Create provider instance
22+
$provider = new OllamaProvider();
23+
echo "Provider created successfully\n";
24+
echo "Provider name: " . $provider->getName() . "\n\n";
25+
26+
// Test 2: Get Available Models
27+
echo "Test 2: Getting available models\n";
28+
echo str_repeat('-', 50) . "\n";
29+
30+
$models = $provider->getAvailableModels();
31+
echo "Available models:\n";
32+
foreach ($models as $model) {
33+
echo "- $model\n";
34+
}
35+
echo "\n";
36+
37+
// Test 3: Pull a new model (llama2 if not present)
38+
echo "Test 3: Pulling a new model (granite-embedding)\n";
39+
echo str_repeat('-', 50) . "\n";
40+
41+
$modelToPull = 'granite-embedding:30m';
42+
if (!in_array($modelToPull, $models)) {
43+
echo "Model $modelToPull not found locally. Pulling...\n";
44+
$result = $provider->pullModel($modelToPull);
45+
echo $result ? "Pull successful!\n" : "Pull failed!\n";
46+
} else {
47+
echo "Model $modelToPull is already available locally.\n";
48+
}
49+
echo "\n";
50+
51+
// Test 4: Attempt to pull an already present model
52+
echo "Test 4: Attempting to pull an already present model\n";
53+
echo str_repeat('-', 50) . "\n";
54+
55+
// Get first available model from the list
56+
if (!empty($models)) {
57+
$existingModel = $models[0];
58+
$result = $provider->pullModel($existingModel);
59+
} else {
60+
echo "No models available to test with.\n";
61+
}
62+
echo "\n";
63+
64+
// Test 5: Attempt to pull a non-existent model
65+
echo "Test 5: Attempting to pull a non-existent model\n";
66+
echo str_repeat('-', 50) . "\n";
67+
68+
$nonExistentModel = 'non-existent-model-123';
69+
try {
70+
echo "Attempting to pull non-existent model: $nonExistentModel\n";
71+
$result = $provider->pullModel($nonExistentModel);
72+
echo "Unexpected success!\n";
73+
} catch (Exception $e) {
74+
echo "Expected error occurred: " . $e->getMessage() . "\n";
75+
}
76+
echo "\n";
77+
78+
// Final check of available models after all operations
79+
echo "Final check: Getting available models after operations\n";
80+
echo str_repeat('-', 50) . "\n";
81+
82+
$finalModels = $provider->getAvailableModels();
83+
echo "Final available models:\n";
84+
foreach ($finalModels as $model) {
85+
echo "- $model\n";
86+
}
87+
echo "\n";
88+
89+
echo "\n" . str_repeat('=', 60) . "\n";
90+
echo "All Ollama Provider tests completed successfully!\n";
91+
92+
} catch (Exception $e) {
93+
echo "Error: " . $e->getMessage() . "\n";
94+
}

Tests/OllamaChat.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use Joomla\AI\Provider\OllamaProvider;
6+
7+
echo "Testing Real Ollama API Calls...\n\n";
8+
9+
try {
10+
// Create provider with your API key
11+
$provider = new OllamaProvider();
12+
13+
echo "Provider created with API key\n";
14+
echo "Provider name: " . $provider->getName() . "\n\n";
15+
16+
// Test 1: Simple prompt
17+
echo "Test 1: Simple prompt\n";
18+
echo str_repeat('-', 50) . "\n";
19+
20+
$response = $provider->chat("Can you write a short poem on Joomla focusing on its features and benefits?");
21+
22+
echo "API call successful!\n";
23+
echo "Response: " . "\n" . $response->getContent() . "\n";
24+
25+
// $metadata = $response->getMetadata();
26+
// if (!empty($metadata)) {
27+
// echo "Model used: " . ($metadata['model'] ?? 'N/A') . "\n";
28+
// echo "Total duration: " . ($metadata['total_duration'] ?? 'N/A') . " ns\n";
29+
// echo "Eval count: " . ($metadata['eval_count'] ?? 'N/A') . "\n";
30+
// if (isset($metadata['usage'])) {
31+
// echo "Input tokens: " . ($metadata['usage']['input_tokens'] ?? 'N/A') . "\n";
32+
// echo "Output tokens: " . ($metadata['usage']['output_tokens'] ?? 'N/A') . "\n";
33+
// }
34+
// }
35+
// echo "\n";
36+
37+
echo "\n" . str_repeat('=', 60) . "\n";
38+
echo "All Chat tests completed successfully!\n";
39+
} catch (Exception $e) {
40+
echo "Error: " . $e->getMessage() . "\n";
41+
}

0 commit comments

Comments
 (0)