Skip to content

Commit

Permalink
Ah the job of huge commits
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 4, 2024
1 parent 96d8e5c commit c6299b0
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 130 deletions.
27 changes: 14 additions & 13 deletions Modules/LlmDriver/app/Functions/SummarizeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace LlmLaraHub\LlmDriver\Functions;

use App\Models\Message;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Prompts\SummarizeCollectionPrompt;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;

class SummarizeCollection extends FunctionContract
Expand Down Expand Up @@ -41,25 +40,27 @@ public function handle(
'token_count_v1' => token_counter($summary),
]);

$prompt = SummarizeCollectionPrompt::prompt($summary, $message->getContent());
$content = $message->getContent();

$messagesArray = [];

$messagesArray[] = MessageInDto::from([
'content' => $prompt,
'role' => 'user',
]);
$prompt = Templatizer::appendContext(true)
->handle(
content: $content,
replacement: $summary,
);

/**
* @NOTE
* I treat chat like completion
* just same results
*/
$results = LlmDriverFacade::driver($message->getDriver())
->setToolType(ToolTypes::NoFunction)
->chat($messagesArray);

$this->response = $results->content;
->completion($prompt);

notify_ui($message->getChat(), 'Summary complete');

return FunctionResponse::from([
'content' => $this->response,
'content' => $results->content,
'prompt' => $prompt,
'requires_followup' => true,
'documentChunks' => collect([]),
Expand Down
4 changes: 4 additions & 0 deletions Modules/LlmDriver/app/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ 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 Down
File renamed without changes.
43 changes: 6 additions & 37 deletions tests/Feature/SummarizeCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace Tests\Feature;

use App\Domains\Agents\VerifyPromptOutputDto;
use App\Models\Collection;
use App\Models\DocumentChunk;
use App\Models\Message;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use LlmLaraHub\LlmDriver\Functions\ParametersDto;
use LlmLaraHub\LlmDriver\Functions\PropertyDto;
use LlmLaraHub\LlmDriver\Functions\SummarizeCollection;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use Tests\TestCase;

Expand All @@ -35,18 +31,10 @@ public function test_can_generate_function_as_array(): void

public function test_gathers_all_content()
{
$RetrieveRelated = new \LlmLaraHub\LlmDriver\Functions\SummarizeCollection();
$messageArray = [];

$messageArray[] = MessageInDto::from([
'content' => 'Can you summarize all this content for me',
'role' => 'user',
]);

$dto = CompletionResponse::from([
'content' => 'This is a summary of the content',
]);
LlmDriverFacade::shouldReceive('driver->setToolType->chat')
LlmDriverFacade::shouldReceive('driver->setToolType->completion')
->once()
->andReturn($dto);

Expand All @@ -57,37 +45,18 @@ public function test_gathers_all_content()
'chatable_id' => $collection->id,
]);

VerifyResponseAgent::shouldReceive('verify')->never()->andReturn(
VerifyPromptOutputDto::from(
[
'chattable' => $chat,
'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!',
]
));

$document = \App\Models\Document::factory()->create([
\App\Models\Document::factory(3)->create([
'collection_id' => $collection->id,
'summary' => 'Foo bar',
]);

DocumentChunk::factory(3)->create(
$message = Message::factory()->create(
[
'document_id' => $document->id,
'chat_id' => $chat->id,
'body' => 'Can you summarize all this content for me separating the content into 2 parts',
]
);

$functionCallDto = \LlmLaraHub\LlmDriver\Functions\FunctionCallDto::from([
'function_name' => 'summarize_collection',
'arguments' => json_encode([
'prompt' => 'Can you summarize all this content for me',
]),
]);

$message = Message::factory()->create();

$results = (new SummarizeCollection())->handle($message);

$this->assertInstanceOf(\LlmLaraHub\LlmDriver\Responses\FunctionResponse::class, $results);
Expand Down
36 changes: 16 additions & 20 deletions tests/fixtures/messages_before_final_job_claude.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
"meta_data": null
},
{
"content": "It seems like there was no original thread to summarize. The conversation started with a request to list solutions based on the provided requirements, but it appears that I didn't receive any actual text to work with. If you'd like to provide the original text or clarify what you're looking for, I'll be happy to assist!",
"role": "assistant",
"content": "can you tldr this thread for me",
"role": "user",
"is_ai": false,
"show": true,
"tool": "",
"tool": "chat",
"tool_id": "",
"args": [],
"meta_data": null
},
{
"content": "can you summarize the results from this thread",
"role": "user",
"content": "Certainly! Here's a TL;DR of our conversation:\n\nTL;DR: The requirements outline a project for enhancing a website and resolving API integration issues for TechInnovate Solutions. The project involves fixing Laravel backend API problems and improving a VueJS frontend banner. Based on these requirements, we identified several relevant solutions from our collection, including:\n\n1. Experienced Laravel developer\n2. Full-stack development capability\n3. Iterative development approach\n4. Filament for Laravel admin panel\n5. Inertia and Vue for frontend\n6. Agile development methodology\n7. Performance optimization techniques\n\nThese solutions address various aspects of the project, from technical expertise to development approaches, aligning with the company's needs for both backend and frontend improvements within a tight timeline.",
"role": "assistant",
"is_ai": false,
"show": true,
"tool": "chat",
Expand All @@ -40,38 +40,34 @@
"meta_data": null
},
{
"content": "Example Domain Example DomainThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.More information...",
"role": "tool",
"content": "can you summarize this collection for me",
"role": "user",
"is_ai": false,
"show": true,
"tool": "get_web_site_from_url",
"tool": null,
"tool_id": "",
"args": {
"url": "https:\/\/www.example.com"
},
"args": [],
"meta_data": null
},
{
"content": "\"The article discusses the best restaurants in Paris, focusing on lesser-known spots outside of the typical tourist areas. The four featured restaurants are:\\n\\n1. Le Maquis (18th arrondissement): A laid-back neighborhood bistro serving excellent bistro cooking with a Southern French or Italian accent.\\n2. Le Cadoret (19th arrondissement): A modern bistro in a former corner cafe, offering market-driven cooking and an affordable price point.\\n3. Cheval d'Or (19th arrondissement): A popular spot for modern Asian bistro cooking, featuring dishes like red tuna tartare and pork belly Tatin.\\n4. Le Baratin (20th arrondissement): A wine bar serving deeply satisfying food, with a menu that changes constantly but often features offal and fish.\\n\\nThe article highlights the unique character of each restaurant and the local neighborhoods they inhabit, making them stand out from the typical tourist destinations in Paris.\"\n\"It seems like you provided a long passage about hotels and restaurants in Paris, but I'll try to summarize the key points:\\n\\nThe passage describes several hotels in Paris, each with its own unique characteristics:\\n\\n1. H\\u00f4tel Le Bristol - A luxurious 5-star hotel with elegant rooms and suites.\\n2. H\\u00f4tel Costes - A chic and trendy 5-star hotel with stylish ambiance and a popular bar.\\n3. H\\u00f4tel Bassano - A charming 4-star boutique hotel offering elegance and modern comfort.\\n4. The Hoxton Paris - A cool 4-star boutique hotel with a vibrant atmosphere and stylish design.\\n5. Grand Pigalle H\\u00f4tel - A hip and stylish 4-star hotel with a unique blend of vintage and contemporary design.\\n6. H\\u00f4tel Presbytere - A charming 3-star hotel housed in a historic building with traditional Parisian architecture.\\n7. La Planque H\\u00f4tel - A boutique 3-star hotel offering a cozy and intimate atmosphere.\\n\\nThe passage also mentions the best food tours in Paris, a guide to secure luggage storage, and a local's food guide to Paris, which features the very best of the city's culinary scene.\\n\\nIf you'd like me to help with anything specific, please let me know!\"\n\"It looks like you've pasted the URL of a Tripadvisor page, which is likely to contain reviews and information about restaurants in Paris. The actual article text isn't present here, but I can try to help you with any specific questions or requests related to this page.\\n\\nIf you're looking for the \\\"RESULTS OF WEB SEARCH ARTICLE ONE OF A FIVE\\\", it seems like you might be referring to a summary or highlights of restaurant recommendations on that Tripadvisor page. Would you like me to:\\n\\n1. Help summarize the top-rated restaurants in Paris based on the Tripadvisor data?\\n2. Look up specific information about a particular restaurant mentioned on this page?\\n\\nPlease let me know how I can assist!\"",
"role": "tool",
"content": "can you summarize this collection for me",
"role": "user",
"is_ai": false,
"show": true,
"tool": "search_the_web",
"tool": null,
"tool_id": "",
"args": {
"search_phrase": "best restaurants in Paris"
},
"args": [],
"meta_data": null
},
{
"content": "This is an example document.",
"content": "Here's a more detailed template with sections for different types of content:\n\n**Summary Template**\n=====================\n\n### Overview\n---------------\n\n* Briefly summarize the main topic and key points.\n* If applicable, mention any notable figures, organizations, or events related to the content.\n\n### Key Points\n----------------\n\n* List the most important takeaways from the data.\n* Keep each point concise and focused on a single idea.\n* Use bullet points for easier reading and comprehension.\n\n### Considerations\n-------------------\n\n* Discuss potential implications, challenges, or limitations of the information.\n* Explore any potential connections to broader topics or real-world applications.\n* Suggest areas where further research or investigation might be necessary.\n\n### Recommendations\n--------------------\n\n* Provide actionable advice or guidance based on the analysis.\n* Offer specific suggestions for implementation or improvement.\n* Emphasize any benefits, such as cost savings, efficiency gains, or increased productivity.\n\n### Conclusion\n--------------\n\n* Summarize the main findings and takeaways from the data.\n* Reiterate the importance of considering these points in future decision-making or planning processes.\n* End with a thought-provoking question or call to action that encourages further exploration.",
"role": "tool",
"is_ai": false,
"show": true,
"tool": "create_document",
"tool": "summarize_collection",
"tool_id": "",
"args": {
"content": "This is an example document."
"prompt": "can you summarize this collection for me"
},
"meta_data": null
}
Expand Down
Loading

0 comments on commit c6299b0

Please sign in to comment.