langgraph/langgraph-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

langgraph/langgraph-php

Composer 安装命令:

composer require langgraph/langgraph-php

包简介

A PHP implementation of LangGraph for building language model workflows

README 文档

README

Latest Version on Packagist

A powerful PHP implementation of Python's LangGraph. This SDK enables you to build stateful, multi-agent applications with complex, graph-based logic. It provides the core components to create robust and scalable AI workflows in your PHP projects.

✨ Core Features

  • Stateful Graphs: Create graphs where state is passed between nodes, allowing for complex, long-running workflows.
  • Node & Edge Control: Define nodes as units of work and use edges to control the flow, including conditional branching.
  • Agent System: A built-in factory for creating and managing AI agents.
  • AI Model Integration: Easily connect to various large language models (e.g., DeepSeek, Qwen).
  • Persistence (Checkpointing): Save and restore the state of your graphs, allowing workflows to be paused and resumed.
  • Streaming: Support for real-time streaming of responses from nodes.

🚀 Installation

You can install the package via Composer:

composer require langgraph/langgraph-php

🛠️ Usage & Functionality

Here’s how to use the core features of the LangGraph PHP SDK.

1. Creating a Basic Workflow

The fundamental building block is the StateGraph. You define a state class, add nodes (functions or callables), and connect them with edges.

use LangGraph\UnifiedGraph\StateGraph;
use LangGraph\UnifiedGraph\State\State;

// 1. Define the state structure for your graph
class MyState extends State {
    // Your state properties here
}

// 2. Initialize the graph with the state class
$graph = new StateGraph(MyState::class);

// 3. Add nodes, which are functions that modify the state
$graph->addNode('start', function (MyState $state) {
    echo "Executing node: start\n";
    $state->set('message', 'Hello from the start!');
    return $state;
});

$graph->addNode('middle', function (MyState $state) {
    echo "Executing node: middle\n";
    $state->set('message', $state->get('message') . ' ... and now the middle!');
    return $state;
});

$graph->addNode('end', function (MyState $state) {
    echo "Executing node: end\n";
    $state->set('message', $state->get('message') . ' ... finished!');
    return $state;
});

// 4. Define the workflow by adding edges
$graph->addEdge('start', 'middle');
$graph->addEdge('middle', 'end');

// 5. Set the entry and finish points
$graph->setEntryPoint('start');
$graph->setFinishPoint('end');

// 6. Compile the graph and run it
$runnable = $graph->compile();
$initialState = new MyState(['value' => 1]);
$finalState = $runnable->execute($initialState);

print_r($finalState->getData());
// Outputs: [ 'value' => 1, 'message' => 'Hello from the start! ... and now the middle! ... finished!' ]

2. Conditional Logic

You can direct the flow of your graph based on the current state using conditional edges. This allows for dynamic, branching workflows.

// A function to decide the next step
$decider = function (MyState $state) {
    if ($state->get('value', 0) > 5) {
        return 'end_workflow';
    } else {
        return 'continue_processing';
    }
};

// Add a conditional edge from 'start'
$graph->addConditionalEdges('start', $decider, [
    'end_workflow' => 'end',
    'continue_processing' => 'middle',
]);

3. Using AI Agents

The SDK includes an AgentFactory to easily create model-based agents that can be used as nodes in your graph.

First, configure your model API keys in config/model.php.

use LangGraph\Agent\AgentFactory;
use LangGraph\Model\Factory\ModelFactory;
use LangGraph\Model\Config\ModelConfig;

// Setup factories
$modelConfig = new ModelConfig(require 'config/model.php');
$modelFactory = new ModelFactory($modelConfig->all());
$agentFactory = new AgentFactory($modelFactory);

// Create an agent
$researcherAgent = $agentFactory->createModelBasedAgent(
    'researcher',
    'deepseek', // Or another model like 'qwen'
    'You are a helpful research assistant.'
);

// Use the agent to process a query
$response = $researcherAgent->execute('What is LangGraph?');
echo $response;

4. Persistence with Checkpoints

Save the state of a workflow at any point and resume it later. The SDK includes a MemoryCheckpointSaver for simple use cases.

use LangGraph\UnifiedGraph\Checkpoint\MemoryCheckpointSaver;

// Set a checkpoint saver on the graph
$checkpointSaver = new MemoryCheckpointSaver();
$graph->setCheckpointSaver($checkpointSaver);

$runnable = $graph->compile();

// Run the workflow with a unique ID to enable checkpointing
$threadId = 'user_session_123';
$initialState = new MyState(['value' => 1]);
$finalState = $runnable->execute($initialState, $threadId);

// You can now retrieve checkpoints for this thread
$checkpoints = $checkpointSaver->list($threadId);

📚 Documentation

For detailed documentation and FAQs, please refer to our documentation:

Advanced Examples

For more complex examples, including multi-agent collaboration, streaming, and web integrations, please explore the scripts in the bin/ and examples/ directories.

🤝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

📄 License

This project is licensed under the MIT License. See the LICENSE file for more information.

langgraph/langgraph-php 适用场景与选型建议

langgraph/langgraph-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「workflow」 「Agent」 「ai」 「chatbot」 「llm」 「language-model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 langgraph/langgraph-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 langgraph/langgraph-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 langgraph/langgraph-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-26