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 model(): Model
{
return new Model(
transporter: $this->transporter,
);
}
}
138 changes: 138 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP;

use DIJ\Langfuse\PHP\Contracts\TransporterInterface;
use DIJ\Langfuse\PHP\Responses\ModelListResponse;
use DIJ\Langfuse\PHP\Responses\ModelResponse;
use JsonException;

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

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

/** @var array{
* id: string,
* modelName: string,
* matchPattern: string,
* startDate: string|null,
* unit: string|null,
* inputPrice: float|null,
* outputPrice: float|null,
* totalPrice: float|null,
* tokenizerId: string|null,
* tokenizerConfig: mixed,
* isLangfuseManaged: bool,
* createdAt: string
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return ModelResponse::fromArray($data);
}

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

/** @var array{
* data: array<int, array{
* id: string,
* modelName: string,
* matchPattern: string,
* startDate: string|null,
* unit: string|null,
* inputPrice: float|null,
* outputPrice: float|null,
* totalPrice: float|null,
* tokenizerId: string|null,
* tokenizerConfig: mixed,
* isLangfuseManaged: bool,
* createdAt: 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 ModelListResponse::fromArray($data);
}

/**
* @throws JsonException
*/
public function create(
string $modelName,
string $matchPattern,
?string $startDate = null,
?string $unit = null,
?float $inputPrice = null,
?float $outputPrice = null,
?float $totalPrice = null,
?string $tokenizerId = null,
mixed $tokenizerConfig = null,
): ModelResponse {
$response = $this->transporter->postJson(
uri: '/api/public/models',
data: array_filter([
'modelName' => $modelName,
'matchPattern' => $matchPattern,
'startDate' => $startDate,
'unit' => $unit,
'inputPrice' => $inputPrice,
'outputPrice' => $outputPrice,
'totalPrice' => $totalPrice,
'tokenizerId' => $tokenizerId,
'tokenizerConfig' => $tokenizerConfig,
], fn ($v) => $v !== null),
);

/** @var array{
* id: string,
* modelName: string,
* matchPattern: string,
* startDate: string|null,
* unit: string|null,
* inputPrice: float|null,
* outputPrice: float|null,
* totalPrice: float|null,
* tokenizerId: string|null,
* tokenizerConfig: mixed,
* isLangfuseManaged: bool,
* createdAt: string
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return ModelResponse::fromArray($data);
}

public function delete(string $modelId): void
{
$this->transporter->delete(
uri: sprintf('/api/public/models/%s', urlencode($modelId)),
);
}
}
48 changes: 48 additions & 0 deletions src/Responses/ModelListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Responses;

use DIJ\Langfuse\PHP\ValueObjects\MetaData;

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

/**
* @param array{
* data: array<int, array{
* id: string,
* modelName: string,
* matchPattern: string,
* startDate: string|null,
* unit: string|null,
* inputPrice: float|null,
* outputPrice: float|null,
* totalPrice: float|null,
* tokenizerId: string|null,
* tokenizerConfig: mixed,
* isLangfuseManaged: bool,
* createdAt: 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): ModelResponse => ModelResponse::fromArray($item),
$data['data']
),
meta: MetaData::fromArray($data['meta']),
);
}
}
57 changes: 57 additions & 0 deletions src/Responses/ModelResponse.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 ModelResponse
{
public function __construct(
public string $id,
public string $modelName,
public string $matchPattern,
public ?string $startDate,
public ?string $unit,
public ?float $inputPrice,
public ?float $outputPrice,
public ?float $totalPrice,
public ?string $tokenizerId,
public mixed $tokenizerConfig,
public bool $isLangfuseManaged,
public string $createdAt,
) {}

/**
* @param array{
* id: string,
* modelName: string,
* matchPattern: string,
* startDate: string|null,
* unit: string|null,
* inputPrice: float|null,
* outputPrice: float|null,
* totalPrice: float|null,
* tokenizerId: string|null,
* tokenizerConfig: mixed,
* isLangfuseManaged: bool,
* createdAt: string
* } $data
*/
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
modelName: $data['modelName'],
matchPattern: $data['matchPattern'],
startDate: $data['startDate'],
unit: $data['unit'],
inputPrice: $data['inputPrice'],
outputPrice: $data['outputPrice'],
totalPrice: $data['totalPrice'],
tokenizerId: $data['tokenizerId'],
tokenizerConfig: $data['tokenizerConfig'],
isLangfuseManaged: $data['isLangfuseManaged'],
createdAt: $data['createdAt'],
);
}
}
18 changes: 18 additions & 0 deletions src/Testing/Responses/DeleteModelResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Testing\Responses;

use GuzzleHttp\Psr7\Response;

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