PHP code example of jasontame / langgraph-client-php
1. Go to this page and download the library: Download jasontame/langgraph-client-php 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/ */
jasontame / langgraph-client-php example snippets
use JasonTame\LangGraphClient\Facades\LangGraphClient;
// Create an assistant
$assistant = LangGraphClient::assistants()->create([
'graph_id' => 'my-graph',
'name' => 'My Assistant',
'config' => ['recursion_limit' => 10],
'metadata' => ['version' => '1.0']
]);
// Create a thread
$thread = LangGraphClient::threads()->create([
'metadata' => ['user_id' => 'user123']
]);
// Run the assistant
$run = LangGraphClient::runs()->create($thread['thread_id'], [
'assistant_id' => $assistant['assistant_id'],
'input' => ['message' => 'Hello, world!']
]);
echo "Run status: " . $run['status'];
use JasonTame\LangGraphClient\LangGraphClient;
// Create client instance
$client = new LangGraphClient([
'api_key' => 'your-api-key',
'base_url' => 'https://api.langchain.com',
'timeout' => 30,
'retries' => 3
]);
// Or use environment variables
$client = LangGraphClient::fromEnvironment();
$client = new LangGraphClient([
'api_key' => 'your-api-key',
'base_url' => 'https://custom-api.example.com',
'timeout' => 60,
'retries' => 5
]);
// Or configure after initialization
$client->configure([
'timeout' => 60,
'retries' => 5
]);
// Create an assistant
$assistant = $client->assistants()->create([
'graph_id' => 'my-graph',
'name' => 'My Assistant',
'config' => ['recursion_limit' => 10],
'metadata' => ['version' => '1.0']
]);
// Find an assistant
$assistant = $client->assistants()->find('assistant-id');
// Search assistants
$assistants = $client->assistants()->search([
'metadata' => ['version' => '1.0'],
'limit' => 10
]);
// Update an assistant
$client->assistants()->update('assistant-id', [
'name' => 'Updated Name'
]);
// Delete an assistant
$client->assistants()->delete('assistant-id');
// Create a thread
$thread = $client->threads()->create([
'metadata' => ['user_id' => 'user123']
]);
// Get thread state
$state = $client->threads()->state($thread['thread_id']);
// Update thread state
$client->threads()->updateState($thread['thread_id'], [
'values' => ['key' => 'value']
]);
// Get thread history
$history = $client->threads()->history($thread['thread_id'], [
'limit' => 10
]);
// Search threads
$threads = $client->threads()->search([
'status' => 'idle',
'limit' => 10
]);
// Create a run
$run = $client->runs()->create($thread['thread_id'], [
'assistant_id' => $assistant['assistant_id'],
'input' => ['message' => 'Hello!']
]);
// Create a run with webhook
$run = $client->runs()->create($thread['thread_id'], [
'assistant_id' => $assistant['assistant_id'],
'input' => ['message' => 'Hello!'],
'webhook' => 'https://your-app.com/webhooks/langgraph'
]);
// Stream a run
foreach ($client->runs()->stream($thread['thread_id'], [
'assistant_id' => $assistant['assistant_id'],
'input' => ['message' => 'Hello!']
]) as $event) {
echo "Event: " . $event['type'] . "\n";
print_r($event['data']);
}
// Wait for a run to complete
$result = $client->runs()->wait($thread['thread_id'], [
'assistant_id' => $assistant['assistant_id'],
'input' => ['message' => 'Hello!']
]);
// List runs
$runs = $client->runs()->list($thread['thread_id']);
// Cancel a run
$client->runs()->cancel($thread['thread_id'], $run['run_id']);
// Create a cron job
$cron = $client->crons()->create([
'assistant_id' => $assistant['assistant_id'],
'schedule' => '0 */6 * * *', // Every 6 hours
'payload' => ['task' => 'periodic_task']
]);
// List cron jobs
$crons = $client->crons()->list([
'assistant_id' => $assistant['assistant_id']
]);
// Enable/disable cron jobs
$client->crons()->enable($cron['cron_id']);
$client->crons()->disable($cron['cron_id']);
// Store data
$client->store()->put('namespace', 'key', ['data' => 'value']);
// Retrieve data
$item = $client->store()->get('namespace', 'key');
// List items in namespace
$items = $client->store()->list('namespace', ['prefix' => 'user_']);
// Delete data
$client->store()->delete('namespace', 'key');
// Batch operations
$client->store()->batchPut([
['namespace' => 'ns1', 'key' => 'key1', 'value' => 'value1'],
['namespace' => 'ns1', 'key' => 'key2', 'value' => 'value2']
]);
foreach ($client->runs()->stream($threadId, [
'assistant_id' => $assistantId,
'input' => ['message' => 'Tell me a story']
]) as $event) {
switch ($event['type']) {
case 'data':
echo "Data: " . json_encode($event['data']) . "\n";
break;
case 'message':
echo "Message: " . json_encode($event['data']) . "\n";
break;
case 'error':
echo "Error: " . json_encode($event['data']) . "\n";
break;
case 'done':
echo "Stream ended\n";
break;
}
}
// Thread-based runs with webhooks
$run = $client->runs()->create($threadId, [
'assistant_id' => $assistantId,
'input' => ['message' => 'Process this data'],
'webhook' => 'https://your-app.com/webhooks/langgraph'
]);
// Stateless runs with webhooks
$client->runs()->createStateless([
'assistant_id' => $assistantId,
'input' => ['message' => 'Process this data'],
'webhook' => 'https://your-app.com/webhooks/langgraph'
]);
use JasonTame\LangGraphClient\Exceptions\LangGraphException;
use JasonTame\LangGraphClient\Exceptions\NotFoundException;
use JasonTame\LangGraphClient\Exceptions\UnauthorizedException;
try {
$assistant = $client->assistants()->find('nonexistent-id');
} catch (NotFoundException $e) {
echo "Assistant not found: " . $e->getMessage();
} catch (UnauthorizedException $e) {
echo "Authentication failed: " . $e->getMessage();
} catch (LangGraphException $e) {
echo "API error: " . $e->getMessage();
// Get additional error details
$response = $e->getResponse();
$responseData = $e->getResponseData();
$errorType = $e->getErrorType();
}
bash
composer bash
php artisan vendor:publish --tag="langgraph-client-php-config"
bash
composer analyse