-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for scoped context in query
Signed-off-by: Anupam Kumar <[email protected]>
- Loading branch information
Showing
8 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace OCA\ContextChat\TextProcessing; | ||
|
||
use OCA\ContextChat\Service\LangRopeService; | ||
use OCA\ContextChat\Service\ScopeType; | ||
use OCP\IL10N; | ||
use OCP\TextProcessing\IProvider; | ||
use OCP\TextProcessing\IProviderWithUserId; | ||
|
||
/** | ||
* @template-implements IProviderWithUserId<ScopedContextChatTaskType> | ||
* @template-implements IProvider<ScopedContextChatTaskType> | ||
*/ | ||
class ScopedContextChatProvider implements IProvider, IProviderWithUserId { | ||
|
||
private ?string $userId = null; | ||
|
||
public function __construct( | ||
private LangRopeService $langRopeService, | ||
private IL10N $l10n, | ||
) { | ||
} | ||
|
||
public function getName(): string { | ||
return $this->l10n->t('Nextcloud Assistant Scoped Context Chat Provider'); | ||
} | ||
|
||
/** | ||
* @param string $prompt JSON string with the following structure: | ||
* { | ||
* "scopeType": string, | ||
* "scopeList": list[string], | ||
* "prompt": string, | ||
* } | ||
* | ||
* @return string | ||
*/ | ||
public function process(string $prompt): string { | ||
if ($this->userId === null) { | ||
throw new \RuntimeException('User ID is required to process the prompt.'); | ||
} | ||
|
||
try { | ||
$parsedData = json_decode($prompt, true, flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE); | ||
} catch (\JsonException $e) { | ||
throw new \RuntimeException( | ||
'Invalid JSON string, expected { "scopeType": string, "scopeList": list[string], "prompt": string }', | ||
intval($e->getCode()), $e, | ||
); | ||
} | ||
|
||
if ( | ||
!is_array($parsedData) | ||
|| !isset($parsedData['scopeType']) | ||
|| !is_string($parsedData['scopeType']) | ||
|| !isset($parsedData['scopeList']) | ||
|| !is_array($parsedData['scopeList']) | ||
|| !isset($parsedData['prompt']) | ||
|| !is_string($parsedData['prompt']) | ||
) { | ||
throw new \RuntimeException('Invalid JSON string, expected { "scopeType": string, "scopeList": list[string], "prompt": string }'); | ||
} | ||
|
||
$scopeTypeEnum = ScopeType::tryFrom($parsedData['scopeType']); | ||
if ($scopeTypeEnum === null) { | ||
throw new \RuntimeException('Invalid scope type: ' . $parsedData['scopeType']); | ||
} | ||
|
||
$response = $this->langRopeService->scopedQuery( | ||
$this->userId, | ||
$parsedData['prompt'], | ||
$scopeTypeEnum, | ||
$parsedData['scopeList'], | ||
); | ||
|
||
if (isset($response['error'])) { | ||
throw new \RuntimeException('No result in ContextChat response. ' . $response['error']); | ||
} | ||
|
||
return $response['message'] ?? ''; | ||
} | ||
|
||
public function getTaskType(): string { | ||
return ScopedContextChatTaskType::class; | ||
} | ||
|
||
public function setUserId(?string $userId): void { | ||
$this->userId = $userId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Julien Veyssier <[email protected]> | ||
* | ||
* @author Julien Veyssier <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\ContextChat\TextProcessing; | ||
|
||
use OCP\IL10N; | ||
use OCP\TextProcessing\ITaskType; | ||
|
||
class ScopedContextChatTaskType implements ITaskType { | ||
public function __construct( | ||
private IL10N $l, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since 27.1.0 | ||
*/ | ||
public function getName(): string { | ||
return $this->l->t('Scoped Context Chat'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @since 27.1.0 | ||
*/ | ||
public function getDescription(): string { | ||
return $this->l->t('Ask a question about the data selected by you.'); | ||
} | ||
} |