1. Go to this page and download the library: Download helgesverre/synapse 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/ */
helgesverre / synapse example snippets
use function HelgeSverre\Synapse\{createChatPrompt, createExecutor, createParser, useLlm};
// Create provider, prompt, and parser (transport auto-discovered if available)
$llm = useLlm('openai', [
'apiKey' => getenv('OPENAI_API_KEY'),
'model' => 'gpt-4o-mini',
]);
$prompt = createChatPrompt()
->addSystemMessage('You are a helpful assistant.')
->addUserMessage('{{question}}', parseTemplate: true);
$parser = createParser('string');
// Create and execute
$executor = createExecutor([
'llm' => $llm,
'prompt' => $prompt,
'parser' => $parser,
]);
$result = $executor->run(['question' => 'What is the capital of France?']);
echo $result->getValue(); // "Paris"
use HelgeSverre\Synapse\Factory;
$client = new \GuzzleHttp\Client();
$psr17Factory = new \GuzzleHttp\Psr7\HttpFactory();
Factory::setDefaultTransport(
Factory::createTransport($client, $psr17Factory, $psr17Factory)
);
use function HelgeSverre\Synapse\{createCoreExecutor, createExecutor};
// CoreExecutor - wrap any function
$calc = createCoreExecutor(fn($input) => $input['a'] + $input['b']);
$result = $calc->run(['a' => 5, 'b' => 3]);
// LlmExecutor - full LLM pipeline
$executor = createExecutor([
'llm' => $provider,
'prompt' => $prompt,
'parser' => $parser,
'model' => 'gpt-4o-mini',
]);
use function HelgeSverre\Synapse\{createChatPrompt, createTextPrompt};
// Chat prompt (recommended)
$prompt = createChatPrompt()
->addSystemMessage('You are an expert on {{topic}}.')
->addUserMessage('{{question}}', parseTemplate: true);
// Text prompt (simple)
$prompt = createTextPrompt()
->addContent('Answer this question about {{topic}}: {{question}}');
// Render with values
$messages = $prompt->render([
'topic' => 'history',
'question' => 'Who was Napoleon?',
]);
use function HelgeSverre\Synapse\{createExecutor, createToolRegistry};
$tools = createToolRegistry([
[
'name' => 'get_weather',
'description' => 'Get weather for a location',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => ['type' => 'string'],
],
'
use GuzzleHttp\Client;
use HelgeSverre\Synapse\Executor\StreamingLlmExecutor;
use HelgeSverre\Synapse\Prompt\TextPrompt;
use HelgeSverre\Synapse\Provider\Http\GuzzleStreamTransport;
use HelgeSverre\Synapse\Streaming\TextDelta;
$transport = new GuzzleStreamTransport(new Client(['timeout' => 60]));
$llm = useLlm('openai', [
'apiKey' => getenv('OPENAI_API_KEY'),
'model' => 'gpt-4o-mini',
'transport' => $transport,
]);
$prompt = (new TextPrompt)->setContent('Write a haiku about PHP.');
$executor = new StreamingLlmExecutor($llm, $prompt, 'gpt-4o-mini');
foreach ($executor->stream([]) as $event) {
if ($event instanceof TextDelta) {
echo $event->text;
}
}
use HelgeSverre\Synapse\State\{ConversationState, Message, ContextItem};
// Create state
$state = new ConversationState();
// Add messages
$state = $state
->withMessage(Message::user('Hello'))
->withMessage(Message::assistant('Hi there!'));
// Add context
$state = $state->withContext(new ContextItem('user_id', '12345'));
// Add attributes
$state = $state->withAttribute('session_start', time());
// Use in prompt
$prompt = createChatPrompt()
->addSystemMessage('You are helpful.')
->addHistoryPlaceholder('history')
->addUserMessage('{{message}}', parseTemplate: true);
$result = $executor->run([
'history' => $state->messages,
'message' => 'What did I say?',
]);
use function HelgeSverre\Synapse\useEmbeddings;
$embeddings = useEmbeddings('openai', [
'apiKey' => getenv('OPENAI_API_KEY'),
]);
$response = $embeddings->embed(
'The quick brown fox jumps over the lazy dog.',
'text-embedding-3-small',
);
$vector = $response->getEmbedding();
use HelgeSverre\Synapse\Provider\{LlmProviderInterface, ProviderCapabilities};
use HelgeSverre\Synapse\Provider\Request\GenerationRequest;
use HelgeSverre\Synapse\Provider\Response\GenerationResponse;
class MyProvider implements LlmProviderInterface
{
public function generate(GenerationRequest $request): GenerationResponse { ... }
public function getCapabilities(): ProviderCapabilities { ... }
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.