Skip to content

Commit

Permalink
going to back off this for a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 5, 2024
1 parent bd8f2ca commit 514f00e
Show file tree
Hide file tree
Showing 29 changed files with 769 additions and 151 deletions.
2 changes: 2 additions & 0 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use LlmLaraHub\LlmDriver\Functions\GetWebSiteFromUrlTool;
use LlmLaraHub\LlmDriver\Functions\ReportingTool;
use LlmLaraHub\LlmDriver\Functions\RetrieveRelated;
use LlmLaraHub\LlmDriver\Functions\SatisfyToolsRequired;
use LlmLaraHub\LlmDriver\Functions\SearchTheWeb;
use LlmLaraHub\LlmDriver\Functions\StandardsChecker;
use LlmLaraHub\LlmDriver\Functions\SummarizeCollection;
Expand Down Expand Up @@ -218,6 +219,7 @@ public function getFunctions(): array
new GetWebSiteFromUrlTool(),
new SearchTheWeb(),
new CreateDocument(),
new SatisfyToolsRequired(),
new Chat(),
]
);
Expand Down
2 changes: 1 addition & 1 deletion Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function remapFunctions(array $functions): array
'required' => $required,
],
];
})->toArray();
})->values()->toArray();
}

/**
Expand Down
58 changes: 58 additions & 0 deletions Modules/LlmDriver/app/Functions/SatisfyToolsRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace LlmLaraHub\LlmDriver\Functions;

use App\Helpers\ChatHelperTrait;
use App\Models\Document;
use App\Models\Message;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;
use LlmLaraHub\LlmDriver\ToolsHelper;

class SatisfyToolsRequired extends FunctionContract
{
use ChatHelperTrait, ToolsHelper;

public array $toolTypes = [
ToolTypes::NoFunction,
];

protected string $name = 'satisfy_tools_required';

protected string $description = 'This tool has no use just for example purposes';

public function handle(
Message $message): FunctionResponse
{
Log::info('[LaraChain] SatisfyToolsRequired');


return FunctionResponse::from([
'content' => "Should not be called",
'prompt' => $message->getPrompt(),
'requires_followup' => false,
'documentChunks' => collect([]),
'save_to_message' => false,
]);
}

/**
* @return PropertyDto[]
*/
protected function getProperties(): array
{
return [
new PropertyDto(
name: 'example_arg',
description: 'Example argument',
type: 'string',
required: true,
),
];
}

public function runAsBatch(): bool
{
return false;
}
}
1 change: 1 addition & 0 deletions Modules/LlmDriver/app/Functions/SummarizeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function handle(
$content = $message->getContent();

$prompt = Templatizer::appendContext(true)
->setMainCollectionPromptOn()
->handle(
content: $content,
replacement: $summary,
Expand Down
1 change: 0 additions & 1 deletion Modules/LlmDriver/app/HasDrivers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function getType(): string;

public function documents(): HasMany;

public function description(): string;

public function getChatable(): HasDrivers;

Expand Down
5 changes: 1 addition & 4 deletions Modules/LlmDriver/app/HasDriversTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public function documents(): HasMany
return $this->getChatable()->documents();
}

public function description(): string
{
return $this->getChatable()->description();
}


public function systemPrompt(): string
{
Expand Down
6 changes: 6 additions & 0 deletions Modules/LlmDriver/app/LlmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use LlmLaraHub\LlmDriver\Functions\GetWebSiteFromUrlTool;
use LlmLaraHub\LlmDriver\Functions\ReportingTool;
use LlmLaraHub\LlmDriver\Functions\RetrieveRelated;
use LlmLaraHub\LlmDriver\Functions\SatisfyToolsRequired;
use LlmLaraHub\LlmDriver\Functions\SearchTheWeb;
use LlmLaraHub\LlmDriver\Functions\StandardsChecker;
use LlmLaraHub\LlmDriver\Functions\SummarizeCollection;
Expand Down Expand Up @@ -98,6 +99,11 @@ public function boot(): void
return new Chat();
});

$this->app->bind('satisfy_tools_required', function () {
return new SatisfyToolsRequired();
});


}

