Skip to content

Commit

Permalink
move to shared query ability
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 16, 2024
1 parent 015ea4f commit 161a616
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 172 deletions.
156 changes: 156 additions & 0 deletions Modules/LlmDriver/app/NonFunctionSearchOrSummarize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace LlmLaraHub\LlmDriver;

use App\Domains\Prompts\DefaultPrompt;
use App\Domains\Prompts\SearchOrSummarize;
use App\Domains\Prompts\SummarizeDocumentPrompt;
use App\Domains\Prompts\SummarizePrompt;
use App\Models\Collection;
use App\Models\DocumentChunk;
use Facades\LlmLaraHub\LlmDriver\DistanceQuery;
use Illuminate\Support\Facades\Log;

class NonFunctionSearchOrSummarize
{
protected string $results = "";

public function handle(string $input, Collection $collection) : string
{

Log::info("[LaraChain] - Using the Non Function Search and Summarize Prompt", [
'collection' => $collection->id,
'input' => $input
]);

$prompt = SearchOrSummarize::prompt($input);

$response = LlmDriverFacade::driver(
$collection->getDriver()
)->completion($prompt);

Log::info('[LaraChain] - Results from search or summarize', [
'results' => $response->content,
]);

if (str($response->content)->contains('search')) {
Log::info('[LaraChain] - LLM Thinks it is Search', [
'response' => $response->content]
);

$embedding = LlmDriverFacade::driver(
$collection->getEmbeddingDriver()
)->embedData($input);

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

//put_fixture("anonymous_embedding_result.json", $embedding);
$documentChunkResults = DistanceQuery::distance(
$embeddingSize,
$collection->id,
$embedding->embedding
);

$content = [];

/** @var DocumentChunk $result */
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$context = implode(' ', $content);

Log::info('[LaraChain] - Content Found', [
'content' => $content,
]);

$contentFlattened = SummarizePrompt::prompt(
originalPrompt: $input,
context: $context
);

Log::info('[LaraChain] - Prompt with Context', [
'prompt' => $contentFlattened,
]);

$response = LlmDriverFacade::driver(
$collection->getDriver()
)->completion($contentFlattened);

$this->results = $response->content;
} elseif (str($response->content)->contains('summarize')) {
Log::info('[LaraChain] - LLM Thinks it is summarize', [
'response' => $response->content]
);

$content = [];

foreach ($collection->documents as $result) {
$contentString = remove_ascii($result->summary);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$contentFlattened = implode(' ', $content);

Log::info('[LaraChain] - Documents Flattened', [
'collection' => $collection->id,
'content' => $content]
);

$prompt = SummarizeDocumentPrompt::prompt($contentFlattened);

$response = LlmDriverFacade::driver(
$collection->getDriver()
)->completion($prompt);


$this->results = $response->content;
} else {
Log::info('[LaraChain] - LLM is not sure :(', [
'response' => $response->content]
);

$embedding = LlmDriverFacade::driver(
$collection->getEmbeddingDriver()
)->embedData($input);

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

$documentChunkResults = DistanceQuery::distance(
$embeddingSize,
$collection->id,
$embedding->embedding
);

$content = [];

/** @var DocumentChunk $result */
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$context = implode(' ', $content);

Log::info('[LaraChain] - Content Found', [
'content' => $content,
]);

$contentFlattened = DefaultPrompt::prompt(
originalPrompt: $input,
context: $context
);

$response = LlmDriverFacade::driver(
$collection->getDriver()
)->completion($contentFlattened);


$this->results = $response->content;

}

return $this->results;
}
}
128 changes: 3 additions & 125 deletions app/Http/Controllers/WebPageOutputController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Helpers\TrimText;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use Facades\LlmLaraHub\LlmDriver\NonFunctionSearchOrSummarize;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;

class WebPageOutputController extends Controller
Expand Down Expand Up @@ -85,132 +86,9 @@ public function chat(Output $output)
'message' => $validated['input']]
);

$prompt = SearchOrSummarize::prompt($input);
$results = NonFunctionSearchOrSummarize::handle($input, $output->collection);

$response = LlmDriverFacade::driver(
$output->collection->getDriver()
)->completion($prompt);

Log::info('[LaraChain] - Results from search or summarize', [
'results' => $response->content,
]);

