Skip to content

Commit

Permalink
updates to search and fix history on Orchestrate
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 20, 2024
1 parent 256001a commit 4d37586
Show file tree
Hide file tree
Showing 18 changed files with 1,193 additions and 29 deletions.
1 change: 1 addition & 0 deletions Modules/LlmDriver/app/DistanceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function distance(

Log::info('[LaraChain] - Distance Query', [
'filter' => $filter?->toArray(),
'embedding_size' => $embeddingSize,
]);

$documentIds = Document::query()
Expand Down
9 changes: 9 additions & 0 deletions Modules/LlmDriver/app/Functions/SearchAndSummarize.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Domains\Agents\VerifyPromptOutputDto;
use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\SummarizePrompt;
use App\Models\PromptHistory;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Facades\LlmLaraHub\LlmDriver\DistanceQuery;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -120,6 +121,13 @@ public function handle(

$message = $model->getChat()->addInput($this->response, RoleEnum::Assistant);

PromptHistory::create([
'prompt' => $contentFlattened,
'chat_id' => $model->getChat()->id,
'message_id' => $message?->id,
'collection_id' => $model->getChat()->getChatable()?->id,
]);

$this->saveDocumentReference($message, $documentChunkResults);

notify_ui($model->getChat(), 'Complete');
Expand All @@ -128,6 +136,7 @@ public function handle(
[
'content' => $this->response,
'save_to_message' => false,
'prompt' => $contentFlattened,
]
);
}
Expand Down
1 change: 0 additions & 1 deletion Modules/LlmDriver/app/NonFunctionSearchOrSummarize.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function handle(string $input,

$embeddingSize = get_embedding_size($collection->getEmbeddingDriver());

//put_fixture("anonymous_embedding_result.json", $embedding);
$documentChunkResults = DistanceQuery::distance(
$embeddingSize,
$collection->id,
Expand Down
12 changes: 3 additions & 9 deletions Modules/LlmDriver/app/Orchestrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Domains\Messages\RoleEnum;
use App\Models\Chat;
use App\Models\Filter;
use App\Models\PromptHistory;
use Facades\App\Domains\Messages\SearchAndSummarizeChatRepo;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -51,6 +50,8 @@ public function handle(

notify_ui($chat, 'We are running the agent back shortly');

Log::info('[LaraChain] - Running function '.$functionName);

$functionClass = app()->make($functionName);

$arguments = data_get($function, 'arguments');
Expand All @@ -66,21 +67,14 @@ public function handle(
/** @var FunctionResponse $response */
$response = $functionClass->handle($messagesArray, $chat, $functionDto);

$message = null;
if ($response->save_to_message) {

$message = $chat->addInput(
message: $response->content,
role: RoleEnum::Assistant,
show_in_thread: true);

if ($response->prompt) {
PromptHistory::create([
'prompt' => $response->prompt,
'chat_id' => $chat->id,
'message_id' => $message->id,
'collection_id' => $chat->getChatable()?->id,
]);
}
}

$messagesArray = Arr::wrap(MessageInDto::from([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public function __construct(
public string $url,
public string $title,
public string $description,
public string $age,
public array $meta_data,
public string $thumbnail,
public ?string $age = null,
) {
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/DocumentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function toArray(Request $request): array
return [
'id' => $this->id,
'file_path' => $this->file_path,
'subject' => $this->subject,
'link' => $this->link,
'summary' => $this->summary,
'summary_markdown' => str($this->summary)->markdown(),
'type' => str($this->type->name)->title()->toString(),
Expand Down
4 changes: 3 additions & 1 deletion app/Jobs/GetWebContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public function handle(): void
[
'source_id' => $this->source->id,
'type' => TypesEnum::HTML,
'file_path' => $this->webResponseDto->url,
'subject' => $this->webResponseDto->title,
'link' => $this->webResponseDto->url,
'collection_id' => $this->source->collection_id,
],
[
'status' => StatusEnum::Pending,
'file_path' => $this->webResponseDto->url,
'status_summary' => StatusEnum::Pending,
'meta_data' => $this->webResponseDto->toArray(),
]
Expand Down
2 changes: 2 additions & 0 deletions database/factories/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function definition(): array
'source_id' => Source::factory(),
'meta_data' => [],
'status_summary' => StatusEnum::random(),
'link' => $this->faker->url(),
'summary' => $this->faker->text(),
'subject' => $this->faker->text(),
'file_path' => $this->faker->url(),
'document_chunk_count' => $this->faker->numberBetween(1, 10),
'collection_id' => Collection::factory(),
Expand Down
28 changes: 28 additions & 0 deletions database/migrations/2024_05_20_003955_add_subject_to_sources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->longText('subject')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('sources', function (Blueprint $table) {
//
});
}
};
28 changes: 28 additions & 0 deletions database/migrations/2024_05_20_012744_add_link_to_documents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->longText('link')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('documents', function (Blueprint $table) {
//
});
}
};
3 changes: 0 additions & 3 deletions resources/js/Pages/Chat/ChatInputThreaded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ onMounted(() => {
})
})
.listen('.update', (e) => {
// Make a better ui for htis
console.log("chat reuslts came in")
console.log(e)
if(e.updateMessage === 'Complete') {
getting_results.value = false
router.reload({
Expand Down
1 change: 0 additions & 1 deletion resources/js/Pages/Collection/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ onMounted(() => {
})
})
.listen('.update', (e) => {
// Make a better ui for htis
toast.success(e.updateMessage, {
position: "bottom-right",
timeout: 2000,
Expand Down
32 changes: 28 additions & 4 deletions resources/js/Pages/Collection/Components/Documents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,35 @@ const emptyDocumentIds = () => {
<td
class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-3">

<a class="underline" target="_blank" :href="route('download.document', {
collection: collection.id,
document_name: document.file_path
})">{{ document.file_path }}</a>

<div v-if="document.subject">
<div class="truncate max-w-2xl">
<div v-if="!document.link">
{{ document.subject }}
</div>
<div v-else>
<a class="underline" target="_blank" :href="document.link">
{{ document.subject }}
</a>
</div>
</div>
<div class="text-gray-400 text-sm">
<a class="underline" target="_blank" :href="route('download.document', {
collection: collection.id,
document_name: document.file_path
})">
{{ document.file_path }}
</a>
</div>
</div>
<div v-else>
<a class="underline" target="_blank" :href="route('download.document', {
collection: collection.id,
document_name: document.file_path
})">
{{ document.file_path }}
</a>
</div>
</td>
<td
class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-3">
Expand Down
2 changes: 1 addition & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ createInertiaApp({
.use(autoAnimatePlugin)
.use(Toast, {
transition: "Vue-Toastification__bounce",
maxToasts: 2,
maxToasts: 1,
newestOnTop: true
})
.use(ZiggyVue)
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/Jobs/GetWebContentJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\Sources\WebSearch\Response\WebResponseDto;
use App\Jobs\GetWebContentJob;
use App\Models\Document;
use App\Models\Source;
use Facades\App\Domains\Sources\WebSearch\GetPage;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -51,6 +52,8 @@ public function test_job_html(): void
$job->handle();
$this->assertDatabaseCount('documents', 1);
$this->assertDatabaseCount('document_chunks', 82);
$document = Document::first();
$this->assertEquals('Example', $document->subject);

}

Expand Down
52 changes: 51 additions & 1 deletion tests/Feature/OrchestrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\User;
use Facades\App\Domains\Messages\SearchAndSummarizeChatRepo;
use Illuminate\Support\Facades\Event;
use LlmLaraHub\LlmDriver\Functions\SearchAndSummarize;
use LlmLaraHub\LlmDriver\Functions\SummarizeCollection;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Orchestrate;
Expand Down Expand Up @@ -70,7 +71,56 @@ public function test_gets_summarize_function(): void
Event::assertDispatched(ChatUiUpdateEvent::class);

$this->assertEquals($results, 'This is the summary of the collection');
}

public function test_makes_history_no_message(): void
{
Event::fake();
LlmDriverFacade::shouldReceive('driver->functionPromptChat')->once()->andReturn([
[
'name' => 'search_and_summarize',
'arguments' => [
'TLDR it for me',
],
],
]);

LlmDriverFacade::shouldReceive('driver->chat')->never();

SearchAndSummarizeChatRepo::shouldReceive('search')->never();

$this->instance(
'search_and_summarize',
Mockery::mock(SearchAndSummarize::class, function ($mock) {
$mock->shouldReceive('handle')
->once()
->andReturn(
FunctionResponse::from(
[
'content' => 'This is the summary of the collection',
'prompt' => 'TLDR it for me',
])
);
})
);

$this->assertDatabaseCount('prompt_histories', 1);
$user = User::factory()->create();
$collection = Collection::factory()->create();
$chat = Chat::factory()->create([
'chatable_id' => $collection->id,
'chatable_type' => Collection::class,
'user_id' => $user->id,
]);

$messageDto = MessageInDto::from([
'content' => 'TLDR it for me',
'role' => 'user',
]);

$results = (new Orchestrate())->handle([$messageDto], $chat);

Event::assertDispatched(ChatUiUpdateEvent::class);

$this->assertEquals($results, 'This is the summary of the collection');
}
}
14 changes: 7 additions & 7 deletions tests/fixtures/ollama_chat_results.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"model": "phi3",
"created_at": "2024-05-18T14:16:02.43799Z",
"created_at": "2024-05-20T01:57:46.168876Z",
"message": {
"role": "assistant",
"content": "To perform a test, you would typically follow these steps:\n\n1. Define the purpose of the test \u2013 Understand what you are testing for and why it's important to have this information.\n\n2. Identify the variables \u2013 Determine which factors or elements will be tested during the process.\n\n3. Develop a methodology \u2013 Create a structured plan detailing how the test will proceed, including procedures, tools needed, and measurements.\n\n4. Execute the test \u2013 Carry out the testing according to your established methodology, ensuring all variables are controlled and consistent throughout the test process.\n\n5. Analyze data \u2013 Gather and evaluate the information collected during the test. Use statistical or qualitative analysis methods as appropriate for the test's purpose.\n\n6. Interpret results \u2013 Draw conclusions from your data to determine whether it meets pre-defined criteria, objectives, or hypotheses.\n\n7. Report findings \u2013 Document and share the outcomes of the test with stakeholders in a clear and comprehensible manner."
"content": "To address the \"test\" instruction effectively, I'll provide a response as if it were part of an automated system designed to handle user inputs or test cases. Since there is no specific context given, let me assume we are discussing a software application that might receive this input for testing purposes:\n\n\n```\n\nTest Case #1: Input Validation Test\n\nObjective: Verify the application correctly handles an unexpected input string (test).\n\nSteps:\n\n1. Open the application and navigate to the relevant input field where test strings are usually entered.\n\n2. Enter \"test\" into the specified text field without any formatting or additional characters.\n\n3. Submit the entry.\n\nExpected Result: The application should either accept \"test\" as a valid input if it's meant to be accepted in this context, or provide an appropriate error message if such input is not expected or supported by the system.\n\n```\n\nThis example demonstrates how one might construct a test case for handling the specific string \"test.\" Depending on what kind of application you are testing (web form, command-line interface, etc.), and what behavior is intended when encountering this string, your actual instructions would vary accordingly."
},
"done_reason": "stop",
"done": true,
"total_duration": 5440443666,
"load_duration": 2888041,
"total_duration": 6352558125,
"load_duration": 4046250,
"prompt_eval_count": 12,
"prompt_eval_duration": 79540000,
"eval_count": 228,
"eval_duration": 5356123000
"prompt_eval_duration": 82735000,
"eval_count": 260,
"eval_duration": 6263825000
}
Loading

0 comments on commit 4d37586

Please sign in to comment.