add uvicorn as py server for faster com

This commit is contained in:
team2
2026-02-22 08:35:13 +01:00
parent f62d102d61
commit 2629774dcd
8 changed files with 547 additions and 127 deletions

View File

@@ -5,16 +5,16 @@ declare(strict_types=1);
namespace App\Tag;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final readonly class TagVectorSearchClient
{
private const MIN_SCORE = 0.4; // 🔥 Tag Confidence Gate
public function __construct(
private string $pythonBin,
private string $scriptPath,
private string $vectorTagsIndexPath,
private string $vectorTagsMetaPath,
private string $embeddingModel,
private LoggerInterface $agentLogger,
private HttpClientInterface $http,
private string $serviceUrl,
private LoggerInterface $agentLogger,
) {}
/**
@@ -22,42 +22,32 @@ final readonly class TagVectorSearchClient
*/
public function search(string $query, int $limit = 8): array
{
if (!is_file($this->scriptPath)) {
$this->agentLogger->warning('Tag vector search script missing: ' . $this->scriptPath);
return [];
}
if (!is_file($this->vectorTagsIndexPath) || !is_file($this->vectorTagsMetaPath)) {
// no tag index available yet => no routing
return [];
}
$limit = max(1, min($limit, 50));
// Positional args, aligned with existing VectorSearchClient approach:
// python vector_search_tags.py <query> <limit> <index> <meta> <model>
$cmd = sprintf(
'%s %s %s %d %s %s %s 2>&1',
escapeshellarg($this->pythonBin),
escapeshellarg($this->scriptPath),
escapeshellarg($query),
$limit,
escapeshellarg($this->vectorTagsIndexPath),
escapeshellarg($this->vectorTagsMetaPath),
escapeshellarg($this->embeddingModel),
);
exec($cmd, $out, $exitCode);
if ($exitCode !== 0 || empty($out)) {
return [];
}
$json = implode("\n", $out);
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable) {
$response = $this->http->request(
'POST',
rtrim($this->serviceUrl, '/') . '/search-tags',
[
'json' => [
'query' => $query,
'limit' => $limit,
],
'timeout' => 10,
]
);
if ($response->getStatusCode() !== 200) {
$this->agentLogger->warning('Tag vector service returned non-200');
return [];
}
$data = $response->toArray(false);
} catch (\Throwable $e) {
$this->agentLogger->warning(
'Tag vector service unreachable: ' . $e->getMessage()
);
return [];
}
@@ -66,20 +56,29 @@ final readonly class TagVectorSearchClient
}
$hits = [];
foreach ($data as $row) {
if (!is_array($row)) {
continue;
}
$tagId = (string)($row['tag_id'] ?? '');
$tagId = (string)($row['chunk_id'] ?? '');
$score = $row['score'] ?? null;
if ($tagId === '' || !is_numeric($score)) {
continue;
}
$score = (float) $score;
// 🔥 Confidence Gate
if ($score < self::MIN_SCORE) {
continue;
}
$hits[] = [
'tag_id' => $tagId,
'score' => (float)$score,
'score' => $score,
];
}