/**
Expand Down
6 changes: 0 additions & 6 deletions Modules/LlmDriver/app/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,8 @@ public function chat(array $messages): CompletionResponse
{
Log::info('LlmDriver::OllamaClient::chat');

put_fixture('ollama_chat_payload_before_remap.json', $messages);

$messages = $this->remapMessages($messages);

put_fixture('ollama_chat_payload.json', $messages);

$payload = [
'model' => $this->getConfig('ollama')['models']['completion_model'],
'messages' => $messages,
Expand All @@ -122,8 +118,6 @@ public function chat(array $messages): CompletionResponse
throw new \Exception('Ollama API Error Chat');
}

put_fixture('ollama_chat_response.json', $response->json());

return OllamaChatCompletionResponse::from($response->json());
}

Expand Down
15 changes: 15 additions & 0 deletions app/Domains/Tokenizer/Templatizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Domains\Tokenizer;

use App\Models\Setting;

class Templatizer
{
protected string $content;
Expand All @@ -11,6 +13,7 @@ class Templatizer
protected ?string $replacement;

protected bool $appendContext = false;
protected bool $addMainCollectionPrompt = false;

public static function getTokens(): array
{
Expand Down Expand Up @@ -46,6 +49,12 @@ public function handle(
}
}

if($this->addMainCollectionPrompt) {
$this->content = str($this->content)
->prepend(Setting::first()?->main_collection_prompt)
->toString();
}

return $this->content;

}
Expand Down Expand Up @@ -82,6 +91,12 @@ public function appendContext(bool $appendContext = false): self
return $this;
}

public function setMainCollectionPromptOn(): self
{
$this->addMainCollectionPrompt = true;
return $this;
}

protected function start_week(): void
{
$replacement = now()->startOfWeek()->format('m/d/Y');
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/GetWebContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function handle(): void
->handle($this->webResponseDto->url, true);

$prompt = Templatizer::appendContext(true)
->handle($this->source->getPrompt(), $htmlResults);
->setMainCollectionPromptOn()
->handle(
content: $this->source->getPrompt(),
replacement: $htmlResults);

$results = LlmDriverFacade::driver(
$this->source->getDriver()
Expand Down
4 changes: 0 additions & 4 deletions app/Jobs/ToolsCompleteJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ public function handle(): void

$messages = $this->chat->getChatResponse();

put_fixture('messages_before_final_job_claude.json', $messages);

$response = LlmDriverFacade::driver($this->chat->chatable->getDriver())
->setToolType(ToolTypes::NoFunction)
->chat($messages);

put_fixture('tool_complete_response_claude.json', $response);

$this->chat->addInput(
message: $response->content,
role: RoleEnum::Assistant,
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
class Collection extends Model implements HasDrivers, TaggableContract
{
use HasDriversTrait;
use HasFactory;
use Taggable;

Expand All @@ -45,6 +44,12 @@ public function getChatable(): HasDrivers
return $this;
}

public function description(): string
{
return $this->description;
}


public function filters(): HasMany
{
return $this->hasMany(Filter::class);
Expand Down
36 changes: 36 additions & 0 deletions database/seeders/CopySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Database\Seeders;

use App\Models\Setting;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class CopySeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$setting = Setting::first();

if($setting) {
$mainPrompt = $setting->main_collection_prompt;
if(!$mainPrompt) {
$prompt = <<<PROMPT
Your primary function is to assist users in interacting with their "collection" of data within a Retrieval Augmented Generation (RAG) system. This collection comprises documents uploaded by the user or imported from web searches, serving as contextual information for our interactions.
Your responsibilities include:
1. Answering questions based on the provided context
2. Generating reports using the available data
3. Automating tasks such as email composition
When responding to user queries, utilize the tools and information at your disposal while maintaining awareness of the collection's context. Adapt your responses to align with the user's specific needs and the nature of their request, whether it's a simple question, a complex analysis, or a task requiring the use of the collection's data.
PROMPT;
$setting->updateQueitly([
'main_collection_prompt' => $prompt,
]);
}
}
}
}
2 changes: 1 addition & 1 deletion resources/js/Pages/Settings/Partials/ClaudeSecrets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const updateSecrets = () => {
form.put(route('settings.update.claude', {
setting: props.setting.id,
}), {
errorBag: 'updateProfileInformation',
errorBag: 'updateClaude',
preserveScroll: true,
});
};
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Settings/Partials/GroqSecrets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const updateSecrets = () => {
form.put(route('settings.update.groq', {
setting: props.setting.id,
}), {
errorBag: 'updateProfileInformation',
errorBag: 'updateGroq',
preserveScroll: true,
});
};
Expand Down
Loading

0 comments on commit 514f00e

Please sign in to comment.