PHP code example of assistant-engine / laravel-assistant
1. Go to this page and download the library: Download assistant-engine/laravel-assistant 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/ */
assistant-engine / laravel-assistant example snippets
use AssistantEngine\SDK\Models\Options\ConversationOption;
// Create a new ConversationOption
$options = new ConversationOption('assistant_key', [
'user_id' => 'user123',
'subject_id' => 'subject456',
'title' => 'New Conversation',
'context' => ['topic' => 'tech support'],
'additional_data' => ['foo' => 'bar'],
'recreate' => true,
]);
#[On(ChatComponent::EVENT_RUN_FINISHED)]
public function onRunFinished()
{
// Handle run finished event
}
// Emitted when the conversation is reset
ChatComponent::EVENT_CONVERSATION_RESET;
// Emitted when a conversation run is completed
ChatComponent::EVENT_RUN_FINISHED;
// Emitted to prompt the UI to scroll to the latest message
ChatComponent::EVENT_SHOULD_SCROLL;
#[On(ChatComponent::EVENT_CONVERSATION_RESET)]
public function onConversationReset()
{
// Handle conversation reset event
}
#[On(ChatComponent::EVENT_RUN_FINISHED)]
public function onRunFinished()
{
// Handle run finished event
}
#[On(ChatComponent::EVENT_SHOULD_SCROLL)]
public function onShouldScroll()
{
// Handle scroll to the latest message event
}
// Triggered when switching between conversations
ChatComponent::EVENT_CHANGE_CONVERSATION;
// Triggered when a new message is processed
ChatComponent::EVENT_PROCESS_MESSAGE;
public function dispatchChangeConversation($conversationData)
{
// Dispatch the event to change the conversation
$conversationData = (new Conversation($conversationData))->toArray();
$this->dispatch(ChatComponent::EVENT_CHANGE_CONVERSATION, $conversationData);
}
public function dispatchProcessMessage($message)
{
// Dispatch the event to process a new message
$this->dispatch(ChatComponent::EVENT_PROCESS_MESSAGE, $message);
}
use AssistantEngine\SDK\AssistantEngine;
class TaskController
{
protected $assistantEngine;
public function __construct(AssistantEngine $assistantEngine)
{
$this->assistantEngine = $assistantEngine;
}
public function executeTask()
{
// Your logic here
}
}
use AssistantEngine\SDK\Models\Options\TaskRunOption;
$taskRunOption = new TaskRunOption(['key' => 'value']);
$taskRunResponse = $assistantEngine->initiateTaskRun('task_key', $taskRunOption);
if (!$taskRunResponse->is_running) {
echo "Task completed with output: " . $taskRunResponse->output;
}