forked from NuQloud/nc-talk-ai
Talk AI is a multi-bot AI assistant manager for Nextcloud Talk: per-bot prompts and models, agentic tool calling (MCP + built-in tools), RAG over Nextcloud files, room-document search, vision and speech-to-text attachments, persistent bot wikis, approval workflows, rate limiting, and multi-provider LLM support (any OpenAI-compatible endpoint). Developed within EDUC - the European Digital UniverCity (https://educalliance.eu), where it runs as the 'EDUC AI' assistant on the alliance-wide Nextcloud portal. This public repository is the upstream point of truth; deployment-specific tools plug in via the tool-provider extension point (docs/TOOL_PROVIDERS.md). License: AGPL-3.0-or-later.
115 lines
3.9 KiB
PHP
115 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\EducAI\Db;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
use OCP\IDBConnection;
|
|
|
|
/**
|
|
* @template-extends QBMapper<Embedding>
|
|
*/
|
|
class EmbeddingMapper extends QBMapper {
|
|
public function __construct(IDBConnection $db) {
|
|
parent::__construct($db, 'educai_embeddings', Embedding::class);
|
|
}
|
|
|
|
/**
|
|
* @return array<int,Embedding>
|
|
*/
|
|
public function findByBot(int $botId): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)));
|
|
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* @return array<int,Embedding>
|
|
*/
|
|
public function findByBotAndModel(int $botId, string $embeddingModel): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('embedding_model', $qb->createNamedParameter($embeddingModel, IQueryBuilder::PARAM_STR)));
|
|
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* @return array<int,Embedding>
|
|
*/
|
|
public function findBySource(int $sourceId): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('source_id', $qb->createNamedParameter($sourceId, IQueryBuilder::PARAM_INT)));
|
|
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
public function findOneByBotAndChunk(int $botId, string $chunkId): ?Embedding {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('chunk_id', $qb->createNamedParameter($chunkId, IQueryBuilder::PARAM_STR)))
|
|
->setMaxResults(1);
|
|
|
|
try {
|
|
return $this->findEntity($qb);
|
|
} catch (DoesNotExistException $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function deleteBySource(int $sourceId): void {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->delete($this->getTableName())
|
|
->where($qb->expr()->eq('source_id', $qb->createNamedParameter($sourceId, IQueryBuilder::PARAM_INT)));
|
|
|
|
$qb->executeStatement();
|
|
}
|
|
|
|
public function deleteByBot(int $botId): void {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->delete($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)));
|
|
|
|
$qb->executeStatement();
|
|
}
|
|
|
|
public function countByBot(int $botId): int {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select($qb->createFunction('COUNT(*)'))
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)));
|
|
|
|
$result = $qb->executeQuery();
|
|
$count = (int)$result->fetchOne();
|
|
$result->closeCursor();
|
|
|
|
return $count;
|
|
}
|
|
|
|
public function countByBotAndModel(int $botId, string $embeddingModel): int {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select($qb->createFunction('COUNT(*)'))
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('embedding_model', $qb->createNamedParameter($embeddingModel, IQueryBuilder::PARAM_STR)));
|
|
|
|
$result = $qb->executeQuery();
|
|
$count = (int)$result->fetchOne();
|
|
$result->closeCursor();
|
|
|
|
return $count;
|
|
}
|
|
}
|