Skip to content

Commit 3951aec

Browse files
Merge pull request #3 from joomla-projects/feature/anthropic-provider
Add Anthropic provider implementation
2 parents ef2d171 + 726981e commit 3951aec

File tree

4 files changed

+724
-0
lines changed

4 files changed

+724
-0
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+
}

0 commit comments

Comments
 (0)