Skip to content

Commit

Permalink
this will try to back off the claude api
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Apr 29, 2024
1 parent 8e0631d commit 8b71d2d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ protected function remapMessages(array $messages): array

public function onQueue(): string
{
return 'default';
return 'api_request';
}
}
4 changes: 1 addition & 3 deletions Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function chat(array $messages): CompletionResponse
*/
$messages = $this->remapMessages($messages);

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

$results = $this->getClient()->post('/messages', [
'model' => $model,
'system' => 'Return a markdown response.',
Expand Down Expand Up @@ -117,7 +115,7 @@ protected function getClient()
throw new \Exception('Claude API Token not found');
}

return Http::withHeaders([
return Http::retry(2, 6000)->withHeaders([
'x-api-key' => $api_token,
'anthropic-beta' => 'tools-2024-04-04',
'anthropic-version' => $this->version,
Expand Down
57 changes: 52 additions & 5 deletions Modules/TagFunction/app/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace LlmLaraHub\TagFunction;

use App\Domains\Agents\VerifyPromptInputDto;
use App\Models\Document;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use App\Domains\Agents\VerifyPromptOutputDto;

class TagManager
{
Expand All @@ -32,10 +35,28 @@ public function handle(Document $document): void
->completion(
prompt: $prompt
);

$this->tags = collect(explode(',', $response->content));

Log::info('[LaraChain] Tags Found: '.$response->content);

$verifyPrompt = <<<'PROMPT'
This was the response from the LLM to get Tags from the content.
Please verify the json is good if not fix it so what you return is just JSON
and remove from tags any text that is not needed and any
tags that are not correct.
PROMPT;

$dto = VerifyPromptInputDto::from(
[
'chattable' => $document,
'originalPrompt' => $prompt,
'context' => $summary,
'llmResponse' => $response->content,
'verifyPrompt' => $verifyPrompt,
]
);

/** @var VerifyPromptOutputDto $response */
$response = VerifyResponseAgent::verify($dto);

$this->tags = collect(explode(',', $response->response));

$this->tags->map(function ($tag) use ($document) {
$document->addTag($tag);
Expand Down Expand Up @@ -69,7 +90,33 @@ public function handle(Document $document): void
prompt: $prompt
);

$tagsChild = explode(',', $response->content);
$verifyPrompt = <<<'PROMPT'
This was the response from the LLM to get Tags from the content page.
Please verify the json is good if not fix it so what you return is just JSON
and remove from tags any text that is not needed and any
tags that are not correct.
PROMPT;

$originalLlm = $response->content;

$dto = VerifyPromptInputDto::from(
[
'chattable' => $document,
'originalPrompt' => $prompt,
'context' => $summary,
'llmResponse' => $originalLlm,
'verifyPrompt' => $verifyPrompt,
]
);

/** @var VerifyPromptOutputDto $response */
$response = VerifyResponseAgent::verify($dto);
Log::info('[LaraChain] TagManager Tagging document VERIFY', [
'response' => $response->response,
'original' => $originalLlm
]);

$tagsChild = explode(',', $response->response);

foreach ($tagsChild as $tag) {
$chunk->addTag($tag);
Expand Down
12 changes: 9 additions & 3 deletions Modules/TagFunction/tests/Feature/TagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LlmLaraHub\TagFunction\Tests\Feature;

use App\Models\Chat;
use App\Models\Collection;
use App\Models\DocumentChunk;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
Expand Down Expand Up @@ -71,13 +73,15 @@ public function test_talks_to_llm(): void
'content' => $content,
]);

$this->fakeVerify($documentChunk->document, 4, 'Tag Example, Tag Example other Test, Tag Example Test');

(new TagManager())->handle($documentChunk->document);

$this->assertCount(10, $documentChunk->refresh()->tags);
$this->assertCount(3, $documentChunk->refresh()->tags);

(new TagManager())->handle($documentChunk->document);

$this->assertCount(10, $documentChunk->refresh()->tags);
$this->assertCount(3, $documentChunk->refresh()->tags);
}

public function test_use_existing_tags_for_document_level(): void
Expand Down Expand Up @@ -138,9 +142,11 @@ public function test_use_existing_tags_for_document_level(): void
'content' => $content,
]);

$this->fakeVerify($documentChunk->document, 2, 'Tag Example, Tag Example other Test, Tag Example Test');

(new TagManager())->handle($documentChunk->document);

$this->assertCount(10, $documentChunk->refresh()->tags);
$this->assertCount(3, $documentChunk->refresh()->tags);

}
}
2 changes: 1 addition & 1 deletion app/Jobs/SummarizeDocumentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function handle(): void

$dto = VerifyPromptInputDto::from(
[
'chattable' => $this->document->collection->getChat(),
'chattable' => $this->document->collection,
'originalPrompt' => $intro,
'context' => $content,
'llmResponse' => $results->content,
Expand Down
13 changes: 13 additions & 0 deletions config/horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@
'timeout' => 600,
'nice' => 0,
],
'api_request' => [
'connection' => 'redis',
'queue' => ['api_request'],
'balance' => 'auto',
'autoScalingStrategy' => 'size',
'maxProcesses' => 3,
'maxTime' => 0,
'maxJobs' => 0,
'memory' => 256,
'tries' => 10,
'timeout' => 600,
'nice' => 0,
],
'ollama' => [
'connection' => 'redis',
'queue' => ['ollama'],
Expand Down
1 change: 1 addition & 0 deletions resources/js/Components/PrimaryButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defineProps({
<template>
<button :type="type" class="
disabled:opacity-50
disabled:cursor-not-allowed
inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 focus:bg-gray-700 active:bg-gray-900 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 transition ease-in-out duration-150">
<slot />
</button>
Expand Down
5 changes: 3 additions & 2 deletions resources/js/Pages/Collection/Components/FileUploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
})">
<input v-bind="getInputProps()" class="h-40" />
<p v-if="isDragActive">Drop the files here ...</p>
<p v-else>Drag 'n' drop some Documents, Images here, or click <span class="underline">select</span> to choose files</p>
<div class="w-full text-center flex gap-2" v-else>Drag 'n' drop some Documents, Images here, or click <span class="underline flex">
here</span> to choose files</div>
</div>

<div class="flex mt-5 justify-center ">
<PrimaryButton type="button" :disabled="form.files.length === 0" @click="submitFiles()">
Save ({{ form.files.length }}) Files
Start Importing ({{ form.files.length }}) Files
</PrimaryButton>

</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Collection/Components/ShowDocument.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
bg-white border-b border-gray-100 dark:border-gray-700 py-6 shadow-xl">
<div class="px-4 sm:px-6">
<div class="flex items-start justify-between">
<DialogTitle class="text-base font-semibold leading-6">Document.. {{ document.file_path }}</DialogTitle>
<DialogTitle class="text-base font-semibold leading-6 text-gray-800">Document {{ document.file_path }}</DialogTitle>
<div class="ml-3 flex h-7 items-center">
<button
type="button" class="relative rounded-md bg-white focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public function createUserWithCurrentTeam()
return $user->refresh();
}

public function fakeVerify(HasDrivers $model) {
VerifyResponseAgent::shouldReceive('verify')->once()->andReturn(
public function fakeVerify(HasDrivers $model, $times = 1, $response = 'verified yay!') {
VerifyResponseAgent::shouldReceive('verify')->times($times)->andReturn(
VerifyPromptOutputDto::from(
[
'chattable' => $model->getChat(),
'chattable' => $model,
'originalPrompt' => 'test',
'context' => 'test',
'llmResponse' => 'test',
'verifyPrompt' => 'This is a completion so the users prompt was past directly to the llm with all the context.',
'response' => 'verified yay!',
'response' => $response,
]
));
}
Expand Down

0 comments on commit 8b71d2d

Please sign in to comment.