Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"test:lint": "@php vendor/bin/pint --config https://raw.githubusercontent.com/DIJ-digital/pint-config/main/pint.json",
"test:unit": "pest",
"test:types": "phpstan",
"test:refactor": "rector --dry-run",
"test:refactor": "rector",
"test": [
"@test:lint",
"@test:type-coverage",
Expand Down
16 changes: 8 additions & 8 deletions src/Contracts/TransporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
interface TransporterInterface
{
/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function request(string $method, string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function get(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function post(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $data
* @param array<string, mixed> $options
* @param array<string, mixed> $data
* @param array<string, mixed> $options
*/
public function postJson(string $uri, array $data = [], array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function delete(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $data
* @param array<string, mixed> $options
* @param array<string, mixed> $data
* @param array<string, mixed> $options
*/
public function patchJson(string $uri, array $data = [], array $options = []): ResponseInterface;
}
10 changes: 8 additions & 2 deletions src/Langfuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public function __construct(
private readonly TransporterInterface $transporter,
private readonly string $environment = 'default',
private readonly string $label = 'latest',
) {
}
) {}

public function prompt(): Prompt
{
Expand All @@ -37,4 +36,11 @@ public function score(): Score
transporter: $this->transporter,
);
}

public function scoreConfig(): ScoreConfig
{
return new ScoreConfig(
transporter: $this->transporter,
);
}
}
44 changes: 44 additions & 0 deletions src/Responses/ScoreConfigListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Responses;

use DIJ\Langfuse\PHP\ValueObjects\MetaData;

readonly class ScoreConfigListResponse
{
/**
* @param array<int, ScoreConfigResponse> $data
*/
public function __construct(
public array $data,
public MetaData $meta,
) {}

/**
* @param array{
* data: array<int, array{
* id: string,
* name: string,
* dataType: string,
* isArchived: bool,
* minValue: float|null,
* maxValue: float|null,
* categories: array<int, array{value: float, label: string}>|null,
* description: string|null,
* projectId: string,
* createdAt: string,
* updatedAt: string
* }>,
* meta: array{page: int, limit: int, totalPages: int, totalItems: int}
* } $data
*/
public static function fromArray(array $data): self
{
return new self(
data: array_map(fn (array $item): ScoreConfigResponse => ScoreConfigResponse::fromArray($item), $data['data']),
meta: MetaData::fromArray($data['meta']),
);
}
}
57 changes: 57 additions & 0 deletions src/Responses/ScoreConfigResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Responses;

readonly class ScoreConfigResponse
{
/**
* @param array<int, array{value: float, label: string}>|null $categories
*/
public function __construct(
public string $id,
public string $name,
public string $dataType,
public bool $isArchived,
public ?float $minValue,
public ?float $maxValue,
public ?array $categories,
public ?string $description,
public string $projectId,
public string $createdAt,
public string $updatedAt,
) {}

/**
* @param array{
* id: string,
* name: string,
* dataType: string,
* isArchived: bool,
* minValue: float|null,
* maxValue: float|null,
* categories: array<int, array{value: float, label: string}>|null,
* description: string|null,
* projectId: string,
* createdAt: string,
* updatedAt: string
* } $data
*/
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
name: $data['name'],
dataType: $data['dataType'],
isArchived: $data['isArchived'],
minValue: $data['minValue'],
maxValue: $data['maxValue'],
categories: $data['categories'],
description: $data['description'],
projectId: $data['projectId'],
createdAt: $data['createdAt'],
updatedAt: $data['updatedAt'],
);
}
}
124 changes: 124 additions & 0 deletions src/ScoreConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP;

use DIJ\Langfuse\PHP\Contracts\TransporterInterface;
use DIJ\Langfuse\PHP\Responses\ScoreConfigListResponse;
use DIJ\Langfuse\PHP\Responses\ScoreConfigResponse;
use JsonException;

class ScoreConfig
{
public function __construct(private readonly TransporterInterface $transporter) {}

/**
* @throws JsonException
*/
public function get(string $configId): ScoreConfigResponse
{
$response = $this->transporter->get(
uri: sprintf('/api/public/score-configs/%s', urlencode($configId)),
);

/** @var array{
* id: string,
* name: string,
* dataType: string,
* isArchived: bool,
* minValue: float|null,
* maxValue: float|null,
* categories: array<int, array{value: float, label: string}>|null,
* description: string|null,
* projectId: string,
* createdAt: string,
* updatedAt: string
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return ScoreConfigResponse::fromArray($data);
}

/**
* @throws JsonException
*/
public function list(
?int $page = null,
?int $limit = null,
): ScoreConfigListResponse {
$response = $this->transporter->get(
uri: '/api/public/score-configs',
options: ['query' => array_filter([
'page' => $page,
'limit' => $limit,
])]
);

/** @var array{
* data: array<int, array{
* id: string,
* name: string,
* dataType: string,
* isArchived: bool,
* minValue: float|null,
* maxValue: float|null,
* categories: array<int, array{value: float, label: string}>|null,
* description: string|null,
* projectId: string,
* createdAt: string,
* updatedAt: string
* }>,
* meta: array{page: int, limit: int, totalPages: int, totalItems: int}
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return ScoreConfigListResponse::fromArray($data);
}

/**
* @param array<int, array{value: float, label: string}>|null $categories
*
* @throws JsonException
*/
public function create(
string $name,
string $dataType,
?float $minValue = null,
?float $maxValue = null,
?array $categories = null,
?string $description = null,
): ScoreConfigResponse {
$response = $this->transporter->postJson(
uri: '/api/public/score-configs',
data: array_filter([
'name' => $name,
'dataType' => $dataType,
'minValue' => $minValue,
'maxValue' => $maxValue,
'categories' => $categories,
'description' => $description,
], fn ($v) => $v !== null),
);

/** @var array{
* id: string,
* name: string,
* dataType: string,
* isArchived: bool,
* minValue: float|null,
* maxValue: float|null,
* categories: array<int, array{value: float, label: string}>|null,
* description: string|null,
* projectId: string,
* createdAt: string,
* updatedAt: string
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return ScoreConfigResponse::fromArray($data);
}
}
65 changes: 65 additions & 0 deletions src/Testing/Responses/GetScoreConfigListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Testing\Responses;

use GuzzleHttp\Psr7\Response;

class GetScoreConfigListResponse extends Response
{
/**
* @param array<array<string>|string> $headers
*/
public function __construct(int $status = 200, array $headers = [], string $version = '1.1', ?string $reason = null)
{
parent::__construct($status, $headers, (string) json_encode($this->payload()), $version, $reason);
}

/**
* @return array<string, mixed>
*/
public function payload(): array
{
return [
'data' => [
[
'id' => 'config-abc123',
'name' => 'quality-score',
'dataType' => 'NUMERIC',
'isArchived' => false,
'minValue' => 0.0,
'maxValue' => 1.0,
'categories' => null,
'description' => 'Quality score for LLM outputs',
'projectId' => 'proj-abc123',
'createdAt' => '2025-01-22T10:00:00.000Z',
'updatedAt' => '2025-01-22T10:00:00.000Z',
],
[
'id' => 'config-def456',
'name' => 'sentiment',
'dataType' => 'CATEGORICAL',
'isArchived' => false,
'minValue' => null,
'maxValue' => null,
'categories' => [
['value' => 1.0, 'label' => 'positive'],
['value' => 0.0, 'label' => 'neutral'],
['value' => -1.0, 'label' => 'negative'],
],
'description' => 'Sentiment classification',
'projectId' => 'proj-abc123',
'createdAt' => '2025-01-22T09:00:00.000Z',
'updatedAt' => '2025-01-22T09:00:00.000Z',
],
],
'meta' => [
'page' => 1,
'limit' => 10,
'totalPages' => 1,
'totalItems' => 2,
],
];
}
}
Loading