1. Go to this page and download the library: Download papi-ai/papi-core 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/ */
papi-ai / papi-core example snippets
use PapiAI\Core\Agent;
use PapiAI\Anthropic\AnthropicProvider;
$agent = new Agent(
provider: new AnthropicProvider(apiKey: $_ENV['ANTHROPIC_API_KEY']),
model: 'claude-sonnet-4-20250514',
instructions: 'You are a helpful assistant.',
);
$response = $agent->run('What is 2 + 2?');
echo $response->text; // "4"
use PapiAI\Core\Tool;
$weatherTool = Tool::make(
name: 'get_weather',
description: 'Get current weather for a city',
parameters: [
'city' => ['type' => 'string', 'description' => 'City name'],
],
handler: fn(array $args) => fetchWeather($args['city']),
);
$agent = new Agent(
provider: $provider,
model: 'claude-sonnet-4-20250514',
tools: [$weatherTool],
);
$response = $agent->run('What is the weather in London?');
use PapiAI\Core\Tool;
use PapiAI\Core\Attributes\Tool as ToolAttr;
use PapiAI\Core\Attributes\Description;
class WebTools
{
#[ToolAttr('Fetch content from a URL')]
public function fetchUrl(
#[Description('The URL to fetch')] string $url,
#[Description('Timeout in seconds')] int $timeout = 30,
): string {
return file_get_contents($url);
}
#[ToolAttr('Search the web')]
public function search(string $query, int $limit = 10): array
{
// Implementation
}
}
$agent = new Agent(
provider: $provider,
model: 'claude-sonnet-4-20250514',
tools: Tool::fromClass(WebTools::class),
);
use PapiAI\Core\Message;
use PapiAI\Core\Conversation;
// Individual messages
$message = Message::user('Hello');
$message = Message::system('You are helpful');
$message = Message::assistant('Hi there!');
$message = Message::userWithImage('What is this?', $imageUrl);
// Conversation helper
$conversation = new Conversation();
$conversation->setSystem('You are a helpful assistant');
$conversation->addUser('Hello');
$conversation->addAssistant('Hi! How can I help?');
$conversation->addUser('Tell me a joke');
$messages = $conversation->getMessages();
$agent = new Agent(
provider: $provider,
model: 'claude-sonnet-4-20250514',
instructions: 'System prompt here',
tools: [...],
hooks: [...],
maxTokens: 4096, // Max response tokens
temperature: 0.7, // 0.0 = deterministic, 1.0 = creative
maxTurns: 10, // Max tool-use loops before stopping
);
use PapiAI\Anthropic\AnthropicProvider;
$provider = new AnthropicProvider(
apiKey: $_ENV['ANTHROPIC_API_KEY'],
defaultModel: 'claude-sonnet-4-20250514',
);
use PapiAI\Google\GoogleProvider;
$provider = new GoogleProvider(
apiKey: $_ENV['GOOGLE_API_KEY'],
defaultModel: GoogleProvider::MODEL_3_6_FLASH,
);
// Available models
GoogleProvider::MODEL_3_6_FLASH // gemini-3.6-flash (default)
GoogleProvider::MODEL_3_5_FLASH // gemini-3.5-flash
GoogleProvider::MODEL_3_1_PRO // gemini-3.1-pro-preview
GoogleProvider::MODEL_2_5_PRO // gemini-2.5-pro
GoogleProvider::MODEL_2_0_FLASH // gemini-2.0-flash
use PapiAI\Core\Contracts\ProviderInterface;
use PapiAI\Core\Response;
class MyProvider implements ProviderInterface
{
public function chat(array $messages, array $options = []): Response
{
// Make API call and return Response
}
public function stream(array $messages, array $options = []): iterable
{
// Yield StreamChunk objects
}
public function supportsTool(): bool { return true; }
public function supportsVision(): bool { return true; }
public function supportsStructuredOutput(): bool { return false; }
public function getName(): string { return 'my-provider'; }
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.