optimize url detection
This commit is contained in:
@@ -40,6 +40,8 @@ final readonly class AgentRunner
|
||||
{
|
||||
$prompt = trim($prompt);
|
||||
$swagFullOutPut = '';
|
||||
$firstThinkLoop = true;
|
||||
$shopResults = [];
|
||||
|
||||
if ($prompt === '') {
|
||||
yield '❌ Empty prompt.';
|
||||
@@ -61,7 +63,28 @@ final readonly class AgentRunner
|
||||
|
||||
yield $this->systemMsg("Ich analysiere deine Anfrage...", "think");
|
||||
|
||||
$promptSwagSearch = '
|
||||
// ---------------------------------------------------------
|
||||
// 2) Extract URL content (if present)
|
||||
// ---------------------------------------------------------
|
||||
yield $this->systemMsg("Ich prüfe auf Internet Quellen...", "think");
|
||||
$urlContent = $this->urlAnalyzer->extractContentFromPrompt($prompt);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 3) Retrieve RAG knowledge
|
||||
// ---------------------------------------------------------
|
||||
yield $this->systemMsg("Ich hole relevante Daten aus meinem RAG Wissen...", "think");
|
||||
$knowledgeChunks = $this->retriever->retrieve($prompt);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 4) commerce/shop search
|
||||
// ---------------------------------------------------------
|
||||
|
||||
$commerceMeta = $this->commerceIntentLite->detect($prompt);
|
||||
$commerceIntent = (string)($commerceMeta['intent'] ?? CommerceIntentLite::NONE);
|
||||
|
||||
if ($commerceIntent === CommerceIntentLite::PRODUCT_SEARCH || $commerceIntent === CommerceIntentLite::ADVISORY_PRODUCT_SEARCH) {
|
||||
//PreOptimize swag search query
|
||||
$promptSwagSearch = '
|
||||
Erzeuge aus dem folgenden Nutzereingabetext einen kurzen Suchtext für die Shopware-6-Suche.
|
||||
|
||||
Regeln:
|
||||
@@ -78,63 +101,43 @@ final readonly class AgentRunner
|
||||
Ausgabeformat:
|
||||
Keyword1 Keyword2 Keyword3
|
||||
|
||||
Text: ' . $prompt . '
|
||||
Nutzereingabetext: ' . $prompt . '
|
||||
';
|
||||
|
||||
$this->thinkSuppressor->reset();
|
||||
//Reset thinkSuppressor
|
||||
$this->thinkSuppressor->reset();
|
||||
|
||||
foreach ($this->ollamaClient->stream($promptSwagSearch) as $swagToken) {
|
||||
yield $this->systemMsg("Ich optimere die Shopanfrage...", "think");
|
||||
|
||||
if (!is_string($swagToken)) {
|
||||
continue;
|
||||
//Call ai for optimized swag query
|
||||
foreach ($this->ollamaClient->stream($promptSwagSearch) as $swagToken) {
|
||||
|
||||
if (!is_string($swagToken)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$swagCleanToken = $this->thinkSuppressor->filter($swagToken);
|
||||
|
||||
if ($swagCleanToken === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$swagFullOutPut .= $swagCleanToken;
|
||||
}
|
||||
|
||||
$swagCleanToken = $this->thinkSuppressor->filter($swagToken);
|
||||
yield $this->systemMsg("Ich rufe Shopdaten ab (type: " . $commerceIntent . ")", "think");
|
||||
|
||||
if ($swagCleanToken === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$swagFullOutPut .= $swagCleanToken;
|
||||
//Search in swag by ai optimized query
|
||||
$shopResults = $swagFullOutPut ? $this->shopSearchService->search($swagFullOutPut, $commerceIntent) : '';
|
||||
}
|
||||
|
||||
yield $this->systemMsg("Ich habe folgende Keywords an die Shopsuche geschickt: " . $swagFullOutPut, "think");
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 2) Extract URL content (if present)
|
||||
// ---------------------------------------------------------
|
||||
$urlContent = $this->urlAnalyzer->extractContentFromPrompt($prompt);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 3) Retrieve RAG knowledge
|
||||
// ---------------------------------------------------------
|
||||
yield $this->systemMsg("Ich hole relevante Daten aus meinem RAG Wissen...", "think");
|
||||
|
||||
$knowledgeChunks = $this->retriever->retrieve($prompt);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 4) commerce/shop search
|
||||
// ---------------------------------------------------------
|
||||
|
||||
$commerceMeta = $this->commerceIntentLite->detect($prompt);
|
||||
$commerceIntent = (string)($commerceMeta['intent'] ?? CommerceIntentLite::NONE);
|
||||
|
||||
yield $this->systemMsg("Ich rufe Shopdaten ab (type: " . $commerceIntent . ")", "think");
|
||||
$shopResults = $swagFullOutPut ? $this->shopSearchService->search($swagFullOutPut, $commerceIntent) : '';
|
||||
|
||||
if ($commerceIntent === CommerceIntentLite::PRODUCT_SEARCH) {
|
||||
$knowledgeChunks = array_slice($knowledgeChunks, 0, 2);
|
||||
} elseif ($commerceIntent === CommerceIntentLite::ADVISORY_PRODUCT_SEARCH) {
|
||||
$knowledgeChunks = array_slice($knowledgeChunks, 0, 3);
|
||||
}
|
||||
|
||||
if ($shopResults) {
|
||||
yield $this->systemMsg("Ich verarbeite Shopdaten...", "think");
|
||||
} else {
|
||||
yield $this->systemMsg("Ich habe keine releveanten Shopdaten gefunden...", "think");
|
||||
}
|
||||
|
||||
yield $this->systemMsg("Ich analysiere gefundene Informationen...", "think");
|
||||
yield $this->systemMsg("Ich analysiere alle Informationen...", "think");
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 5) Build final prompt
|
||||
@@ -179,8 +182,10 @@ final readonly class AgentRunner
|
||||
$cleanToken = $this->thinkSuppressor->filter((string)$token);
|
||||
|
||||
if ($cleanToken === '') {
|
||||
yield $this->systemMsg("Denke nach...", "think");
|
||||
usleep(500);
|
||||
if ($firstThinkLoop) {
|
||||
yield $this->systemMsg("Denke nach...", "think");
|
||||
$firstThinkLoop = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Context;
|
||||
|
||||
use RuntimeException;
|
||||
use fivefilters\Readability\Configuration;
|
||||
use fivefilters\Readability\ParseException;
|
||||
use fivefilters\Readability\Readability;
|
||||
|
||||
/**
|
||||
* UrlAnalyzer
|
||||
@@ -65,30 +67,29 @@ final class UrlAnalyzer
|
||||
],
|
||||
]);
|
||||
|
||||
$handle = @fopen($url, 'rb', false, $context);
|
||||
if ($handle === false) {
|
||||
$html = @file_get_contents($url, false, $context);
|
||||
if ($html === false || $html === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$config = new Configuration();
|
||||
$config->setFixRelativeURLs(true);
|
||||
$config->setOriginalURL($url);
|
||||
|
||||
$readability = new Readability($config);
|
||||
|
||||
try {
|
||||
$html = '';
|
||||
while (!feof($handle) && strlen($html) < $this->maxChars * 2) {
|
||||
$html .= fread($handle, 1024);
|
||||
}
|
||||
} finally {
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
if ($html === '') {
|
||||
$readability->parse($html);
|
||||
$content = $readability->getContent() ?? '';
|
||||
} catch (ParseException) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Remove script and style blocks
|
||||
$html = preg_replace('~<script[^>]*>.*?</script>~is', '', $html) ?? $html;
|
||||
$html = preg_replace('~<style[^>]*>.*?</style>~is', '', $html) ?? $html;
|
||||
if ($content === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Strip remaining HTML and normalize whitespace
|
||||
$text = strip_tags($html);
|
||||
$text = strip_tags($content);
|
||||
$text = preg_replace('/\s+/u', ' ', $text) ?? $text;
|
||||
|
||||
return mb_substr(trim($text), 0, $this->maxChars);
|
||||
|
||||
Reference in New Issue
Block a user