PHP code example of asterixcapri / neuron-interaction
1. Go to this page and download the library: Download asterixcapri/neuron-interaction 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/ */
asterixcapri / neuron-interaction example snippets
use NeuronAI\Agent\Agent;
use NeuronInteraction\Session\SessionStore;
use NeuronInteraction\Storage\FileStorage;
$storage = new FileStorage(__DIR__ . '/interaction-state');
$sessionStore = new SessionStore($storage, 'local-user');
$agent = Agent::make();
$agent = $sessionStore->create()->bindTo($agent);
use NeuronAI\Agent\Agent;
use NeuronAI\Chat\Messages\Stream\Chunks\TextChunk;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\HttpClient\Curl\CurlHttpClient;
use NeuronAI\Providers\OpenAI\Responses\OpenAIResponses;
use NeuronAI\HttpClient\StoppableHttpClient;
use NeuronInteraction\Interruption\StopSignal;
use NeuronInteraction\Storage\FileStorage;
$storage = new FileStorage(__DIR__ . '/interaction-state');
$stopSignal = new StopSignal(storage: $storage, key: $chatId);
$client = new StoppableHttpClient(
client: new CurlHttpClient(),
shouldStop: $stopSignal->stopCallback(),
);
$agent = Agent::make()->setThreadId($chatId);
$agent->setAiProvider(new OpenAIResponses(
key: $apiKey,
model: $model,
httpClient: $client,
));
// Clear any previous stop request before starting a new response.
$stopSignal->clear();
$stream = $agent->stream(new UserMessage($prompt));
foreach ($stream as $event) {
if ($event instanceof TextChunk) {
echo $event->content;
}
}
$response = $stream->getReturn()->getMessage();
$stopSignal = new StopSignal(
storage: new FileStorage(__DIR__ . '/interaction-state'),
key: $chatId,
);
$stopSignal->request();
use NeuronAI\Chat\Messages\UserMessage;
use NeuronInteraction\InputHistory\InputHistory;
$inputs = new InputHistory($storage);
$inputs->record(new UserMessage('/resume session-key'));
$inputs->record(new UserMessage('A message exactly as submitted'));
$submitted = $inputs->entries(); // Oldest first, across sessions.
$recalled = $inputs->older(new UserMessage('Unsubmitted draft'));
$newer = $inputs->newer(); // Restores the draft past the newest input.
use NeuronInteraction\Command\Commands;
use NeuronInteraction\Command\HelpCommand;
use NeuronInteraction\Command\LeaveCommand;
use NeuronInteraction\Command\ClearCommand;
use NeuronInteraction\Command\ResumeCommand;
$commands = (new Commands())->addCommand([
new ClearCommand(),
new ResumeCommand(),
new HelpCommand(),
new LeaveCommand(),
]);
// $adapter connects the Commands to your application.
$output = $commands->run('/resume', '', $adapter);
use NeuronInteraction\Command\CommandAdapterInterface;
use NeuronInteraction\Command\CommandInterface;
final class HelloCommand implements CommandInterface
{
public function name(): string
{
return '/hello';
}
public function describe(): string
{
return 'Say hello.';
}
public function run(CommandAdapterInterface $adapter, string $value): void
{
$adapter->notify('Hello!');
}
}
$commands->addCommand(new HelloCommand());
use NeuronInteraction\Configuration\ConfigurationStore;
$settings = new ConfigurationStore($storage, 'local-user');
$settings->write('model', 'chosen-model');
$model = $settings->read('model', 'default-model');
$processors = (new UserMessageProcessors())
->addProcessor($first)
->addProcessor([$second, $third]);