Skip to content
Merged
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
84 changes: 0 additions & 84 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,71 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^Cannot access an offset on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''completion_tokens'' on Prism\\Prism\\ValueObjects\\Usage\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''message'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''prompt_tokens'' on Prism\\Prism\\ValueObjects\\Usage\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''total_tokens'' on Prism\\Prism\\ValueObjects\\Usage\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''user'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot cast Prism\\Prism\\Text\\Response to string\.$#'
identifier: cast.string
count: 2
path: src/Http/Controllers/LoopController.php

-
message: '#^Method Kirschbaum\\Loop\\Http\\Controllers\\LoopController\:\:getStoredMessages\(\) should return array\<mixed\> but returns mixed\.$#'
identifier: return.type
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Method Kirschbaum\\Loop\\Http\\Controllers\\LoopController\:\:storeMessageInCache\(\) has parameter \$message with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: src/Http/Controllers/LoopController.php

-
message: '#^Cannot access offset ''message'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: src/Loop.php

-
message: '#^Cannot access offset ''user'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Loop.php

-
message: '#^Cannot call method build\(\) on Kirschbaum\\Loop\\Contracts\\Tool\|null\.$#'
identifier: method.nonObject
Expand All @@ -78,24 +12,6 @@ parameters:
count: 1
path: src/Loop.php

-
message: '#^Parameter \#1 \$content of class Prism\\Prism\\ValueObjects\\Messages\\AssistantMessage constructor expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Loop.php

-
message: '#^Parameter \#1 \$content of class Prism\\Prism\\ValueObjects\\Messages\\UserMessage constructor expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Loop.php

-
message: '#^Parameter \#1 \$tools of method Prism\\Prism\\Text\\PendingRequest\:\:withTools\(\) expects array\<int, Prism\\Prism\\Tool\>, Illuminate\\Support\\Collection given\.$#'
identifier: argument.type
count: 1
path: src/Loop.php

-
message: '#^Instanceof between Prism\\Prism\\Tool and Prism\\Prism\\Tool will always evaluate to true\.$#'
identifier: instanceof.alwaysTrue
Expand Down
145 changes: 0 additions & 145 deletions src/Http/Controllers/LoopController.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
namespace Kirschbaum\Loop;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Kirschbaum\Loop\Contracts\Tool;
use Kirschbaum\Loop\Contracts\Toolkit;
use Prism\Prism\Enums\Provider;
use Prism\Prism\Prism;
use Prism\Prism\Text\Response;
use Prism\Prism\Tool as PrismTool;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
use Prism\Prism\ValueObjects\Messages\UserMessage;

class Loop
{
Expand Down Expand Up @@ -42,53 +36,6 @@ public function toolkit(Toolkit $toolkit): static
return $this;
}

/**
* @param Collection<array-key, mixed> $messages
*/
public function ask(string $question, Collection $messages): Response
{
$prompt = sprintf(
"
You are a helpful assistant. You will have many tools available to you. You need to give informations about the data and ask the user what you need to give him what he needs. \n\n
Today is %s. Current month is %s. Current day is %s. Database being used is %s. \n\n
When using the tools, always pass all the parameters listed in the tool. If you don't have all the information, ask the user for it. If it's optional, pass null. \n
When a field is tagged with a access_type read, it means that the field is automatically calculated and is not stored in the database. \n
When referencing an ID, try to fetch the resource of that ID from the database and give additional informations about it. \n\n
When giving the final output, please compress the information to the minimum needed to answer the question. No need to explain what math you did unless explicitly asked. \n\n
Parameter names in tools never include the $ symbol. \n\n
%s \n\n
You are logged in as %s (User ID: %s)
",
now()->format('Y-m-d'),
now()->format('F'),
now()->format('d'),
config()->string('database.default'),
$this->context,
Auth::user()?->name, /** @phpstan-ignore property.notFound */
Auth::user()?->id,
);
dump($prompt);

$messages = $messages
->reject(fn ($message) => empty($message['message']))
->map(function ($message) {
return $message['user'] === 'AI'
? new AssistantMessage($message['message'])
: new UserMessage($message['message']);
})->toArray();

$messages[] = new UserMessage($question);

return Prism::text()
// ->using(Provider::Anthropic, 'claude-3-5-sonnet-latest')
->using(Provider::OpenAI, 'gpt-4o-mini')
->withMaxSteps(10)
->withMessages($messages)
->withSystemPrompt($prompt)
->withTools($this->getPrismTools())
->asText();
}

public function getPrismTools(): Collection
{
return $this->loopTools
Expand Down