1. Go to this page and download the library: Download memran/marwa-ai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
memran / marwa-ai example snippets
use Marwa\AI\MarwaAI;
// Simple completion
$response = MarwaAI::instance()->conversation('Hello, how are you?')->send();
echo $response->getContent();
// With provider-specific options
$response = MarwaAI::driver('anthropic')
->completion([
['role' => 'user', 'content' => 'Explain quantum computing']
], [
'temperature' => 0.7,
'max_tokens' => 500,
]);
use function Marwa\AI\{ai, complete, conversation};
// Quick completion
$result = complete('Tell me a joke');
echo $result->getContent();
// Create conversation with helper
$conv = conversation('You are a helpful assistant');
$conv->user('What is PHP?')
->send()
->getContent();
$conv = ai()->conversation([
['role' => 'system', 'content' => 'You are a PHP expert.'],
['role' => 'user', 'content' => 'What is dependency injection?']
]);
$response = $conv->send();
echo $response->getContent();
// Continue the conversation
$conv->user('Can you show me an example?')
->send();
$conv = ai()->conversation()
->withContext(['user_id' => 123, 'locale' => 'en'])
->setSystem('Remember the user prefers concise answers.');
// Later, restore from storage
$conv = \Marwa\AI\Support\Conversation::fromArray($savedData);
// Using the helper
use function Marwa\AI\stream;
stream('Write a poem about code', function ($chunk) {
echo $chunk->getDelta();
flush();
});
// Or using conversation directly
foreach (ai()->conversation('Write a poem about code')->stream() as $chunk) {
echo $chunk->getDelta();
}
$template = ai()->prompt('
You are a {{role}} expert.
{{#show_tips}}
Provide practical examples.
{{/show_tips}}
Question: {{question}}
');
$response = $template
->variable('role', 'PHP developer')
->variable('show_tips', true)
->render([
'question' => 'What is a closure?'
]);
use function Marwa\AI\ai_tool;
ai()->tool(ai_tool(
'get_weather',
'Get current weather for a location',
[
'type' => 'object',
'properties' => [
'city' => ['type' => 'string', 'description' => 'City name'],
'country' => ['type' => 'string', 'description' => 'Country code'],
],
'conv->continueWithTools(ai()->getTools());
echo $final->getContent();
}
use function Marwa\AI\ai_classify;
// Sentiment analysis
$sentiment = ai_classify()->sentiment('I love this product!');
if ($sentiment->isPositive()) {
echo "Positive score: " . $sentiment->getSentimentScore();
}
// Intent detection
$intent = ai_classify()->detectIntent(
'I need to reset my password',
['greeting', 'password_reset', 'billing', 'technical_support']
);
echo "Detected: " . $intent->getIntent();
echo "Confidence: " . $intent->getConfidence();
// Entity extraction
$entities = ai_classify()->extractEntities(
'Contact John Doe at [email protected]',
['person', 'email', 'organization']
);
// Zero-shot classification
$result = ai_classify()->zeroShot(
'The new iPhone has an amazing camera',
['technology', 'sports', 'politics']
);
use Marwa\AI\Contracts\MCPServerInterface;
use Marwa\AI\Contracts\MCPResponse;
$server = new class implements MCPServerInterface {
public function getName(): string { return 'my-server'; }
public function getVersion(): string { return '1.0.0'; }
public function getCapabilities() { /* ... */ }
public function listTools(): array { return [/* ... */]; }
public function handleToolCall(string $tool, array $args): MCPResponse {
// Handle MCP tool calls
return MCPResponse::success($result);
}
};
ai()->registerMcpServer('my-server', $server);
use Marwa\AI\Exceptions\AIException;
use Marwa\AI\Exceptions\ConfigurationException;
use Marwa\AI\Exceptions\RateLimitException;
try {
$resp = ai()->driver('openai')->completion($messages);
} catch (RateLimitException $e) {
echo "Rate limited. Retry after {$e->retryAfter} seconds";
sleep($e->retryAfter);
} catch (ConfigurationException $e) {
echo "Check your API key configuration";
} catch (AIException $e) {
echo "AI error: " . $e->getMessage();
}
bash
# Interactive chat session
php bin/marwa-ai chat --provider=openai
# Single question
php bin/marwa-ai ask "What is PHP?"
# List available providers
php bin/marwa-ai providers
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.