dij-digital/langfuse-php
Composer 安装命令:
composer require dij-digital/langfuse-php
包简介
A langfuse wrapper for PHP
README 文档
README
This package provides a wrapper around the Langfuse API, allowing you to easily integrate Langfuse into your PHP applications. It uses as few dependencies as possible.
This package supports the following features:
Prompts
- Get text prompts
- Get chat prompts
- Compile text prompts
- Compile chat prompts
- Create text prompts
- Create chat prompts
- List prompts (auto-paginated)
- Update prompt labels
- Fallback handling for prompt fetching errors
- Fallback handling when no prompt is found
Ingestion
- Create and update traces
- Create and update spans (with nesting)
- Create and update generations
- Automatic
traceIdandparentObservationIdthreading - Sends directly to the Langfuse v2 ingestion API
Scores
- Create scores
- Get scores
- List scores
- Delete scores
- V2 API support for scores
Install the package using Composer:
composer require dij-digital/langfuse-php
How to use this package
Setup
use DIJ\Langfuse\PHP\Langfuse; use DIJ\Langfuse\PHP\Transporters\HttpTransporter; use GuzzleHttp\Client; $langfuse = new Langfuse( transporter: new HttpTransporter(new Client([ 'base_uri' => 'https://cloud.langfuse.com', 'auth' => ['PUBLIC_KEY', 'SECRET_KEY'], ])), environment: 'production', // optional, defaults to 'default' );
Prompts
// Get and compile prompts $langfuse->prompt()->text(promptName: 'promptName')->compile(params: ['key' => 'value']); $langfuse->prompt()->chat(promptName: 'chatName')->compile(params: ['key' => 'value']); // List all prompts (returns a Generator that auto-paginates) foreach ($langfuse->prompt()->list() as $prompt) { echo $prompt->name; } // Create a prompt $langfuse->prompt()->create(promptName: 'promptName', prompt: 'text', type: PromptType::TEXT); // Update prompt labels $langfuse->prompt()->update(promptName: 'promptName', version: 1, labels: ['production']);
Ingestion
Every call to trace(), span(), or generation() immediately sends a request to the Langfuse ingestion API. No buffering, no flushing required.
$ingestion = $langfuse->ingestion();
Trace
A trace is the root of an observation tree.
$trace = $ingestion->trace( name: 'my-workflow', userId: 'user-456', input: 'user question', ); // Update the trace (sends immediately) $trace->update( output: 'final answer', metadata: ['duration_ms' => 1234], );
Span
Spans group related work within a trace. Create them from a Trace or another Span -- traceId and parentObservationId are set automatically.
// Create a span from the trace $span = $trace->span(name: 'web-search-batch'); // Nest a child span under the parent span $childSpan = $span->span(name: 'single-search'); // Update spans when work is done $childSpan->update(output: ['results' => 3], endTime: date('c')); $span->update(output: ['total' => 3], endTime: date('c'));
Generation
Generations represent LLM calls. Create them from a Trace or Span -- context IDs are threaded automatically.
// Generation on a trace $gen = $trace->generation( input: ['messages' => [['role' => 'user', 'content' => 'Hello']]], output: 'Hi there!', name: 'llm-call', model: 'gpt-4o', modelParameters: ['temperature' => 0.7], promptName: 'my-prompt', promptVersion: 1, ); // Generation nested under a span $gen = $span->generation( input: 'summarize this', output: 'summary text', name: 'summarize-call', model: 'gpt-4o', ); // Update a generation after the LLM responds $gen->update( output: 'updated response', metadata: ['tokens' => 150], );
Full example
$ingestion = $langfuse->ingestion(); $trace = $ingestion->trace( name: 'handle-request', userId: 'user-789', input: 'What is the weather?', ); $span = $trace->span(name: 'search-batch'); $child = $span->span(name: 'weather-api-call'); $child->update(output: ['temp' => 22], endTime: date('c')); $span->generation( input: 'Summarize weather data', output: 'It is 22 degrees and sunny.', name: 'summarize', model: 'gpt-4o', ); $span->update(output: ['answer' => 'It is 22 degrees.'], endTime: date('c')); $trace->update(output: 'It is 22 degrees and sunny.');
Scores
use DIJ\Langfuse\PHP; use DIJ\Langfuse\PHP\Enums\ScoreDataType; // Create a score $score = $langfuse->score()->create( traceId: 'trace-id-123', name: 'accuracy', value: 0.95, dataType: ScoreDataType::NUMERIC, comment: 'High accuracy score' ); // Get a specific score (using v2 API) $score = $langfuse->score()->get('score-id-123'); // List scores with filters (using v2 API) $scores = $langfuse->score()->list( traceId: 'trace-id-123', dataType: ScoreDataType::NUMERIC, limit: 10 ); // Delete a score $langfuse->score()->delete('score-id-123');
Architecture
Langfuse(transporter, environment?)
├── prompt() → Prompt
│ ├── text() → TextPromptResponse|FallbackPrompt
│ ├── chat() → ChatPromptResponse|FallbackPrompt
│ ├── list() → Generator<PromptListItem>
│ ├── create() → TextPromptResponse|ChatPromptResponse
│ └── update() → TextPromptResponse|ChatPromptResponse
├── ingestion() → Ingestion
│ ├── trace() → Trace
│ │ ├── update()
│ │ ├── span() → Span
│ │ └── generation() → Generation
│ ├── span() → Span
│ │ ├── update()
│ │ ├── span() → Span
│ │ └── generation() → Generation
│ └── generation() → Generation
│ └── update()
└── score() → Score
├── create()
├── get()
├── list()
└── delete()
Each trace(), span(), generation(), and update() call sends a request to the Langfuse POST /api/public/ingestion endpoint immediately.
Langfuse PHP was created by Tycho Engberink and is maintained by DIJ Digital under the MIT license.
dij-digital/langfuse-php 适用场景与选型建议
dij-digital/langfuse-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.49k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2025 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「langfuse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dij-digital/langfuse-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dij-digital/langfuse-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dij-digital/langfuse-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP SDK for the LangFuse API
This is my package langfuse-laravel
Seamless integration of Langfuse observability for Laravel and Laravel Prism.
Alfabank REST API integration
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
A lightweight PHP logger for Langfuse to track LLM API calls (OpenAI, Claude, Gemini, Grok) with token usage analytics
统计信息
- 总下载量: 8.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-24