if (str($response->content)->contains('search')) {
Log::info('[LaraChain] - LLM Thinks it is Search', [
'response' => $response->content]
);

$embedding = LlmDriverFacade::driver(
$output->collection->getEmbeddingDriver()
)->embedData($input);

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

//put_fixture("anonymous_embedding_result.json", $embedding);
$documentChunkResults = DistanceQuery::distance(
$embeddingSize,
$output->collection->id,
$embedding->embedding
);

$content = [];

/** @var DocumentChunk $result */
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$context = implode(' ', $content);

Log::info('[LaraChain] - Content Found', [
'content' => $content,
]);

$contentFlattened = SummarizePrompt::prompt(
originalPrompt: $input,
context: $context
);

Log::info('[LaraChain] - Prompt with Context', [
'prompt' => $contentFlattened,
]);

$response = LlmDriverFacade::driver(
$output->collection->getDriver()
)->completion($contentFlattened);

$this->setChatMessages($response->content, 'assistant');

} elseif (str($response->content)->contains('summarize')) {
Log::info('[LaraChain] - LLM Thinks it is summarize', [
'response' => $response->content]
);

$content = [];

foreach ($output->collection->documents as $result) {
$contentString = remove_ascii($result->summary);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$contentFlattened = implode(' ', $content);

Log::info('[LaraChain] - Documents Flattened', [
'collection' => $output->collection_id,
'content' => $content]
);

$prompt = SummarizeDocumentPrompt::prompt($contentFlattened);

$response = LlmDriverFacade::driver(
$output->collection->getDriver()
)->completion($prompt);

$this->setChatMessages($response->content, 'assistant');
} else {
Log::info('[LaraChain] - LLM is not sure :(', [
'response' => $response->content]
);

$embedding = LlmDriverFacade::driver(
$output->collection->getEmbeddingDriver()
)->embedData($input);

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

$documentChunkResults = DistanceQuery::distance(
$embeddingSize,
$output->collection->id,
$embedding->embedding
);

$content = [];

/** @var DocumentChunk $result */
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

$context = implode(' ', $content);

Log::info('[LaraChain] - Content Found', [
'content' => $content,
]);

$contentFlattened = DefaultPrompt::prompt(
originalPrompt: $input,
context: $context
);

$response = LlmDriverFacade::driver(
$output->collection->getDriver()
)->completion($contentFlattened);

$this->setChatMessages($response->content, 'assistant');

}
$this->setChatMessages($results, 'assistant');

return back();
}
Expand Down
52 changes: 5 additions & 47 deletions tests/Feature/Http/Controllers/WebPageOutputControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
use Facades\LlmLaraHub\LlmDriver\DistanceQuery;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use Facades\LlmLaraHub\LlmDriver\NonFunctionSearchOrSummarize;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use LlmLaraHub\LlmDriver\Responses\EmbeddingsResponseDto;
use Pgvector\Laravel\Vector;
Expand Down Expand Up @@ -65,34 +66,12 @@ public function test_chat_summarize()

public function test_chat_search()
{

$documentChunk = DocumentChunk::factory()->create();

DistanceQuery::shouldReceive('distance')->once()->andReturn(DocumentChunk::all());

$output = Output::factory()->create([
'active' => true,
'public' => true,
]);

$question = get_fixture('embedding_question_distance.json');

$vector = new Vector($question);

LlmDriverFacade::shouldReceive('driver->embedData')
->once()
->andReturn(EmbeddingsResponseDto::from(
[
'embedding' => $vector,
'token_count' => 2,
]
));

LlmDriverFacade::shouldReceive('driver->completion')
->twice()
->andReturn(CompletionResponse::from([
'content' => 'search',
]));
NonFunctionSearchOrSummarize::shouldReceive('handle')
->once()->andReturn("Foo");

$this->post(route(
'collections.outputs.web_page.chat', [
Expand All @@ -105,35 +84,14 @@ public function test_chat_search()

public function test_no_search_no_summary()
{

DocumentChunk::factory()->create();

DistanceQuery::shouldReceive('distance')->once()->andReturn(DocumentChunk::all());
NonFunctionSearchOrSummarize::shouldReceive('handle')
->once()->andReturn("Foo");

$output = Output::factory()->create([
'active' => true,
'public' => true,
]);

$question = get_fixture('embedding_question_distance.json');

$vector = new Vector($question);

LlmDriverFacade::shouldReceive('driver->embedData')
->once()
->andReturn(EmbeddingsResponseDto::from(
[
'embedding' => $vector,
'token_count' => 2,
]
));

LlmDriverFacade::shouldReceive('driver->completion')
->twice()
->andReturn(CompletionResponse::from([
'content' => 'not sure :(',
]));

$this->post(route(
'collections.outputs.web_page.chat', [
'output' => $output->id,
Expand Down
Loading

0 comments on commit 161a616

Please sign in to comment.