Skip to content
Draft
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"laravel/serializable-closure": "^2.0"
},
"require-dev": {
"larastan/larastan": "^3.9",
"laravel/pint": "^1.26",
"mockery/mockery": "^1.6.12",
"orchestra/testbench": "^10.6|^11.0",
"pestphp/pest": "^3.0|^4.0",
"pestphp/pest-plugin-laravel": "^3.0|^4.0",
"phpstan/phpstan": "^2.1"
"pestphp/pest-plugin-laravel": "^3.0|^4.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
7 changes: 6 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
includes:
- vendor/larastan/larastan/extension.neon

parameters:
paths:
- config
- src
- database

level: 1
level: 2

noEnvCallsOutsideOfConfig: false
2 changes: 1 addition & 1 deletion src/Contracts/Providers/AudioProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Laravel\Ai\Contracts\Gateway\AudioGateway;
use Laravel\Ai\Responses\AudioResponse;

interface AudioProvider
interface AudioProvider extends Provider
{
/**
* Generate audio from the given text.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Providers/EmbeddingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Laravel\Ai\Contracts\Gateway\EmbeddingGateway;
use Laravel\Ai\Responses\EmbeddingsResponse;

interface EmbeddingProvider
interface EmbeddingProvider extends Provider
{
/**
* Get embedding vectors representing the given inputs.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Providers/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Laravel\Ai\Responses\FileResponse;
use Laravel\Ai\Responses\StoredFileResponse;

interface FileProvider
interface FileProvider extends Provider
{
/**
* Get a file by its ID.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Providers/ImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Laravel\Ai\Files\Image;
use Laravel\Ai\Responses\ImageResponse;

interface ImageProvider
interface ImageProvider extends Provider
{
/**
* Generate an image.
Expand Down
30 changes: 30 additions & 0 deletions src/Contracts/Providers/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Laravel\Ai\Contracts\Providers;

interface Provider
{
/**
* Get the name of the underlying AI provider.
*/
public function name(): string;

/**
* Get the name of the underlying AI driver.
*/
public function driver(): string;

/**
* Get the credentials for the underlying AI provider.
*
* @return array<string, mixed>
*/
public function providerCredentials(): array;

/**
* Get the provider connection configuration other than the driver, key, and name.
*
* @return array<string, mixed>
*/
public function additionalConfiguration(): array;
}
2 changes: 1 addition & 1 deletion src/Contracts/Providers/RerankingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Laravel\Ai\Contracts\Gateway\RerankingGateway;
use Laravel\Ai\Responses\RerankingResponse;

interface RerankingProvider
interface RerankingProvider extends Provider
{
/**
* Rerank the given documents based on their relevance to the query.
Expand Down
6 changes: 4 additions & 2 deletions src/Contracts/Providers/StoreProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Laravel\Ai\Contracts\Gateway\StoreGateway;
use Laravel\Ai\Store;

interface StoreProvider
interface StoreProvider extends Provider
{
/**
* Get a vector store by its ID.
Expand All @@ -27,8 +27,10 @@ public function createStore(

/**
* Add a file to a vector store.
*
* @param array<string, mixed> $metadata
*/
public function addFileToStore(string $storeId, HasProviderId $file): string;
public function addFileToStore(string $storeId, HasProviderId $file, array $metadata = []): string;

/**
* Remove a file from a vector store.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Providers/TextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Laravel\Ai\Responses\AgentResponse;
use Laravel\Ai\Responses\StreamableAgentResponse;

interface TextProvider
interface TextProvider extends Provider
{
/**
* Invoke the given agent.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Providers/TranscriptionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Laravel\Ai\Contracts\Gateway\TranscriptionGateway;
use Laravel\Ai\Responses\TranscriptionResponse;

interface TranscriptionProvider
interface TranscriptionProvider extends Provider
{
/**
* Generate audio from the given text.
Expand Down
2 changes: 1 addition & 1 deletion src/Events/AgentFailedOver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Laravel\Ai\Events;

use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\Providers\Provider;
use Laravel\Ai\Exceptions\FailoverableException;
use Laravel\Ai\Providers\Provider;

class AgentFailedOver extends ProviderFailedOver
{
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ProviderFailedOver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Ai\Events;

use Laravel\Ai\Contracts\Providers\Provider;
use Laravel\Ai\Exceptions\FailoverableException;
use Laravel\Ai\Providers\Provider;

class ProviderFailedOver
{
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Anthropic/AnthropicGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$body,
0,
null,
Expand Down
4 changes: 2 additions & 2 deletions src/Gateway/Anthropic/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$requestBody,
$depth + 1,
$maxSteps,
Expand Down Expand Up @@ -516,7 +516,7 @@ protected function resumeFromPauseTurn(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$requestBody,
$depth + 1,
$maxSteps,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/AzureOpenAi/AzureOpenAiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
0,
null,
$timeout,
Expand Down
9 changes: 5 additions & 4 deletions src/Gateway/Bedrock/Concerns/MapsAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use InvalidArgumentException;
use Laravel\Ai\Contracts\Files\StorableFile;
use Laravel\Ai\Files\Base64Document;
use Laravel\Ai\Files\Base64Image;
use Laravel\Ai\Files\Document;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected function mapAttachments(Collection $attachments): array
/**
* Build a Bedrock document content block.
*/
protected function buildDocumentBlock(Document $document): array
protected function buildDocumentBlock(Document&StorableFile $document): array
{
return [
'document' => [
Expand All @@ -70,7 +71,7 @@ protected function buildDocumentBlock(Document $document): array
/**
* Build a Bedrock image content block.
*/
protected function buildImageBlock(Image $image, string $bytes): array
protected function buildImageBlock(Image&StorableFile $image, string $bytes): array
{
return [
'image' => [
Expand All @@ -85,7 +86,7 @@ protected function buildImageBlock(Image $image, string $bytes): array
/**
* Map a Document's MIME type to a Bedrock document format.
*/
protected function getDocumentFormat(Document $document): string
protected function getDocumentFormat(Document&StorableFile $document): string
{
$mime = strtolower(trim(strtok($document->mimeType() ?? 'text/plain', ';')));

Expand Down Expand Up @@ -124,7 +125,7 @@ protected function getDocumentName(Document $document): string
*
* @throws InvalidArgumentException if the MIME type cannot be determined or is unsupported.
*/
protected function getImageFormat(Image $image): string
protected function getImageFormat(Image&StorableFile $image): string
{
$mime = $image->mimeType();

Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/DeepSeek/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$originalMessages,
$depth + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/DeepSeek/DeepSeekGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$messages,
0,
Expand Down
24 changes: 18 additions & 6 deletions src/Gateway/FakeTextGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function generateText(
?TextGenerationOptions $options = null,
?int $timeout = null,
): TextResponse {
$message = (new Collection($messages))->last(function ($message) {
return $message instanceof UserMessage;
});
$message = $this->lastUserMessageOrFail($messages);

$response = $this->nextResponse(
$provider, $model, $message->content, $message->attachments, $schema
Expand Down Expand Up @@ -135,9 +133,7 @@ public function streamText(
yield new StreamStart(ulid(), $provider->name(), $model, time());
yield new TextStart(ulid(), $messageId, time());

$message = (new Collection($messages))->last(function ($message) {
return $message instanceof UserMessage;
});
$message = $this->lastUserMessageOrFail($messages);

$fakeResponse = $this->nextResponse(
$provider, $model, $message->content, $message->attachments, $schema
Expand All @@ -162,6 +158,22 @@ public function streamText(
yield new StreamEnd(ulid(), 'stop', new Usage, time());
}

/**
* Get the most recent user message in the conversation.
*
* @param array<int, mixed> $messages
*/
protected function lastUserMessageOrFail(array $messages): UserMessage
{
$message = (new Collection($messages))->last(fn ($message) => $message instanceof UserMessage);

if (! $message instanceof UserMessage) {
throw new RuntimeException('A user message is required to fake a text response.');
}

return $message;
}

/**
* Get the next response instance.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Gemini/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$contents,
$instructions,
$depth + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Gemini/GeminiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$contents,
$instructions,
0,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Groq/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$originalMessages,
$depth + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Groq/GroqGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$messages,
0,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Mistral/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$originalMessages,
$depth + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Mistral/MistralGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$messages,
timeout: $timeout,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Ollama/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$originalMessages,
$depth + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/Ollama/OllamaGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$instructions,
$messages,
0,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/OpenAi/Concerns/HandlesTextStreaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ protected function handleStreamingToolCalls(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
$depth + 1,
$maxSteps,
$timeout,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/OpenAi/OpenAiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function streamText(
$tools,
$schema,
$options,
$response->getBody(),
$response->toPsrResponse()->getBody(),
0,
null,
$timeout,
Expand Down
Loading