PHP code example of avocet-shores / laravel-conduit
1. Go to this page and download the library: Download avocet-shores/laravel-conduit 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/ */
$response = Conduit::make('openai', 'gpt-4o')
->withInstructions(
"Write a haiku about the user's input. " .
'Return your response in the JSON format { "haiku": string }.'
)
->addMessage('Laravel Conduit', Role::USER)
->withJsonOutput() // Automatically decodes the JSON into the response object
->run();
/**
* Code flows in bridging
* Weave AI in Laravel
* Seamless paths converge
*/
echo $response->outputArray['haiku'];
use AvocetShores\Conduit\Enums\Role;
$response = Conduit::make('openai', 'gpt-4o')
->withFallback('amazon_bedrock', 'claude sonnet 3.5 v2')
->withInstructions('You are a helpful assistant.')
->addMessage('Hello from Conduit!', Role::USER)
->run();
use AvocetShores\Conduit\Features\StructuredOutputs\Schema;
$response = Conduit::make('openai', 'gpt-4o')
->withInstructions('Please return a JSON object following the schema.')
->enableStructuredOutput(new Schema(/* ... */))
->run();
new Schema(
name: 'research_paper_extraction',
description: 'Extract the title, authors, and abstract from a research paper.',
properties: [
Input::string('title', 'The title of the research paper.'),
Input::array('authors', 'The authors of the research paper.', [Input::string()]),
Input::string('abstract', 'The abstract of the research paper.')
]
);
$response = Conduit::make('openai', 'gpt-4o')
->withInstructions('Please return a JSON object in the format { "haiku": string }.')
->pushMiddleware(function (AIRequestContext $context, $next) {
// Validate the response
$response = $next($context);
if (!isset($response->outputArray['haiku'])) {
throw new Exception('The AI response did not match the expected schema.');
}
return $response;
})
->withJsonOutput()
->run();
$response = Conduit::make('openai', 'gpt-4o')
->pushMiddleware(function (AIRequestContext $context, $next) {
// Example: remove SSN or credit card info from messages
$messages = $context->getMessages();
foreach ($messages as &$message) {
$message->content = preg_replace('/\d{3}-\d{2}-\d{4}/', '[REDACTED_SSN]', $message->content);
$message->content = preg_replace('/\b\d{16}\b/', '[REDACTED_CREDIT_CARD]', $message->content);
}
$context->setMessages($messages);
// Continue down the pipeline
return $next($context);
})
->withInstructions('')
->run();
$response = Conduit::make('openai', 'gpt-4o')
->pushMiddleware(function (AIRequestContext $context, $next) {
// Log the request
Log::info('AI Request Messages: ', $context->getMessages());
// Add some metadata
$context->setMetadata('request_time', now());
// Continue down the pipeline
return $next($context);
})
->pushMiddleware(function (AIRequestContext $context, $next) {
// Run after the AI request is completed
$response = $next($context);
// Log the response and use the metadata from the previous middleware
Log::info('AI Response: ', [
'response' => $response->outputArray,
'execution_time' => now() - $context->getMetadata('request_time')
]);
return $response;
})
->withInstructions('')
->run();
class MyCustomDriver implements DriverInterface
{
public function run(AIRequestContext $context): ConversationResponse
{
// Make request to your custom AI service
// Parse and return in a ConversationResponse
return new ConversationResponse(
outputArray: json_decode($response->getBody(), true)
);
}
}